Skip to content

Commit

Permalink
Merge pull request #12989 from terraform-providers/t-clean-up-kms-arn…
Browse files Browse the repository at this point in the history
…-tests

service/kms: Update acceptance tests to use ARN testing check functions
  • Loading branch information
gdavison authored Apr 29, 2020
2 parents 770f86d + c00f56e commit 726947d
Showing 1 changed file with 5 additions and 47 deletions.
52 changes: 5 additions & 47 deletions aws/data_source_aws_kms_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand All @@ -23,9 +22,9 @@ func TestAccDataSourceAwsKmsAlias_AwsService(t *testing.T) {
Config: testAccDataSourceAwsKmsAlias_name(name),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsKmsAliasCheckExists(resourceName),
resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:kms:[^:]+:[^:]+:%s$", name))),
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "kms", name),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestMatchResourceAttr(resourceName, "target_key_arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:kms:[^:]+:[^:]+:key/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}$"))),
testAccMatchResourceAttrRegionalARN(resourceName, "target_key_arn", "kms", regexp.MustCompile(`key/[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}`)),
resource.TestMatchResourceAttr(resourceName, "target_key_id", regexp.MustCompile(fmt.Sprintf("^[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}$"))),
),
},
Expand All @@ -46,7 +45,9 @@ func TestAccDataSourceAwsKmsAlias_CMK(t *testing.T) {
Config: testAccDataSourceAwsKmsAlias_CMK(rInt),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsKmsAliasCheckExists(datasourceAliasResourceName),
testAccDataSourceAwsKmsAliasCheckCMKAttributes(aliasResourceName, datasourceAliasResourceName),
resource.TestCheckResourceAttrPair(datasourceAliasResourceName, "arn", aliasResourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceAliasResourceName, "target_key_arn", aliasResourceName, "target_key_arn"),
resource.TestCheckResourceAttrPair(datasourceAliasResourceName, "target_key_id", aliasResourceName, "target_key_id"),
),
},
},
Expand All @@ -64,49 +65,6 @@ func testAccDataSourceAwsKmsAliasCheckExists(name string) resource.TestCheckFunc
}
}

func testAccDataSourceAwsKmsAliasCheckCMKAttributes(aliasResourceName, datasourceAliasResourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[datasourceAliasResourceName]
if !ok {
return fmt.Errorf("root module has no resource called %s", datasourceAliasResourceName)
}

kmsKeyRs, ok := s.RootModule().Resources[aliasResourceName]
if !ok {
return fmt.Errorf("can't find %s in state", aliasResourceName)
}

attr := rs.Primary.Attributes

if attr["arn"] != kmsKeyRs.Primary.Attributes["arn"] {
return fmt.Errorf(
"arn is %s; want %s",
attr["arn"],
kmsKeyRs.Primary.Attributes["arn"],
)
}

expectedTargetKeyArnSuffix := fmt.Sprintf("key/%s", kmsKeyRs.Primary.Attributes["target_key_id"])
if !strings.HasSuffix(attr["target_key_arn"], expectedTargetKeyArnSuffix) {
return fmt.Errorf(
"target_key_arn is %s; want suffix %s",
attr["target_key_arn"],
expectedTargetKeyArnSuffix,
)
}

if attr["target_key_id"] != kmsKeyRs.Primary.Attributes["target_key_id"] {
return fmt.Errorf(
"target_key_id is %s; want %s",
attr["target_key_id"],
kmsKeyRs.Primary.Attributes["target_key_id"],
)
}

return nil
}
}

func testAccDataSourceAwsKmsAlias_name(name string) string {
return fmt.Sprintf(`
data "aws_kms_alias" "test" {
Expand Down

0 comments on commit 726947d

Please sign in to comment.