github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/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 68 69 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 70 var v ec2.Instance 71 72 testCheck := func(enabled bool) resource.TestCheckFunc { 73 return func(*terraform.State) error { 74 if v.SourceDestCheck != enabled { 75 return fmt.Errorf("bad source_dest_check: %#v", v.SourceDestCheck) 76 } 77 78 return nil 79 } 80 } 81 82 resource.Test(t, resource.TestCase{ 83 PreCheck: func() { testAccPreCheck(t) }, 84 Providers: testAccProviders, 85 CheckDestroy: testAccCheckInstanceDestroy, 86 Steps: []resource.TestStep{ 87 resource.TestStep{ 88 Config: testAccInstanceConfigSourceDest, 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckInstanceExists( 91 "aws_instance.foo", &v), 92 testCheck(true), 93 ), 94 }, 95 96 resource.TestStep{ 97 Config: testAccInstanceConfigSourceDestDisable, 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckInstanceExists( 100 "aws_instance.foo", &v), 101 testCheck(false), 102 ), 103 }, 104 }, 105 }) 106 } 107 108 func TestAccAWSInstance_vpc(t *testing.T) { 109 var v ec2.Instance 110 111 resource.Test(t, resource.TestCase{ 112 PreCheck: func() { testAccPreCheck(t) }, 113 Providers: testAccProviders, 114 CheckDestroy: testAccCheckInstanceDestroy, 115 Steps: []resource.TestStep{ 116 resource.TestStep{ 117 Config: testAccInstanceConfigVPC, 118 Check: resource.ComposeTestCheckFunc( 119 testAccCheckInstanceExists( 120 "aws_instance.foo", &v), 121 ), 122 }, 123 }, 124 }) 125 } 126 127 func TestAccInstance_tags(t *testing.T) { 128 var v ec2.Instance 129 130 resource.Test(t, resource.TestCase{ 131 PreCheck: func() { testAccPreCheck(t) }, 132 Providers: testAccProviders, 133 CheckDestroy: testAccCheckInstanceDestroy, 134 Steps: []resource.TestStep{ 135 resource.TestStep{ 136 Config: testAccCheckInstanceConfigTags, 137 Check: resource.ComposeTestCheckFunc( 138 testAccCheckInstanceExists("aws_instance.foo", &v), 139 testAccCheckTags(&v.Tags, "foo", "bar"), 140 ), 141 }, 142 143 resource.TestStep{ 144 Config: testAccCheckInstanceConfigTagsUpdate, 145 Check: resource.ComposeTestCheckFunc( 146 testAccCheckInstanceExists("aws_instance.foo", &v), 147 testAccCheckTags(&v.Tags, "foo", ""), 148 testAccCheckTags(&v.Tags, "bar", "baz"), 149 ), 150 }, 151 }, 152 }) 153 } 154 155 func testAccCheckInstanceDestroy(s *terraform.State) error { 156 conn := testAccProvider.ec2conn 157 158 for _, rs := range s.RootModule().Resources { 159 if rs.Type != "aws_instance" { 160 continue 161 } 162 163 // Try to find the resource 164 resp, err := conn.Instances( 165 []string{rs.Primary.ID}, ec2.NewFilter()) 166 if err == nil { 167 if len(resp.Reservations) > 0 { 168 return fmt.Errorf("still exist.") 169 } 170 171 return nil 172 } 173 174 // Verify the error is what we want 175 ec2err, ok := err.(*ec2.Error) 176 if !ok { 177 return err 178 } 179 if ec2err.Code != "InvalidInstanceID.NotFound" { 180 return err 181 } 182 } 183 184 return nil 185 } 186 187 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 188 return func(s *terraform.State) error { 189 rs, ok := s.RootModule().Resources[n] 190 if !ok { 191 return fmt.Errorf("Not found: %s", n) 192 } 193 194 if rs.Primary.ID == "" { 195 return fmt.Errorf("No ID is set") 196 } 197 198 conn := testAccProvider.ec2conn 199 resp, err := conn.Instances( 200 []string{rs.Primary.ID}, ec2.NewFilter()) 201 if err != nil { 202 return err 203 } 204 if len(resp.Reservations) == 0 { 205 return fmt.Errorf("Instance not found") 206 } 207 208 *i = resp.Reservations[0].Instances[0] 209 210 return nil 211 } 212 } 213 214 const testAccInstanceConfig = ` 215 resource "aws_security_group" "tf_test_foo" { 216 name = "tf_test_foo" 217 description = "foo" 218 219 ingress { 220 protocol = "icmp" 221 from_port = -1 222 to_port = -1 223 cidr_blocks = ["0.0.0.0/0"] 224 } 225 } 226 227 resource "aws_instance" "foo" { 228 # us-west-2 229 ami = "ami-4fccb37f" 230 availability_zone = "us-west-2a" 231 232 instance_type = "m1.small" 233 security_groups = ["${aws_security_group.tf_test_foo.name}"] 234 user_data = "foo" 235 } 236 ` 237 238 const testAccInstanceConfigSourceDest = ` 239 resource "aws_vpc" "foo" { 240 cidr_block = "10.1.0.0/16" 241 } 242 243 resource "aws_subnet" "foo" { 244 cidr_block = "10.1.1.0/24" 245 vpc_id = "${aws_vpc.foo.id}" 246 } 247 248 resource "aws_instance" "foo" { 249 # us-west-2 250 ami = "ami-4fccb37f" 251 instance_type = "m1.small" 252 subnet_id = "${aws_subnet.foo.id}" 253 source_dest_check = true 254 } 255 ` 256 257 const testAccInstanceConfigSourceDestDisable = ` 258 resource "aws_vpc" "foo" { 259 cidr_block = "10.1.0.0/16" 260 } 261 262 resource "aws_subnet" "foo" { 263 cidr_block = "10.1.1.0/24" 264 vpc_id = "${aws_vpc.foo.id}" 265 } 266 267 resource "aws_instance" "foo" { 268 # us-west-2 269 ami = "ami-4fccb37f" 270 instance_type = "m1.small" 271 subnet_id = "${aws_subnet.foo.id}" 272 source_dest_check = false 273 } 274 ` 275 276 const testAccInstanceConfigVPC = ` 277 resource "aws_vpc" "foo" { 278 cidr_block = "10.1.0.0/16" 279 } 280 281 resource "aws_subnet" "foo" { 282 cidr_block = "10.1.1.0/24" 283 vpc_id = "${aws_vpc.foo.id}" 284 } 285 286 resource "aws_instance" "foo" { 287 # us-west-2 288 ami = "ami-4fccb37f" 289 instance_type = "m1.small" 290 subnet_id = "${aws_subnet.foo.id}" 291 associate_public_ip_address = true 292 } 293 ` 294 295 const testAccCheckInstanceConfigTags = ` 296 resource "aws_instance" "foo" { 297 tags { 298 foo = "bar" 299 } 300 } 301 ` 302 303 const testAccCheckInstanceConfigTagsUpdate = ` 304 resource "aws_instance" "foo" { 305 tags { 306 bar = "baz" 307 } 308 } 309 `