github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/resource_aws_iam_role_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/aws/awserr" 9 "github.com/awslabs/aws-sdk-go/service/iam" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSRole_normal(t *testing.T) { 15 var conf iam.GetRoleOutput 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSRoleDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSRoleConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSRoleExists("aws_iam_role.role", &conf), 26 testAccCheckAWSRoleAttributes(&conf), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckAWSRoleDestroy(s *terraform.State) error { 34 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 35 36 for _, rs := range s.RootModule().Resources { 37 if rs.Type != "aws_iam_role" { 38 continue 39 } 40 41 // Try to get role 42 _, err := iamconn.GetRole(&iam.GetRoleInput{ 43 RoleName: 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 testAccCheckAWSRoleExists(n string, res *iam.GetRoleOutput) 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 Role name is set") 71 } 72 73 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 74 75 resp, err := iamconn.GetRole(&iam.GetRoleInput{ 76 RoleName: 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 testAccCheckAWSRoleAttributes(role *iam.GetRoleOutput) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 if *role.Role.RoleName != "test-role" { 91 return fmt.Errorf("Bad name: %s", *role.Role.RoleName) 92 } 93 94 if *role.Role.Path != "/" { 95 return fmt.Errorf("Bad path: %s", *role.Role.Path) 96 } 97 return nil 98 } 99 } 100 101 const testAccAWSRoleConfig = ` 102 resource "aws_iam_role" "role" { 103 name = "test-role" 104 path = "/" 105 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 106 } 107 `