Skip to content

Commit

Permalink
Added additional test cases for expiration date for kms
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth Mishra authored and hkantare committed Oct 21, 2020
1 parent c2c6169 commit cadfd6b
Showing 1 changed file with 100 additions and 4 deletions.
104 changes: 100 additions & 4 deletions ibm/resource_ibm_kms_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) },
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}

0 comments on commit cadfd6b

Please sign in to comment.