provider/aws: Randomize names in code deploy test to reduce chances of name collision on tests

This commit is contained in:
clint shryock 2016-06-09 15:39:35 -05:00
parent 2c7b702d1f
commit 253a46b573
1 changed files with 98 additions and 62 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
@ -18,19 +19,21 @@ import (
func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
var group codedeploy.DeploymentGroupInfo
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup,
Config: testAccAWSCodeDeployDeploymentGroup(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app"),
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo"),
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
@ -45,13 +48,13 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
),
},
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroupModified,
Config: testAccAWSCodeDeployDeploymentGroupModified(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app"),
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "bar"),
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "bar_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
@ -72,19 +75,21 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) {
var group codedeploy.DeploymentGroupInfo
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroupOnPremiseTags,
Config: testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app"),
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo"),
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
@ -105,32 +110,34 @@ func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) {
func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_basic(t *testing.T) {
var group codedeploy.DeploymentGroupInfo
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create,
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app"),
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group"),
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
testAccCheckTriggerEvents(&group, "foo-trigger", []string{
"DeploymentFailure",
}),
),
},
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update,
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app"),
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group"),
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
testAccCheckTriggerEvents(&group, "foo-trigger", []string{
"DeploymentFailure",
"DeploymentSuccess",
@ -144,19 +151,21 @@ func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_basic(t *testing.T
func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_multiple(t *testing.T) {
var group codedeploy.DeploymentGroupInfo
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple,
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app"),
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group"),
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
testAccCheckTriggerEvents(&group, "foo-trigger", []string{
"DeploymentFailure",
}),
@ -164,17 +173,17 @@ func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_multiple(t *testin
"InstanceFailure",
}),
testAccCheckTriggerTargetArn(&group, "bar-trigger",
regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:bar-topic$")),
regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:bar-topic-"+rName+"$")),
),
},
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple,
Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app"),
"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group"),
"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
testAccCheckTriggerEvents(&group, "foo-trigger", []string{
"DeploymentFailure",
"DeploymentStart",
@ -185,7 +194,7 @@ func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_multiple(t *testin
"InstanceFailure",
}),
testAccCheckTriggerTargetArn(&group, "bar-trigger",
regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:baz-topic$")),
regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:baz-topic-"+rName+"$")),
),
},
},
@ -422,13 +431,14 @@ func testAccCheckAWSCodeDeployDeploymentGroupExists(name string, group *codedepl
}
}
var testAccAWSCodeDeployDeploymentGroup = `
func testAccAWSCodeDeployDeploymentGroup(rName string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo_app" {
name = "foo_app"
name = "foo_app_%s"
}
resource "aws_iam_role_policy" "foo_policy" {
name = "foo_policy"
name = "foo_policy_%s"
role = "${aws_iam_role.foo_role.id}"
policy = <<EOF
{
@ -456,7 +466,7 @@ EOF
}
resource "aws_iam_role" "foo_role" {
name = "foo_role"
name = "foo_role_%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
@ -478,22 +488,24 @@ EOF
resource "aws_codedeploy_deployment_group" "foo" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo"
deployment_group_name = "foo_%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "filtervalue"
}
}`
}`, rName, rName, rName, rName)
}
var testAccAWSCodeDeployDeploymentGroupModified = `
func testAccAWSCodeDeployDeploymentGroupModified(rName string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo_app" {
name = "foo_app"
name = "foo_app_%s"
}
resource "aws_iam_role_policy" "foo_policy" {
name = "foo_policy"
name = "foo_policy_%s"
role = "${aws_iam_role.foo_role.id}"
policy = <<EOF
{
@ -521,7 +533,7 @@ EOF
}
resource "aws_iam_role" "foo_role" {
name = "foo_role"
name = "foo_role_%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
@ -543,22 +555,24 @@ EOF
resource "aws_codedeploy_deployment_group" "foo" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "bar"
deployment_group_name = "bar_%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "anotherfiltervalue"
}
}`
}`, rName, rName, rName, rName)
}
var testAccAWSCodeDeployDeploymentGroupOnPremiseTags = `
func testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo_app" {
name = "foo_app"
name = "foo_app_%s"
}
resource "aws_iam_role_policy" "foo_policy" {
name = "foo_policy"
name = "foo_policy_%s"
role = "${aws_iam_role.foo_role.id}"
policy = <<EOF
{
@ -586,7 +600,7 @@ EOF
}
resource "aws_iam_role" "foo_role" {
name = "foo_role"
name = "foo_role_%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
@ -608,22 +622,24 @@ EOF
resource "aws_codedeploy_deployment_group" "foo" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo"
deployment_group_name = "foo_%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
on_premises_instance_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "filtervalue"
}
}`
}`, rName, rName, rName, rName)
}
const baseCodeDeployConfig = `
func baseCodeDeployConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo_app" {
name = "foo-app"
name = "foo-app-%s"
}
resource "aws_iam_role_policy" "foo_policy" {
name = "foo-policy"
name = "foo-policy-%s"
role = "${aws_iam_role.foo_role.id}"
policy = <<EOF
{
@ -638,6 +654,7 @@ resource "aws_iam_role_policy" "foo_policy" {
"autoscaling:DescribeLifecycleHooks",
"autoscaling:PutLifecycleHook",
"autoscaling:RecordLifecycleActionHeartbeat",
"codedeploy:*",
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"tag:GetTags",
@ -652,7 +669,7 @@ EOF
}
resource "aws_iam_role" "foo_role" {
name = "foo-role"
name = "foo-role-%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
@ -671,15 +688,18 @@ EOF
}
resource "aws_sns_topic" "foo_topic" {
name = "foo-topic"
name = "foo-topic-%s"
}`, rName, rName, rName, rName)
}
`
func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create(rName string) string {
return fmt.Sprintf(`
%s
const testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create = baseCodeDeployConfig + `
resource "aws_codedeploy_deployment_group" "foo_group" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo-group"
deployment_group_name = "foo-group-%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
trigger_configuration {
@ -687,12 +707,17 @@ resource "aws_codedeploy_deployment_group" "foo_group" {
trigger_name = "foo-trigger"
trigger_target_arn = "${aws_sns_topic.foo_topic.arn}"
}
}`
}`, baseCodeDeployConfig(rName), rName)
}
func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update(rName string) string {
return fmt.Sprintf(`
%s
const testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update = baseCodeDeployConfig + `
resource "aws_codedeploy_deployment_group" "foo_group" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo-group"
deployment_group_name = "foo-group-%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
trigger_configuration {
@ -700,16 +725,21 @@ resource "aws_codedeploy_deployment_group" "foo_group" {
trigger_name = "foo-trigger"
trigger_target_arn = "${aws_sns_topic.foo_topic.arn}"
}
}`
}`, baseCodeDeployConfig(rName), rName)
}
func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple(rName string) string {
return fmt.Sprintf(`
%s
const testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple = baseCodeDeployConfig + `
resource "aws_sns_topic" "bar_topic" {
name = "bar-topic"
name = "bar-topic-%s"
}
resource "aws_codedeploy_deployment_group" "foo_group" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo-group"
deployment_group_name = "foo-group-%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
trigger_configuration {
@ -723,20 +753,25 @@ resource "aws_codedeploy_deployment_group" "foo_group" {
trigger_name = "bar-trigger"
trigger_target_arn = "${aws_sns_topic.bar_topic.arn}"
}
}`
}`, baseCodeDeployConfig(rName), rName, rName)
}
func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple(rName string) string {
return fmt.Sprintf(`
%s
const testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple = baseCodeDeployConfig + `
resource "aws_sns_topic" "bar_topic" {
name = "bar-topic"
name = "bar-topic-%s"
}
resource "aws_sns_topic" "baz_topic" {
name = "baz-topic"
name = "baz-topic-%s"
}
resource "aws_codedeploy_deployment_group" "foo_group" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo-group"
deployment_group_name = "foo-group-%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
trigger_configuration {
@ -750,4 +785,5 @@ resource "aws_codedeploy_deployment_group" "foo_group" {
trigger_name = "bar-trigger"
trigger_target_arn = "${aws_sns_topic.baz_topic.arn}"
}
}`
}`, baseCodeDeployConfig(rName), rName, rName, rName)
}