github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/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 testCheckAzureRMVirtualMachineExists(name string) resource.TestCheckFunc {
   117  	return func(s *terraform.State) error {
   118  		// Ensure we have enough information in state to look up in API
   119  		rs, ok := s.RootModule().Resources[name]
   120  		if !ok {
   121  			return fmt.Errorf("Not found: %s", name)
   122  		}
   123  
   124  		vmName := rs.Primary.Attributes["name"]
   125  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   126  		if !hasResourceGroup {
   127  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName)
   128  		}
   129  
   130  		conn := testAccProvider.Meta().(*ArmClient).vmClient
   131  
   132  		resp, err := conn.Get(resourceGroup, vmName, "")
   133  		if err != nil {
   134  			return fmt.Errorf("Bad: Get on vmClient: %s", err)
   135  		}
   136  
   137  		if resp.StatusCode == http.StatusNotFound {
   138  			return fmt.Errorf("Bad: VirtualMachine %q (resource group: %q) does not exist", vmName, resourceGroup)
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error {
   146  	conn := testAccProvider.Meta().(*ArmClient).vmClient
   147  
   148  	for _, rs := range s.RootModule().Resources {
   149  		if rs.Type != "azurerm_virtual_machine" {
   150  			continue
   151  		}
   152  
   153  		name := rs.Primary.Attributes["name"]
   154  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   155  
   156  		resp, err := conn.Get(resourceGroup, name, "")
   157  
   158  		if err != nil {
   159  			return nil
   160  		}
   161  
   162  		if resp.StatusCode != http.StatusNotFound {
   163  			return fmt.Errorf("Virtual Machine still exists:\n%#v", resp.Properties)
   164  		}
   165  	}
   166  
   167  	return nil
   168  }
   169  
   170  var testAccAzureRMVirtualMachine_basicLinuxMachine = `
   171  resource "azurerm_resource_group" "test" {
   172      name = "acctestrg-%d"
   173      location = "West US"
   174  }
   175  
   176  resource "azurerm_virtual_network" "test" {
   177      name = "acctvn-%d"
   178      address_space = ["10.0.0.0/16"]
   179      location = "West US"
   180      resource_group_name = "${azurerm_resource_group.test.name}"
   181  }
   182  
   183  resource "azurerm_subnet" "test" {
   184      name = "acctsub-%d"
   185      resource_group_name = "${azurerm_resource_group.test.name}"
   186      virtual_network_name = "${azurerm_virtual_network.test.name}"
   187      address_prefix = "10.0.2.0/24"
   188  }
   189  
   190  resource "azurerm_network_interface" "test" {
   191      name = "acctni-%d"
   192      location = "West US"
   193      resource_group_name = "${azurerm_resource_group.test.name}"
   194  
   195      ip_configuration {
   196      	name = "testconfiguration1"
   197      	subnet_id = "${azurerm_subnet.test.id}"
   198      	private_ip_address_allocation = "dynamic"
   199      }
   200  }
   201  
   202  resource "azurerm_storage_account" "test" {
   203      name = "accsa%d"
   204      resource_group_name = "${azurerm_resource_group.test.name}"
   205      location = "westus"
   206      account_type = "Standard_LRS"
   207  
   208      tags {
   209          environment = "staging"
   210      }
   211  }
   212  
   213  resource "azurerm_storage_container" "test" {
   214      name = "vhds"
   215      resource_group_name = "${azurerm_resource_group.test.name}"
   216      storage_account_name = "${azurerm_storage_account.test.name}"
   217      container_access_type = "private"
   218  }
   219  
   220  resource "azurerm_virtual_machine" "test" {
   221      name = "acctvm-%d"
   222      location = "West US"
   223      resource_group_name = "${azurerm_resource_group.test.name}"
   224      network_interface_ids = ["${azurerm_network_interface.test.id}"]
   225      vm_size = "Standard_A0"
   226  
   227      storage_image_reference {
   228  	publisher = "Canonical"
   229  	offer = "UbuntuServer"
   230  	sku = "14.04.2-LTS"
   231  	version = "latest"
   232      }
   233  
   234      storage_os_disk {
   235          name = "myosdisk1"
   236          vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   237          caching = "ReadWrite"
   238          create_option = "FromImage"
   239      }
   240  
   241      os_profile {
   242  	computer_name = "hostname%d"
   243  	admin_username = "testadmin"
   244  	admin_password = "Password1234!"
   245      }
   246  
   247      os_profile_linux_config {
   248  	disable_password_authentication = false
   249      }
   250  
   251      tags {
   252      	environment = "Production"
   253      	cost-center = "Ops"
   254      }
   255  }
   256  `
   257  
   258  var testAccAzureRMVirtualMachine_basicLinuxMachineUpdated = `
   259  resource "azurerm_resource_group" "test" {
   260      name = "acctestrg-%d"
   261      location = "West US"
   262  }
   263  
   264  resource "azurerm_virtual_network" "test" {
   265      name = "acctvn-%d"
   266      address_space = ["10.0.0.0/16"]
   267      location = "West US"
   268      resource_group_name = "${azurerm_resource_group.test.name}"
   269  }
   270  
   271  resource "azurerm_subnet" "test" {
   272      name = "acctsub-%d"
   273      resource_group_name = "${azurerm_resource_group.test.name}"
   274      virtual_network_name = "${azurerm_virtual_network.test.name}"
   275      address_prefix = "10.0.2.0/24"
   276  }
   277  
   278  resource "azurerm_network_interface" "test" {
   279      name = "acctni-%d"
   280      location = "West US"
   281      resource_group_name = "${azurerm_resource_group.test.name}"
   282  
   283      ip_configuration {
   284      	name = "testconfiguration1"
   285      	subnet_id = "${azurerm_subnet.test.id}"
   286      	private_ip_address_allocation = "dynamic"
   287      }
   288  }
   289  
   290  resource "azurerm_storage_account" "test" {
   291      name = "accsa%d"
   292      resource_group_name = "${azurerm_resource_group.test.name}"
   293      location = "westus"
   294      account_type = "Standard_LRS"
   295  
   296      tags {
   297          environment = "staging"
   298      }
   299  }
   300  
   301  resource "azurerm_storage_container" "test" {
   302      name = "vhds"
   303      resource_group_name = "${azurerm_resource_group.test.name}"
   304      storage_account_name = "${azurerm_storage_account.test.name}"
   305      container_access_type = "private"
   306  }
   307  
   308  resource "azurerm_virtual_machine" "test" {
   309      name = "acctvm-%d"
   310      location = "West US"
   311      resource_group_name = "${azurerm_resource_group.test.name}"
   312      network_interface_ids = ["${azurerm_network_interface.test.id}"]
   313      vm_size = "Standard_A0"
   314  
   315      storage_image_reference {
   316  	publisher = "Canonical"
   317  	offer = "UbuntuServer"
   318  	sku = "14.04.2-LTS"
   319  	version = "latest"
   320      }
   321  
   322      storage_os_disk {
   323          name = "myosdisk1"
   324          vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   325          caching = "ReadWrite"
   326          create_option = "FromImage"
   327      }
   328  
   329      os_profile {
   330  	computer_name = "hostname%d"
   331  	admin_username = "testadmin"
   332  	admin_password = "Password1234!"
   333      }
   334  
   335      os_profile_linux_config {
   336  	disable_password_authentication = false
   337      }
   338  
   339      tags {
   340      	environment = "Production"
   341      }
   342  }
   343  `
   344  
   345  var testAccAzureRMVirtualMachine_updatedLinuxMachine = `
   346  resource "azurerm_resource_group" "test" {
   347      name = "acctestrg-%d"
   348      location = "West US"
   349  }
   350  
   351  resource "azurerm_virtual_network" "test" {
   352      name = "acctvn-%d"
   353      address_space = ["10.0.0.0/16"]
   354      location = "West US"
   355      resource_group_name = "${azurerm_resource_group.test.name}"
   356  }
   357  
   358  resource "azurerm_subnet" "test" {
   359      name = "acctsub-%d"
   360      resource_group_name = "${azurerm_resource_group.test.name}"
   361      virtual_network_name = "${azurerm_virtual_network.test.name}"
   362      address_prefix = "10.0.2.0/24"
   363  }
   364  
   365  resource "azurerm_network_interface" "test" {
   366      name = "acctni-%d"
   367      location = "West US"
   368      resource_group_name = "${azurerm_resource_group.test.name}"
   369  
   370      ip_configuration {
   371      	name = "testconfiguration1"
   372      	subnet_id = "${azurerm_subnet.test.id}"
   373      	private_ip_address_allocation = "dynamic"
   374      }
   375  }
   376  
   377  resource "azurerm_storage_account" "test" {
   378      name = "accsa%d"
   379      resource_group_name = "${azurerm_resource_group.test.name}"
   380      location = "westus"
   381      account_type = "Standard_LRS"
   382  
   383      tags {
   384          environment = "staging"
   385      }
   386  }
   387  
   388  resource "azurerm_storage_container" "test" {
   389      name = "vhds"
   390      resource_group_name = "${azurerm_resource_group.test.name}"
   391      storage_account_name = "${azurerm_storage_account.test.name}"
   392      container_access_type = "private"
   393  }
   394  
   395  resource "azurerm_virtual_machine" "test" {
   396      name = "acctvm-%d"
   397      location = "West US"
   398      resource_group_name = "${azurerm_resource_group.test.name}"
   399      network_interface_ids = ["${azurerm_network_interface.test.id}"]
   400      vm_size = "Standard_A1"
   401  
   402      storage_image_reference {
   403  	publisher = "Canonical"
   404  	offer = "UbuntuServer"
   405  	sku = "14.04.2-LTS"
   406  	version = "latest"
   407      }
   408  
   409      storage_os_disk {
   410          name = "myosdisk1"
   411          vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   412          caching = "ReadWrite"
   413          create_option = "FromImage"
   414      }
   415  
   416      os_profile {
   417  	computer_name = "hostname%d"
   418  	admin_username = "testadmin"
   419  	admin_password = "Password1234!"
   420      }
   421  
   422      os_profile_linux_config {
   423  	disable_password_authentication = false
   424      }
   425  }
   426  `
   427  
   428  var testAccAzureRMVirtualMachine_basicWindowsMachine = `
   429  resource "azurerm_resource_group" "test" {
   430      name = "acctestrg-%d"
   431      location = "West US"
   432  }
   433  
   434  resource "azurerm_virtual_network" "test" {
   435      name = "acctvn-%d"
   436      address_space = ["10.0.0.0/16"]
   437      location = "West US"
   438      resource_group_name = "${azurerm_resource_group.test.name}"
   439  }
   440  
   441  resource "azurerm_subnet" "test" {
   442      name = "acctsub-%d"
   443      resource_group_name = "${azurerm_resource_group.test.name}"
   444      virtual_network_name = "${azurerm_virtual_network.test.name}"
   445      address_prefix = "10.0.2.0/24"
   446  }
   447  
   448  resource "azurerm_network_interface" "test" {
   449      name = "acctni-%d"
   450      location = "West US"
   451      resource_group_name = "${azurerm_resource_group.test.name}"
   452  
   453      ip_configuration {
   454      	name = "testconfiguration1"
   455      	subnet_id = "${azurerm_subnet.test.id}"
   456      	private_ip_address_allocation = "dynamic"
   457      }
   458  }
   459  
   460  resource "azurerm_storage_account" "test" {
   461      name = "accsa%d"
   462      resource_group_name = "${azurerm_resource_group.test.name}"
   463      location = "westus"
   464      account_type = "Standard_LRS"
   465  
   466      tags {
   467          environment = "staging"
   468      }
   469  }
   470  
   471  resource "azurerm_storage_container" "test" {
   472      name = "vhds"
   473      resource_group_name = "${azurerm_resource_group.test.name}"
   474      storage_account_name = "${azurerm_storage_account.test.name}"
   475      container_access_type = "private"
   476  }
   477  
   478  resource "azurerm_virtual_machine" "test" {
   479      name = "acctvm-%d"
   480      location = "West US"
   481      resource_group_name = "${azurerm_resource_group.test.name}"
   482      network_interface_ids = ["${azurerm_network_interface.test.id}"]
   483      vm_size = "Standard_A0"
   484  
   485      storage_image_reference {
   486  	publisher = "MicrosoftWindowsServer"
   487  	offer = "WindowsServer"
   488  	sku = "2012-Datacenter"
   489  	version = "latest"
   490      }
   491  
   492      storage_os_disk {
   493          name = "myosdisk1"
   494          vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
   495          caching = "ReadWrite"
   496          create_option = "FromImage"
   497      }
   498  
   499      os_profile {
   500  	computer_name = "winhost01"
   501  	admin_username = "testadmin"
   502  	admin_password = "Password1234!"
   503      }
   504  
   505      os_profile_windows_config {
   506  	enable_automatic_upgrades = false
   507  	provision_vm_agent = true
   508      }
   509  }
   510  `