github.com/grange74/terraform@v0.7.0-rc3.0.20160722171430-8c8803864753/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_1 servers.Server 320 var secgroup_1 secgroups.SecurityGroup 321 var testAccComputeV2Instance_multi_secgroups = fmt.Sprintf(` 322 resource "openstack_compute_secgroup_v2" "secgroup_1" { 323 name = "secgroup_1" 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" "instance_1" { 334 name = "instance_1" 335 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.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.secgroup_1", &secgroup_1), 351 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 352 ), 353 }, 354 }, 355 }) 356 } 357 358 func TestAccComputeV2Instance_multi_secgroups_update(t *testing.T) { 359 var instance_1 servers.Server 360 var secgroup_1, secgroup_2 secgroups.SecurityGroup 361 var testAccComputeV2Instance_multi_secgroups_update_1 = fmt.Sprintf(` 362 resource "openstack_compute_secgroup_v2" "secgroup_1" { 363 name = "secgroup_1" 364 description = "a security group" 365 rule { 366 from_port = 22 367 to_port = 22 368 ip_protocol = "tcp" 369 cidr = "0.0.0.0/0" 370 } 371 } 372 373 resource "openstack_compute_secgroup_v2" "secgroup_2" { 374 name = "secgroup_2" 375 description = "another security group" 376 rule { 377 from_port = 80 378 to_port = 80 379 ip_protocol = "tcp" 380 cidr = "0.0.0.0/0" 381 } 382 } 383 384 resource "openstack_compute_instance_v2" "instance_1" { 385 name = "instance_1" 386 security_groups = ["default"] 387 }`) 388 389 var testAccComputeV2Instance_multi_secgroups_update_2 = fmt.Sprintf(` 390 resource "openstack_compute_secgroup_v2" "secgroup_1" { 391 name = "secgroup_1" 392 description = "a security group" 393 rule { 394 from_port = 22 395 to_port = 22 396 ip_protocol = "tcp" 397 cidr = "0.0.0.0/0" 398 } 399 } 400 401 resource "openstack_compute_secgroup_v2" "secgroup_2" { 402 name = "secgroup_2" 403 description = "another security group" 404 rule { 405 from_port = 80 406 to_port = 80 407 ip_protocol = "tcp" 408 cidr = "0.0.0.0/0" 409 } 410 } 411 412 resource "openstack_compute_instance_v2" "instance_1" { 413 name = "instance_1" 414 security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}", "${openstack_compute_secgroup_v2.secgroup_2.name}"] 415 }`) 416 417 resource.Test(t, resource.TestCase{ 418 PreCheck: func() { testAccPreCheck(t) }, 419 Providers: testAccProviders, 420 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 421 Steps: []resource.TestStep{ 422 resource.TestStep{ 423 Config: testAccComputeV2Instance_multi_secgroups_update_1, 424 Check: resource.ComposeTestCheckFunc( 425 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1), 426 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2), 427 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 428 ), 429 }, 430 resource.TestStep{ 431 Config: testAccComputeV2Instance_multi_secgroups_update_2, 432 Check: resource.ComposeTestCheckFunc( 433 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup_1), 434 testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_2", &secgroup_2), 435 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance_1), 436 ), 437 }, 438 }, 439 }) 440 } 441 442 func TestAccComputeV2Instance_bootFromVolumeImage(t *testing.T) { 443 var instance servers.Server 444 var testAccComputeV2Instance_bootFromVolumeImage = fmt.Sprintf(` 445 resource "openstack_compute_instance_v2" "foo" { 446 name = "terraform-test" 447 security_groups = ["default"] 448 block_device { 449 uuid = "%s" 450 source_type = "image" 451 volume_size = 5 452 boot_index = 0 453 destination_type = "volume" 454 delete_on_termination = true 455 } 456 }`, 457 os.Getenv("OS_IMAGE_ID")) 458 459 resource.Test(t, resource.TestCase{ 460 PreCheck: func() { testAccPreCheck(t) }, 461 Providers: testAccProviders, 462 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 463 Steps: []resource.TestStep{ 464 resource.TestStep{ 465 Config: testAccComputeV2Instance_bootFromVolumeImage, 466 Check: resource.ComposeTestCheckFunc( 467 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 468 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 469 ), 470 }, 471 }, 472 }) 473 } 474 475 func TestAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume(t *testing.T) { 476 var instance servers.Server 477 var testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume = fmt.Sprintf(` 478 resource "openstack_blockstorage_volume_v1" "volume_1" { 479 name = "volume_1" 480 size = 1 481 } 482 483 resource "openstack_compute_instance_v2" "instance_1" { 484 name = "instance_1" 485 security_groups = ["default"] 486 block_device { 487 uuid = "%s" 488 source_type = "image" 489 volume_size = 2 490 boot_index = 0 491 destination_type = "volume" 492 delete_on_termination = true 493 } 494 495 volume { 496 volume_id = "${openstack_blockstorage_volume_v1.volume_1.id}" 497 } 498 }`, 499 os.Getenv("OS_IMAGE_ID")) 500 501 resource.Test(t, resource.TestCase{ 502 PreCheck: func() { testAccPreCheck(t) }, 503 Providers: testAccProviders, 504 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 505 Steps: []resource.TestStep{ 506 resource.TestStep{ 507 Config: testAccComputeV2Instance_bootFromVolumeImageWithAttachedVolume, 508 Check: resource.ComposeTestCheckFunc( 509 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance), 510 ), 511 }, 512 }, 513 }) 514 } 515 516 func TestAccComputeV2Instance_bootFromVolumeVolume(t *testing.T) { 517 var instance servers.Server 518 var testAccComputeV2Instance_bootFromVolumeVolume = fmt.Sprintf(` 519 resource "openstack_blockstorage_volume_v1" "foo" { 520 name = "terraform-test" 521 size = 5 522 image_id = "%s" 523 } 524 525 resource "openstack_compute_instance_v2" "foo" { 526 name = "terraform-test" 527 security_groups = ["default"] 528 block_device { 529 uuid = "${openstack_blockstorage_volume_v1.foo.id}" 530 source_type = "volume" 531 boot_index = 0 532 destination_type = "volume" 533 delete_on_termination = true 534 } 535 }`, 536 os.Getenv("OS_IMAGE_ID")) 537 538 resource.Test(t, resource.TestCase{ 539 PreCheck: func() { testAccPreCheck(t) }, 540 Providers: testAccProviders, 541 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 542 Steps: []resource.TestStep{ 543 resource.TestStep{ 544 Config: testAccComputeV2Instance_bootFromVolumeVolume, 545 Check: resource.ComposeTestCheckFunc( 546 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 547 testAccCheckComputeV2InstanceBootVolumeAttachment(&instance), 548 ), 549 }, 550 }, 551 }) 552 } 553 554 func TestAccComputeV2Instance_bootFromVolumeForceNew(t *testing.T) { 555 var instance1_1 servers.Server 556 var instance1_2 servers.Server 557 var testAccComputeV2Instance_bootFromVolumeForceNew_1 = fmt.Sprintf(` 558 resource "openstack_compute_instance_v2" "instance_1" { 559 name = "instance_1" 560 security_groups = ["default"] 561 block_device { 562 uuid = "%s" 563 source_type = "image" 564 volume_size = 5 565 boot_index = 0 566 destination_type = "volume" 567 delete_on_termination = true 568 } 569 }`, 570 os.Getenv("OS_IMAGE_ID")) 571 572 var testAccComputeV2Instance_bootFromVolumeForceNew_2 = fmt.Sprintf(` 573 resource "openstack_compute_instance_v2" "instance_1" { 574 name = "instance_1" 575 security_groups = ["default"] 576 block_device { 577 uuid = "%s" 578 source_type = "image" 579 volume_size = 4 580 boot_index = 0 581 destination_type = "volume" 582 delete_on_termination = true 583 } 584 }`, 585 os.Getenv("OS_IMAGE_ID")) 586 587 resource.Test(t, resource.TestCase{ 588 PreCheck: func() { testAccPreCheck(t) }, 589 Providers: testAccProviders, 590 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 591 Steps: []resource.TestStep{ 592 resource.TestStep{ 593 Config: testAccComputeV2Instance_bootFromVolumeForceNew_1, 594 Check: resource.ComposeTestCheckFunc( 595 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1), 596 ), 597 }, 598 resource.TestStep{ 599 Config: testAccComputeV2Instance_bootFromVolumeForceNew_2, 600 Check: resource.ComposeTestCheckFunc( 601 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2), 602 testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2), 603 ), 604 }, 605 }, 606 }) 607 } 608 609 // TODO: verify the personality really exists on the instance. 610 func TestAccComputeV2Instance_personality(t *testing.T) { 611 var instance servers.Server 612 var testAccComputeV2Instance_personality = fmt.Sprintf(` 613 resource "openstack_compute_instance_v2" "foo" { 614 name = "terraform-test" 615 security_groups = ["default"] 616 personality { 617 file = "/tmp/foobar.txt" 618 content = "happy" 619 } 620 personality { 621 file = "/tmp/barfoo.txt" 622 content = "angry" 623 } 624 }`) 625 626 resource.Test(t, resource.TestCase{ 627 PreCheck: func() { testAccPreCheck(t) }, 628 Providers: testAccProviders, 629 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 630 Steps: []resource.TestStep{ 631 resource.TestStep{ 632 Config: testAccComputeV2Instance_personality, 633 Check: resource.ComposeTestCheckFunc( 634 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 635 ), 636 }, 637 }, 638 }) 639 } 640 641 func TestAccComputeV2Instance_multiEphemeral(t *testing.T) { 642 var instance servers.Server 643 var testAccComputeV2Instance_multiEphemeral = fmt.Sprintf(` 644 resource "openstack_compute_instance_v2" "foo" { 645 name = "terraform-test" 646 security_groups = ["default"] 647 block_device { 648 boot_index = 0 649 delete_on_termination = true 650 destination_type = "local" 651 source_type = "image" 652 uuid = "%s" 653 } 654 block_device { 655 boot_index = -1 656 delete_on_termination = true 657 destination_type = "local" 658 source_type = "blank" 659 volume_size = 1 660 } 661 block_device { 662 boot_index = -1 663 delete_on_termination = true 664 destination_type = "local" 665 source_type = "blank" 666 volume_size = 1 667 } 668 }`, 669 os.Getenv("OS_IMAGE_ID")) 670 671 resource.Test(t, resource.TestCase{ 672 PreCheck: func() { testAccPreCheck(t) }, 673 Providers: testAccProviders, 674 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 675 Steps: []resource.TestStep{ 676 resource.TestStep{ 677 Config: testAccComputeV2Instance_multiEphemeral, 678 Check: resource.ComposeTestCheckFunc( 679 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 680 ), 681 }, 682 }, 683 }) 684 } 685 686 func TestAccComputeV2Instance_accessIPv4(t *testing.T) { 687 var instance servers.Server 688 var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(` 689 resource "openstack_compute_floatingip_v2" "myip" { 690 } 691 692 resource "openstack_networking_network_v2" "network_1" { 693 name = "network_1" 694 } 695 696 resource "openstack_networking_subnet_v2" "subnet_1" { 697 name = "subnet_1" 698 network_id = "${openstack_networking_network_v2.network_1.id}" 699 cidr = "192.168.1.0/24" 700 ip_version = 4 701 enable_dhcp = true 702 no_gateway = true 703 } 704 705 resource "openstack_compute_instance_v2" "instance_1" { 706 depends_on = ["openstack_networking_subnet_v2.subnet_1"] 707 708 name = "instance_1" 709 security_groups = ["default"] 710 floating_ip = "${openstack_compute_floatingip_v2.myip.address}" 711 712 network { 713 uuid = "%s" 714 } 715 716 network { 717 uuid = "${openstack_networking_network_v2.network_1.id}" 718 fixed_ip_v4 = "192.168.1.100" 719 access_network = true 720 } 721 }`, os.Getenv("OS_NETWORK_ID")) 722 723 resource.Test(t, resource.TestCase{ 724 PreCheck: func() { testAccPreCheck(t) }, 725 Providers: testAccProviders, 726 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 727 Steps: []resource.TestStep{ 728 resource.TestStep{ 729 Config: testAccComputeV2Instance_accessIPv4, 730 Check: resource.ComposeTestCheckFunc( 731 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance), 732 resource.TestCheckResourceAttr( 733 "openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"), 734 ), 735 }, 736 }, 737 }) 738 } 739 740 func TestAccComputeV2Instance_ChangeFixedIP(t *testing.T) { 741 var instance1_1 servers.Server 742 var instance1_2 servers.Server 743 var testAccComputeV2Instance_ChangeFixedIP_1 = fmt.Sprintf(` 744 resource "openstack_compute_instance_v2" "instance_1" { 745 name = "instance_1" 746 security_groups = ["default"] 747 network { 748 uuid = "%s" 749 fixed_ip_v4 = "10.0.0.24" 750 } 751 }`, 752 os.Getenv("OS_NETWORK_ID")) 753 754 var testAccComputeV2Instance_ChangeFixedIP_2 = fmt.Sprintf(` 755 resource "openstack_compute_instance_v2" "instance_1" { 756 name = "instance_1" 757 security_groups = ["default"] 758 network { 759 uuid = "%s" 760 fixed_ip_v4 = "10.0.0.25" 761 } 762 }`, 763 os.Getenv("OS_NETWORK_ID")) 764 765 resource.Test(t, resource.TestCase{ 766 PreCheck: func() { testAccPreCheck(t) }, 767 Providers: testAccProviders, 768 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 769 Steps: []resource.TestStep{ 770 resource.TestStep{ 771 Config: testAccComputeV2Instance_ChangeFixedIP_1, 772 Check: resource.ComposeTestCheckFunc( 773 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_1), 774 ), 775 }, 776 resource.TestStep{ 777 Config: testAccComputeV2Instance_ChangeFixedIP_2, 778 Check: resource.ComposeTestCheckFunc( 779 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1_2), 780 testAccCheckComputeV2InstanceInstanceIDsDoNotMatch(&instance1_1, &instance1_2), 781 ), 782 }, 783 }, 784 }) 785 } 786 787 func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error { 788 config := testAccProvider.Meta().(*Config) 789 computeClient, err := config.computeV2Client(OS_REGION_NAME) 790 if err != nil { 791 return fmt.Errorf("(testAccCheckComputeV2InstanceDestroy) Error creating OpenStack compute client: %s", err) 792 } 793 794 for _, rs := range s.RootModule().Resources { 795 if rs.Type != "openstack_compute_instance_v2" { 796 continue 797 } 798 799 _, err := servers.Get(computeClient, rs.Primary.ID).Extract() 800 if err == nil { 801 return fmt.Errorf("Instance still exists") 802 } 803 } 804 805 return nil 806 } 807 808 func testAccCheckComputeV2InstanceExists(t *testing.T, n string, instance *servers.Server) resource.TestCheckFunc { 809 return func(s *terraform.State) error { 810 rs, ok := s.RootModule().Resources[n] 811 if !ok { 812 return fmt.Errorf("Not found: %s", n) 813 } 814 815 if rs.Primary.ID == "" { 816 return fmt.Errorf("No ID is set") 817 } 818 819 config := testAccProvider.Meta().(*Config) 820 computeClient, err := config.computeV2Client(OS_REGION_NAME) 821 if err != nil { 822 return fmt.Errorf("(testAccCheckComputeV2InstanceExists) Error creating OpenStack compute client: %s", err) 823 } 824 825 found, err := servers.Get(computeClient, rs.Primary.ID).Extract() 826 if err != nil { 827 return err 828 } 829 830 if found.ID != rs.Primary.ID { 831 return fmt.Errorf("Instance not found") 832 } 833 834 *instance = *found 835 836 return nil 837 } 838 } 839 840 func testAccCheckComputeV2InstanceMetadata( 841 instance *servers.Server, k string, v string) resource.TestCheckFunc { 842 return func(s *terraform.State) error { 843 if instance.Metadata == nil { 844 return fmt.Errorf("No metadata") 845 } 846 847 for key, value := range instance.Metadata { 848 if k != key { 849 continue 850 } 851 852 if v == value.(string) { 853 return nil 854 } 855 856 return fmt.Errorf("Bad value for %s: %s", k, value) 857 } 858 859 return fmt.Errorf("Metadata not found: %s", k) 860 } 861 } 862 863 func testAccCheckComputeV2InstanceVolumeAttachment( 864 instance *servers.Server, volume *volumes.Volume) resource.TestCheckFunc { 865 return func(s *terraform.State) error { 866 var attachments []volumeattach.VolumeAttachment 867 868 config := testAccProvider.Meta().(*Config) 869 computeClient, err := config.computeV2Client(OS_REGION_NAME) 870 if err != nil { 871 return err 872 } 873 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 874 actual, err := volumeattach.ExtractVolumeAttachments(page) 875 if err != nil { 876 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 877 } 878 879 attachments = actual 880 return true, nil 881 }) 882 883 for _, attachment := range attachments { 884 if attachment.VolumeID == volume.ID { 885 return nil 886 } 887 } 888 889 return fmt.Errorf("Volume not found: %s", volume.ID) 890 } 891 } 892 893 func testAccCheckComputeV2InstanceVolumesDetached(instance *servers.Server) resource.TestCheckFunc { 894 return func(s *terraform.State) error { 895 var attachments []volumeattach.VolumeAttachment 896 897 config := testAccProvider.Meta().(*Config) 898 computeClient, err := config.computeV2Client(OS_REGION_NAME) 899 if err != nil { 900 return err 901 } 902 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 903 actual, err := volumeattach.ExtractVolumeAttachments(page) 904 if err != nil { 905 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 906 } 907 908 attachments = actual 909 return true, nil 910 }) 911 912 if len(attachments) > 0 { 913 return fmt.Errorf("Volumes are still attached.") 914 } 915 916 return nil 917 } 918 } 919 920 func testAccCheckComputeV2InstanceBootVolumeAttachment( 921 instance *servers.Server) resource.TestCheckFunc { 922 return func(s *terraform.State) error { 923 var attachments []volumeattach.VolumeAttachment 924 925 config := testAccProvider.Meta().(*Config) 926 computeClient, err := config.computeV2Client(OS_REGION_NAME) 927 if err != nil { 928 return err 929 } 930 err = volumeattach.List(computeClient, instance.ID).EachPage(func(page pagination.Page) (bool, error) { 931 actual, err := volumeattach.ExtractVolumeAttachments(page) 932 if err != nil { 933 return false, fmt.Errorf("Unable to lookup attachment: %s", err) 934 } 935 936 attachments = actual 937 return true, nil 938 }) 939 940 if len(attachments) == 1 { 941 return nil 942 } 943 944 return fmt.Errorf("No attached volume found.") 945 } 946 } 947 948 func testAccCheckComputeV2InstanceFloatingIPAttach( 949 instance *servers.Server, fip *floatingip.FloatingIP) resource.TestCheckFunc { 950 return func(s *terraform.State) error { 951 if fip.InstanceID == instance.ID { 952 return nil 953 } 954 955 return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.ID, instance.ID) 956 } 957 } 958 func testAccCheckComputeV2InstanceInstanceIDsDoNotMatch( 959 instance1, instance2 *servers.Server) resource.TestCheckFunc { 960 return func(s *terraform.State) error { 961 if instance1.ID == instance2.ID { 962 return fmt.Errorf("Instance was not recreated.") 963 } 964 965 return nil 966 } 967 } 968 969 func TestAccComputeV2Instance_stop_before_destroy(t *testing.T) { 970 var instance servers.Server 971 var testAccComputeV2Instance_stop_before_destroy = fmt.Sprintf(` 972 resource "openstack_compute_instance_v2" "foo" { 973 name = "terraform-test" 974 security_groups = ["default"] 975 network { 976 uuid = "%s" 977 } 978 stop_before_destroy = true 979 }`, 980 os.Getenv("OS_NETWORK_ID")) 981 982 resource.Test(t, resource.TestCase{ 983 PreCheck: func() { testAccPreCheck(t) }, 984 Providers: testAccProviders, 985 CheckDestroy: testAccCheckComputeV2InstanceDestroy, 986 Steps: []resource.TestStep{ 987 resource.TestStep{ 988 Config: testAccComputeV2Instance_stop_before_destroy, 989 Check: resource.ComposeTestCheckFunc( 990 testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.foo", &instance), 991 ), 992 }, 993 }, 994 }) 995 }