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"