Skip to content

Commit

Permalink
tests/service/elasticache: Remove hardcoded us-east-1 handling (#16034)
Browse files Browse the repository at this point in the history
Reference: #8316
Reference: #15737
Reference: #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)
```
  • Loading branch information
bflad committed Nov 11, 2020
1 parent 5ec1482 commit ba26435
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
67 changes: 51 additions & 16 deletions aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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(`
Expand Down
38 changes: 16 additions & 22 deletions aws/resource_aws_elasticache_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"fmt"
"log"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -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"),
),
},
{
Expand All @@ -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" {
Expand Down Expand Up @@ -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),
})
Expand All @@ -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
Expand All @@ -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))
}

0 comments on commit ba26435

Please sign in to comment.