|
6 | 6 | "fmt"
|
7 | 7 | "os"
|
8 | 8 | "path/filepath"
|
| 9 | + "regexp" |
9 | 10 | "testing"
|
10 | 11 |
|
11 | 12 | libvirt "github.com/digitalocean/go-libvirt"
|
@@ -408,6 +409,125 @@ func TestAccLibvirtVolume_DownloadFromSourceFormat(t *testing.T) {
|
408 | 409 | })
|
409 | 410 | }
|
410 | 411 |
|
| 412 | +func TestAccLibvirtVolume_DownloadFromSourceSize(t *testing.T) { |
| 413 | + size := 128 * 1024 * 1024 |
| 414 | + var volumeRaw libvirt.StorageVol |
| 415 | + var volumeQCOW2 libvirt.StorageVol |
| 416 | + randomVolumeNameRaw := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 417 | + randomVolumeNameQCOW := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 418 | + randomVolumeResourceRaw := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 419 | + randomVolumeResourceQCOW := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 420 | + randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 421 | + randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName |
| 422 | + qcow2Path, err := filepath.Abs("testdata/test.qcow2") |
| 423 | + if err != nil { |
| 424 | + t.Fatal(err) |
| 425 | + } |
| 426 | + |
| 427 | + rawPath, err := filepath.Abs("testdata/initrd.img") |
| 428 | + if err != nil { |
| 429 | + t.Fatal(err) |
| 430 | + } |
| 431 | + |
| 432 | + config := fmt.Sprintf(` |
| 433 | + resource "libvirt_pool" "%s" { |
| 434 | + name = "%s" |
| 435 | + type = "dir" |
| 436 | + path = "%s" |
| 437 | + } |
| 438 | + resource "libvirt_volume" "%s" { |
| 439 | + name = "%s" |
| 440 | + source = "%s" |
| 441 | + size = %d |
| 442 | + pool = "${libvirt_pool.%s.name}" |
| 443 | + } |
| 444 | + resource "libvirt_volume" "%s" { |
| 445 | + name = "%s" |
| 446 | + source = "%s" |
| 447 | + size = %d |
| 448 | + pool = "${libvirt_pool.%s.name}" |
| 449 | + }`, randomPoolName, randomPoolName, randomPoolPath, |
| 450 | + randomVolumeResourceRaw, randomVolumeNameRaw, fmt.Sprintf("file://%s", rawPath), size, randomPoolName, |
| 451 | + randomVolumeResourceQCOW, randomVolumeNameQCOW, fmt.Sprintf("file://%s", qcow2Path), size, randomPoolName) |
| 452 | + resource.Test(t, resource.TestCase{ |
| 453 | + PreCheck: func() { testAccPreCheck(t) }, |
| 454 | + Providers: testAccProviders, |
| 455 | + CheckDestroy: testAccCheckLibvirtVolumeDestroy, |
| 456 | + Steps: []resource.TestStep{ |
| 457 | + { |
| 458 | + Config: config, |
| 459 | + Check: resource.ComposeTestCheckFunc( |
| 460 | + testAccCheckLibvirtVolumeExists("libvirt_volume."+randomVolumeResourceRaw, &volumeRaw), |
| 461 | + testAccCheckLibvirtVolumeExists("libvirt_volume."+randomVolumeResourceQCOW, &volumeQCOW2), |
| 462 | + resource.TestCheckResourceAttr( |
| 463 | + "libvirt_volume."+randomVolumeResourceRaw, "name", randomVolumeNameRaw), |
| 464 | + resource.TestCheckResourceAttr( |
| 465 | + "libvirt_volume."+randomVolumeResourceRaw, "format", "raw"), |
| 466 | + resource.TestCheckResourceAttr( |
| 467 | + "libvirt_volume."+randomVolumeResourceRaw, "size", fmt.Sprintf("%d", size)), |
| 468 | + resource.TestCheckResourceAttr( |
| 469 | + "libvirt_volume."+randomVolumeResourceQCOW, "name", randomVolumeNameQCOW), |
| 470 | + resource.TestCheckResourceAttr( |
| 471 | + "libvirt_volume."+randomVolumeResourceQCOW, "format", "qcow2"), |
| 472 | + resource.TestCheckResourceAttr( |
| 473 | + "libvirt_volume."+randomVolumeResourceQCOW, "size", fmt.Sprintf("%d", size)), |
| 474 | + ), |
| 475 | + }, |
| 476 | + }, |
| 477 | + }) |
| 478 | +} |
| 479 | + |
| 480 | +func TestAccLibvirtVolume_DownloadFromSourceSize_TooSmall(t *testing.T) { |
| 481 | + size := 16 |
| 482 | + randomVolumeNameRaw := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 483 | + randomVolumeNameQCOW := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 484 | + randomVolumeResourceRaw := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 485 | + randomVolumeResourceQCOW := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 486 | + randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) |
| 487 | + randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName |
| 488 | + qcow2Path, err := filepath.Abs("testdata/test.qcow2") |
| 489 | + if err != nil { |
| 490 | + t.Fatal(err) |
| 491 | + } |
| 492 | + |
| 493 | + rawPath, err := filepath.Abs("testdata/initrd.img") |
| 494 | + if err != nil { |
| 495 | + t.Fatal(err) |
| 496 | + } |
| 497 | + |
| 498 | + config := fmt.Sprintf(` |
| 499 | + resource "libvirt_pool" "%s" { |
| 500 | + name = "%s" |
| 501 | + type = "dir" |
| 502 | + path = "%s" |
| 503 | + } |
| 504 | + resource "libvirt_volume" "%s" { |
| 505 | + name = "%s" |
| 506 | + source = "%s" |
| 507 | + size = %d |
| 508 | + pool = "${libvirt_pool.%s.name}" |
| 509 | + } |
| 510 | + resource "libvirt_volume" "%s" { |
| 511 | + name = "%s" |
| 512 | + source = "%s" |
| 513 | + size = %d |
| 514 | + pool = "${libvirt_pool.%s.name}" |
| 515 | + }`, randomPoolName, randomPoolName, randomPoolPath, |
| 516 | + randomVolumeResourceRaw, randomVolumeNameRaw, fmt.Sprintf("file://%s", rawPath), size, randomPoolName, |
| 517 | + randomVolumeResourceQCOW, randomVolumeNameQCOW, fmt.Sprintf("file://%s", qcow2Path), size, randomPoolName) |
| 518 | + resource.Test(t, resource.TestCase{ |
| 519 | + PreCheck: func() { testAccPreCheck(t) }, |
| 520 | + Providers: testAccProviders, |
| 521 | + CheckDestroy: testAccCheckLibvirtVolumeDestroy, |
| 522 | + Steps: []resource.TestStep{ |
| 523 | + { |
| 524 | + Config: config, |
| 525 | + ExpectError: regexp.MustCompile(`'size' can't be smaller than the size of the 'source'`), |
| 526 | + }, |
| 527 | + }, |
| 528 | + }) |
| 529 | +} |
| 530 | + |
411 | 531 | func TestAccLibvirtVolume_Format(t *testing.T) {
|
412 | 532 | var volume libvirt.StorageVol
|
413 | 533 | randomVolumeResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
|
|
0 commit comments