Merge pull request #11642 from hashicorp/f-improve-iam-tests

provider/aws: Improve IAM Group AccTests
This commit is contained in:
Jake Champlin 2017-02-02 14:10:51 -05:00 committed by GitHub
commit 188a00cbe0
2 changed files with 32 additions and 24 deletions

View File

@ -3,10 +3,12 @@ package aws
import ( import (
"testing" "testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
) )
func TestAccAWSIAMGroup_importBasic(t *testing.T) { func TestAccAWSIAMGroup_importBasic(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "aws_iam_group.group" resourceName := "aws_iam_group.group"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -14,11 +16,11 @@ func TestAccAWSIAMGroup_importBasic(t *testing.T) {
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSGroupDestroy, CheckDestroy: testAccCheckAWSGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccAWSGroupConfig, Config: testAccAWSGroupConfig(rInt),
}, },
resource.TestStep{ {
ResourceName: resourceName, ResourceName: resourceName,
ImportState: true, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,

View File

@ -1,12 +1,14 @@
package aws package aws
import ( import (
"errors"
"fmt" "fmt"
"testing" "testing"
"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/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"
) )
@ -24,9 +26,9 @@ func TestValidateIamGroupName(t *testing.T) {
"test+group@hashicorp.com", "test+group@hashicorp.com",
} }
for _, v := range validNames { for _, v := range validNames {
_, errors := validateAwsIamGroupName(v, "name") _, errs := validateAwsIamGroupName(v, "name")
if len(errors) != 0 { if len(errs) != 0 {
t.Fatalf("%q should be a valid IAM Group name: %q", v, errors) 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/", "slash-at-the-end/",
} }
for _, v := range invalidNames { for _, v := range invalidNames {
_, errors := validateAwsIamGroupName(v, "name") _, errs := validateAwsIamGroupName(v, "name")
if len(errors) == 0 { if len(errs) == 0 {
t.Fatalf("%q should be an invalid IAM Group name", v) 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) { func TestAccAWSIAMGroup_basic(t *testing.T) {
var conf iam.GetGroupOutput var conf iam.GetGroupOutput
rInt := 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: testAccCheckAWSGroupDestroy, CheckDestroy: testAccCheckAWSGroupDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccAWSGroupConfig, Config: testAccAWSGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGroupExists("aws_iam_group.group", &conf), 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( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGroupExists("aws_iam_group.group2", &conf), 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), GroupName: aws.String(rs.Primary.ID),
}) })
if err == nil { if err == nil {
return fmt.Errorf("still exist.") return errors.New("still exist.")
} }
// Verify the error is what we want // Verify the error is what we want
@ -111,7 +114,7 @@ func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) resource.Test
} }
if rs.Primary.ID == "" { 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 iamconn := testAccProvider.Meta().(*AWSClient).iamconn
@ -143,15 +146,18 @@ func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput, name string, path
} }
} }
const testAccAWSGroupConfig = ` func testAccAWSGroupConfig(rInt int) string {
resource "aws_iam_group" "group" { return fmt.Sprintf(`
name = "test-group" resource "aws_iam_group" "group" {
path = "/" name = "test-group-%d"
path = "/"
}`, rInt)
} }
`
const testAccAWSGroupConfig2 = ` func testAccAWSGroupConfig2(rInt int) string {
return fmt.Sprintf(`
resource "aws_iam_group" "group2" { resource "aws_iam_group" "group2" {
name = "test-group2" name = "test-group-%d-2"
path = "/funnypath/" path = "/funnypath/"
}`, rInt)
} }
`