github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_virtual_machine_scale_set_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 TestAccAzureRMVirtualMachineScaleSet_basicLinux(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMVirtualMachineScaleSet_basicLinux, ri, ri, ri, ri, ri, ri, ri, ri)
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testCheckAzureRMVirtualMachineScaleSetDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: config,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testCheckAzureRMVirtualMachineScaleSetExists("azurerm_virtual_machine_scale_set.test"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccAzureRMVirtualMachineScaleSet_basicLinux_disappears(t *testing.T) {
    32  	ri := acctest.RandInt()
    33  	config := fmt.Sprintf(testAccAzureRMVirtualMachineScaleSet_basicLinux, ri, ri, ri, ri, ri, ri, ri, ri)
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testCheckAzureRMVirtualMachineScaleSetDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: config,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testCheckAzureRMVirtualMachineScaleSetExists("azurerm_virtual_machine_scale_set.test"),
    43  					testCheckAzureRMVirtualMachineScaleSetDisappears("azurerm_virtual_machine_scale_set.test"),
    44  				),
    45  				ExpectNonEmptyPlan: true,
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  //func TestAccAzureRMVirtualMachineScaleSet_basicWindowsMachine(t *testing.T) {
    52  //	ri := acctest.RandInt()
    53  //	rs := acctest.RandString(6)
    54  //	config := fmt.Sprintf(testAccAzureRMVirtualMachineScaleSet_basicWindows, ri, ri, ri, ri, ri, ri, rs, ri)
    55  //	resource.Test(t, resource.TestCase{
    56  //		PreCheck:     func() { testAccPreCheck(t) },
    57  //		Providers:    testAccProviders,
    58  //		CheckDestroy: testCheckAzureRMVirtualMachineScaleSetDestroy,
    59  //		Steps: []resource.TestStep{
    60  //			{
    61  //				Config: config,
    62  //				Check: resource.ComposeTestCheckFunc(
    63  //					testCheckAzureRMVirtualMachineScaleSetExists("azurerm_virtual_machine_scale_set.test"),
    64  //				),
    65  //			},
    66  //		},
    67  //	})
    68  //}
    69  
    70  func testCheckAzureRMVirtualMachineScaleSetExists(name string) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		// Ensure we have enough information in state to look up in API
    73  		rs, ok := s.RootModule().Resources[name]
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", name)
    76  		}
    77  
    78  		name := rs.Primary.Attributes["name"]
    79  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    80  		if !hasResourceGroup {
    81  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: scale set %s", name)
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*ArmClient).vmScaleSetClient
    85  
    86  		resp, err := conn.Get(resourceGroup, name)
    87  		if err != nil {
    88  			return fmt.Errorf("Bad: Get on vmScaleSetClient: %s", err)
    89  		}
    90  
    91  		if resp.StatusCode == http.StatusNotFound {
    92  			return fmt.Errorf("Bad: VirtualMachineScaleSet %q (resource group: %q) does not exist", name, resourceGroup)
    93  		}
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  func testCheckAzureRMVirtualMachineScaleSetDisappears(name string) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		// Ensure we have enough information in state to look up in API
   102  		rs, ok := s.RootModule().Resources[name]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", name)
   105  		}
   106  
   107  		name := rs.Primary.Attributes["name"]
   108  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   109  		if !hasResourceGroup {
   110  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: scale set %s", name)
   111  		}
   112  
   113  		conn := testAccProvider.Meta().(*ArmClient).vmScaleSetClient
   114  
   115  		_, err := conn.Delete(resourceGroup, name, make(chan struct{}))
   116  		if err != nil {
   117  			return fmt.Errorf("Bad: Delete on vmScaleSetClient: %s", err)
   118  		}
   119  
   120  		return nil
   121  	}
   122  }
   123  
   124  func testCheckAzureRMVirtualMachineScaleSetDestroy(s *terraform.State) error {
   125  	conn := testAccProvider.Meta().(*ArmClient).vmScaleSetClient
   126  
   127  	for _, rs := range s.RootModule().Resources {
   128  		if rs.Type != "azurerm_virtual_machine_scale_set" {
   129  			continue
   130  		}
   131  
   132  		name := rs.Primary.Attributes["name"]
   133  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   134  
   135  		resp, err := conn.Get(resourceGroup, name)
   136  
   137  		if err != nil {
   138  			return nil
   139  		}
   140  
   141  		if resp.StatusCode != http.StatusNotFound {
   142  			return fmt.Errorf("Virtual Machine Scale Set still exists:\n%#v", resp.Properties)
   143  		}
   144  	}
   145  
   146  	return nil
   147  }
   148  
   149  var testAccAzureRMVirtualMachineScaleSet_basicLinux = `
   150  resource "azurerm_resource_group" "test" {
   151      name = "acctestRG-%d"
   152      location = "West US"
   153  }
   154  
   155  resource "azurerm_virtual_network" "test" {
   156      name = "acctvn-%d"
   157      address_space = ["10.0.0.0/16"]
   158      location = "West US"
   159      resource_group_name = "${azurerm_resource_group.test.name}"
   160  }
   161  
   162  resource "azurerm_subnet" "test" {
   163      name = "acctsub-%d"
   164      resource_group_name = "${azurerm_resource_group.test.name}"
   165      virtual_network_name = "${azurerm_virtual_network.test.name}"
   166      address_prefix = "10.0.2.0/24"
   167  }
   168  
   169  resource "azurerm_network_interface" "test" {
   170      name = "acctni-%d"
   171      location = "West US"
   172      resource_group_name = "${azurerm_resource_group.test.name}"
   173  
   174      ip_configuration {
   175      	name = "testconfiguration1"
   176      	subnet_id = "${azurerm_subnet.test.id}"
   177      	private_ip_address_allocation = "dynamic"
   178      }
   179  }
   180  
   181  resource "azurerm_storage_account" "test" {
   182      name = "accsa%d"
   183      resource_group_name = "${azurerm_resource_group.test.name}"
   184      location = "westus"
   185      account_type = "Standard_LRS"
   186  
   187      tags {
   188          environment = "staging"
   189      }
   190  }
   191  
   192  resource "azurerm_storage_container" "test" {
   193      name = "vhds"
   194      resource_group_name = "${azurerm_resource_group.test.name}"
   195      storage_account_name = "${azurerm_storage_account.test.name}"
   196      container_access_type = "private"
   197  }
   198  
   199  resource "azurerm_virtual_machine_scale_set" "test" {
   200    name = "acctvmss-%d"
   201    location = "West US"
   202    resource_group_name = "${azurerm_resource_group.test.name}"
   203    upgrade_policy_mode = "Manual"
   204  
   205    sku {
   206      name = "Standard_A0"
   207      tier = "Standard"
   208      capacity = 2
   209    }
   210  
   211    os_profile {
   212      computer_name_prefix = "testvm-%d"
   213      admin_username = "myadmin"
   214      admin_password = "Passwword1234"
   215    }
   216  
   217    network_profile {
   218        name = "TestNetworkProfile-%d"
   219        primary = true
   220        ip_configuration {
   221          name = "TestIPConfiguration"
   222          subnet_id = "${azurerm_subnet.test.id}"
   223        }
   224    }
   225  
   226    storage_profile_os_disk {
   227      name = "osDiskProfile"
   228      caching       = "ReadWrite"
   229      create_option = "FromImage"
   230      vhd_containers = ["${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}"]
   231    }
   232  
   233    storage_profile_image_reference {
   234      publisher = "Canonical"
   235      offer     = "UbuntuServer"
   236      sku       = "14.04.2-LTS"
   237      version   = "latest"
   238    }
   239  }
   240  `
   241  
   242  var testAccAzureRMVirtualMachineScaleSet_basicWindows = `
   243  resource "azurerm_resource_group" "test" {
   244      name = "acctestRG-%d"
   245      location = "West US"
   246  }
   247  
   248  resource "azurerm_virtual_network" "test" {
   249      name = "acctvn-%d"
   250      address_space = ["10.0.0.0/16"]
   251      location = "West US"
   252      resource_group_name = "${azurerm_resource_group.test.name}"
   253  }
   254  
   255  resource "azurerm_subnet" "test" {
   256      name = "acctsub-%d"
   257      resource_group_name = "${azurerm_resource_group.test.name}"
   258      virtual_network_name = "${azurerm_virtual_network.test.name}"
   259      address_prefix = "10.0.2.0/24"
   260  }
   261  
   262  resource "azurerm_network_interface" "test" {
   263      name = "acctni-%d"
   264      location = "West US"
   265      resource_group_name = "${azurerm_resource_group.test.name}"
   266  
   267      ip_configuration {
   268      	name = "testconfiguration1"
   269      	subnet_id = "${azurerm_subnet.test.id}"
   270      	private_ip_address_allocation = "dynamic"
   271      }
   272  }
   273  
   274  resource "azurerm_storage_account" "test" {
   275      name = "accsa%d"
   276      resource_group_name = "${azurerm_resource_group.test.name}"
   277      location = "westus"
   278      account_type = "Standard_LRS"
   279  
   280      tags {
   281          environment = "staging"
   282      }
   283  }
   284  
   285  resource "azurerm_storage_container" "test" {
   286      name = "vhds"
   287      resource_group_name = "${azurerm_resource_group.test.name}"
   288      storage_account_name = "${azurerm_storage_account.test.name}"
   289      container_access_type = "private"
   290  }
   291  
   292  resource "azurerm_virtual_machine_scale_set" "test" {
   293    name = "acctvmss-%d"
   294    location = "West US"
   295    resource_group_name = "${azurerm_resource_group.test.name}"
   296    upgrade_policy_mode = "Manual"
   297  
   298    sku {
   299      name = "Standard_A0"
   300      tier = "Standard"
   301      capacity = 2
   302    }
   303  
   304    os_profile {
   305      computer_name_prefix = "vm-%s"
   306      admin_username = "myadmin"
   307      admin_password = "Passwword1234"
   308    }
   309  
   310    network_profile {
   311        name = "TestNetworkProfile-%d"
   312        primary = true
   313        ip_configuration {
   314          name = "TestIPConfiguration"
   315          subnet_id = "${azurerm_subnet.test.id}"
   316        }
   317    }
   318  
   319    storage_profile_os_disk {
   320       name = "osDiskProfile"
   321       caching       = "ReadWrite"
   322       create_option = "FromImage"
   323       vhd_containers = ["${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}"]
   324    }
   325  
   326    storage_profile_image_reference {
   327       publisher = "MicrosoftWindowsServer"
   328       offer = "WindowsServer"
   329       sku = "2012-R2-Datacenter"
   330       version = "4.0.20160518"
   331    }
   332  }
   333  `