github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_deviceID(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: testAccCheckCloudStackDiskDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccCloudStackDisk_deviceID, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckCloudStackDiskExists( 44 "cloudstack_disk.foo", &disk), 45 testAccCheckCloudStackDiskAttributes(&disk), 46 resource.TestCheckResourceAttr( 47 "cloudstack_disk.foo", "device_id", "4"), 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: testAccCheckCloudStackDiskDestroy, 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 _, _, err := cs.Volume.GetVolumeByID(rs.Primary.ID) 155 if err == nil { 156 return fmt.Errorf("Disk %s still exists", rs.Primary.ID) 157 } 158 } 159 160 return nil 161 } 162 163 var testAccCloudStackDisk_basic = fmt.Sprintf(` 164 resource "cloudstack_disk" "foo" { 165 name = "terraform-disk" 166 attach = false 167 disk_offering = "%s" 168 zone = "%s" 169 }`, 170 CLOUDSTACK_DISK_OFFERING_1, 171 CLOUDSTACK_ZONE) 172 173 var testAccCloudStackDisk_deviceID = fmt.Sprintf(` 174 resource "cloudstack_instance" "foobar" { 175 name = "terraform-test" 176 display_name = "terraform" 177 service_offering= "%s" 178 network_id = "%s" 179 template = "%s" 180 zone = "%s" 181 expunge = true 182 } 183 184 resource "cloudstack_disk" "foo" { 185 name = "terraform-disk" 186 attach = true 187 device_id = 4 188 disk_offering = "%s" 189 virtual_machine_id = "${cloudstack_instance.foobar.id}" 190 zone = "${cloudstack_instance.foobar.zone}" 191 }`, 192 CLOUDSTACK_SERVICE_OFFERING_1, 193 CLOUDSTACK_NETWORK_1, 194 CLOUDSTACK_TEMPLATE, 195 CLOUDSTACK_ZONE, 196 CLOUDSTACK_DISK_OFFERING_1) 197 198 var testAccCloudStackDisk_update = fmt.Sprintf(` 199 resource "cloudstack_instance" "foobar" { 200 name = "terraform-test" 201 display_name = "terraform" 202 service_offering= "%s" 203 network_id = "%s" 204 template = "%s" 205 zone = "%s" 206 expunge = true 207 } 208 209 resource "cloudstack_disk" "foo" { 210 name = "terraform-disk" 211 attach = true 212 disk_offering = "%s" 213 virtual_machine_id = "${cloudstack_instance.foobar.id}" 214 zone = "${cloudstack_instance.foobar.zone}" 215 }`, 216 CLOUDSTACK_SERVICE_OFFERING_1, 217 CLOUDSTACK_NETWORK_1, 218 CLOUDSTACK_TEMPLATE, 219 CLOUDSTACK_ZONE, 220 CLOUDSTACK_DISK_OFFERING_1) 221 222 var testAccCloudStackDisk_resize = fmt.Sprintf(` 223 resource "cloudstack_instance" "foobar" { 224 name = "terraform-test" 225 display_name = "terraform" 226 service_offering= "%s" 227 network_id = "%s" 228 template = "%s" 229 zone = "%s" 230 expunge = true 231 } 232 233 resource "cloudstack_disk" "foo" { 234 name = "terraform-disk" 235 attach = true 236 disk_offering = "%s" 237 virtual_machine_id = "${cloudstack_instance.foobar.id}" 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_2)