provider/aws: Update IAM Role Policy Attachment Acctests

Leaked resources may prevent this resource from correctly passing acceptance tests. Seeding the policy names with random integer suffixes allows tests to pass regardless of resource leaks.

```
$ make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSRolePolicyAttachment_basic'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/03/22 19:58:58 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSRolePolicyAttachment_basic -timeout 120m
=== RUN   TestAccAWSRolePolicyAttachment_basic
--- PASS: TestAccAWSRolePolicyAttachment_basic (31.98s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/aws    31.989s
```
This commit is contained in:
Jake Champlin 2017-03-22 20:02:12 -04:00
parent c105784c60
commit ea40ef9596
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
1 changed files with 118 additions and 111 deletions

View File

@ -7,30 +7,35 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/iam"
"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"
) )
func TestAccAWSRolePolicyAttachment_basic(t *testing.T) { func TestAccAWSRolePolicyAttachment_basic(t *testing.T) {
var out iam.ListAttachedRolePoliciesOutput var out iam.ListAttachedRolePoliciesOutput
rInt := acctest.RandInt()
testPolicy := fmt.Sprintf("test-policy-%d", rInt)
testPolicy2 := fmt.Sprintf("test-policy2-%d", rInt)
testPolicy3 := fmt.Sprintf("test-policy3-%d", rInt)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRolePolicyAttachmentDestroy, CheckDestroy: testAccCheckAWSRolePolicyAttachmentDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccAWSRolePolicyAttachConfig, Config: testAccAWSRolePolicyAttachConfig(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 1, &out), testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 1, &out),
testAccCheckAWSRolePolicyAttachmentAttributes([]string{"test-policy"}, &out), testAccCheckAWSRolePolicyAttachmentAttributes([]string{testPolicy}, &out),
), ),
}, },
resource.TestStep{ {
Config: testAccAWSRolePolicyAttachConfigUpdate, Config: testAccAWSRolePolicyAttachConfigUpdate(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 2, &out), testAccCheckAWSRolePolicyAttachmentExists("aws_iam_role_policy_attachment.test-attach", 2, &out),
testAccCheckAWSRolePolicyAttachmentAttributes([]string{"test-policy2", "test-policy3"}, &out), testAccCheckAWSRolePolicyAttachmentAttributes([]string{testPolicy2, testPolicy3}, &out),
), ),
}, },
}, },
@ -88,135 +93,137 @@ func testAccCheckAWSRolePolicyAttachmentAttributes(policies []string, out *iam.L
} }
} }
const testAccAWSRolePolicyAttachConfig = ` func testAccAWSRolePolicyAttachConfig(rInt int) string {
resource "aws_iam_role" "role" { return fmt.Sprintf(`
name = "test-role" resource "aws_iam_role" "role" {
assume_role_policy = <<EOF name = "test-role-%d"
assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
{ {
"Action": "sts:AssumeRole", "Action": "sts:AssumeRole",
"Principal": { "Principal": {
"Service": "ec2.amazonaws.com" "Service": "ec2.amazonaws.com"
}, },
"Effect": "Allow", "Effect": "Allow",
"Sid": "" "Sid": ""
} }
] ]
} }
EOF EOF
} }
resource "aws_iam_policy" "policy" { resource "aws_iam_policy" "policy" {
name = "test-policy" name = "test-policy-%d"
description = "A test policy" description = "A test policy"
policy = <<EOF policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
{ {
"Action": [ "Action": [
"iam:ChangePassword" "iam:ChangePassword"
], ],
"Resource": "*", "Resource": "*",
"Effect": "Allow" "Effect": "Allow"
} }
] ]
} }
EOF EOF
}
resource "aws_iam_role_policy_attachment" "test-attach" {
role = "${aws_iam_role.role.name}"
policy_arn = "${aws_iam_policy.policy.arn}"
}`, rInt, rInt)
} }
resource "aws_iam_role_policy_attachment" "test-attach" { func testAccAWSRolePolicyAttachConfigUpdate(rInt int) string {
role = "${aws_iam_role.role.name}" return fmt.Sprintf(`
policy_arn = "${aws_iam_policy.policy.arn}" resource "aws_iam_role" "role" {
} name = "test-role-%d"
` assume_role_policy = <<EOF
const testAccAWSRolePolicyAttachConfigUpdate = `
resource "aws_iam_role" "role" {
name = "test-role"
assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
{ {
"Action": "sts:AssumeRole", "Action": "sts:AssumeRole",
"Principal": { "Principal": {
"Service": "ec2.amazonaws.com" "Service": "ec2.amazonaws.com"
}, },
"Effect": "Allow", "Effect": "Allow",
"Sid": "" "Sid": ""
} }
] ]
} }
EOF EOF
} }
resource "aws_iam_policy" "policy" { resource "aws_iam_policy" "policy" {
name = "test-policy" name = "test-policy-%d"
description = "A test policy" description = "A test policy"
policy = <<EOF policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
{ {
"Action": [ "Action": [
"iam:ChangePassword" "iam:ChangePassword"
], ],
"Resource": "*", "Resource": "*",
"Effect": "Allow" "Effect": "Allow"
} }
] ]
} }
EOF EOF
} }
resource "aws_iam_policy" "policy2" { resource "aws_iam_policy" "policy2" {
name = "test-policy2" name = "test-policy2-%d"
description = "A test policy" description = "A test policy"
policy = <<EOF policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
{ {
"Action": [ "Action": [
"iam:ChangePassword" "iam:ChangePassword"
], ],
"Resource": "*", "Resource": "*",
"Effect": "Allow" "Effect": "Allow"
} }
] ]
} }
EOF EOF
} }
resource "aws_iam_policy" "policy3" { resource "aws_iam_policy" "policy3" {
name = "test-policy3" name = "test-policy3-%d"
description = "A test policy" description = "A test policy"
policy = <<EOF policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
"Statement": [ "Statement": [
{ {
"Action": [ "Action": [
"iam:ChangePassword" "iam:ChangePassword"
], ],
"Resource": "*", "Resource": "*",
"Effect": "Allow" "Effect": "Allow"
} }
] ]
} }
EOF EOF
} }
resource "aws_iam_role_policy_attachment" "test-attach" { resource "aws_iam_role_policy_attachment" "test-attach" {
role = "${aws_iam_role.role.name}" role = "${aws_iam_role.role.name}"
policy_arn = "${aws_iam_policy.policy2.arn}" policy_arn = "${aws_iam_policy.policy2.arn}"
} }
resource "aws_iam_role_policy_attachment" "test-attach2" { resource "aws_iam_role_policy_attachment" "test-attach2" {
role = "${aws_iam_role.role.name}" role = "${aws_iam_role.role.name}"
policy_arn = "${aws_iam_policy.policy3.arn}" policy_arn = "${aws_iam_policy.policy3.arn}"
}`, rInt, rInt, rInt, rInt)
} }
`