From 643a52ab3e31593b32576740469eff5bb39b5bcd Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Thu, 2 Feb 2017 13:03:39 -0500 Subject: [PATCH] provider/aws: Improve IAM Group AccTests Improve the iam_group acceptance tests. Previously, the iam_group would overlap when running our tests in parallel. This fixes that. --- .../aws/import_aws_iam_group_test.go | 8 ++-- .../aws/resource_aws_iam_group_test.go | 48 +++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/builtin/providers/aws/import_aws_iam_group_test.go b/builtin/providers/aws/import_aws_iam_group_test.go index 76da67d25..7e94f116f 100644 --- a/builtin/providers/aws/import_aws_iam_group_test.go +++ b/builtin/providers/aws/import_aws_iam_group_test.go @@ -3,10 +3,12 @@ package aws import ( "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" ) func TestAccAWSIAMGroup_importBasic(t *testing.T) { + rInt := acctest.RandInt() resourceName := "aws_iam_group.group" resource.Test(t, resource.TestCase{ @@ -14,11 +16,11 @@ func TestAccAWSIAMGroup_importBasic(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckAWSGroupDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccAWSGroupConfig, + { + Config: testAccAWSGroupConfig(rInt), }, - resource.TestStep{ + { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, diff --git a/builtin/providers/aws/resource_aws_iam_group_test.go b/builtin/providers/aws/resource_aws_iam_group_test.go index 00880a015..c5c27ef71 100644 --- a/builtin/providers/aws/resource_aws_iam_group_test.go +++ b/builtin/providers/aws/resource_aws_iam_group_test.go @@ -1,12 +1,14 @@ package aws import ( + "errors" "fmt" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "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" ) @@ -24,9 +26,9 @@ func TestValidateIamGroupName(t *testing.T) { "test+group@hashicorp.com", } for _, v := range validNames { - _, errors := validateAwsIamGroupName(v, "name") - if len(errors) != 0 { - t.Fatalf("%q should be a valid IAM Group name: %q", v, errors) + _, errs := validateAwsIamGroupName(v, "name") + if len(errs) != 0 { + t.Fatalf("%q should be a valid IAM Group name: %q", v, errs) } } @@ -41,8 +43,8 @@ func TestValidateIamGroupName(t *testing.T) { "slash-at-the-end/", } for _, v := range invalidNames { - _, errors := validateAwsIamGroupName(v, "name") - if len(errors) == 0 { + _, errs := validateAwsIamGroupName(v, "name") + if len(errs) == 0 { t.Fatalf("%q should be an invalid IAM Group name", v) } } @@ -50,24 +52,25 @@ func TestValidateIamGroupName(t *testing.T) { func TestAccAWSIAMGroup_basic(t *testing.T) { var conf iam.GetGroupOutput + rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSGroupDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccAWSGroupConfig, + { + Config: testAccAWSGroupConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSGroupExists("aws_iam_group.group", &conf), - testAccCheckAWSGroupAttributes(&conf, "test-group", "/"), + testAccCheckAWSGroupAttributes(&conf, fmt.Sprintf("test-group-%d", rInt), "/"), ), }, - resource.TestStep{ - Config: testAccAWSGroupConfig2, + { + Config: testAccAWSGroupConfig2(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckAWSGroupExists("aws_iam_group.group2", &conf), - testAccCheckAWSGroupAttributes(&conf, "test-group2", "/funnypath/"), + testAccCheckAWSGroupAttributes(&conf, fmt.Sprintf("test-group-%d-2", rInt), "/funnypath/"), ), }, }, @@ -87,7 +90,7 @@ func testAccCheckAWSGroupDestroy(s *terraform.State) error { GroupName: aws.String(rs.Primary.ID), }) if err == nil { - return fmt.Errorf("still exist.") + return errors.New("still exist.") } // Verify the error is what we want @@ -111,7 +114,7 @@ func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) resource.Test } if rs.Primary.ID == "" { - return fmt.Errorf("No Group name is set") + return errors.New("No Group name is set") } iamconn := testAccProvider.Meta().(*AWSClient).iamconn @@ -143,15 +146,18 @@ func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput, name string, path } } -const testAccAWSGroupConfig = ` -resource "aws_iam_group" "group" { - name = "test-group" - path = "/" +func testAccAWSGroupConfig(rInt int) string { + return fmt.Sprintf(` + resource "aws_iam_group" "group" { + name = "test-group-%d" + path = "/" + }`, rInt) } -` -const testAccAWSGroupConfig2 = ` + +func testAccAWSGroupConfig2(rInt int) string { + return fmt.Sprintf(` resource "aws_iam_group" "group2" { - name = "test-group2" + name = "test-group-%d-2" path = "/funnypath/" +}`, rInt) } -`