From 324e78020d9ea9beb54a97b5ad1e2ce519bc4148 Mon Sep 17 00:00:00 2001 From: Peter McAtominey Date: Thu, 14 Jul 2016 14:20:42 +0100 Subject: [PATCH] provider/azurerm: fix tests removed in c1410509290507d77dab7b07932062e6f995802e (#7640) The tests were removed due to a nil pointer panic in testCheckAzureRMVirtualMachineOSDiskVHDExistance when the storage account itself had been deleted in the destroy stage Added a test to cover opting out of VHD delete rather than polluting the basic linux VM test. ``` TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMVirtualMachine_deleteVHDOpt -timeout 120m === RUN TestAccAzureRMVirtualMachine_deleteVHDOptOut --- PASS: TestAccAzureRMVirtualMachine_deleteVHDOptOut (731.54s) === RUN TestAccAzureRMVirtualMachine_deleteVHDOptIn --- PASS: TestAccAzureRMVirtualMachine_deleteVHDOptIn (590.87s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 1322.529s ``` ``` TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMVirtualMachine_basicLinuxMachine -timeout 120m === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine ^[--- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine (587.63s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 587.738s ``` --- .../resource_arm_virtual_machine_test.go | 84 +++++++++++++++++-- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/builtin/providers/azurerm/resource_arm_virtual_machine_test.go b/builtin/providers/azurerm/resource_arm_virtual_machine_test.go index a37fba2d4..789e6b059 100644 --- a/builtin/providers/azurerm/resource_arm_virtual_machine_test.go +++ b/builtin/providers/azurerm/resource_arm_virtual_machine_test.go @@ -14,11 +14,9 @@ func TestAccAzureRMVirtualMachine_basicLinuxMachine(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: resource.ComposeTestCheckFunc( - testCheckAzureRMVirtualMachineDestroy, - ), + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineDestroy, Steps: []resource.TestStep{ { Config: config, @@ -169,6 +167,52 @@ func TestAccAzureRMVirtualMachine_winRMConfig(t *testing.T) { }) } +func TestAccAzureRMVirtualMachine_deleteVHDOptOut(t *testing.T) { + ri := acctest.RandInt() + preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri) + postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), + ), + }, + { + Config: postConfig, + Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(true), + }, + }, + }) +} + +func TestAccAzureRMVirtualMachine_deleteVHDOptIn(t *testing.T) { + ri := acctest.RandInt() + preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDestroyOSDisk, ri, ri, ri, ri, ri, ri, ri) + postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineDeleteVM, ri, ri, ri, ri, ri) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"), + ), + }, + { + Config: postConfig, + Check: testCheckAzureRMVirtualMachineOSDiskVHDExistance(false), + }, + }, + }) +} + func testCheckAzureRMVirtualMachineExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API @@ -223,6 +267,36 @@ func testCheckAzureRMVirtualMachineDestroy(s *terraform.State) error { return nil } +func testCheckAzureRMVirtualMachineOSDiskVHDExistance(shouldExist bool) resource.TestCheckFunc { + return func(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_storage_container" { + continue + } + + // fetch storage account and container name + resourceGroup := rs.Primary.Attributes["resource_group_name"] + storageAccountName := rs.Primary.Attributes["storage_account_name"] + containerName := rs.Primary.Attributes["name"] + storageClient, _, err := testAccProvider.Meta().(*ArmClient).getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + return fmt.Errorf("Error creating Blob storage client: %s", err) + } + + exists, err := storageClient.BlobExists(containerName, "myosdisk1.vhd") + if err != nil { + return fmt.Errorf("Error checking if OS Disk VHD Blob exists: %s", err) + } + + if exists && !shouldExist { + return fmt.Errorf("OS Disk VHD Blob still exists") + } + } + + return nil + } +} + var testAccAzureRMVirtualMachine_basicLinuxMachine = ` resource "azurerm_resource_group" "test" { name = "acctestrg-%d"