Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/service/elasticache: Remove hardcoded us-east-1 handling #16034

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}