github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/aws/resource_aws_iam_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/iam" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSIAMGroup_basic(t *testing.T) { 15 var conf iam.GetGroupOutput 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSGroupDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSGroupConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSGroupExists("aws_iam_group.group", &conf), 26 testAccCheckAWSGroupAttributes(&conf), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckAWSGroupDestroy(s *terraform.State) error { 34 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "aws_iam_group" { 38 continue 39 } 40 41 // Try to get group 42 _, err := iamconn.GetGroup(&iam.GetGroupInput{ 43 GroupName: aws.String(rs.Primary.ID), 44 }) 45 if err == nil { 46 return fmt.Errorf("still exist.") 47 } 48 49 // Verify the error is what we want 50 ec2err, ok := err.(awserr.Error) 51 if !ok { 52 return err 53 } 54 if ec2err.Code() != "NoSuchEntity" { 55 return err 56 } 57 } 58 59 return nil 60 } 61 62 func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) resource.TestCheckFunc { 63 return func(s *terraform.State) error { 64 rs, ok := s.RootModule().Resources[n] 65 if !ok { 66 return fmt.Errorf("Not found: %s", n) 67 } 68 69 if rs.Primary.ID == "" { 70 return fmt.Errorf("No Group name is set") 71 } 72 73 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 74 75 resp, err := iamconn.GetGroup(&iam.GetGroupInput{ 76 GroupName: aws.String(rs.Primary.ID), 77 }) 78 if err != nil { 79 return err 80 } 81 82 *res = *resp 83 84 return nil 85 } 86 } 87 88 func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 if *group.Group.GroupName != "test-group" { 91 return fmt.Errorf("Bad name: %s", *group.Group.GroupName) 92 } 93 94 if *group.Group.Path != "/" { 95 return fmt.Errorf("Bad path: %s", *group.Group.Path) 96 } 97 98 return nil 99 } 100 } 101 102 const testAccAWSGroupConfig = ` 103 resource "aws_iam_group" "group" { 104 name = "test-group" 105 path = "/" 106 } 107 `