github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/builtin/providers/aws/resource_aws_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/goamz/ec2" 10 ) 11 12 func TestAccAWSInstance_normal(t *testing.T) { 13 var v ec2.Instance 14 15 testCheck := func(*terraform.State) error { 16 if v.AvailZone != "us-west-2a" { 17 return fmt.Errorf("bad availability zone: %#v", v.AvailZone) 18 } 19 20 if len(v.SecurityGroups) == 0 { 21 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 22 } 23 if v.SecurityGroups[0].Name != "tf_test_foo" { 24 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 25 } 26 27 return nil 28 } 29 30 resource.Test(t, resource.TestCase{ 31 PreCheck: func() { testAccPreCheck(t) }, 32 Providers: testAccProviders, 33 CheckDestroy: testAccCheckInstanceDestroy, 34 Steps: []resource.TestStep{ 35 resource.TestStep{ 36 Config: testAccInstanceConfig, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckInstanceExists( 39 "aws_instance.foo", &v), 40 testCheck, 41 resource.TestCheckResourceAttr( 42 "aws_instance.foo", 43 "user_data", 44 "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), 45 ), 46 }, 47 48 // We repeat the exact same test so that we can be sure 49 // that the user data hash stuff is working without generating 50 // an incorrect diff. 51 resource.TestStep{ 52 Config: testAccInstanceConfig, 53 Check: resource.ComposeTestCheckFunc( 54 testAccCheckInstanceExists( 55 "aws_instance.foo", &v), 56 testCheck, 57 resource.TestCheckResourceAttr( 58 "aws_instance.foo", 59 "user_data", 60 "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 68 var v ec2.Instance 69 70 testCheck := func(enabled bool) resource.TestCheckFunc { 71 return func(*terraform.State) error { 72 if v.SourceDestCheck != enabled { 73 return fmt.Errorf("bad source_dest_check: %#v", v.SourceDestCheck) 74 } 75 76 return nil 77 } 78 } 79 80 resource.Test(t, resource.TestCase{ 81 PreCheck: func() { testAccPreCheck(t) }, 82 Providers: testAccProviders, 83 CheckDestroy: testAccCheckInstanceDestroy, 84 Steps: []resource.TestStep{ 85 resource.TestStep{ 86 Config: testAccInstanceConfigSourceDest, 87 Check: resource.ComposeTestCheckFunc( 88 testAccCheckInstanceExists( 89 "aws_instance.foo", &v), 90 testCheck(true), 91 ), 92 }, 93 94 resource.TestStep{ 95 Config: testAccInstanceConfigSourceDestDisable, 96 Check: resource.ComposeTestCheckFunc( 97 testAccCheckInstanceExists( 98 "aws_instance.foo", &v), 99 testCheck(false), 100 ), 101 }, 102 }, 103 }) 104 } 105 106 func TestAccAWSInstance_vpc(t *testing.T) { 107 var v ec2.Instance 108 109 resource.Test(t, resource.TestCase{ 110 PreCheck: func() { testAccPreCheck(t) }, 111 Providers: testAccProviders, 112 CheckDestroy: testAccCheckInstanceDestroy, 113 Steps: []resource.TestStep{ 114 resource.TestStep{ 115 Config: testAccInstanceConfigVPC, 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckInstanceExists( 118 "aws_instance.foo", &v), 119 ), 120 }, 121 }, 122 }) 123 } 124 125 func TestAccInstance_tags(t *testing.T) { 126 var v ec2.Instance 127 128 resource.Test(t, resource.TestCase{ 129 PreCheck: func() { testAccPreCheck(t) }, 130 Providers: testAccProviders, 131 CheckDestroy: testAccCheckInstanceDestroy, 132 Steps: []resource.TestStep{ 133 resource.TestStep{ 134 Config: testAccCheckInstanceConfigTags, 135 Check: resource.ComposeTestCheckFunc( 136 testAccCheckInstanceExists("aws_instance.foo", &v), 137 testAccCheckTags(&v.Tags, "foo", "bar"), 138 ), 139 }, 140 141 resource.TestStep{ 142 Config: testAccCheckInstanceConfigTagsUpdate, 143 Check: resource.ComposeTestCheckFunc( 144 testAccCheckInstanceExists("aws_instance.foo", &v), 145 testAccCheckTags(&v.Tags, "foo", ""), 146 testAccCheckTags(&v.Tags, "bar", "baz"), 147 ), 148 }, 149 }, 150 }) 151 } 152 153 func testAccCheckInstanceDestroy(s *terraform.State) error { 154 conn := testAccProvider.ec2conn 155 156 for _, rs := range s.RootModule().Resources { 157 if rs.Type != "aws_instance" { 158 continue 159 } 160 161 // Try to find the resource 162 resp, err := conn.Instances( 163 []string{rs.Primary.ID}, ec2.NewFilter()) 164 if err == nil { 165 if len(resp.Reservations) > 0 { 166 return fmt.Errorf("still exist.") 167 } 168 169 return nil 170 } 171 172 // Verify the error is what we want 173 ec2err, ok := err.(*ec2.Error) 174 if !ok { 175 return err 176 } 177 if ec2err.Code != "InvalidInstanceID.NotFound" { 178 return err 179 } 180 } 181 182 return nil 183 } 184 185 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 186 return func(s *terraform.State) error { 187 rs, ok := s.RootModule().Resources[n] 188 if !ok { 189 return fmt.Errorf("Not found: %s", n) 190 } 191 192 if rs.Primary.ID == "" { 193 return fmt.Errorf("No ID is set") 194 } 195 196 conn := testAccProvider.ec2conn 197 resp, err := conn.Instances( 198 []string{rs.Primary.ID}, ec2.NewFilter()) 199 if err != nil { 200 return err 201 } 202 if len(resp.Reservations) == 0 { 203 return fmt.Errorf("Instance not found") 204 } 205 206 *i = resp.Reservations[0].Instances[0] 207 208 return nil 209 } 210 } 211 212 const testAccInstanceConfig = ` 213 resource "aws_security_group" "tf_test_foo" { 214 name = "tf_test_foo" 215 description = "foo" 216 217 ingress { 218 protocol = "icmp" 219 from_port = -1 220 to_port = -1 221 cidr_blocks = ["0.0.0.0/0"] 222 } 223 } 224 225 resource "aws_instance" "foo" { 226 # us-west-2 227 ami = "ami-4fccb37f" 228 availability_zone = "us-west-2a" 229 230 instance_type = "m1.small" 231 security_groups = ["${aws_security_group.tf_test_foo.name}"] 232 user_data = "foo" 233 } 234 ` 235 236 const testAccInstanceConfigSourceDest = ` 237 resource "aws_vpc" "foo" { 238 cidr_block = "10.1.0.0/16" 239 } 240 241 resource "aws_subnet" "foo" { 242 cidr_block = "10.1.1.0/24" 243 vpc_id = "${aws_vpc.foo.id}" 244 } 245 246 resource "aws_instance" "foo" { 247 # us-west-2 248 ami = "ami-4fccb37f" 249 instance_type = "m1.small" 250 subnet_id = "${aws_subnet.foo.id}" 251 source_dest_check = true 252 } 253 ` 254 255 const testAccInstanceConfigSourceDestDisable = ` 256 resource "aws_vpc" "foo" { 257 cidr_block = "10.1.0.0/16" 258 } 259 260 resource "aws_subnet" "foo" { 261 cidr_block = "10.1.1.0/24" 262 vpc_id = "${aws_vpc.foo.id}" 263 } 264 265 resource "aws_instance" "foo" { 266 # us-west-2 267 ami = "ami-4fccb37f" 268 instance_type = "m1.small" 269 subnet_id = "${aws_subnet.foo.id}" 270 source_dest_check = false 271 } 272 ` 273 274 const testAccInstanceConfigVPC = ` 275 resource "aws_vpc" "foo" { 276 cidr_block = "10.1.0.0/16" 277 } 278 279 resource "aws_subnet" "foo" { 280 cidr_block = "10.1.1.0/24" 281 vpc_id = "${aws_vpc.foo.id}" 282 } 283 284 resource "aws_instance" "foo" { 285 # us-west-2 286 ami = "ami-4fccb37f" 287 instance_type = "m1.small" 288 subnet_id = "${aws_subnet.foo.id}" 289 associate_public_ip_address = true 290 } 291 ` 292 293 const testAccCheckInstanceConfigTags = ` 294 resource "aws_instance" "foo" { 295 tags { 296 foo = "bar" 297 } 298 } 299 ` 300 301 const testAccCheckInstanceConfigTagsUpdate = ` 302 resource "aws_instance" "foo" { 303 tags { 304 bar = "baz" 305 } 306 } 307 `