github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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, "test-group", "/"), 27 ), 28 }, 29 resource.TestStep{ 30 Config: testAccAWSGroupConfig2, 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckAWSGroupExists("aws_iam_group.group", &conf), 33 testAccCheckAWSGroupAttributes(&conf, "test-group2", "/funnypath/"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckAWSGroupDestroy(s *terraform.State) error { 41 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 42 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "aws_iam_group" { 45 continue 46 } 47 48 // Try to get group 49 _, err := iamconn.GetGroup(&iam.GetGroupInput{ 50 GroupName: aws.String(rs.Primary.ID), 51 }) 52 if err == nil { 53 return fmt.Errorf("still exist.") 54 } 55 56 // Verify the error is what we want 57 ec2err, ok := err.(awserr.Error) 58 if !ok { 59 return err 60 } 61 if ec2err.Code() != "NoSuchEntity" { 62 return err 63 } 64 } 65 66 return nil 67 } 68 69 func testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 rs, ok := s.RootModule().Resources[n] 72 if !ok { 73 return fmt.Errorf("Not found: %s", n) 74 } 75 76 if rs.Primary.ID == "" { 77 return fmt.Errorf("No Group name is set") 78 } 79 80 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 81 82 resp, err := iamconn.GetGroup(&iam.GetGroupInput{ 83 GroupName: aws.String(rs.Primary.ID), 84 }) 85 if err != nil { 86 return err 87 } 88 89 *res = *resp 90 91 return nil 92 } 93 } 94 95 func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput, name string, path string) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 if *group.Group.GroupName != name { 98 return fmt.Errorf("Bad name: %s when %s was expected", *group.Group.GroupName, name) 99 } 100 101 if *group.Group.Path != path { 102 return fmt.Errorf("Bad path: %s when %s was expected", *group.Group.Path, path) 103 } 104 105 return nil 106 } 107 } 108 109 const testAccAWSGroupConfig = ` 110 resource "aws_iam_group" "group" { 111 name = "test-group" 112 path = "/" 113 } 114 ` 115 const testAccAWSGroupConfig2 = ` 116 resource "aws_iam_group" "group" { 117 name = "test-group2" 118 path = "/funnypath/" 119 } 120 `