provider/azurerm: fix tests removed in c141050929 (#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
```
This commit is contained in:
Peter McAtominey 2016-07-14 14:20:42 +01:00 committed by Paul Stack
parent 854c9bd488
commit 324e78020d
1 changed files with 79 additions and 5 deletions

View File

@ -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"