7
7
"regexp"
8
8
"testing"
9
9
10
+ "github.com/hashicorp/terraform/helper/acctest"
10
11
"github.com/hashicorp/terraform/helper/resource"
11
12
"github.com/hashicorp/terraform/terraform"
12
13
libvirt "github.com/libvirt/libvirt-go"
@@ -126,26 +127,25 @@ func testAccCheckLibvirtVolumeIsBackingStore(name string, volume *libvirt.Storag
126
127
127
128
func TestAccLibvirtVolume_Basic (t * testing.T ) {
128
129
var volume libvirt.StorageVol
129
-
130
- const testAccCheckLibvirtVolumeConfigBasic = `
131
- resource "libvirt_volume" "terraform-acceptance-test-1" {
132
- name = "terraform-test"
133
- size = 1073741824
134
- }`
130
+ randomVolumeResource := acctest .RandString (10 )
135
131
136
132
resource .Test (t , resource.TestCase {
137
133
PreCheck : func () { testAccPreCheck (t ) },
138
134
Providers : testAccProviders ,
139
135
CheckDestroy : testAccCheckLibvirtVolumeDestroy ,
140
136
Steps : []resource.TestStep {
141
137
{
142
- Config : testAccCheckLibvirtVolumeConfigBasic ,
138
+ Config : fmt .Sprintf (`
139
+ resource "libvirt_volume" "%s" {
140
+ name = "terraform-test"
141
+ size = 1073741824
142
+ }` , randomVolumeResource ),
143
143
Check : resource .ComposeTestCheckFunc (
144
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-1" , & volume ),
144
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeResource , & volume ),
145
145
resource .TestCheckResourceAttr (
146
- "libvirt_volume.terraform-acceptance-test-1" , "name" , "terraform-test" ),
146
+ "libvirt_volume." + randomVolumeResource , "name" , "terraform-test" ),
147
147
resource .TestCheckResourceAttr (
148
- "libvirt_volume.terraform-acceptance-test-1" , "size" , "1073741824" ),
148
+ "libvirt_volume." + randomVolumeResource , "size" , "1073741824" ),
149
149
),
150
150
},
151
151
},
@@ -155,26 +155,25 @@ func TestAccLibvirtVolume_Basic(t *testing.T) {
155
155
func TestAccLibvirtVolume_BackingStoreTestByID (t * testing.T ) {
156
156
var volume libvirt.StorageVol
157
157
var volume2 libvirt.StorageVol
158
- const testAccCheckLibvirtVolumeConfigBasic = `
159
- resource "libvirt_volume" "terraform-acceptance-test-3" {
160
- name = "terraform-test3"
161
- size = 1073741824
162
- }
163
- resource "libvirt_volume" "backing-store" {
164
- name = "backing-store"
165
- base_volume_id = "${libvirt_volume.terraform-acceptance-test-3.id}"
166
- }
167
- `
168
-
158
+ randomVolumeResource := acctest .RandString (10 )
169
159
resource .Test (t , resource.TestCase {
170
160
PreCheck : func () { testAccPreCheck (t ) },
171
161
Providers : testAccProviders ,
172
162
CheckDestroy : testAccCheckLibvirtVolumeDestroy ,
173
163
Steps : []resource.TestStep {
174
164
{
175
- Config : testAccCheckLibvirtVolumeConfigBasic ,
165
+ Config : fmt .Sprintf (`
166
+ resource "libvirt_volume" "%s" {
167
+ name = "terraform-test3"
168
+ size = 1073741824
169
+ }
170
+ resource "libvirt_volume" "backing-store" {
171
+ name = "backing-store"
172
+ base_volume_id = "${libvirt_volume.%s.id}"
173
+ }
174
+ ` , randomVolumeResource , randomVolumeResource ),
176
175
Check : resource .ComposeTestCheckFunc (
177
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-3" , & volume ),
176
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeResource , & volume ),
178
177
testAccCheckLibvirtVolumeIsBackingStore ("libvirt_volume.backing-store" , & volume2 ),
179
178
),
180
179
},
@@ -185,26 +184,24 @@ func TestAccLibvirtVolume_BackingStoreTestByID(t *testing.T) {
185
184
func TestAccLibvirtVolume_BackingStoreTestByName (t * testing.T ) {
186
185
var volume libvirt.StorageVol
187
186
var volume2 libvirt.StorageVol
188
- const testAccCheckLibvirtVolumeConfigBasic = `
189
- resource "libvirt_volume" "terraform-acceptance-test-3" {
190
- name = "terraform-test3"
191
- size = 1073741824
192
- }
193
- resource "libvirt_volume" "backing-store" {
194
- name = "backing-store"
195
- base_volume_name = "${libvirt_volume.terraform-acceptance-test-3.name}"
196
- }
197
- `
198
-
187
+ randomVolumeResource := acctest .RandString (10 )
199
188
resource .Test (t , resource.TestCase {
200
189
PreCheck : func () { testAccPreCheck (t ) },
201
190
Providers : testAccProviders ,
202
191
CheckDestroy : testAccCheckLibvirtVolumeDestroy ,
203
192
Steps : []resource.TestStep {
204
193
{
205
- Config : testAccCheckLibvirtVolumeConfigBasic ,
194
+ Config : fmt .Sprintf (`
195
+ resource "libvirt_volume" "%s" {
196
+ name = "terraform-test3"
197
+ size = 1073741824
198
+ }
199
+ resource "libvirt_volume" "backing-store" {
200
+ name = "backing-store"
201
+ base_volume_name = "${libvirt_volume.%s.name}"
202
+ } ` , randomVolumeResource , randomVolumeResource ),
206
203
Check : resource .ComposeTestCheckFunc (
207
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-3" , & volume ),
204
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeResource , & volume ),
208
205
testAccCheckLibvirtVolumeIsBackingStore ("libvirt_volume.backing-store" , & volume2 ),
209
206
),
210
207
},
@@ -218,12 +215,12 @@ func TestAccLibvirtVolume_BackingStoreTestByName(t *testing.T) {
218
215
// This test should fail without a proper "Exists" implementation
219
216
func TestAccLibvirtVolume_ManuallyDestroyed (t * testing.T ) {
220
217
var volume libvirt.StorageVol
221
-
222
- const testAccCheckLibvirtVolumeConfigBasic = `
223
- resource "libvirt_volume" "terraform-acceptance-test-1 " {
218
+ randomVolumeResource := acctest . RandString ( 10 )
219
+ testAccCheckLibvirtVolumeConfigBasic := fmt . Sprintf ( `
220
+ resource "libvirt_volume" "%s " {
224
221
name = "terraform-test"
225
222
size = 1073741824
226
- }`
223
+ }` , randomVolumeResource )
227
224
228
225
resource .Test (t , resource.TestCase {
229
226
PreCheck : func () { testAccPreCheck (t ) },
@@ -233,7 +230,7 @@ func TestAccLibvirtVolume_ManuallyDestroyed(t *testing.T) {
233
230
{
234
231
Config : testAccCheckLibvirtVolumeConfigBasic ,
235
232
Check : resource .ComposeTestCheckFunc (
236
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-1" , & volume ),
233
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeResource , & volume ),
237
234
),
238
235
},
239
236
{
@@ -253,17 +250,19 @@ func TestAccLibvirtVolume_ManuallyDestroyed(t *testing.T) {
253
250
}
254
251
255
252
func TestAccLibvirtVolume_UniqueName (t * testing.T ) {
256
- const config = `
257
- resource "libvirt_volume" "terraform-acceptance-test-1" {
253
+ randomVolumeName := acctest .RandString (10 )
254
+ randomVolumeName2 := acctest .RandString (10 )
255
+ config := fmt .Sprintf (`
256
+ resource "libvirt_volume" "%s" {
258
257
name = "terraform-test"
259
258
size = 1073741824
260
259
}
261
260
262
- resource "libvirt_volume" "terraform-acceptance-test-2 " {
261
+ resource "libvirt_volume" "%s " {
263
262
name = "terraform-test"
264
263
size = 1073741824
265
264
}
266
- `
265
+ ` , randomVolumeName , randomVolumeName2 )
267
266
268
267
resource .Test (t , resource.TestCase {
269
268
PreCheck : func () { testAccPreCheck (t ) },
@@ -280,6 +279,7 @@ func TestAccLibvirtVolume_UniqueName(t *testing.T) {
280
279
281
280
func TestAccLibvirtVolume_DownloadFromSource (t * testing.T ) {
282
281
var volume libvirt.StorageVol
282
+ randomVolumeName := acctest .RandString (10 )
283
283
284
284
fws := fileWebServer {}
285
285
if err := fws .Start (); err != nil {
@@ -293,13 +293,11 @@ func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) {
293
293
t .Fatal (err )
294
294
}
295
295
296
- const testAccCheckLibvirtVolumeConfigSource = `
297
- resource "libvirt_volume" "terraform-acceptance-test-2 " {
296
+ config := fmt . Sprintf ( `
297
+ resource "libvirt_volume" "%s " {
298
298
name = "terraform-test"
299
299
source = "%s"
300
- }`
301
-
302
- config := fmt .Sprintf (testAccCheckLibvirtVolumeConfigSource , url )
300
+ }` , randomVolumeName , url )
303
301
304
302
resource .Test (t , resource.TestCase {
305
303
PreCheck : func () { testAccPreCheck (t ) },
@@ -309,9 +307,9 @@ func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) {
309
307
{
310
308
Config : config ,
311
309
Check : resource .ComposeTestCheckFunc (
312
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-2" , & volume ),
310
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeName , & volume ),
313
311
resource .TestCheckResourceAttr (
314
- "libvirt_volume.terraform-acceptance-test-2" , "name" , "terraform-test" ),
312
+ "libvirt_volume." + randomVolumeName , "name" , "terraform-test" ),
315
313
),
316
314
},
317
315
},
@@ -321,7 +319,8 @@ func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) {
321
319
func TestAccLibvirtVolume_DownloadFromSourceFormat (t * testing.T ) {
322
320
var volumeRaw libvirt.StorageVol
323
321
var volumeQCOW2 libvirt.StorageVol
324
-
322
+ randomVolumeNameRaw := acctest .RandString (10 )
323
+ randomVolumeNameQCOW := acctest .RandString (10 )
325
324
qcow2Path , err := filepath .Abs ("testdata/test.qcow2" )
326
325
if err != nil {
327
326
t .Fatal (err )
@@ -332,19 +331,15 @@ func TestAccLibvirtVolume_DownloadFromSourceFormat(t *testing.T) {
332
331
t .Fatal (err )
333
332
}
334
333
335
- const testAccCheckLibvirtVolumeConfigSource = `
336
- resource "libvirt_volume" "terraform-acceptance-test-raw " {
334
+ config := fmt . Sprintf ( `
335
+ resource "libvirt_volume" "%s " {
337
336
name = "terraform-test-raw"
338
337
source = "%s"
339
338
}
340
-
341
- resource "libvirt_volume" "terraform-acceptance-test-qcow2" {
339
+ resource "libvirt_volume" "%s" {
342
340
name = "terraform-test-qcow2"
343
341
source = "%s"
344
- }`
345
- config := fmt .Sprintf (testAccCheckLibvirtVolumeConfigSource ,
346
- fmt .Sprintf ("file://%s" , rawPath ),
347
- fmt .Sprintf ("file://%s" , qcow2Path ))
342
+ }` , randomVolumeNameRaw , fmt .Sprintf ("file://%s" , rawPath ), randomVolumeNameQCOW , fmt .Sprintf ("file://%s" , qcow2Path ))
348
343
349
344
resource .Test (t , resource.TestCase {
350
345
PreCheck : func () { testAccPreCheck (t ) },
@@ -354,16 +349,16 @@ func TestAccLibvirtVolume_DownloadFromSourceFormat(t *testing.T) {
354
349
{
355
350
Config : config ,
356
351
Check : resource .ComposeTestCheckFunc (
357
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-raw" , & volumeRaw ),
358
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-qcow2" , & volumeQCOW2 ),
352
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeNameRaw , & volumeRaw ),
353
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeNameQCOW , & volumeQCOW2 ),
359
354
resource .TestCheckResourceAttr (
360
- "libvirt_volume.terraform-acceptance-test-raw" , "name" , "terraform-test-raw" ),
355
+ "libvirt_volume." + randomVolumeNameRaw , "name" , "terraform-test-raw" ),
361
356
resource .TestCheckResourceAttr (
362
- "libvirt_volume.terraform-acceptance-test-raw" , "format" , "raw" ),
357
+ "libvirt_volume." + randomVolumeNameRaw , "format" , "raw" ),
363
358
resource .TestCheckResourceAttr (
364
- "libvirt_volume.terraform-acceptance-test-qcow2" , "name" , "terraform-test-qcow2" ),
359
+ "libvirt_volume." + randomVolumeNameQCOW , "name" , "terraform-test-qcow2" ),
365
360
resource .TestCheckResourceAttr (
366
- "libvirt_volume.terraform-acceptance-test-qcow2" , "format" , "qcow2" ),
361
+ "libvirt_volume." + randomVolumeNameQCOW , "format" , "qcow2" ),
367
362
),
368
363
},
369
364
},
@@ -372,29 +367,28 @@ func TestAccLibvirtVolume_DownloadFromSourceFormat(t *testing.T) {
372
367
373
368
func TestAccLibvirtVolume_Format (t * testing.T ) {
374
369
var volume libvirt.StorageVol
375
-
376
- const testAccCheckLibvirtVolumeConfigFormat = `
377
- resource "libvirt_volume" "terraform-acceptance-test-3" {
378
- name = "terraform-test"
379
- format = "raw"
380
- size = 1073741824
381
- }`
370
+ randomVolumeName := acctest .RandString (10 )
382
371
383
372
resource .Test (t , resource.TestCase {
384
373
PreCheck : func () { testAccPreCheck (t ) },
385
374
Providers : testAccProviders ,
386
375
CheckDestroy : testAccCheckLibvirtVolumeDestroy ,
387
376
Steps : []resource.TestStep {
388
377
{
389
- Config : testAccCheckLibvirtVolumeConfigFormat ,
378
+ Config : fmt .Sprintf (`
379
+ resource "libvirt_volume" "%s" {
380
+ name = "terraform-test"
381
+ format = "raw"
382
+ size = 1073741824
383
+ }` , randomVolumeName ),
390
384
Check : resource .ComposeTestCheckFunc (
391
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-3" , & volume ),
385
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeName , & volume ),
392
386
resource .TestCheckResourceAttr (
393
- "libvirt_volume.terraform-acceptance-test-3" , "name" , "terraform-test" ),
387
+ "libvirt_volume." + randomVolumeName , "name" , "terraform-test" ),
394
388
resource .TestCheckResourceAttr (
395
- "libvirt_volume.terraform-acceptance-test-3" , "size" , "1073741824" ),
389
+ "libvirt_volume." + randomVolumeName , "size" , "1073741824" ),
396
390
resource .TestCheckResourceAttr (
397
- "libvirt_volume.terraform-acceptance-test-3" , "format" , "raw" ),
391
+ "libvirt_volume." + randomVolumeName , "format" , "raw" ),
398
392
),
399
393
},
400
394
},
@@ -403,33 +397,30 @@ func TestAccLibvirtVolume_Format(t *testing.T) {
403
397
404
398
func TestAccLibvirtVolume_Import (t * testing.T ) {
405
399
var volume libvirt.StorageVol
406
-
407
- const testAccCheckLibvirtVolumeConfigImport = `
408
- resource "libvirt_volume" "terraform-acceptance-test-4" {
409
- name = "terraform-test"
410
- format = "raw"
411
- size = 1073741824
412
- }`
413
-
414
- resourceName := "libvirt_volume.terraform-acceptance-test-4"
400
+ randomVolumeName := acctest .RandString (10 )
415
401
416
402
resource .Test (t , resource.TestCase {
417
403
PreCheck : func () { testAccPreCheck (t ) },
418
404
Providers : testAccProviders ,
419
405
CheckDestroy : testAccCheckLibvirtVolumeDestroy ,
420
406
Steps : []resource.TestStep {
421
407
resource.TestStep {
422
- Config : testAccCheckLibvirtVolumeConfigImport ,
408
+ Config : fmt .Sprintf (`
409
+ resource "libvirt_volume" "%s" {
410
+ name = "terraform-test"
411
+ format = "raw"
412
+ size = 1073741824
413
+ }` , randomVolumeName ),
423
414
},
424
415
resource.TestStep {
425
- ResourceName : resourceName ,
416
+ ResourceName : "libvirt_volume." + randomVolumeName ,
426
417
ImportState : true ,
427
418
Check : resource .ComposeTestCheckFunc (
428
- testAccCheckLibvirtVolumeExists ("libvirt_volume.terraform-acceptance-test-4" , & volume ),
419
+ testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeName , & volume ),
429
420
resource .TestCheckResourceAttr (
430
- "libvirt_volume.terraform-acceptance-test-4" , "name" , "terraform-test" ),
421
+ "libvirt_volume." + randomVolumeName , "name" , "terraform-test" ),
431
422
resource .TestCheckResourceAttr (
432
- "libvirt_volume.terraform-acceptance-test-4" , "size" , "1073741824" ),
423
+ "libvirt_volume." + randomVolumeName , "size" , "1073741824" ),
433
424
),
434
425
},
435
426
},
0 commit comments