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