diff --git a/ibm/resource_ibm_kms_key_test.go b/ibm/resource_ibm_kms_key_test.go index a320b5d5c7..5f7ed4cbb6 100644 --- a/ibm/resource_ibm_kms_key_test.go +++ b/ibm/resource_ibm_kms_key_test.go @@ -2,7 +2,10 @@ package ibm import ( "fmt" + "math/rand" + "regexp" "testing" + "time" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -15,10 +18,6 @@ func TestAccIBMKMSResource_basic(t *testing.T) { keyName := fmt.Sprintf("key_%d", acctest.RandIntRange(10, 100)) payload := "LqMWNtSi3Snr4gFNO0PsFFLFRNs57mSXCQE7O2oE+g0=" hpcskeyName := fmt.Sprintf("hpcs_%d", acctest.RandIntRange(10, 100)) - // hours := time.Duration(rand.Intn(24) + 1) - // mins := time.Duration(rand.Intn(60) + 1) - // sec := time.Duration(rand.Intn(60) + 1) - // expirationdate := ((time.Now().Add(time.Hour*hours + time.Minute*mins + time.Second*sec)).Format(time.RFC3339)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -52,6 +51,65 @@ func TestAccIBMKMSResource_basic(t *testing.T) { }) } +// Test for valid expiration date for create key operation +func TestAccIBMKMSResource_ValidExpDate(t *testing.T) { + + instanceName := fmt.Sprintf("kms_%d", acctest.RandIntRange(10, 100)) + keyName := fmt.Sprintf("key_%d", acctest.RandIntRange(10, 100)) + + hours := time.Duration(rand.Intn(24) + 1) + mins := time.Duration(rand.Intn(60) + 1) + sec := time.Duration(rand.Intn(60) + 1) + expirationDateValid := ((time.Now().Add(time.Hour*hours + time.Minute*mins + time.Second*sec)).Format(time.RFC3339)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIBMKmsCreateStandardKeyConfig(instanceName, keyName, expirationDateValid), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("ibm_kms_key.test", "key_name", keyName), + resource.TestCheckResourceAttr("ibm_kms_key.test", "expiration_date", expirationDateValid), + ), + }, + resource.TestStep{ + Config: testAccCheckIBMKmsCreateRootKeyConfig(instanceName, keyName, expirationDateValid), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("ibm_kms_key.test", "key_name", keyName), + resource.TestCheckResourceAttr("ibm_kms_key.test", "expiration_date", expirationDateValid), + ), + }, + }, + }) +} + +// Test for invalid expiration date for create key operation +func TestAccIBMKMSResource_InvalidExpDate(t *testing.T) { + instanceName := fmt.Sprintf("kms_%d", acctest.RandIntRange(10, 100)) + keyName := fmt.Sprintf("key_%d", acctest.RandIntRange(10, 100)) + + hours := time.Duration(rand.Intn(24) + 1) + mins := time.Duration(rand.Intn(60) + 1) + sec := time.Duration(rand.Intn(60) + 1) + expirationDateInvalid := (time.Now().Add(time.Hour*hours + time.Minute*mins + time.Second*sec)).String() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckIBMKmsCreateStandardKeyConfig(instanceName, keyName, expirationDateInvalid), + ExpectError: regexp.MustCompile("Invalid time format"), + }, + resource.TestStep{ + Config: testAccCheckIBMKmsCreateRootKeyConfig(instanceName, keyName, expirationDateInvalid), + ExpectError: regexp.MustCompile("Invalid time format"), + }, + }, + }) +} + func testAccCheckIBMKmsResourceStandardConfig(instanceName, KeyName string) string { return fmt.Sprintf(` resource "ibm_resource_instance" "kms_instance" { @@ -143,3 +201,41 @@ func testAccCheckIBMKmsResourceHpcsConfig(hpcsInstanceID, KeyName string) string `, hpcsInstanceID, KeyName) } + +func testAccCheckIBMKmsCreateStandardKeyConfig(instanceName, KeyName, expirationDate string) string { + return fmt.Sprintf(` + resource "ibm_resource_instance" "kms_instance" { + name = "%s" + service = "kms" + plan = "tiered-pricing" + location = "us-south" + } + resource "ibm_kms_key" "test" { + instance_id = "${ibm_resource_instance.kms_instance.guid}" + key_name = "%s" + standard_key = true + force_delete = true + expiration_date = "%s" + } + +`, instanceName, KeyName, expirationDate) +} + +func testAccCheckIBMKmsCreateRootKeyConfig(instanceName, KeyName, expirationDate string) string { + return fmt.Sprintf(` + resource "ibm_resource_instance" "kms_instance" { + name = "%s" + service = "kms" + plan = "tiered-pricing" + location = "us-south" + } + resource "ibm_kms_key" "test" { + instance_id = "${ibm_resource_instance.kms_instance.guid}" + key_name = "%s" + standard_key = false + force_delete = true + expiration_date = "%s" + } + +`, instanceName, KeyName, expirationDate) +}