provider/aws: More randomization to our Acc tests (#6124)

* provider/aws: Add more Randomization to DB Parameter Group Tests, to avoid collisions

* provider/aws: Add more randomization to Autoscaling group tests
This commit is contained in:
Clint 2016-04-11 12:06:28 -05:00
parent e9a918f83d
commit 2ea8c64079
2 changed files with 57 additions and 43 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling" "github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -18,21 +19,23 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
var group autoscaling.Group var group autoscaling.Group
var lc autoscaling.LaunchConfiguration var lc autoscaling.LaunchConfiguration
randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig, Config: testAccAWSAutoScalingGroupConfig(randName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2), testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2),
testAccCheckAWSAutoScalingGroupAttributes(&group), testAccCheckAWSAutoScalingGroupAttributes(&group, randName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "name", "foobar3-terraform-test"), "aws_autoscaling_group.bar", "name", randName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "max_size", "5"), "aws_autoscaling_group.bar", "max_size", "5"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -53,7 +56,7 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
}, },
resource.TestStep{ resource.TestStep{
Config: testAccAWSAutoScalingGroupConfigUpdate, Config: testAccAWSAutoScalingGroupConfigUpdate(randName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc), testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc),
@ -139,13 +142,15 @@ func TestAccAWSAutoScalingGroup_terminationPolicies(t *testing.T) {
func TestAccAWSAutoScalingGroup_tags(t *testing.T) { func TestAccAWSAutoScalingGroup_tags(t *testing.T) {
var group autoscaling.Group var group autoscaling.Group
randName := fmt.Sprintf("tfautotags-%s", acctest.RandString(5))
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig, Config: testAccAWSAutoScalingGroupConfig(randName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAutoscalingTags(&group.Tags, "Foo", map[string]interface{}{ testAccCheckAutoscalingTags(&group.Tags, "Foo", map[string]interface{}{
@ -156,7 +161,7 @@ func TestAccAWSAutoScalingGroup_tags(t *testing.T) {
}, },
resource.TestStep{ resource.TestStep{
Config: testAccAWSAutoScalingGroupConfigUpdate, Config: testAccAWSAutoScalingGroupConfigUpdate(randName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAutoscalingTagNotExists(&group.Tags, "Foo"), testAccCheckAutoscalingTagNotExists(&group.Tags, "Foo"),
@ -217,7 +222,7 @@ func TestAccAWSAutoScalingGroup_WithLoadBalancer(t *testing.T) {
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: fmt.Sprintf(testAccAWSAutoScalingGroupConfigWithLoadBalancer), Config: testAccAWSAutoScalingGroupConfigWithLoadBalancer,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group), testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group),
@ -230,17 +235,18 @@ func TestAccAWSAutoScalingGroup_WithLoadBalancer(t *testing.T) {
func TestAccAWSAutoScalingGroup_withPlacementGroup(t *testing.T) { func TestAccAWSAutoScalingGroup_withPlacementGroup(t *testing.T) {
var group autoscaling.Group var group autoscaling.Group
randName := fmt.Sprintf("tf_placement_test-%s", acctest.RandString(5))
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_withPlacementGroup, Config: testAccAWSAutoScalingGroupConfig_withPlacementGroup(randName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "placement_group", "tf_placement_test"), "aws_autoscaling_group.bar", "placement_group", randName),
), ),
}, },
}, },
@ -310,14 +316,14 @@ func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group) resource.TestCheckFunc { func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group, name string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if *group.AvailabilityZones[0] != "us-west-2a" { if *group.AvailabilityZones[0] != "us-west-2a" {
return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0]) return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0])
} }
if *group.AutoScalingGroupName != "foobar3-terraform-test" { if *group.AutoScalingGroupName != name {
return fmt.Errorf("Bad name: %s", *group.AutoScalingGroupName) return fmt.Errorf("Bad Autoscaling Group name, expected (%s), got (%s)", name, *group.AutoScalingGroupName)
} }
if *group.MaxSize != 5 { if *group.MaxSize != 5 {
@ -539,20 +545,21 @@ resource "aws_autoscaling_group" "bar" {
} }
` `
const testAccAWSAutoScalingGroupConfig = ` func testAccAWSAutoScalingGroupConfig(name string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" { resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11" image_id = "ami-21f78e11"
instance_type = "t1.micro" instance_type = "t1.micro"
} }
resource "aws_placement_group" "test" { resource "aws_placement_group" "test" {
name = "test" name = "%s"
strategy = "cluster" strategy = "cluster"
} }
resource "aws_autoscaling_group" "bar" { resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"] availability_zones = ["us-west-2a"]
name = "foobar3-terraform-test" name = "%s"
max_size = 5 max_size = 5
min_size = 2 min_size = 2
health_check_type = "ELB" health_check_type = "ELB"
@ -568,9 +575,11 @@ resource "aws_autoscaling_group" "bar" {
propagate_at_launch = true propagate_at_launch = true
} }
} }
` `, name, name)
}
const testAccAWSAutoScalingGroupConfigUpdate = ` func testAccAWSAutoScalingGroupConfigUpdate(name string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" { resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11" image_id = "ami-21f78e11"
instance_type = "t1.micro" instance_type = "t1.micro"
@ -583,7 +592,7 @@ resource "aws_launch_configuration" "new" {
resource "aws_autoscaling_group" "bar" { resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"] availability_zones = ["us-west-2a"]
name = "foobar3-terraform-test" name = "%s"
max_size = 5 max_size = 5
min_size = 2 min_size = 2
health_check_grace_period = 300 health_check_grace_period = 300
@ -600,7 +609,8 @@ resource "aws_autoscaling_group" "bar" {
propagate_at_launch = true propagate_at_launch = true
} }
} }
` `, name)
}
const testAccAWSAutoScalingGroupConfigWithLoadBalancer = ` const testAccAWSAutoScalingGroupConfigWithLoadBalancer = `
resource "aws_vpc" "foo" { resource "aws_vpc" "foo" {
@ -668,7 +678,6 @@ resource "aws_launch_configuration" "foobar" {
resource "aws_autoscaling_group" "bar" { resource "aws_autoscaling_group" "bar" {
availability_zones = ["${aws_subnet.foo.availability_zone}"] availability_zones = ["${aws_subnet.foo.availability_zone}"]
vpc_zone_identifier = ["${aws_subnet.foo.id}"] vpc_zone_identifier = ["${aws_subnet.foo.id}"]
name = "foobar3-terraform-test"
max_size = 2 max_size = 2
min_size = 2 min_size = 2
health_check_grace_period = 300 health_check_grace_period = 300
@ -747,20 +756,21 @@ resource "aws_autoscaling_group" "bar" {
} }
` `
const testAccAWSAutoScalingGroupConfig_withPlacementGroup = ` func testAccAWSAutoScalingGroupConfig_withPlacementGroup(name string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" { resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11" image_id = "ami-21f78e11"
instance_type = "c3.large" instance_type = "c3.large"
} }
resource "aws_placement_group" "test" { resource "aws_placement_group" "test" {
name = "tf_placement_test" name = "%s"
strategy = "cluster" strategy = "cluster"
} }
resource "aws_autoscaling_group" "bar" { resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"] availability_zones = ["us-west-2a"]
name = "foobar3-terraform-test" name = "%s"
max_size = 1 max_size = 1
min_size = 1 min_size = 1
health_check_grace_period = 300 health_check_grace_period = 300
@ -778,7 +788,8 @@ resource "aws_autoscaling_group" "bar" {
propagate_at_launch = true propagate_at_launch = true
} }
} }
` `, name, name)
}
const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = `
resource "aws_launch_configuration" "foobar" { resource "aws_launch_configuration" "foobar" {

View File

@ -17,18 +17,20 @@ import (
func TestAccAWSDBParameterGroup_basic(t *testing.T) { func TestAccAWSDBParameterGroup_basic(t *testing.T) {
var v rds.DBParameterGroup var v rds.DBParameterGroup
groupName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBParameterGroupDestroy, CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSDBParameterGroupConfig(acctest.RandInt()), Config: testAccAWSDBParameterGroupConfig(groupName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
testAccCheckAWSDBParameterGroupAttributes(&v), testAccCheckAWSDBParameterGroupAttributes(&v, groupName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"), "aws_db_parameter_group.bar", "name", groupName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "family", "mysql5.6"), "aws_db_parameter_group.bar", "family", "mysql5.6"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -50,12 +52,12 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) {
), ),
}, },
resource.TestStep{ resource.TestStep{
Config: testAccAWSDBParameterGroupAddParametersConfig(acctest.RandInt()), Config: testAccAWSDBParameterGroupAddParametersConfig(groupName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
testAccCheckAWSDBParameterGroupAttributes(&v), testAccCheckAWSDBParameterGroupAttributes(&v, groupName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"), "aws_db_parameter_group.bar", "name", groupName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "family", "mysql5.6"), "aws_db_parameter_group.bar", "family", "mysql5.6"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -88,21 +90,22 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) {
}) })
} }
func TestAccAWSDBParameterGroupOnly(t *testing.T) { func TestAccAWSDBParameterGroup_Only(t *testing.T) {
var v rds.DBParameterGroup var v rds.DBParameterGroup
groupName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBParameterGroupDestroy, CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSDBParameterGroupOnlyConfig(acctest.RandInt()), Config: testAccAWSDBParameterGroupOnlyConfig(groupName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
testAccCheckAWSDBParameterGroupAttributes(&v), testAccCheckAWSDBParameterGroupAttributes(&v, groupName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"), "aws_db_parameter_group.bar", "name", groupName),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "family", "mysql5.6"), "aws_db_parameter_group.bar", "family", "mysql5.6"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -187,11 +190,11 @@ func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckAWSDBParameterGroupAttributes(v *rds.DBParameterGroup) resource.TestCheckFunc { func testAccCheckAWSDBParameterGroupAttributes(v *rds.DBParameterGroup, name string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if *v.DBParameterGroupName != "parameter-group-test-terraform" { if *v.DBParameterGroupName != name {
return fmt.Errorf("bad name: %#v", v.DBParameterGroupName) return fmt.Errorf("Bad Parameter Group name, expected (%s), got (%s)", name, *v.DBParameterGroupName)
} }
if *v.DBParameterGroupFamily != "mysql5.6" { if *v.DBParameterGroupFamily != "mysql5.6" {
@ -250,10 +253,10 @@ func randomString(strlen int) string {
return string(result) return string(result)
} }
func testAccAWSDBParameterGroupConfig(n int) string { func testAccAWSDBParameterGroupConfig(n string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "aws_db_parameter_group" "bar" { resource "aws_db_parameter_group" "bar" {
name = "parameter-group-test-terraform-%d" name = "%s"
family = "mysql5.6" family = "mysql5.6"
description = "Test parameter group for terraform" description = "Test parameter group for terraform"
parameter { parameter {
@ -274,10 +277,10 @@ resource "aws_db_parameter_group" "bar" {
}`, n) }`, n)
} }
func testAccAWSDBParameterGroupAddParametersConfig(n int) string { func testAccAWSDBParameterGroupAddParametersConfig(n string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "aws_db_parameter_group" "bar" { resource "aws_db_parameter_group" "bar" {
name = "parameter-group-test-terraform-%d" name = "%s"
family = "mysql5.6" family = "mysql5.6"
description = "Test parameter group for terraform" description = "Test parameter group for terraform"
parameter { parameter {
@ -307,10 +310,10 @@ resource "aws_db_parameter_group" "bar" {
}`, n) }`, n)
} }
func testAccAWSDBParameterGroupOnlyConfig(n int) string { func testAccAWSDBParameterGroupOnlyConfig(n string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "aws_db_parameter_group" "bar" { resource "aws_db_parameter_group" "bar" {
name = "parameter-group-test-terraform-%d" name = "%s"
family = "mysql5.6" family = "mysql5.6"
description = "Test parameter group for terraform" description = "Test parameter group for terraform"
}`, n) }`, n)