github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testCheckAzureRMVirtualMachineScaleSetExists(name string) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		// Ensure we have enough information in state to look up in API
    54  		rs, ok := s.RootModule().Resources[name]
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", name)
    57  		}
    58  
    59  		name := rs.Primary.Attributes["name"]
    60  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    61  		if !hasResourceGroup {
    62  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: scale set %s", name)
    63  		}
    64  
    65  		conn := testAccProvider.Meta().(*ArmClient).vmScaleSetClient
    66  
    67  		resp, err := conn.Get(resourceGroup, name)
    68  		if err != nil {
    69  			return fmt.Errorf("Bad: Get on vmScaleSetClient: %s", err)
    70  		}
    71  
    72  		if resp.StatusCode == http.StatusNotFound {
    73  			return fmt.Errorf("Bad: VirtualMachineScaleSet %q (resource group: %q) does not exist", name, resourceGroup)
    74  		}
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testCheckAzureRMVirtualMachineScaleSetDisappears(name string) resource.TestCheckFunc {
    81  	return func(s *terraform.State) error {
    82  		// Ensure we have enough information in state to look up in API
    83  		rs, ok := s.RootModule().Resources[name]
    84  		if !ok {
    85  			return fmt.Errorf("Not found: %s", name)
    86  		}
    87  
    88  		name := rs.Primary.Attributes["name"]
    89  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    90  		if !hasResourceGroup {
    91  			return fmt.Errorf("Bad: no resource group found in state for virtual machine: scale set %s", name)
    92  		}
    93  
    94  		conn := testAccProvider.Meta().(*ArmClient).vmScaleSetClient
    95  
    96  		_, err := conn.Delete(resourceGroup, name, make(chan struct{}))
    97  		if err != nil {
    98  			return fmt.Errorf("Bad: Delete on vmScaleSetClient: %s", err)
    99  		}
   100  
   101  		return nil
   102  	}
   103  }
   104  
   105  func testCheckAzureRMVirtualMachineScaleSetDestroy(s *terraform.State) error {
   106  	conn := testAccProvider.Meta().(*ArmClient).vmScaleSetClient
   107  
   108  	for _, rs := range s.RootModule().Resources {
   109  		if rs.Type != "azurerm_virtual_machine_scale_set" {
   110  			continue
   111  		}
   112  
   113  		name := rs.Primary.Attributes["name"]
   114  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   115  
   116  		resp, err := conn.Get(resourceGroup, name)
   117  
   118  		if err != nil {
   119  			return nil
   120  		}
   121  
   122  		if resp.StatusCode != http.StatusNotFound {
   123  			return fmt.Errorf("Virtual Machine Scale Set still exists:\n%#v", resp.VirtualMachineScaleSetProperties)
   124  		}
   125  	}
   126  
   127  	return nil
   128  }
   129  
   130  var testAccAzureRMVirtualMachineScaleSet_basicLinux = `
   131  resource "azurerm_resource_group" "test" {
   132      name = "acctestRG-%d"
   133      location = "West US"
   134  }
   135  
   136  resource "azurerm_virtual_network" "test" {
   137      name = "acctvn-%d"
   138      address_space = ["10.0.0.0/16"]
   139      location = "West US"
   140      resource_group_name = "${azurerm_resource_group.test.name}"
   141  }
   142  
   143  resource "azurerm_subnet" "test" {
   144      name = "acctsub-%d"
   145      resource_group_name = "${azurerm_resource_group.test.name}"
   146      virtual_network_name = "${azurerm_virtual_network.test.name}"
   147      address_prefix = "10.0.2.0/24"
   148  }
   149  
   150  resource "azurerm_network_interface" "test" {
   151      name = "acctni-%d"
   152      location = "West US"
   153      resource_group_name = "${azurerm_resource_group.test.name}"
   154  
   155      ip_configuration {
   156      	name = "testconfiguration1"
   157      	subnet_id = "${azurerm_subnet.test.id}"
   158      	private_ip_address_allocation = "dynamic"
   159      }
   160  }
   161  
   162  resource "azurerm_storage_account" "test" {
   163      name = "accsa%d"
   164      resource_group_name = "${azurerm_resource_group.test.name}"
   165      location = "westus"
   166      account_type = "Standard_LRS"
   167  
   168      tags {
   169          environment = "staging"
   170      }
   171  }
   172  
   173  resource "azurerm_storage_container" "test" {
   174      name = "vhds"
   175      resource_group_name = "${azurerm_resource_group.test.name}"
   176      storage_account_name = "${azurerm_storage_account.test.name}"
   177      container_access_type = "private"
   178  }
   179  
   180  resource "azurerm_virtual_machine_scale_set" "test" {
   181    name = "acctvmss-%d"
   182    location = "West US"
   183    resource_group_name = "${azurerm_resource_group.test.name}"
   184    upgrade_policy_mode = "Manual"
   185  
   186    sku {
   187      name = "Standard_A0"
   188      tier = "Standard"
   189      capacity = 2
   190    }
   191  
   192    os_profile {
   193      computer_name_prefix = "testvm-%d"
   194      admin_username = "myadmin"
   195      admin_password = "Passwword1234"
   196    }
   197  
   198    network_profile {
   199        name = "TestNetworkProfile-%d"
   200        primary = true
   201        ip_configuration {
   202          name = "TestIPConfiguration"
   203          subnet_id = "${azurerm_subnet.test.id}"
   204        }
   205    }
   206  
   207    storage_profile_os_disk {
   208      name = "osDiskProfile"
   209      caching       = "ReadWrite"
   210      create_option = "FromImage"
   211      vhd_containers = ["${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}"]
   212    }
   213  
   214    storage_profile_image_reference {
   215      publisher = "Canonical"
   216      offer     = "UbuntuServer"
   217      sku       = "14.04.2-LTS"
   218      version   = "latest"
   219    }
   220  }
   221  `