github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/cloudstack/resource_cloudstack_disk_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func TestAccCloudStackDisk_basic(t *testing.T) {
    13  	var disk cloudstack.Volume
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackDiskDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackDisk_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackDiskExists(
    24  						"cloudstack_disk.foo", &disk),
    25  					testAccCheckCloudStackDiskAttributes(&disk),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccCloudStackDisk_device(t *testing.T) {
    33  	var disk cloudstack.Volume
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckCloudStackDiskDestroyAdvanced,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccCloudStackDisk_device,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckCloudStackDiskExists(
    44  						"cloudstack_disk.foo", &disk),
    45  					testAccCheckCloudStackDiskAttributes(&disk),
    46  					resource.TestCheckResourceAttr(
    47  						"cloudstack_disk.foo", "device", "/dev/xvde"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func TestAccCloudStackDisk_update(t *testing.T) {
    55  	var disk cloudstack.Volume
    56  
    57  	resource.Test(t, resource.TestCase{
    58  		PreCheck:     func() { testAccPreCheck(t) },
    59  		Providers:    testAccProviders,
    60  		CheckDestroy: testAccCheckCloudStackDiskDestroyAdvanced,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccCloudStackDisk_update,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckCloudStackDiskExists(
    66  						"cloudstack_disk.foo", &disk),
    67  					testAccCheckCloudStackDiskAttributes(&disk),
    68  				),
    69  			},
    70  
    71  			resource.TestStep{
    72  				Config: testAccCloudStackDisk_resize,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckCloudStackDiskExists(
    75  						"cloudstack_disk.foo", &disk),
    76  					testAccCheckCloudStackDiskResized(&disk),
    77  					resource.TestCheckResourceAttr(
    78  						"cloudstack_disk.foo", "disk_offering", CLOUDSTACK_DISK_OFFERING_2),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckCloudStackDiskExists(
    86  	n string, disk *cloudstack.Volume) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		rs, ok := s.RootModule().Resources[n]
    89  		if !ok {
    90  			return fmt.Errorf("Not found: %s", n)
    91  		}
    92  
    93  		if rs.Primary.ID == "" {
    94  			return fmt.Errorf("No disk ID is set")
    95  		}
    96  
    97  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    98  		volume, _, err := cs.Volume.GetVolumeByID(rs.Primary.ID)
    99  
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		if volume.Id != rs.Primary.ID {
   105  			return fmt.Errorf("Disk not found")
   106  		}
   107  
   108  		*disk = *volume
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  func testAccCheckCloudStackDiskAttributes(
   115  	disk *cloudstack.Volume) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  
   118  		if disk.Name != "terraform-disk" {
   119  			return fmt.Errorf("Bad name: %s", disk.Name)
   120  		}
   121  
   122  		if disk.Diskofferingname != CLOUDSTACK_DISK_OFFERING_1 {
   123  			return fmt.Errorf("Bad disk offering: %s", disk.Diskofferingname)
   124  		}
   125  
   126  		return nil
   127  	}
   128  }
   129  
   130  func testAccCheckCloudStackDiskResized(
   131  	disk *cloudstack.Volume) resource.TestCheckFunc {
   132  	return func(s *terraform.State) error {
   133  
   134  		if disk.Diskofferingname != CLOUDSTACK_DISK_OFFERING_2 {
   135  			return fmt.Errorf("Bad disk offering: %s", disk.Diskofferingname)
   136  		}
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  func testAccCheckCloudStackDiskDestroy(s *terraform.State) error {
   143  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   144  
   145  	for _, rs := range s.RootModule().Resources {
   146  		if rs.Type != "cloudstack_disk" {
   147  			continue
   148  		}
   149  
   150  		if rs.Primary.ID == "" {
   151  			return fmt.Errorf("No disk ID is set")
   152  		}
   153  
   154  		p := cs.Volume.NewDeleteVolumeParams(rs.Primary.ID)
   155  		err, _ := cs.Volume.DeleteVolume(p)
   156  
   157  		if err != nil {
   158  			return fmt.Errorf(
   159  				"Error deleting disk (%s): %s",
   160  				rs.Primary.ID, err)
   161  		}
   162  	}
   163  
   164  	return nil
   165  }
   166  
   167  func testAccCheckCloudStackDiskDestroyAdvanced(s *terraform.State) error {
   168  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   169  
   170  	for _, rs := range s.RootModule().Resources {
   171  		if rs.Type != "cloudstack_disk" {
   172  			continue
   173  		}
   174  
   175  		if rs.Primary.ID == "" {
   176  			return fmt.Errorf("No disk ID is set")
   177  		}
   178  
   179  		p := cs.Volume.NewDeleteVolumeParams(rs.Primary.ID)
   180  		err, _ := cs.Volume.DeleteVolume(p)
   181  
   182  		if err != nil {
   183  			return fmt.Errorf(
   184  				"Error deleting disk (%s): %s",
   185  				rs.Primary.ID, err)
   186  		}
   187  	}
   188  
   189  	for _, rs := range s.RootModule().Resources {
   190  		if rs.Type != "cloudstack_instance" {
   191  			continue
   192  		}
   193  
   194  		if rs.Primary.ID == "" {
   195  			return fmt.Errorf("No instance ID is set")
   196  		}
   197  
   198  		p := cs.VirtualMachine.NewDestroyVirtualMachineParams(rs.Primary.ID)
   199  		err, _ := cs.VirtualMachine.DestroyVirtualMachine(p)
   200  
   201  		if err != nil {
   202  			return fmt.Errorf(
   203  				"Error deleting instance (%s): %s",
   204  				rs.Primary.ID, err)
   205  		}
   206  	}
   207  
   208  	return nil
   209  }
   210  
   211  var testAccCloudStackDisk_basic = fmt.Sprintf(`
   212  resource "cloudstack_disk" "foo" {
   213    name = "terraform-disk"
   214    attach = false
   215    disk_offering = "%s"
   216    zone = "%s"
   217  }`,
   218  	CLOUDSTACK_DISK_OFFERING_1,
   219  	CLOUDSTACK_ZONE)
   220  
   221  var testAccCloudStackDisk_device = fmt.Sprintf(`
   222  resource "cloudstack_instance" "foobar" {
   223    name = "terraform-test"
   224    display_name = "terraform"
   225    service_offering= "%s"
   226    network = "%s"
   227    template = "%s"
   228    zone = "%s"
   229    expunge = true
   230  }
   231  
   232  resource "cloudstack_disk" "foo" {
   233    name = "terraform-disk"
   234    attach = true
   235    device = "/dev/xvde"
   236    disk_offering = "%s"
   237    virtual_machine = "${cloudstack_instance.foobar.name}"
   238    zone = "${cloudstack_instance.foobar.zone}"
   239  }`,
   240  	CLOUDSTACK_SERVICE_OFFERING_1,
   241  	CLOUDSTACK_NETWORK_1,
   242  	CLOUDSTACK_TEMPLATE,
   243  	CLOUDSTACK_ZONE,
   244  	CLOUDSTACK_DISK_OFFERING_1)
   245  
   246  var testAccCloudStackDisk_update = fmt.Sprintf(`
   247  resource "cloudstack_instance" "foobar" {
   248    name = "terraform-test"
   249    display_name = "terraform"
   250    service_offering= "%s"
   251    network = "%s"
   252    template = "%s"
   253    zone = "%s"
   254    expunge = true
   255  }
   256  
   257  resource "cloudstack_disk" "foo" {
   258    name = "terraform-disk"
   259    attach = true
   260    disk_offering = "%s"
   261    virtual_machine = "${cloudstack_instance.foobar.name}"
   262    zone = "${cloudstack_instance.foobar.zone}"
   263  }`,
   264  	CLOUDSTACK_SERVICE_OFFERING_1,
   265  	CLOUDSTACK_NETWORK_1,
   266  	CLOUDSTACK_TEMPLATE,
   267  	CLOUDSTACK_ZONE,
   268  	CLOUDSTACK_DISK_OFFERING_1)
   269  
   270  var testAccCloudStackDisk_resize = fmt.Sprintf(`
   271  resource "cloudstack_instance" "foobar" {
   272    name = "terraform-test"
   273    display_name = "terraform"
   274    service_offering= "%s"
   275    network = "%s"
   276    template = "%s"
   277    zone = "%s"
   278    expunge = true
   279  }
   280  
   281  resource "cloudstack_disk" "foo" {
   282    name = "terraform-disk"
   283    attach = true
   284    disk_offering = "%s"
   285    virtual_machine = "${cloudstack_instance.foobar.name}"
   286    zone = "${cloudstack_instance.foobar.zone}"
   287  }`,
   288  	CLOUDSTACK_SERVICE_OFFERING_1,
   289  	CLOUDSTACK_NETWORK_1,
   290  	CLOUDSTACK_TEMPLATE,
   291  	CLOUDSTACK_ZONE,
   292  	CLOUDSTACK_DISK_OFFERING_2)