github.com/ljosa/terraform@v0.7.0-rc2.0.20160617205345-fe540b408f59/builtin/providers/azurerm/resource_arm_virtual_machine_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAzureRMVirtualMachine_basicLinuxMachine(t *testing.T) { 14 ri := acctest.RandInt() 15 config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 20 Steps: []resource.TestStep{ 21 { 22 Config: config, 23 Check: resource.ComposeTestCheckFunc( 24 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccAzureRMVirtualMachine_tags(t *testing.T) { 32 ri := acctest.RandInt() 33 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 34 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineUpdated, ri, ri, ri, ri, ri, ri, ri) 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 39 Steps: []resource.TestStep{ 40 { 41 Config: preConfig, 42 Check: resource.ComposeTestCheckFunc( 43 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 44 resource.TestCheckResourceAttr( 45 "azurerm_virtual_machine.test", "tags.%", "2"), 46 resource.TestCheckResourceAttr( 47 "azurerm_virtual_machine.test", "tags.environment", "Production"), 48 resource.TestCheckResourceAttr( 49 "azurerm_virtual_machine.test", "tags.cost-center", "Ops"), 50 ), 51 }, 52 53 { 54 Config: postConfig, 55 Check: resource.ComposeTestCheckFunc( 56 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 57 resource.TestCheckResourceAttr( 58 "azurerm_virtual_machine.test", "tags.%", "1"), 59 resource.TestCheckResourceAttr( 60 "azurerm_virtual_machine.test", "tags.environment", "Production"), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 //This is a regression test around https://github.com/hashicorp/terraform/issues/6517 68 //Because we use CreateOrUpdate, we were sending an empty password on update requests 69 func TestAccAzureRMVirtualMachine_updateMachineSize(t *testing.T) { 70 ri := acctest.RandInt() 71 preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 72 postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_updatedLinuxMachine, ri, ri, ri, ri, ri, ri, ri) 73 resource.Test(t, resource.TestCase{ 74 PreCheck: func() { testAccPreCheck(t) }, 75 Providers: testAccProviders, 76 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 77 Steps: []resource.TestStep{ 78 { 79 Config: preConfig, 80 Check: resource.ComposeTestCheckFunc( 81 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 82 resource.TestCheckResourceAttr( 83 "azurerm_virtual_machine.test", "vm_size", "Standard_A0"), 84 ), 85 }, 86 { 87 Config: postConfig, 88 Check: resource.ComposeTestCheckFunc( 89 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 90 resource.TestCheckResourceAttr( 91 "azurerm_virtual_machine.test", "vm_size", "Standard_A1"), 92 ), 93 }, 94 }, 95 }) 96 } 97 98 func TestAccAzureRMVirtualMachine_basicWindowsMachine(t *testing.T) { 99 ri := acctest.RandInt() 100 config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicWindowsMachine, ri, ri, ri, ri, ri, ri) 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 105 Steps: []resource.TestStep{ 106 { 107 Config: config, 108 Check: resource.ComposeTestCheckFunc( 109 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 110 ), 111 }, 112 }, 113 }) 114 } 115 116 func TestAccAzureRMVirtualMachine_windowsUnattendedConfig(t *testing.T) { 117 ri := acctest.RandInt() 118 config := fmt.Sprintf(testAccAzureRMVirtualMachine_windowsUnattendedConfig, ri, ri, ri, ri, ri, ri) 119 resource.Test(t, resource.TestCase{ 120 PreCheck: func() { testAccPreCheck(t) }, 121 Providers: testAccProviders, 122 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 123 Steps: []resource.TestStep{ 124 { 125 Config: config, 126 Check: resource.ComposeTestCheckFunc( 127 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 128 ), 129 }, 130 }, 131 }) 132 } 133 134 func TestAccAzureRMVirtualMachine_winRMConfig(t *testing.T) { 135 ri := acctest.RandInt() 136 config := fmt.Sprintf(testAccAzureRMVirtualMachine_winRMConfig, ri, ri, ri, ri, ri, ri) 137 resource.Test(t, resource.TestCase{ 138 PreCheck: func() { testAccPreCheck(t) }, 139 Providers: testAccProviders, 140 CheckDestroy: testCheckAzureRMVirtualMachineDestroy, 141 Steps: []resource.TestStep{ 142 { 143 Config: config, 144 Check: resource.ComposeTestCheckFunc( 145 testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), 146 ), 147 }, 148 }, 149 }) 150 } 151 152 func testCheckAzureRMVirtualMachineExists(name string) resource.TestCheckFunc { 153 return func(s *terraform.State) error { 154 // Ensure we have enough information in state to look up in API 155 rs, ok := s.RootModule().Resources[name] 156 if !ok { 157 return fmt.Errorf("Not found: %s", name) 158 } 159 160 vmName := rs.Primary.Attributes["name"] 161 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 162 if !hasResourceGroup { 163 return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName) 164 } 165 166 conn := testAccProvider.Meta().(*ArmClient).vmClient 167 168 resp, err := conn.Get(resourceGroup, vmName, "") 169 if err != nil { 170 return fmt.Errorf("Bad: Get on vmClient: %s", err) 171 } 172 173 if resp.StatusCode == http.StatusNotFound { 174 return fmt.Errorf("Bad: VirtualMachine %q (resource group: %q) does not exist", vmName, resourceGroup) 175 } 176 177 return nil 178 } 179 } 180 181 func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error { 182 conn := testAccProvider.Meta().(*ArmClient).vmClient 183 184 for _, rs := range s.RootModule().Resources { 185 if rs.Type != "azurerm_virtual_machine" { 186 continue 187 } 188 189 name := rs.Primary.Attributes["name"] 190 resourceGroup := rs.Primary.Attributes["resource_group_name"] 191 192 resp, err := conn.Get(resourceGroup, name, "") 193 194 if err != nil { 195 return nil 196 } 197 198 if resp.StatusCode != http.StatusNotFound { 199 return fmt.Errorf("Virtual Machine still exists:\n%#v", resp.Properties) 200 } 201 } 202 203 return nil 204 } 205 206 var testAccAzureRMVirtualMachine_basicLinuxMachine = ` 207 resource "azurerm_resource_group" "test" { 208 name = "acctestrg-%d" 209 location = "West US" 210 } 211 212 resource "azurerm_virtual_network" "test" { 213 name = "acctvn-%d" 214 address_space = ["10.0.0.0/16"] 215 location = "West US" 216 resource_group_name = "${azurerm_resource_group.test.name}" 217 } 218 219 resource "azurerm_subnet" "test" { 220 name = "acctsub-%d" 221 resource_group_name = "${azurerm_resource_group.test.name}" 222 virtual_network_name = "${azurerm_virtual_network.test.name}" 223 address_prefix = "10.0.2.0/24" 224 } 225 226 resource "azurerm_network_interface" "test" { 227 name = "acctni-%d" 228 location = "West US" 229 resource_group_name = "${azurerm_resource_group.test.name}" 230 231 ip_configuration { 232 name = "testconfiguration1" 233 subnet_id = "${azurerm_subnet.test.id}" 234 private_ip_address_allocation = "dynamic" 235 } 236 } 237 238 resource "azurerm_storage_account" "test" { 239 name = "accsa%d" 240 resource_group_name = "${azurerm_resource_group.test.name}" 241 location = "westus" 242 account_type = "Standard_LRS" 243 244 tags { 245 environment = "staging" 246 } 247 } 248 249 resource "azurerm_storage_container" "test" { 250 name = "vhds" 251 resource_group_name = "${azurerm_resource_group.test.name}" 252 storage_account_name = "${azurerm_storage_account.test.name}" 253 container_access_type = "private" 254 } 255 256 resource "azurerm_virtual_machine" "test" { 257 name = "acctvm-%d" 258 location = "West US" 259 resource_group_name = "${azurerm_resource_group.test.name}" 260 network_interface_ids = ["${azurerm_network_interface.test.id}"] 261 vm_size = "Standard_A0" 262 263 storage_image_reference { 264 publisher = "Canonical" 265 offer = "UbuntuServer" 266 sku = "14.04.2-LTS" 267 version = "latest" 268 } 269 270 storage_os_disk { 271 name = "myosdisk1" 272 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 273 caching = "ReadWrite" 274 create_option = "FromImage" 275 } 276 277 os_profile { 278 computer_name = "hostname%d" 279 admin_username = "testadmin" 280 admin_password = "Password1234!" 281 } 282 283 os_profile_linux_config { 284 disable_password_authentication = false 285 } 286 287 tags { 288 environment = "Production" 289 cost-center = "Ops" 290 } 291 } 292 ` 293 294 var testAccAzureRMVirtualMachine_basicLinuxMachineUpdated = ` 295 resource "azurerm_resource_group" "test" { 296 name = "acctestrg-%d" 297 location = "West US" 298 } 299 300 resource "azurerm_virtual_network" "test" { 301 name = "acctvn-%d" 302 address_space = ["10.0.0.0/16"] 303 location = "West US" 304 resource_group_name = "${azurerm_resource_group.test.name}" 305 } 306 307 resource "azurerm_subnet" "test" { 308 name = "acctsub-%d" 309 resource_group_name = "${azurerm_resource_group.test.name}" 310 virtual_network_name = "${azurerm_virtual_network.test.name}" 311 address_prefix = "10.0.2.0/24" 312 } 313 314 resource "azurerm_network_interface" "test" { 315 name = "acctni-%d" 316 location = "West US" 317 resource_group_name = "${azurerm_resource_group.test.name}" 318 319 ip_configuration { 320 name = "testconfiguration1" 321 subnet_id = "${azurerm_subnet.test.id}" 322 private_ip_address_allocation = "dynamic" 323 } 324 } 325 326 resource "azurerm_storage_account" "test" { 327 name = "accsa%d" 328 resource_group_name = "${azurerm_resource_group.test.name}" 329 location = "westus" 330 account_type = "Standard_LRS" 331 332 tags { 333 environment = "staging" 334 } 335 } 336 337 resource "azurerm_storage_container" "test" { 338 name = "vhds" 339 resource_group_name = "${azurerm_resource_group.test.name}" 340 storage_account_name = "${azurerm_storage_account.test.name}" 341 container_access_type = "private" 342 } 343 344 resource "azurerm_virtual_machine" "test" { 345 name = "acctvm-%d" 346 location = "West US" 347 resource_group_name = "${azurerm_resource_group.test.name}" 348 network_interface_ids = ["${azurerm_network_interface.test.id}"] 349 vm_size = "Standard_A0" 350 351 storage_image_reference { 352 publisher = "Canonical" 353 offer = "UbuntuServer" 354 sku = "14.04.2-LTS" 355 version = "latest" 356 } 357 358 storage_os_disk { 359 name = "myosdisk1" 360 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 361 caching = "ReadWrite" 362 create_option = "FromImage" 363 } 364 365 os_profile { 366 computer_name = "hostname%d" 367 admin_username = "testadmin" 368 admin_password = "Password1234!" 369 } 370 371 os_profile_linux_config { 372 disable_password_authentication = false 373 } 374 375 tags { 376 environment = "Production" 377 } 378 } 379 ` 380 381 var testAccAzureRMVirtualMachine_updatedLinuxMachine = ` 382 resource "azurerm_resource_group" "test" { 383 name = "acctestrg-%d" 384 location = "West US" 385 } 386 387 resource "azurerm_virtual_network" "test" { 388 name = "acctvn-%d" 389 address_space = ["10.0.0.0/16"] 390 location = "West US" 391 resource_group_name = "${azurerm_resource_group.test.name}" 392 } 393 394 resource "azurerm_subnet" "test" { 395 name = "acctsub-%d" 396 resource_group_name = "${azurerm_resource_group.test.name}" 397 virtual_network_name = "${azurerm_virtual_network.test.name}" 398 address_prefix = "10.0.2.0/24" 399 } 400 401 resource "azurerm_network_interface" "test" { 402 name = "acctni-%d" 403 location = "West US" 404 resource_group_name = "${azurerm_resource_group.test.name}" 405 406 ip_configuration { 407 name = "testconfiguration1" 408 subnet_id = "${azurerm_subnet.test.id}" 409 private_ip_address_allocation = "dynamic" 410 } 411 } 412 413 resource "azurerm_storage_account" "test" { 414 name = "accsa%d" 415 resource_group_name = "${azurerm_resource_group.test.name}" 416 location = "westus" 417 account_type = "Standard_LRS" 418 419 tags { 420 environment = "staging" 421 } 422 } 423 424 resource "azurerm_storage_container" "test" { 425 name = "vhds" 426 resource_group_name = "${azurerm_resource_group.test.name}" 427 storage_account_name = "${azurerm_storage_account.test.name}" 428 container_access_type = "private" 429 } 430 431 resource "azurerm_virtual_machine" "test" { 432 name = "acctvm-%d" 433 location = "West US" 434 resource_group_name = "${azurerm_resource_group.test.name}" 435 network_interface_ids = ["${azurerm_network_interface.test.id}"] 436 vm_size = "Standard_A1" 437 438 storage_image_reference { 439 publisher = "Canonical" 440 offer = "UbuntuServer" 441 sku = "14.04.2-LTS" 442 version = "latest" 443 } 444 445 storage_os_disk { 446 name = "myosdisk1" 447 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 448 caching = "ReadWrite" 449 create_option = "FromImage" 450 } 451 452 os_profile { 453 computer_name = "hostname%d" 454 admin_username = "testadmin" 455 admin_password = "Password1234!" 456 } 457 458 os_profile_linux_config { 459 disable_password_authentication = false 460 } 461 } 462 ` 463 464 var testAccAzureRMVirtualMachine_basicWindowsMachine = ` 465 resource "azurerm_resource_group" "test" { 466 name = "acctestrg-%d" 467 location = "West US" 468 } 469 470 resource "azurerm_virtual_network" "test" { 471 name = "acctvn-%d" 472 address_space = ["10.0.0.0/16"] 473 location = "West US" 474 resource_group_name = "${azurerm_resource_group.test.name}" 475 } 476 477 resource "azurerm_subnet" "test" { 478 name = "acctsub-%d" 479 resource_group_name = "${azurerm_resource_group.test.name}" 480 virtual_network_name = "${azurerm_virtual_network.test.name}" 481 address_prefix = "10.0.2.0/24" 482 } 483 484 resource "azurerm_network_interface" "test" { 485 name = "acctni-%d" 486 location = "West US" 487 resource_group_name = "${azurerm_resource_group.test.name}" 488 489 ip_configuration { 490 name = "testconfiguration1" 491 subnet_id = "${azurerm_subnet.test.id}" 492 private_ip_address_allocation = "dynamic" 493 } 494 } 495 496 resource "azurerm_storage_account" "test" { 497 name = "accsa%d" 498 resource_group_name = "${azurerm_resource_group.test.name}" 499 location = "westus" 500 account_type = "Standard_LRS" 501 502 tags { 503 environment = "staging" 504 } 505 } 506 507 resource "azurerm_storage_container" "test" { 508 name = "vhds" 509 resource_group_name = "${azurerm_resource_group.test.name}" 510 storage_account_name = "${azurerm_storage_account.test.name}" 511 container_access_type = "private" 512 } 513 514 resource "azurerm_virtual_machine" "test" { 515 name = "acctvm-%d" 516 location = "West US" 517 resource_group_name = "${azurerm_resource_group.test.name}" 518 network_interface_ids = ["${azurerm_network_interface.test.id}"] 519 vm_size = "Standard_A0" 520 521 storage_image_reference { 522 publisher = "MicrosoftWindowsServer" 523 offer = "WindowsServer" 524 sku = "2012-Datacenter" 525 version = "latest" 526 } 527 528 storage_os_disk { 529 name = "myosdisk1" 530 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 531 caching = "ReadWrite" 532 create_option = "FromImage" 533 } 534 535 os_profile { 536 computer_name = "winhost01" 537 admin_username = "testadmin" 538 admin_password = "Password1234!" 539 } 540 541 os_profile_windows_config { 542 enable_automatic_upgrades = false 543 provision_vm_agent = true 544 } 545 } 546 ` 547 548 var testAccAzureRMVirtualMachine_windowsUnattendedConfig = ` 549 resource "azurerm_resource_group" "test" { 550 name = "acctestrg-%d" 551 location = "West US" 552 } 553 554 resource "azurerm_virtual_network" "test" { 555 name = "acctvn-%d" 556 address_space = ["10.0.0.0/16"] 557 location = "West US" 558 resource_group_name = "${azurerm_resource_group.test.name}" 559 } 560 561 resource "azurerm_subnet" "test" { 562 name = "acctsub-%d" 563 resource_group_name = "${azurerm_resource_group.test.name}" 564 virtual_network_name = "${azurerm_virtual_network.test.name}" 565 address_prefix = "10.0.2.0/24" 566 } 567 568 resource "azurerm_network_interface" "test" { 569 name = "acctni-%d" 570 location = "West US" 571 resource_group_name = "${azurerm_resource_group.test.name}" 572 573 ip_configuration { 574 name = "testconfiguration1" 575 subnet_id = "${azurerm_subnet.test.id}" 576 private_ip_address_allocation = "dynamic" 577 } 578 } 579 580 resource "azurerm_storage_account" "test" { 581 name = "accsa%d" 582 resource_group_name = "${azurerm_resource_group.test.name}" 583 location = "westus" 584 account_type = "Standard_LRS" 585 586 tags { 587 environment = "staging" 588 } 589 } 590 591 resource "azurerm_storage_container" "test" { 592 name = "vhds" 593 resource_group_name = "${azurerm_resource_group.test.name}" 594 storage_account_name = "${azurerm_storage_account.test.name}" 595 container_access_type = "private" 596 } 597 598 resource "azurerm_virtual_machine" "test" { 599 name = "acctvm-%d" 600 location = "West US" 601 resource_group_name = "${azurerm_resource_group.test.name}" 602 network_interface_ids = ["${azurerm_network_interface.test.id}"] 603 vm_size = "Standard_A0" 604 605 storage_image_reference { 606 publisher = "MicrosoftWindowsServer" 607 offer = "WindowsServer" 608 sku = "2012-Datacenter" 609 version = "latest" 610 } 611 612 storage_os_disk { 613 name = "myosdisk1" 614 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 615 caching = "ReadWrite" 616 create_option = "FromImage" 617 } 618 619 os_profile { 620 computer_name = "winhost01" 621 admin_username = "testadmin" 622 admin_password = "Password1234!" 623 } 624 625 os_profile_windows_config { 626 provision_vm_agent = true 627 additional_unattend_config { 628 pass = "oobeSystem" 629 component = "Microsoft-Windows-Shell-Setup" 630 setting_name = "FirstLogonCommands" 631 content = "<FirstLogonCommands><SynchronousCommand><CommandLine>shutdown /r /t 0 /c \"initial reboot\"</CommandLine><Description>reboot</Description><Order>1</Order></SynchronousCommand></FirstLogonCommands>" 632 } 633 } 634 } 635 ` 636 637 var testAccAzureRMVirtualMachine_winRMConfig = ` 638 resource "azurerm_resource_group" "test" { 639 name = "acctestrg-%d" 640 location = "West US" 641 } 642 643 resource "azurerm_virtual_network" "test" { 644 name = "acctvn-%d" 645 address_space = ["10.0.0.0/16"] 646 location = "West US" 647 resource_group_name = "${azurerm_resource_group.test.name}" 648 } 649 650 resource "azurerm_subnet" "test" { 651 name = "acctsub-%d" 652 resource_group_name = "${azurerm_resource_group.test.name}" 653 virtual_network_name = "${azurerm_virtual_network.test.name}" 654 address_prefix = "10.0.2.0/24" 655 } 656 657 resource "azurerm_network_interface" "test" { 658 name = "acctni-%d" 659 location = "West US" 660 resource_group_name = "${azurerm_resource_group.test.name}" 661 662 ip_configuration { 663 name = "testconfiguration1" 664 subnet_id = "${azurerm_subnet.test.id}" 665 private_ip_address_allocation = "dynamic" 666 } 667 } 668 669 resource "azurerm_storage_account" "test" { 670 name = "accsa%d" 671 resource_group_name = "${azurerm_resource_group.test.name}" 672 location = "westus" 673 account_type = "Standard_LRS" 674 675 tags { 676 environment = "staging" 677 } 678 } 679 680 resource "azurerm_storage_container" "test" { 681 name = "vhds" 682 resource_group_name = "${azurerm_resource_group.test.name}" 683 storage_account_name = "${azurerm_storage_account.test.name}" 684 container_access_type = "private" 685 } 686 687 resource "azurerm_virtual_machine" "test" { 688 name = "acctvm-%d" 689 location = "West US" 690 resource_group_name = "${azurerm_resource_group.test.name}" 691 network_interface_ids = ["${azurerm_network_interface.test.id}"] 692 vm_size = "Standard_A0" 693 694 storage_image_reference { 695 publisher = "MicrosoftWindowsServer" 696 offer = "WindowsServer" 697 sku = "2012-Datacenter" 698 version = "latest" 699 } 700 701 storage_os_disk { 702 name = "myosdisk1" 703 vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" 704 caching = "ReadWrite" 705 create_option = "FromImage" 706 } 707 708 os_profile { 709 computer_name = "winhost01" 710 admin_username = "testadmin" 711 admin_password = "Password1234!" 712 } 713 714 os_profile_windows_config { 715 winrm { 716 protocol = "http" 717 } 718 } 719 } 720 `