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