@@ -92,6 +92,33 @@ func testAccCheckLibvirtVolumeIsBackingStore(name string, volume *libvirt.Storag
92
92
}
93
93
}
94
94
95
+ func testAccCheckLibvirtVolumeIsNotBackingStore (name string , volume * libvirt.StorageVol ) resource.TestCheckFunc {
96
+ return func (state * terraform.State ) error {
97
+ virConn := testAccProvider .Meta ().(* Client ).libvirt
98
+
99
+ vol , err := getVolumeFromTerraformState (name , state , * virConn )
100
+ if err != nil {
101
+ return err
102
+ }
103
+
104
+ volXMLDesc , err := vol .GetXMLDesc (0 )
105
+ if err != nil {
106
+ return fmt .Errorf ("Error retrieving libvirt volume XML description: %s" , err )
107
+ }
108
+
109
+ volumeDef := newDefVolume ()
110
+ err = xml .Unmarshal ([]byte (volXMLDesc ), & volumeDef )
111
+ if err != nil {
112
+ return fmt .Errorf ("Error reading libvirt volume XML description: %s" , err )
113
+ }
114
+ if volumeDef .BackingStore != nil {
115
+ return fmt .Errorf ("FAIL: the volume was not supposed to be a backingstore, but it is" )
116
+ }
117
+
118
+ return nil
119
+ }
120
+ }
121
+
95
122
func TestAccLibvirtVolume_Basic (t * testing.T ) {
96
123
var volume libvirt.StorageVol
97
124
randomVolumeResource := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
@@ -206,6 +233,90 @@ func TestAccLibvirtVolume_BackingStoreTestByName(t *testing.T) {
206
233
})
207
234
}
208
235
236
+ func TestAccLibvirtVolume_BackingStoreCopyDir (t * testing.T ) {
237
+ var volume libvirt.StorageVol
238
+ var volume2 libvirt.StorageVol
239
+ random := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
240
+ randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + random
241
+ // for qcow2 file images regardless of user specified size, the final size will
242
+ // be the same as the backing image's size. This is so because libvirt
243
+ // invokes qemu-img create followed by qemu-img convert for StorageVolCreateXMLFrom.
244
+ // This behavior can be tested like this:
245
+ // $ qemu-img create -f qcow2 test.qcow2 1024K
246
+ // $ qemu-img info test.qcow2
247
+ // $ qemu-img create -f qcow2 test-copy.qcow2 2048K
248
+ // $ qemu-img info test-copy.qcow2
249
+ // $ qemu-img convert -f qcow2 -O qcow2 test.qcow2 test-copy.qcow2
250
+ // $ qemu-img info test-copy.qcow2
251
+ resource .Test (t , resource.TestCase {
252
+ PreCheck : func () { testAccPreCheck (t ) },
253
+ Providers : testAccProviders ,
254
+ CheckDestroy : resource .ComposeAggregateTestCheckFunc (
255
+ testAccCheckLibvirtVolumeDestroy ,
256
+ testAccCheckLibvirtPoolDestroy ,
257
+ ),
258
+ Steps : []resource.TestStep {
259
+ {
260
+ Config : fmt .Sprintf (`
261
+ resource "libvirt_pool" "%s" {
262
+ name = "%s"
263
+ type = "dir"
264
+ path = "%s"
265
+ }
266
+ resource "libvirt_volume" "backing-%s" {
267
+ name = "backing-%s"
268
+ size = 1000448
269
+ pool = "${libvirt_pool.%s.name}"
270
+ }
271
+ resource "libvirt_volume" "%s" {
272
+ name = "%s"
273
+ base_volume_copy = true
274
+ base_volume_id = "${libvirt_volume.backing-%s.id}"
275
+ pool = "${libvirt_pool.%s.name}"
276
+ }
277
+ ` , random , random , randomPoolPath , random , random , random , random , random , random , random ),
278
+ Check : resource .ComposeTestCheckFunc (
279
+ testAccCheckLibvirtVolumeExists ("libvirt_volume.backing-" + random , & volume ),
280
+ testAccCheckLibvirtVolumeIsNotBackingStore ("libvirt_volume." + random , & volume2 ),
281
+ ),
282
+ },
283
+ },
284
+ })
285
+ }
286
+
287
+ func TestAccLibvirtVolume_NoBackingStoreCopyError (t * testing.T ) {
288
+ random := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
289
+ randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + random
290
+ expectedError := regexp .MustCompile (fmt .Sprintf (
291
+ "Can't find base volume to copy to '%s'" , random ,
292
+ ))
293
+ resource .Test (t , resource.TestCase {
294
+ PreCheck : func () { testAccPreCheck (t ) },
295
+ Providers : testAccProviders ,
296
+ CheckDestroy : resource .ComposeAggregateTestCheckFunc (
297
+ testAccCheckLibvirtVolumeDestroy ,
298
+ testAccCheckLibvirtPoolDestroy ,
299
+ ),
300
+ Steps : []resource.TestStep {
301
+ {
302
+ Config : fmt .Sprintf (`
303
+ resource "libvirt_pool" "%s" {
304
+ name = "%s"
305
+ type = "dir"
306
+ path = "%s"
307
+ }
308
+ resource "libvirt_volume" "%s" {
309
+ name = "%s"
310
+ base_volume_copy = true
311
+ pool = "${libvirt_pool.%s.name}"
312
+ }
313
+ ` , random , random , randomPoolPath , random , random , random ),
314
+ ExpectError : expectedError ,
315
+ },
316
+ },
317
+ })
318
+ }
319
+
209
320
// The destroy function should always handle the case where the resource might already be destroyed
210
321
// (manually, for example). If the resource is already destroyed, this should not return an error.
211
322
// This allows Terraform users to manually delete resources without breaking Terraform.
0 commit comments