github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azure/resource_azure_data_disk_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/management"
     9  	"github.com/Azure/azure-sdk-for-go/management/virtualmachinedisk"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAzureDataDisk_basic(t *testing.T) {
    16  	var disk virtualmachinedisk.DataDiskResponse
    17  	name := fmt.Sprintf("terraform-test%d", acctest.RandInt())
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAzureDataDiskDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAzureDataDisk_basic(name),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAzureDataDiskExists(
    28  						"azure_data_disk.foo", &disk),
    29  					testAccCheckAzureDataDiskAttributes(&disk),
    30  					resource.TestCheckResourceAttr(
    31  						"azure_data_disk.foo", "label", fmt.Sprintf("%s-0", name)),
    32  					resource.TestCheckResourceAttr(
    33  						"azure_data_disk.foo", "size", "10"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccAzureDataDisk_update(t *testing.T) {
    41  	var disk virtualmachinedisk.DataDiskResponse
    42  	name := fmt.Sprintf("terraform-test%d", acctest.RandInt())
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckAzureDataDiskDestroy,
    48  		Steps: []resource.TestStep{
    49  			resource.TestStep{
    50  				Config: testAccAzureDataDisk_advanced(name),
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckAzureDataDiskExists(
    53  						"azure_data_disk.foo", &disk),
    54  					resource.TestCheckResourceAttr(
    55  						"azure_data_disk.foo", "label", fmt.Sprintf("%s-1", name)),
    56  					resource.TestCheckResourceAttr(
    57  						"azure_data_disk.foo", "lun", "1"),
    58  					resource.TestCheckResourceAttr(
    59  						"azure_data_disk.foo", "size", "10"),
    60  					resource.TestCheckResourceAttr(
    61  						"azure_data_disk.foo", "caching", "ReadOnly"),
    62  					resource.TestCheckResourceAttr(
    63  						"azure_data_disk.foo", "virtual_machine", name),
    64  				),
    65  			},
    66  
    67  			resource.TestStep{
    68  				Config: testAccAzureDataDisk_update(name),
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCheckAzureDataDiskExists(
    71  						"azure_data_disk.foo", &disk),
    72  					resource.TestCheckResourceAttr(
    73  						"azure_data_disk.foo", "label", fmt.Sprintf("%s-1", name)),
    74  					resource.TestCheckResourceAttr(
    75  						"azure_data_disk.foo", "lun", "2"),
    76  					resource.TestCheckResourceAttr(
    77  						"azure_data_disk.foo", "size", "20"),
    78  					resource.TestCheckResourceAttr(
    79  						"azure_data_disk.foo", "caching", "ReadWrite"),
    80  					resource.TestCheckResourceAttr(
    81  						"azure_data_disk.foo", "virtual_machine", "terraform-test2"),
    82  				),
    83  			},
    84  		},
    85  	})
    86  }
    87  
    88  func testAccCheckAzureDataDiskExists(
    89  	n string,
    90  	disk *virtualmachinedisk.DataDiskResponse) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  		rs, ok := s.RootModule().Resources[n]
    93  		if !ok {
    94  			return fmt.Errorf("Not found: %s", n)
    95  		}
    96  
    97  		if rs.Primary.ID == "" {
    98  			return fmt.Errorf("No Data Disk ID is set")
    99  		}
   100  
   101  		vm := rs.Primary.Attributes["virtual_machine"]
   102  		lun, err := strconv.Atoi(rs.Primary.Attributes["lun"])
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		vmDiskClient := testAccProvider.Meta().(*Client).vmDiskClient
   108  		d, err := vmDiskClient.GetDataDisk(vm, vm, vm, lun)
   109  		if err != nil {
   110  			return err
   111  		}
   112  
   113  		if d.DiskName != rs.Primary.ID {
   114  			return fmt.Errorf("Data Disk not found")
   115  		}
   116  
   117  		*disk = d
   118  
   119  		return nil
   120  	}
   121  }
   122  
   123  func testAccCheckAzureDataDiskAttributes(
   124  	disk *virtualmachinedisk.DataDiskResponse) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  
   127  		if disk.Lun != 0 {
   128  			return fmt.Errorf("Bad lun: %d", disk.Lun)
   129  		}
   130  
   131  		if disk.LogicalDiskSizeInGB != 10 {
   132  			return fmt.Errorf("Bad size: %d", disk.LogicalDiskSizeInGB)
   133  		}
   134  
   135  		if disk.HostCaching != "None" {
   136  			return fmt.Errorf("Bad caching: %s", disk.HostCaching)
   137  		}
   138  
   139  		return nil
   140  	}
   141  }
   142  
   143  func testAccCheckAzureDataDiskDestroy(s *terraform.State) error {
   144  	vmDiskClient := testAccProvider.Meta().(*Client).vmDiskClient
   145  
   146  	for _, rs := range s.RootModule().Resources {
   147  		if rs.Type != "azure_data_disk" {
   148  			continue
   149  		}
   150  
   151  		if rs.Primary.ID == "" {
   152  			return fmt.Errorf("No Disk ID is set")
   153  		}
   154  
   155  		vm := rs.Primary.Attributes["virtual_machine"]
   156  		lun, err := strconv.Atoi(rs.Primary.Attributes["lun"])
   157  		if err != nil {
   158  			return err
   159  		}
   160  
   161  		_, err = vmDiskClient.GetDataDisk(vm, vm, vm, lun)
   162  		if err == nil {
   163  			return fmt.Errorf("Data disk %s still exists", rs.Primary.ID)
   164  		}
   165  
   166  		if !management.IsResourceNotFoundError(err) {
   167  			return err
   168  		}
   169  	}
   170  
   171  	return nil
   172  }
   173  
   174  func testAccAzureDataDisk_basic(name string) string {
   175  	return fmt.Sprintf(`
   176  		resource "azure_instance" "foo" {
   177  				name = "%s"
   178  				image = "Ubuntu Server 14.04 LTS"
   179  				size = "Basic_A1"
   180  				storage_service_name = "%s"
   181  				location = "West US"
   182  				username = "terraform"
   183  				password = "Pass!admin123"
   184  		}
   185  
   186  		resource "azure_data_disk" "foo" {
   187  				lun = 0
   188  				size = 10
   189  				storage_service_name = "${azure_instance.foo.storage_service_name}"
   190  				virtual_machine = "${azure_instance.foo.id}"
   191  		}`, name, testAccStorageServiceName)
   192  }
   193  
   194  func testAccAzureDataDisk_advanced(name string) string {
   195  	return fmt.Sprintf(`
   196  		resource "azure_instance" "foo" {
   197  				name = "%s"
   198  				image = "Ubuntu Server 14.04 LTS"
   199  				size = "Basic_A1"
   200  				storage_service_name = "%s"
   201  				location = "West US"
   202  				username = "terraform"
   203  				password = "Pass!admin123"
   204  		}
   205  
   206  		resource "azure_data_disk" "foo" {
   207  				lun = 1
   208  				size = 10
   209  				caching = "ReadOnly"
   210  				storage_service_name = "${azure_instance.foo.storage_service_name}"
   211  				virtual_machine = "${azure_instance.foo.id}"
   212  		}`, name, testAccStorageServiceName)
   213  }
   214  
   215  func testAccAzureDataDisk_update(name string) string {
   216  	return fmt.Sprintf(`
   217  		resource "azure_instance" "foo" {
   218  				name = "%s"
   219  				image = "Ubuntu Server 14.04 LTS"
   220  				size = "Basic_A1"
   221  				storage_service_name = "%s"
   222  				location = "West US"
   223  				username = "terraform"
   224  				password = "Pass!admin123"
   225  		}
   226  
   227  		resource "azure_instance" "bar" {
   228  				name = "terraform-test2"
   229  				image = "Ubuntu Server 14.04 LTS"
   230  				size = "Basic_A1"
   231  				storage_service_name = "${azure_instance.foo.storage_service_name}"
   232  				location = "West US"
   233  				username = "terraform"
   234  				password = "Pass!admin123"
   235  		}
   236  
   237  		resource "azure_data_disk" "foo" {
   238  				lun = 2
   239  				size = 20
   240  				caching = "ReadWrite"
   241  				storage_service_name = "${azure_instance.bar.storage_service_name}"
   242  				virtual_machine = "${azure_instance.bar.id}"
   243  		}`, name, testAccStorageServiceName)
   244  }