github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_iam_group_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/iam" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestValidateIamGroupName(t *testing.T) { 17 validNames := []string{ 18 "test-group", 19 "test_group", 20 "testgroup123", 21 "TestGroup", 22 "Test-Group", 23 "test.group", 24 "test.123,group", 25 "testgroup@hashicorp", 26 "test+group@hashicorp.com", 27 } 28 for _, v := range validNames { 29 _, errs := validateAwsIamGroupName(v, "name") 30 if len(errs) != 0 { 31 t.Fatalf("%q should be a valid IAM Group name: %q", v, errs) 32 } 33 } 34 35 invalidNames := []string{ 36 "!", 37 "/", 38 " ", 39 ":", 40 ";", 41 "test name", 42 "/slash-at-the-beginning", 43 "slash-at-the-end/", 44 } 45 for _, v := range invalidNames { 46 _, errs := validateAwsIamGroupName(v, "name") 47 if len(errs) == 0 { 48 t.Fatalf("%q should be an invalid IAM Group name", v) 49 } 50 } 51 } 52 53 func TestAccAWSIAMGroup_basic(t *testing.T) { 54 var conf iam.GetGroupOutput 55 rInt := acctest.RandInt() 56 57 resource.Test(t, resource.TestCase{ 58 PreCheck: func() { testAccPreCheck(t) }, 59 Providers: testAccProviders, 60 CheckDestroy: testAccCheckAWSGroupDestroy, 61 Steps: []resource.TestStep{ 62 { 63 Config: testAccAWSGroupConfig(rInt), 64 Check: resource.ComposeTestCheckFunc( 65 testAccCheckAWSGroupExists("aws_iam_group.group", &conf), 66 testAccCheckAWSGroupAttributes(&conf, fmt.Sprintf("test-group-%d", rInt), "/"), 67 ), 68 }, 69 { 70 Config: testAccAWSGroupConfig2(rInt), 71 Check: resource.ComposeTestCheckFunc( 72 testAccCheckAWSGroupExists("aws_iam_group.group2", &conf), 73 testAccCheckAWSGroupAttributes(&conf, fmt.Sprintf("test-group-%d-2", rInt), "/funnypath/"), 74 ), 75 }, 76 }, 77 }) 78 } 79 80 func testAccCheckAWSGroupDestroy(s *terraform.State) error { 81 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 82 83 for _, rs := range s.RootModule().Resources { 84 if rs.Type != "aws_iam_group" { 85 continue 86 } 87 88 // Try to get group 89 _, err := iamconn.GetGroup(&iam.GetGroupInput{ 90 GroupName: aws.String(rs.Primary.ID), 91 }) 92 if err == nil { 93 return errors.New("still exist.") 94 } 95 96 // Verify the error is what we want 97 ec2err, ok := err.(awserr.Error) 98 if !ok { 99 return err 100 } 101 if ec2err.Code() != "NoSuchEntity" { 102 return err 103 } 104 } 105 106 return nil 107 } 108 109 func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) resource.TestCheckFunc { 110 return func(s *terraform.State) error { 111 rs, ok := s.RootModule().Resources[n] 112 if !ok { 113 return fmt.Errorf("Not found: %s", n) 114 } 115 116 if rs.Primary.ID == "" { 117 return errors.New("No Group name is set") 118 } 119 120 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 121 122 resp, err := iamconn.GetGroup(&iam.GetGroupInput{ 123 GroupName: aws.String(rs.Primary.ID), 124 }) 125 if err != nil { 126 return err 127 } 128 129 *res = *resp 130 131 return nil 132 } 133 } 134 135 func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput, name string, path string) resource.TestCheckFunc { 136 return func(s *terraform.State) error { 137 if *group.Group.GroupName != name { 138 return fmt.Errorf("Bad name: %s when %s was expected", *group.Group.GroupName, name) 139 } 140 141 if *group.Group.Path != path { 142 return fmt.Errorf("Bad path: %s when %s was expected", *group.Group.Path, path) 143 } 144 145 return nil 146 } 147 } 148 149 func testAccAWSGroupConfig(rInt int) string { 150 return fmt.Sprintf(` 151 resource "aws_iam_group" "group" { 152 name = "test-group-%d" 153 path = "/" 154 }`, rInt) 155 } 156 157 func testAccAWSGroupConfig2(rInt int) string { 158 return fmt.Sprintf(` 159 resource "aws_iam_group" "group2" { 160 name = "test-group-%d-2" 161 path = "/funnypath/" 162 }`, rInt) 163 }