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