github.com/joshgarnett/terraform@v0.5.4-0.20160219181435-92dc20bb3594/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 11 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" 12 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip" 13 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups" 14 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach" 15 "github.com/rackspace/gophercloud/openstack/compute/v2/servers" 16 "github.com/rackspace/gophercloud/pagination" 17 ) 18 19 func TestAccComputeV2Instance_basic(t *testing.T) { 20 var instance servers.Server 21 var testAccComputeV2Instance_basic = fmt.Sprintf(` 22 resource "openstack_compute_instance_v2" "foo" { 23 name = "terraform-test" 24 security_groups = ["default"] 25 network { 26 uuid = "%s" 27 } 28 metadata { 29 foo = "bar" 30 } 31 }`, 32 os.Getenv("OS_NETWORK_ID")) 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccComputeV2Instance_basic, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 43 testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"), 44 ), 45 }, 46 }, 47 }) 48 } 49 50 func TestAccComputeV2Instance_volumeAttach(t *testing.T) { 51 var instance servers.Server 52 var volume volumes.Volume 53 54 var testAccComputeV2Instance_volumeAttach = fmt.Sprintf(` 55 resource "openstack_blockstorage_volume_v1" "myvol" { 56 name = "myvol" 57 size = 1 58 } 59 60 resource "openstack_compute_instance_v2" "foo" { 61 name = "terraform-test" 62 security_groups = ["default"] 63 volume { 64 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 65 } 66 }`) 67 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 72 Steps: []resource.TestStep{ 73 resource.TestStep{ 74 Config: testAccComputeV2Instance_volumeAttach, 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 77 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 78 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 79 ), 80 }, 81 }, 82 }) 83 } 84 85 func TestAccComputeV2Instance_volumeAttachPostCreation(t *testing.T) { 86 var instance servers.Server 87 var volume volumes.Volume 88 89 var testAccComputeV2Instance_volumeAttachPostCreationInstance = fmt.Sprintf(` 90 resource "openstack_compute_instance_v2" "foo" { 91 name = "terraform-test" 92 security_groups = ["default"] 93 }`) 94 95 var testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume = fmt.Sprintf(` 96 resource "openstack_blockstorage_volume_v1" "myvol" { 97 name = "myvol" 98 size = 1 99 } 100 101 resource "openstack_compute_instance_v2" "foo" { 102 name = "terraform-test" 103 security_groups = ["default"] 104 volume { 105 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 106 } 107 }`) 108 109 resource.Test(t, resource.TestCase{ 110 PreCheck: func() { testAccPreCheck(t) }, 111 Providers: testAccProviders, 112 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 113 Steps: []resource.TestStep{ 114 resource.TestStep{ 115 Config: testAccComputeV2Instance_volumeAttachPostCreationInstance, 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 118 ), 119 }, 120 resource.TestStep{ 121 Config: testAccComputeV2Instance_volumeAttachPostCreationInstanceAndVolume, 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 124 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 125 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 126 ), 127 }, 128 }, 129 }) 130 } 131 132 func TestAccComputeV2Instance_volumeDetachPostCreation(t *testing.T) { 133 var instance servers.Server 134 var volume volumes.Volume 135 136 var testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume = fmt.Sprintf(` 137 resource "openstack_blockstorage_volume_v1" "myvol" { 138 name = "myvol" 139 size = 1 140 } 141 142 resource "openstack_compute_instance_v2" "foo" { 143 name = "terraform-test" 144 security_groups = ["default"] 145 volume { 146 volume_id = "${openstack_blockstorage_volume_v1.myvol.id}" 147 } 148 }`) 149 150 var testAccComputeV2Instance_volumeDetachPostCreationInstance = fmt.Sprintf(` 151 resource "openstack_compute_instance_v2" "foo" { 152 name = "terraform-test" 153 security_groups = ["default"] 154 }`) 155 156 resource.Test(t, resource.TestCase{ 157 PreCheck: func() { testAccPreCheck(t) }, 158 Providers: testAccProviders, 159 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 160 Steps: []resource.TestStep{ 161 resource.TestStep{ 162 Config: testAccComputeV2Instance_volumeDetachPostCreationInstanceAndVolume, 163 Check: resource.ComposeTestCheckFunc( 164 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.myvol", &volume), 165 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 166 testAccCheckComputeV2InstanceVolumeAttachment(&instance, &volume), 167 ), 168 }, 169 resource.TestStep{ 170 Config: testAccComputeV2Instance_volumeDetachPostCreationInstance, 171 Check: resource.ComposeTestCheckFunc( 172 testAccCheckBlockStorageV1VolumeDoesNotExist(t, "openstack_blockstorage_volume_v1.myvol", &volume), 173 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 174 testAccCheckComputeV2InstanceVolumesDetached(&instance), 175 ), 176 }, 177 }, 178 }) 179 } 180 181 func TestAccComputeV2Instance_floatingIPAttachGlobally(t *testing.T) { 182 var instance servers.Server 183 var fip floatingip.FloatingIP 184 var testAccComputeV2Instance_floatingIPAttachGlobally = fmt.Sprintf(` 185 resource "openstack_compute_floatingip_v2" "myip" { 186 } 187 188 resource "openstack_compute_instance_v2" "foo" { 189 name = "terraform-test" 190 security_groups = ["default"] 191 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 192 193 network { 194 uuid = "%s" 195 } 196 }`, 197 os.Getenv("OS_NETWORK_ID")) 198 199 resource.Test(t, resource.TestCase{ 200 PreCheck: func() { testAccPreCheck(t) }, 201 Providers: testAccProviders, 202 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 203 Steps: []resource.TestStep{ 204 resource.TestStep{ 205 Config: testAccComputeV2Instance_floatingIPAttachGlobally, 206 Check: resource.ComposeTestCheckFunc( 207 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), 208 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 209 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 210 ), 211 }, 212 }, 213 }) 214 } 215 216 func TestAccComputeV2Instance_floatingIPAttachToNetwork(t *testing.T) { 217 var instance servers.Server 218 var fip floatingip.FloatingIP 219 var testAccComputeV2Instance_floatingIPAttachToNetwork = fmt.Sprintf(` 220 resource "openstack_compute_floatingip_v2" "myip" { 221 } 222 223 resource "openstack_compute_instance_v2" "foo" { 224 name = "terraform-test" 225 security_groups = ["default"] 226 227 network { 228 uuid = "%s" 229 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 230 access_network = true 231 } 232 }`, 233 os.Getenv("OS_NETWORK_ID")) 234 235 resource.Test(t, resource.TestCase{ 236 PreCheck: func() { testAccPreCheck(t) }, 237 Providers: testAccProviders, 238 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 239 Steps: []resource.TestStep{ 240 resource.TestStep{ 241 Config: testAccComputeV2Instance_floatingIPAttachToNetwork, 242 Check: resource.ComposeTestCheckFunc( 243 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip", &fip), 244 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 245 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 246 ), 247 }, 248 }, 249 }) 250 } 251 252 func TestAccComputeV2Instance_floatingIPAttachAndChange(t *testing.T) { 253 var instance servers.Server 254 var fip floatingip.FloatingIP 255 var testAccComputeV2Instance_floatingIPAttachToNetwork_1 = fmt.Sprintf(` 256 resource "openstack_compute_floatingip_v2" "myip_1" { 257 } 258 259 resource "openstack_compute_floatingip_v2" "myip_2" { 260 } 261 262 resource "openstack_compute_instance_v2" "foo" { 263 name = "terraform-test" 264 security_groups = ["default"] 265 266 network { 267 uuid = "%s" 268 floating_ip = "${openstack_compute_floatingip_v2.myip_1.address}" 269 access_network = true 270 } 271 }`, 272 os.Getenv("OS_NETWORK_ID")) 273 274 var testAccComputeV2Instance_floatingIPAttachToNetwork_2 = fmt.Sprintf(` 275 resource "openstack_compute_floatingip_v2" "myip_1" { 276 } 277 278 resource "openstack_compute_floatingip_v2" "myip_2" { 279 } 280 281 resource "openstack_compute_instance_v2" "foo" { 282 name = "terraform-test" 283 security_groups = ["default"] 284 285 network { 286 uuid = "%s" 287 floating_ip = "${openstack_compute_floatingip_v2.myip_2.address}" 288 access_network = true 289 } 290 }`, 291 os.Getenv("OS_NETWORK_ID")) 292 293 resource.Test(t, resource.TestCase{ 294 PreCheck: func() { testAccPreCheck(t) }, 295 Providers: testAccProviders, 296 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 297 Steps: []resource.TestStep{ 298 resource.TestStep{ 299 Config: testAccComputeV2Instance_floatingIPAttachToNetwork_1, 300 Check: resource.ComposeTestCheckFunc( 301 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_1", &fip), 302 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 303 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 304 ), 305 }, 306 resource.TestStep{ 307 Config: testAccComputeV2Instance_floatingIPAttachToNetwork_2, 308 Check: resource.ComposeTestCheckFunc( 309 testAccCheckComputeV2FloatingIPExists(t, "openstack_compute_floatingip_v2.myip_2", &fip), 310 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 311 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 312 ), 313 }, 314 }, 315 }) 316 } 317 318 func TestAccComputeV2Instance_multi_secgroups(t *testing.T) { 319 var instance servers.Server 320 var secgroup secgroups.SecurityGroup 321 var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(` 322 resource "openstack_compute_secgroup_v2" "foo" { 323 name = "terraform-test" 324 description = "a security group" 325 rule { 326 from_port = 22 327 to_port = 22 328 ip_protocol = "tcp" 329 cidr = "0.0.0.0/0" 330 } 331 } 332 333 resource "openstack_compute_instance_v2" "foo" { 334 name = "terraform-test" 335 security_groups = ["default", "${openstack_compute_secgroup_v2.foo.name}"] 336 network { 337 uuid = "%s" 338 } 339 }`, 340 os.Getenv("OS_NETWORK_ID")) 341 342 resource.Test(t, resource.TestCase{ 343 PreCheck: func() { testAccPreCheck(t) }, 344 Providers: testAccProviders, 345 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 346 Steps: []resource.TestStep{ 347 resource.TestStep{ 348 Config: testAccComputeV2Instance_multi_secgroups, 349 Check: resource.ComposeTestCheckFunc( 350 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.foo", &secgroup), 351 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 352 ), 353 }, 354 }, 355 }) 356 } 357 358 func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) { 359 var instance servers.Server 360 var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(` 361 resource "openstack_compute_instance_v2" "foo" { 362 name = "terraform-test" 363 security_groups = ["default"] 364 block_device { 365 uuid = "%s" 366 source_type = "image" 367 volume_size = 5 368 boot_index = 0 369 destination_type = "volume" 370 delete_on_termination = true 371 } 372 }`, 373 os.Getenv("OS_IMAGE_ID")) 374 375 resource.Test(t, resource.TestCase{ 376 PreCheck: func() { testAccPreCheck(t) }, 377 Providers: testAccProviders, 378 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 379 Steps: []resource.TestStep{ 380 resource.TestStep{ 381 Config: testAccComputeV2Instance_bootFromVolumeImage, 382 Check: resource.ComposeTestCheckFunc( 383 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 384 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 385 ), 386 }, 387 }, 388 }) 389 } 390 391 func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) { 392 var instance servers.Server 393 var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(` 394 resource "openstack_blockstorage_volume_v1" "foo" { 395 name = "terraform-test" 396 size = 5 397 image_id = "%s" 398 } 399 400 resource "openstack_compute_instance_v2" "foo" { 401 name = "terraform-test" 402 security_groups = ["default"] 403 block_device { 404 uuid = "${openstack_blockstorage_volume_v1.foo.id}" 405 source_type = "volume" 406 volume_size = 5 407 boot_index = 0 408 destination_type = "volume" 409 delete_on_termination = true 410 } 411 }`, 412 os.Getenv("OS_IMAGE_ID")) 413 414 resource.Test(t, resource.TestCase{ 415 PreCheck: func() { testAccPreCheck(t) }, 416 Providers: testAccProviders, 417 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 418 Steps: []resource.TestStep{ 419 resource.TestStep{ 420 Config: testAccComputeV2Instance_bootFromVolumeVolume, 421 Check: resource.ComposeTestCheckFunc( 422 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 423 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 424 ), 425 }, 426 }, 427 }) 428 } 429 430 // TODO: verify the personality really exists on the instance. 431 func TestAccComputeV2Instance_personality(t *testing.T) { 432 var instance servers.Server 433 var testAccComputeV2Instance_personality = fmt.Sprintf(` 434 resource "openstack_compute_instance_v2" "foo" { 435 name = "terraform-test" 436 security_groups = ["default"] 437 personality { 438 file = "/tmp/foobar.txt" 439 content = "happy" 440 } 441 personality { 442 file = "/tmp/barfoo.txt" 443 content = "angry" 444 } 445 }`) 446 447 resource.Test(t, resource.TestCase{ 448 PreCheck: func() { testAccPreCheck(t) }, 449 Providers: testAccProviders, 450 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 451 Steps: []resource.TestStep{ 452 resource.TestStep{ 453 Config: testAccComputeV2Instance_personality, 454 Check: resource.ComposeTestCheckFunc( 455 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 456 ), 457 }, 458 }, 459 }) 460 } 461 462 func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error { 463 config := testAccProvider.Meta().(*Config) 464 computeClient, err := config.computeV2Client(OS_REGION_NAME) 465 if err != nil { 466 return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err) 467 } 468 469 for _, rs := range s.RootModule().Resources { 470 if rs.Type != "openstack_compute_instance_v2" { 471 continue 472 } 473 474 _, err := servers.Get(computeClient, rs.Primary.ID).Extract() 475 if err == nil { 476 return fmt.Errorf("Instance still exists") 477 } 478 } 479 480 return nil 481 } 482 483 func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc { 484 return func(s *terraform.State) error { 485 rs, ok := s.RootModule().Resources[n] 486 if !ok { 487 return fmt.Errorf("Not found: %s", n) 488 } 489 490 if rs.Primary.ID == "" { 491 return fmt.Errorf("No ID is set") 492 } 493 494 config := testAccProvider.Meta().(*Config) 495 computeClient, err := config.computeV2Client(OS_REGION_NAME) 496 if err != nil { 497 return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err) 498 } 499 500 found, err := servers.Get(computeClient, rs.Primary.ID).Extract() 501 if err != nil { 502 return err 503 } 504 505 if found.ID != rs.Primary.ID { 506 return fmt.Errorf("Instance not found") 507 } 508 509 *instance = *found 510 511 return nil 512 } 513 } 514 515 func testAccCheckComputeV2InstanceMetadata( 516 instance *servers.Server, k string, v string) resource.TestCheckFunc { 517 return func(s *terraform.State) error { 518 if instance.Metadata == nil { 519 return fmt.Errorf("No metadata") 520 } 521 522 for key, value := range instance.Metadata { 523 if k != key { 524 continue 525 } 526 527 if v == value.(string) { 528 return nil 529 } 530 531 return fmt.Errorf("Bad value for %s: %s", k, value) 532 } 533 534 return fmt.Errorf("Metadata not found: %s", k) 535 } 536 } 537 538 func testAccCheckComputeV2InstanceVolumeAttachment( 539 instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc { 540 return func(s *terraform.State) error { 541 var attachments []volumeattach.VolumeAttachment 542 543 config := testAccProvider.Meta().(*Config) 544 computeClient, err := config.computeV2Client(OS_REGION_NAME) 545 if err != nil { 546 return err 547 } 548 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 549 actual, err := volumeattach.ExtractVolumeAttachments(page) 550 if err != nil { 551 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 552 } 553 554 attachments = actual 555 return true, nil 556 }) 557 558 for _, attachment := range attachments { 559 if attachment.VolumeID == volume.ID { 560 return nil 561 } 562 } 563 564 return fmt.Errorf("Volume not found: %s", volume.ID) 565 } 566 } 567 568 func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc { 569 return func(s *terraform.State) error { 570 var attachments []volumeattach.VolumeAttachment 571 572 config := testAccProvider.Meta().(*Config) 573 computeClient, err := config.computeV2Client(OS_REGION_NAME) 574 if err != nil { 575 return err 576 } 577 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 578 actual, err := volumeattach.ExtractVolumeAttachments(page) 579 if err != nil { 580 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 581 } 582 583 attachments = actual 584 return true, nil 585 }) 586 587 if len(attachments) > 0 { 588 return fmt.Errorf("Volumes are still attached.") 589 } 590 591 return nil 592 } 593 } 594 595 func testAccCheckComputeV2InstanceBootVolumeAttachment( 596 instance *servers.Server) resource.TestCheckFunc { 597 return func(s *terraform.State) error { 598 var attachments []volumeattach.VolumeAttachment 599 600 config := testAccProvider.Meta().(*Config) 601 computeClient, err := config.computeV2Client(OS_REGION_NAME) 602 if err != nil { 603 return err 604 } 605 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 606 actual, err := volumeattach.ExtractVolumeAttachments(page) 607 if err != nil { 608 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 609 } 610 611 attachments = actual 612 return true, nil 613 }) 614 615 if len(attachments) == 1 { 616 return nil 617 } 618 619 return fmt.Errorf("No attached volume found.") 620 } 621 } 622 623 func testAccCheckComputeV2InstanceFloatingIPAttach( 624 instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc { 625 return func(s *terraform.State) error { 626 if fip.InstanceID == instance.ID { 627 return nil 628 } 629 630 return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID) 631 632 } 633 }