Skip to content

Commit

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

service/config: Update acceptance tests to use ARN testing check functions
  • Loading branch information
gdavison authored Apr 23, 2020
2 parents f4357ec + 45ce6fe commit 6ee2c4b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 53 deletions.
6 changes: 4 additions & 2 deletions aws/resource_aws_config_aggregate_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func TestAccAWSConfigAggregateAuthorization_basic(t *testing.T) {
rString := acctest.RandStringFromCharSet(12, "0123456789")
resourceName := "aws_config_aggregate_authorization.example"

region := "eu-west-1"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -70,8 +72,8 @@ func TestAccAWSConfigAggregateAuthorization_basic(t *testing.T) {
Config: testAccAWSConfigAggregateAuthorizationConfig_basic(rString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", rString),
resource.TestCheckResourceAttr(resourceName, "region", "eu-west-1"),
resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(`^arn:aws:config:[\w-]+:\d{12}:aggregation-authorization/\d{12}/[\w-]+$`)),
resource.TestCheckResourceAttr(resourceName, "region", region),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "config", regexp.MustCompile(fmt.Sprintf(`aggregation-authorization/%s/%s$`, rString, region))),
),
},
{
Expand Down
48 changes: 22 additions & 26 deletions aws/resource_aws_config_config_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func testAccConfigConfigRule_ownerAws(t *testing.T) {
var cr configservice.ConfigRule
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_config_config_rule.test"
expectedArn := regexp.MustCompile("arn:aws:config:[a-z0-9-]+:[0-9]{12}:config-rule/config-rule-([a-z0-9]+)")
expectedRuleId := regexp.MustCompile("config-rule-[a-z0-9]+")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -54,9 +52,9 @@ func testAccConfigConfigRule_ownerAws(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckConfigConfigRuleExists(resourceName, &cr),
testAccCheckConfigConfigRuleName(resourceName, rName, &cr),
resource.TestMatchResourceAttr(resourceName, "arn", expectedArn),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "config", regexp.MustCompile("config-rule/config-rule-[a-z0-9]+$")),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestMatchResourceAttr(resourceName, "rule_id", expectedRuleId),
resource.TestMatchResourceAttr(resourceName, "rule_id", regexp.MustCompile("config-rule-[a-z0-9]+$")),
resource.TestCheckResourceAttr(resourceName, "description", "Terraform Acceptance tests"),
resource.TestCheckResourceAttr(resourceName, "source.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source.0.owner", "AWS"),
Expand All @@ -75,12 +73,10 @@ func testAccConfigConfigRule_ownerAws(t *testing.T) {
func testAccConfigConfigRule_customlambda(t *testing.T) {
var cr configservice.ConfigRule
rInt := acctest.RandInt()
resourceName := "aws_config_config_rule.test"

expectedName := fmt.Sprintf("tf-acc-test-%d", rInt)
path := "test-fixtures/lambdatest.zip"
expectedArn := regexp.MustCompile("arn:aws:config:[a-z0-9-]+:[0-9]{12}:config-rule/config-rule-([a-z0-9]+)")
expectedFunctionArn := regexp.MustCompile(fmt.Sprintf("arn:aws:lambda:[a-z0-9-]+:[0-9]{12}:function:tf_acc_lambda_awsconfig_%d", rInt))
expectedRuleId := regexp.MustCompile("config-rule-[a-z0-9]+")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -90,23 +86,23 @@ func testAccConfigConfigRule_customlambda(t *testing.T) {
{
Config: testAccConfigConfigRuleConfig_customLambda(rInt, path),
Check: resource.ComposeTestCheckFunc(
testAccCheckConfigConfigRuleExists("aws_config_config_rule.foo", &cr),
testAccCheckConfigConfigRuleName("aws_config_config_rule.foo", expectedName, &cr),
resource.TestMatchResourceAttr("aws_config_config_rule.foo", "arn", expectedArn),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "name", expectedName),
resource.TestMatchResourceAttr("aws_config_config_rule.foo", "rule_id", expectedRuleId),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "description", "Terraform Acceptance tests"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "maximum_execution_frequency", "Six_Hours"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "source.#", "1"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "source.0.owner", "CUSTOM_LAMBDA"),
resource.TestMatchResourceAttr("aws_config_config_rule.foo", "source.0.source_identifier", expectedFunctionArn),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "source.0.source_detail.#", "1"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "source.0.source_detail.3026922761.event_source", "aws.config"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "source.0.source_detail.3026922761.message_type", "ConfigurationSnapshotDeliveryCompleted"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "source.0.source_detail.3026922761.maximum_execution_frequency", ""),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "scope.#", "1"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "scope.0.tag_key", "IsTemporary"),
resource.TestCheckResourceAttr("aws_config_config_rule.foo", "scope.0.tag_value", "yes"),
testAccCheckConfigConfigRuleExists(resourceName, &cr),
testAccCheckConfigConfigRuleName(resourceName, expectedName, &cr),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "config", regexp.MustCompile("config-rule/config-rule-[a-z0-9]+$")),
resource.TestCheckResourceAttr(resourceName, "name", expectedName),
resource.TestMatchResourceAttr(resourceName, "rule_id", regexp.MustCompile("config-rule-[a-z0-9]+$")),
resource.TestCheckResourceAttr(resourceName, "description", "Terraform Acceptance tests"),
resource.TestCheckResourceAttr(resourceName, "maximum_execution_frequency", "Six_Hours"),
resource.TestCheckResourceAttr(resourceName, "source.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source.0.owner", "CUSTOM_LAMBDA"),
resource.TestCheckResourceAttrPair(resourceName, "source.0.source_identifier", "aws_lambda_function.f", "arn"),
resource.TestCheckResourceAttr(resourceName, "source.0.source_detail.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source.0.source_detail.3026922761.event_source", "aws.config"),
resource.TestCheckResourceAttr(resourceName, "source.0.source_detail.3026922761.message_type", "ConfigurationSnapshotDeliveryCompleted"),
resource.TestCheckResourceAttr(resourceName, "source.0.source_detail.3026922761.maximum_execution_frequency", ""),
resource.TestCheckResourceAttr(resourceName, "scope.#", "1"),
resource.TestCheckResourceAttr(resourceName, "scope.0.tag_key", "IsTemporary"),
resource.TestCheckResourceAttr(resourceName, "scope.0.tag_value", "yes"),
),
},
},
Expand Down Expand Up @@ -136,7 +132,7 @@ func testAccConfigConfigRule_importAws(t *testing.T) {
}

func testAccConfigConfigRule_importLambda(t *testing.T) {
resourceName := "aws_config_config_rule.foo"
resourceName := "aws_config_config_rule.test"
rInt := acctest.RandInt()

path := "test-fixtures/lambdatest.zip"
Expand Down Expand Up @@ -430,7 +426,7 @@ PARAMS

func testAccConfigConfigRuleConfig_customLambda(randInt int, path string) string {
return fmt.Sprintf(`
resource "aws_config_config_rule" "foo" {
resource "aws_config_config_rule" "test" {
name = "tf-acc-test-%d"
description = "Terraform Acceptance tests"
maximum_execution_frequency = "Six_Hours"
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_config_configuration_aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestAccAWSConfigConfigurationAggregator_organization(t *testing.T) {
testAccCheckAWSConfigConfigurationAggregatorName(resourceName, rName, &ca),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "organization_aggregation_source.#", "1"),
resource.TestMatchResourceAttr(resourceName, "organization_aggregation_source.0.role_arn", regexp.MustCompile(`^arn:aws:iam::\d+:role/`)),
resource.TestCheckResourceAttrPair(resourceName, "organization_aggregation_source.0.role_arn", "aws_iam_role.r", "arn"),
resource.TestCheckResourceAttr(resourceName, "organization_aggregation_source.0.all_regions", "true"),
),
},
Expand Down
8 changes: 4 additions & 4 deletions aws/resource_aws_config_configuration_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/configservice"
)

Expand Down Expand Up @@ -99,7 +98,7 @@ func resourceAwsConfigConfigurationRecorderRead(d *schema.ResourceData, meta int
}
out, err := conn.DescribeConfigurationRecorders(&input)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchConfigurationRecorderException" {
if isAWSErr(err, configservice.ErrCodeNoSuchConfigurationRecorderException, "") {
log.Printf("[WARN] Configuration Recorder %q is gone (NoSuchConfigurationRecorderException)", d.Id())
d.SetId("")
return nil
Expand Down Expand Up @@ -142,8 +141,9 @@ func resourceAwsConfigConfigurationRecorderDelete(d *schema.ResourceData, meta i
}
_, err := conn.DeleteConfigurationRecorder(&input)
if err != nil {
return fmt.Errorf("Deleting Configuration Recorder failed: %s", err)
if !isAWSErr(err, configservice.ErrCodeNoSuchConfigurationRecorderException, "") {
return fmt.Errorf("Deleting Configuration Recorder failed: %s", err)
}
}

return nil
}
11 changes: 4 additions & 7 deletions aws/resource_aws_config_configuration_recorder_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/configservice"
)

Expand Down Expand Up @@ -79,12 +78,10 @@ func resourceAwsConfigConfigurationRecorderStatusRead(d *schema.ResourceData, me
}
statusOut, err := conn.DescribeConfigurationRecorderStatus(&statusInput)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NoSuchConfigurationRecorderException" {
log.Printf("[WARN] Configuration Recorder (status) %q is gone (NoSuchConfigurationRecorderException)", name)
d.SetId("")
return nil
}
if isAWSErr(err, configservice.ErrCodeNoSuchConfigurationRecorderException, "") {
log.Printf("[WARN] Configuration Recorder (status) %q is gone (NoSuchConfigurationRecorderException)", name)
d.SetId("")
return nil
}
return fmt.Errorf("Failed describing Configuration Recorder %q status: %s",
name, err)
Expand Down
4 changes: 0 additions & 4 deletions aws/resource_aws_config_configuration_recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func testAccConfigConfigurationRecorder_basic(t *testing.T) {
testAccCheckConfigConfigurationRecorderRoleArn("aws_config_configuration_recorder.foo",
regexp.MustCompile(`arn:aws:iam::[0-9]{12}:role/`+expectedRoleName), &cr),
resource.TestCheckResourceAttr("aws_config_configuration_recorder.foo", "name", expectedName),
resource.TestMatchResourceAttr("aws_config_configuration_recorder.foo", "role_arn",
regexp.MustCompile(`arn:aws:iam::[0-9]{12}:role/`+expectedRoleName)),
),
},
},
Expand All @@ -109,8 +107,6 @@ func testAccConfigConfigurationRecorder_allParams(t *testing.T) {
testAccCheckConfigConfigurationRecorderRoleArn("aws_config_configuration_recorder.foo",
regexp.MustCompile(`arn:aws:iam::[0-9]{12}:role/`+expectedRoleName), &cr),
resource.TestCheckResourceAttr("aws_config_configuration_recorder.foo", "name", expectedName),
resource.TestMatchResourceAttr("aws_config_configuration_recorder.foo", "role_arn",
regexp.MustCompile(`arn:aws:iam::[0-9]{12}:role/`+expectedRoleName)),
resource.TestCheckResourceAttr("aws_config_configuration_recorder.foo", "recording_group.#", "1"),
resource.TestCheckResourceAttr("aws_config_configuration_recorder.foo", "recording_group.0.all_supported", "false"),
resource.TestCheckResourceAttr("aws_config_configuration_recorder.foo", "recording_group.0.include_global_resource_types", "false"),
Expand Down
17 changes: 8 additions & 9 deletions aws/resource_aws_config_delivery_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -97,11 +96,11 @@ func testAccConfigDeliveryChannel_basic(t *testing.T) {
}

func testAccConfigDeliveryChannel_allParams(t *testing.T) {
resourceName := "aws_config_delivery_channel.foo"
var dc configservice.DeliveryChannel
rInt := acctest.RandInt()
expectedName := fmt.Sprintf("tf-acc-test-awsconfig-%d", rInt)
expectedBucketName := fmt.Sprintf("tf-acc-test-awsconfig-%d", rInt)
expectedSnsTopicArn := regexp.MustCompile(fmt.Sprintf("arn:aws:sns:[a-z0-9-]+:[0-9]{12}:tf-acc-test-%d", rInt))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -111,13 +110,13 @@ func testAccConfigDeliveryChannel_allParams(t *testing.T) {
{
Config: testAccConfigDeliveryChannelConfig_allParams(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckConfigDeliveryChannelExists("aws_config_delivery_channel.foo", &dc),
testAccCheckConfigDeliveryChannelName("aws_config_delivery_channel.foo", expectedName, &dc),
resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "name", expectedName),
resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "s3_bucket_name", expectedBucketName),
resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "s3_key_prefix", "one/two/three"),
resource.TestMatchResourceAttr("aws_config_delivery_channel.foo", "sns_topic_arn", expectedSnsTopicArn),
resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "snapshot_delivery_properties.0.delivery_frequency", "Six_Hours"),
testAccCheckConfigDeliveryChannelExists(resourceName, &dc),
testAccCheckConfigDeliveryChannelName(resourceName, expectedName, &dc),
resource.TestCheckResourceAttr(resourceName, "name", expectedName),
resource.TestCheckResourceAttr(resourceName, "s3_bucket_name", expectedBucketName),
resource.TestCheckResourceAttr(resourceName, "s3_key_prefix", "one/two/three"),
resource.TestCheckResourceAttrPair(resourceName, "sns_topic_arn", "aws_sns_topic.t", "arn"),
resource.TestCheckResourceAttr(resourceName, "snapshot_delivery_properties.0.delivery_frequency", "Six_Hours"),
),
},
},
Expand Down

0 comments on commit 6ee2c4b

Please sign in to comment.