provider/aws: Make iam_user_policy_attachment_test work as expected: (#9733)

Was failing due to using IAM user `test-name` as it was being used in
more than 1 place - this has been replaced by a random user and random
policy names now

```
% make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSUserPolicyAttachment_basic'                                                                               2 ↵ ✹
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/10/31 08:39:08 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSUserPolicyAttachment_basic -timeout 120m
=== RUN   TestAccAWSUserPolicyAttachment_basic
--- PASS: TestAccAWSUserPolicyAttachment_basic (32.04s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/aws	32.053s
```
This commit is contained in:
Paul Stack 2016-10-31 09:52:38 +00:00 committed by GitHub
parent 6a45056b56
commit 3accd5485a
1 changed files with 23 additions and 16 deletions

View File

@ -7,30 +7,35 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSUserPolicyAttachment_basic(t *testing.T) {
var out iam.ListAttachedUserPoliciesOutput
rName := acctest.RandString(10)
policyName1 := fmt.Sprintf("test-policy-%s", acctest.RandString(10))
policyName2 := fmt.Sprintf("test-policy-%s", acctest.RandString(10))
policyName3 := fmt.Sprintf("test-policy-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSUserPolicyAttachmentDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSUserPolicyAttachConfig,
{
Config: testAccAWSUserPolicyAttachConfig(rName, policyName1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserPolicyAttachmentExists("aws_iam_user_policy_attachment.test-attach", 1, &out),
testAccCheckAWSUserPolicyAttachmentAttributes([]string{"test-policy"}, &out),
testAccCheckAWSUserPolicyAttachmentAttributes([]string{policyName1}, &out),
),
},
resource.TestStep{
Config: testAccAWSUserPolicyAttachConfigUpdate,
{
Config: testAccAWSUserPolicyAttachConfigUpdate(rName, policyName1, policyName2, policyName3),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserPolicyAttachmentExists("aws_iam_user_policy_attachment.test-attach", 2, &out),
testAccCheckAWSUserPolicyAttachmentAttributes([]string{"test-policy2", "test-policy3"}, &out),
testAccCheckAWSUserPolicyAttachmentAttributes([]string{policyName2, policyName3}, &out),
),
},
},
@ -88,13 +93,14 @@ func testAccCheckAWSUserPolicyAttachmentAttributes(policies []string, out *iam.L
}
}
const testAccAWSUserPolicyAttachConfig = `
func testAccAWSUserPolicyAttachConfig(rName, policyName string) string {
return fmt.Sprintf(`
resource "aws_iam_user" "user" {
name = "test-user"
name = "test-user-%s"
}
resource "aws_iam_policy" "policy" {
name = "test-policy"
name = "%s"
description = "A test policy"
policy = <<EOF
{
@ -115,16 +121,17 @@ EOF
resource "aws_iam_user_policy_attachment" "test-attach" {
user = "${aws_iam_user.user.name}"
policy_arn = "${aws_iam_policy.policy.arn}"
}`, rName, policyName)
}
`
const testAccAWSUserPolicyAttachConfigUpdate = `
func testAccAWSUserPolicyAttachConfigUpdate(rName, policyName1, policyName2, policyName3 string) string {
return fmt.Sprintf(`
resource "aws_iam_user" "user" {
name = "test-user"
name = "test-user-%s"
}
resource "aws_iam_policy" "policy" {
name = "test-policy"
name = "%s"
description = "A test policy"
policy = <<EOF
{
@ -143,7 +150,7 @@ EOF
}
resource "aws_iam_policy" "policy2" {
name = "test-policy2"
name = "%s"
description = "A test policy"
policy = <<EOF
{
@ -162,7 +169,7 @@ EOF
}
resource "aws_iam_policy" "policy3" {
name = "test-policy3"
name = "%s"
description = "A test policy"
policy = <<EOF
{
@ -188,5 +195,5 @@ resource "aws_iam_user_policy_attachment" "test-attach" {
resource "aws_iam_user_policy_attachment" "test-attach2" {
user = "${aws_iam_user.user.name}"
policy_arn = "${aws_iam_policy.policy3.arn}"
}`, rName, policyName1, policyName2, policyName3)
}
`