From ba26435c337ff068c80764697c31a88c1cadd1f8 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 10 Nov 2020 19:51:08 -0500 Subject: [PATCH] tests/service/elasticache: Remove hardcoded us-east-1 handling (#16034) Reference: https://github.com/hashicorp/terraform-provider-aws/issues/8316 Reference: https://github.com/hashicorp/terraform-provider-aws/issues/15737 Reference: https://github.com/hashicorp/terraform-provider-aws/issues/15791 Previously in AWS GovCloud (US): ``` === RUN TestAccAWSElasticacheCluster_SecurityGroup_Ec2Classic TestAccAWSElasticacheCluster_SecurityGroup_Ec2Classic: provider_test.go:184: [{0 error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid. status code: 403, request id: 9df5bb8f-fad4-4e95-9262-c306e610a3cf []}] --- FAIL: TestAccAWSElasticacheCluster_SecurityGroup_Ec2Classic (0.32s) === CONT TestAccAWSElasticacheSecurityGroup_basic TestAccAWSElasticacheSecurityGroup_basic: provider_test.go:184: [{0 error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid. status code: 403, request id: 925ee8e0-4c50-4e40-a1aa-115a09a1603c []}] --- FAIL: TestAccAWSElasticacheSecurityGroup_basic (0.43s) ``` Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSElasticacheCluster_SecurityGroup_Ec2Classic (533.02s) --- PASS: TestAccAWSElasticacheSecurityGroup_basic (13.61s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAWSElasticacheCluster_SecurityGroup_Ec2Classic (3.28s) --- SKIP: TestAccAWSElasticacheSecurityGroup_basic (2.85s) ``` --- aws/resource_aws_elasticache_cluster_test.go | 67 ++++++++++++++----- ...rce_aws_elasticache_security_group_test.go | 38 +++++------ 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/aws/resource_aws_elasticache_cluster_test.go b/aws/resource_aws_elasticache_cluster_test.go index aad64a1813e..7650ce6f4d3 100644 --- a/aws/resource_aws_elasticache_cluster_test.go +++ b/aws/resource_aws_elasticache_cluster_test.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "log" - "os" "regexp" "strconv" "strings" @@ -216,24 +215,21 @@ func TestAccAWSElasticacheCluster_Port(t *testing.T) { } func TestAccAWSElasticacheCluster_SecurityGroup_Ec2Classic(t *testing.T) { - oldvar := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", "us-east-1") - defer os.Setenv("AWS_DEFAULT_REGION", oldvar) - var ec elasticache.CacheCluster resourceName := "aws_elasticache_cluster.test" resourceSecurityGroupName := "aws_elasticache_security_group.test" + rName := acctest.RandomWithPrefix("tf-acc-test") - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSElasticacheClusterConfig_SecurityGroup_Ec2Classic, + Config: testAccAWSElasticacheClusterConfig_SecurityGroup_Ec2Classic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticacheSecurityGroupExists(resourceSecurityGroupName), - testAccCheckAWSElasticacheClusterExists(resourceName, &ec), + testAccCheckAWSElasticacheClusterEc2ClassicExists(resourceName, &ec), resource.TestCheckResourceAttr(resourceName, "cache_nodes.0.id", "0001"), resource.TestCheckResourceAttrSet(resourceName, "configuration_endpoint"), resource.TestCheckResourceAttrSet(resourceName, "cluster_address"), @@ -795,6 +791,41 @@ func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheClust } } +func testAccCheckAWSElasticacheClusterEc2ClassicExists(n string, v *elasticache.CacheCluster) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No cache cluster ID is set") + } + + conn := testAccProviderEc2Classic.Meta().(*AWSClient).elasticacheconn + + input := &elasticache.DescribeCacheClustersInput{ + CacheClusterId: aws.String(rs.Primary.ID), + } + + output, err := conn.DescribeCacheClusters(input) + + if err != nil { + return fmt.Errorf("error describing Elasticache Cluster (%s): %w", rs.Primary.ID, err) + } + + for _, c := range output.CacheClusters { + if aws.StringValue(c.CacheClusterId) == rs.Primary.ID { + *v = *c + + return nil + } + } + + return fmt.Errorf("Elasticache Cluster (%s) not found", rs.Primary.ID) + } +} + func testAccAWSElasticacheClusterConfig_Engine_Memcached(rName string) string { return fmt.Sprintf(` resource "aws_elasticache_cluster" "test" { @@ -842,9 +873,12 @@ resource "aws_elasticache_cluster" "test" { `, rName, port) } -var testAccAWSElasticacheClusterConfig_SecurityGroup_Ec2Classic = fmt.Sprintf(` +func testAccAWSElasticacheClusterConfig_SecurityGroup_Ec2Classic(rName string) string { + return composeConfig( + testAccEc2ClassicRegionProviderConfig(), + fmt.Sprintf(` resource "aws_security_group" "test" { - name = "tf-test-security-group-%03d" + name = %[1]q description = "tf-test-security-group-descr" ingress { @@ -860,13 +894,13 @@ resource "aws_security_group" "test" { } resource "aws_elasticache_security_group" "test" { - name = "tf-test-security-group-%03d" + name = %[1]q description = "tf-test-security-group-descr" security_group_names = [aws_security_group.test.name] } resource "aws_elasticache_cluster" "test" { - cluster_id = "tf-%s" + cluster_id = %[1]q engine = "memcached" # tflint-ignore: aws_elasticache_cluster_previous_type @@ -875,7 +909,8 @@ resource "aws_elasticache_cluster" "test" { port = 11211 security_group_names = [aws_elasticache_security_group.test.name] } -`, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10)) +`, rName)) +} func testAccAWSElasticacheClusterConfig_snapshots(rName string) string { return fmt.Sprintf(` diff --git a/aws/resource_aws_elasticache_security_group_test.go b/aws/resource_aws_elasticache_security_group_test.go index 54ee8c89f5d..27cc09cd8b6 100644 --- a/aws/resource_aws_elasticache_security_group_test.go +++ b/aws/resource_aws_elasticache_security_group_test.go @@ -3,7 +3,6 @@ package aws import ( "fmt" "log" - "os" "testing" "github.com/aws/aws-sdk-go/aws" @@ -67,24 +66,19 @@ func testSweepElasticacheCacheSecurityGroups(region string) error { } func TestAccAWSElasticacheSecurityGroup_basic(t *testing.T) { - // Use EC2-Classic enabled us-east-1 for testing - oldRegion := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", "us-east-1") - defer os.Setenv("AWS_DEFAULT_REGION", oldRegion) - + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_elasticache_security_group.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSElasticacheSecurityGroupDestroy, + PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckAWSElasticacheSecurityGroupDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSElasticacheSecurityGroupConfig, + Config: testAccAWSElasticacheSecurityGroupConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSElasticacheSecurityGroupExists(resourceName), - resource.TestCheckResourceAttr( - resourceName, "description", "Managed by Terraform"), + resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"), ), }, { @@ -97,7 +91,7 @@ func TestAccAWSElasticacheSecurityGroup_basic(t *testing.T) { } func testAccCheckAWSElasticacheSecurityGroupDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).elasticacheconn + conn := testAccProviderEc2Classic.Meta().(*AWSClient).elasticacheconn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_elasticache_security_group" { @@ -129,7 +123,7 @@ func testAccCheckAWSElasticacheSecurityGroupExists(n string) resource.TestCheckF return fmt.Errorf("No cache security group ID is set") } - conn := testAccProvider.Meta().(*AWSClient).elasticacheconn + conn := testAccProviderEc2Classic.Meta().(*AWSClient).elasticacheconn _, err := conn.DescribeCacheSecurityGroups(&elasticache.DescribeCacheSecurityGroupsInput{ CacheSecurityGroupName: aws.String(rs.Primary.ID), }) @@ -140,13 +134,12 @@ func testAccCheckAWSElasticacheSecurityGroupExists(n string) resource.TestCheckF } } -var testAccAWSElasticacheSecurityGroupConfig = fmt.Sprintf(` -provider "aws" { - region = "us-east-1" -} - +func testAccAWSElasticacheSecurityGroupConfig(rName string) string { + return composeConfig( + testAccEc2ClassicRegionProviderConfig(), + fmt.Sprintf(` resource "aws_security_group" "test" { - name = "tf-test-security-group-%03d" + name = %[1]q ingress { from_port = -1 @@ -157,7 +150,8 @@ resource "aws_security_group" "test" { } resource "aws_elasticache_security_group" "test" { - name = "tf-test-security-group-%03d" + name = %[1]q security_group_names = [aws_security_group.test.name] } -`, acctest.RandInt(), acctest.RandInt()) +`, rName)) +}