github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_iam_instance_profile_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strings" 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 TestAccAWSIAMInstanceProfile_importBasic(t *testing.T) { 17 resourceName := "aws_iam_instance_profile.test" 18 rName := acctest.RandString(5) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAWSInstanceProfileDestroy, 24 Steps: []resource.TestStep{ 25 { 26 Config: testAccAWSInstanceProfilePrefixNameConfig(rName), 27 }, 28 29 { 30 ResourceName: resourceName, 31 ImportState: true, 32 ImportStateVerify: true, 33 ImportStateVerifyIgnore: []string{"name_prefix"}, 34 }, 35 }, 36 }) 37 } 38 39 func TestAccAWSIAMInstanceProfile_basic(t *testing.T) { 40 rName := acctest.RandString(5) 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 Steps: []resource.TestStep{ 45 { 46 Config: testAccAwsIamInstanceProfileConfig(rName), 47 }, 48 }, 49 }) 50 } 51 52 func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) { 53 var conf iam.GetInstanceProfileOutput 54 rName := acctest.RandString(5) 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 IDRefreshName: "aws_iam_instance_profile.test", 59 IDRefreshIgnore: []string{"name_prefix"}, 60 Providers: testAccProviders, 61 CheckDestroy: testAccCheckAWSInstanceProfileDestroy, 62 Steps: []resource.TestStep{ 63 { 64 Config: testAccAWSInstanceProfilePrefixNameConfig(rName), 65 Check: resource.ComposeTestCheckFunc( 66 testAccCheckAWSInstanceProfileExists("aws_iam_instance_profile.test", &conf), 67 testAccCheckAWSInstanceProfileGeneratedNamePrefix( 68 "aws_iam_instance_profile.test", "test-"), 69 ), 70 }, 71 }, 72 }) 73 } 74 75 func testAccCheckAWSInstanceProfileGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc { 76 return func(s *terraform.State) error { 77 r, ok := s.RootModule().Resources[resource] 78 if !ok { 79 return fmt.Errorf("Resource not found") 80 } 81 name, ok := r.Primary.Attributes["name"] 82 if !ok { 83 return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes) 84 } 85 if !strings.HasPrefix(name, prefix) { 86 return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix) 87 } 88 return nil 89 } 90 } 91 92 func testAccCheckAWSInstanceProfileDestroy(s *terraform.State) error { 93 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 94 95 for _, rs := range s.RootModule().Resources { 96 if rs.Type != "aws_iam_instance_profile" { 97 continue 98 } 99 100 // Try to get role 101 _, err := iamconn.GetInstanceProfile(&iam.GetInstanceProfileInput{ 102 InstanceProfileName: aws.String(rs.Primary.ID), 103 }) 104 if err == nil { 105 return fmt.Errorf("still exist.") 106 } 107 108 // Verify the error is what we want 109 ec2err, ok := err.(awserr.Error) 110 if !ok { 111 return err 112 } 113 if ec2err.Code() != "NoSuchEntity" { 114 return err 115 } 116 } 117 118 return nil 119 } 120 121 func testAccCheckAWSInstanceProfileExists(n string, res *iam.GetInstanceProfileOutput) resource.TestCheckFunc { 122 return func(s *terraform.State) error { 123 rs, ok := s.RootModule().Resources[n] 124 if !ok { 125 return fmt.Errorf("Not found: %s", n) 126 } 127 128 if rs.Primary.ID == "" { 129 return fmt.Errorf("No Instance Profile name is set") 130 } 131 132 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 133 134 resp, err := iamconn.GetInstanceProfile(&iam.GetInstanceProfileInput{ 135 InstanceProfileName: aws.String(rs.Primary.ID), 136 }) 137 if err != nil { 138 return err 139 } 140 141 *res = *resp 142 143 return nil 144 } 145 } 146 147 func testAccAwsIamInstanceProfileConfig(rName string) string { 148 return fmt.Sprintf(` 149 resource "aws_iam_role" "test" { 150 name = "test-%s" 151 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 152 } 153 154 resource "aws_iam_instance_profile" "test" { 155 name = "test" 156 roles = ["${aws_iam_role.test.name}"] 157 }`, rName) 158 } 159 160 func testAccAWSInstanceProfilePrefixNameConfig(rName string) string { 161 return fmt.Sprintf(` 162 resource "aws_iam_role" "test" { 163 name = "test-%s" 164 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 165 } 166 167 resource "aws_iam_instance_profile" "test" { 168 name_prefix = "test-" 169 roles = ["${aws_iam_role.test.name}"] 170 }`, rName) 171 }