managed disk acceptance tests

This commit is contained in:
Brandon Tosch 2017-03-03 18:15:05 -08:00
parent ca6bc8823c
commit 9158b95736
3 changed files with 172 additions and 22 deletions

View File

@ -9,28 +9,19 @@ import (
)
func TestAccAzureRMManagedDisk_importEmpty(t *testing.T) {
runTestAzureRMManagedDisk_import(t, "azurerm_disk.test", testAccAzureRMManagedDisk_empty)
}
/*func TestAccAzureRMManagedDisk_importBlob(t *testing.T) {
runTestAzureRMManagedDisk_import(t, "azurerm_disk.test", testAccAzureRMManagedDisk_blob)
}*/
func runTestAzureRMManagedDisk_import(t *testing.T, resourceName string, configSource string) {
ri := acctest.RandInt()
config := fmt.Sprintf(configSource, ri, ri)
config := fmt.Sprintf(testAccAzureRMManagedDisk_empty, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
},
resource.TestStep{
ResourceName: resourceName,
{
ResourceName: "azurerm_managed_disk.test",
ImportState: true,
ImportStateVerify: true,
},

View File

@ -50,10 +50,11 @@ func resourceArmManagedDisk() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{
string(disk.Import),
string(disk.Empty),
//todo: add support for snapshots as a source (disk.Copy)
}, true),
},
"vhd_uri": {
"source_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
@ -70,7 +71,7 @@ func resourceArmManagedDisk() *schema.Resource {
"disk_size_gb": {
Type: schema.TypeInt,
Required: true,
Optional: true,
ValidateFunc: validateDiskSizeGB,
},
@ -114,7 +115,7 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro
OsType: disk.OperatingSystemTypes(osType),
}
if v := d.Get("disk_size_gb"); v != nil {
if v := d.Get("disk_size_gb"); v != 0 {
diskSize := int32(v.(int))
createDisk.Properties.DiskSizeGB = &diskSize
}
@ -125,10 +126,10 @@ func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) erro
}
if strings.EqualFold(createOption, string(disk.Import)) {
if vhdUri := d.Get("vhd_uri").(string); vhdUri != "" {
creationData.SourceURI = &vhdUri
if sourceUri := d.Get("source_uri").(string); sourceUri != "" {
creationData.SourceURI = &sourceUri
} else {
return fmt.Errorf("[ERROR] vhd_uri must be specified when create_option is `%s`", disk.Import)
return fmt.Errorf("[ERROR] source_uri must be specified when create_option is `%s` or `%s`", disk.Import, disk.Copy)
}
}
@ -193,8 +194,8 @@ func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("[DEBUG] Error setting managed disk creation data: %#v", err)
} else {
d.Set("create_option", m["create_option"])
if m["vhd_uri"] != nil {
d.Set("vhd_uri", m["vhd_uri"])
if m["source_uri"] != nil {
d.Set("source_uri", m["source_uri"])
}
}
}
@ -236,7 +237,7 @@ func flattenAzureRmManagedDiskCreationData(creationData *disk.CreationData) (map
result := make(map[string]interface{})
result["create_option"] = string(creationData.CreateOption)
if creationData.SourceURI != nil {
result["vhd_uri"] = *creationData.SourceURI
result["source_uri"] = *creationData.SourceURI
}
return result, nil

View File

@ -5,6 +5,7 @@ import (
"net/http"
"testing"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/disk"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
@ -30,6 +31,82 @@ func TestAccAzureRMManagedDisk_empty(t *testing.T) {
})
}
func TestAccAzureRMManagedDisk_import(t *testing.T) {
var d disk.Model
var vm compute.VirtualMachine
ri := acctest.RandInt()
vmConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri)
config := fmt.Sprintf(testAccAzureRMManagedDisk_import, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
{
//need to create a vm and then delete it so we can use the vhd to test import
Config: vmConfig,
Destroy: false,
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
testDeleteAzureRMVirtualMachine("azurerm_virtual_machine.test"),
),
},
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d),
),
},
},
})
}
func TestAccAzureRMManagedDisk_update(t *testing.T) {
var d disk.Model
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMManagedDisk_empty, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMManagedDisk_empty_updated, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMManagedDiskDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.%", "2"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.environment", "acctest"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.cost-center", "ops"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "disk_size_gb", "20"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "storage_account_type", string(disk.StandardLRS)),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMManagedDiskExists("azurerm_managed_disk.test", &d),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.%", "1"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "tags.environment", "acctest"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "disk_size_gb", "30"),
resource.TestCheckResourceAttr(
"azurerm_managed_disk.test", "storage_account_type", string(disk.PremiumLRS)),
),
},
},
})
}
func testCheckAzureRMManagedDiskExists(name string, d *disk.Model) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
@ -85,6 +162,30 @@ func testCheckAzureRMManagedDiskDestroy(s *terraform.State) error {
return nil
}
func testDeleteAzureRMVirtualMachine(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
vmName := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for virtual machine: %s", vmName)
}
conn := testAccProvider.Meta().(*ArmClient).vmClient
_, err := conn.Delete(resourceGroup, vmName, make(chan struct{}))
if err != nil {
return fmt.Errorf("Bad: Delete on vmClient: %s", err)
}
return nil
}
}
var testAccAzureRMManagedDisk_empty = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
@ -99,6 +200,63 @@ resource "azurerm_managed_disk" "test" {
create_option = "Empty"
disk_size_gb = "20"
tags {
environment = "acctest"
cost-center = "ops"
}
}`
var testAccAzureRMManagedDisk_empty_updated = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
resource "azurerm_managed_disk" "test" {
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Premium_LRS"
create_option = "Empty"
disk_size_gb = "30"
tags {
environment = "acctest"
}
}`
var testAccAzureRMManagedDisk_import = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
resource "azurerm_storage_account" "test" {
name = "accsa%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "West US 2"
account_type = "Standard_LRS"
tags {
environment = "staging"
}
}
resource "azurerm_storage_container" "test" {
name = "vhds"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.test.name}"
container_access_type = "private"
}
resource "azurerm_managed_disk" "test" {
name = "acctestd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Standard_LRS"
create_option = "Import"
source_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
tags {
environment = "acctest"
}