diff options
author | Eamonn O'Toole <eamonn.otoole@hpe.com> | 2017-03-02 11:00:59 +0000 |
---|---|---|
committer | Eamonn O'Toole <eamonn.otoole@hpe.com> | 2017-03-23 11:32:31 +0000 |
commit | f130f9848e61924bded600602a91822cf76e0959 (patch) | |
tree | a75c0c56fe384d1789cc59b56408ae23e43d5b00 /libvirt/resource_libvirt_coreos_ignition_test.go | |
parent | a07e21b74726d9ce443ac9332417c169b2ccd708 (diff) | |
download | terraform-provider-libvirt-f130f9848e61924bded600602a91822cf76e0959.tar terraform-provider-libvirt-f130f9848e61924bded600602a91822cf76e0959.tar.gz |
Write Ignition file as a volume in a libvirt storage pool
This avoids the problem where the Ignition file is remote to
the host on which the libvirt domain is being created. We've
added a "libvirt_ignition" resource which manages the Ignition
file in the libvirt volume - creates it, and destroys it. The
"coreos_ignition" field in the libvirt_domain definition must
point to the Id of a "libvirt_ignition" resource.
The code is modelled on that used for CloudInit.
Diffstat (limited to 'libvirt/resource_libvirt_coreos_ignition_test.go')
-rw-r--r-- | libvirt/resource_libvirt_coreos_ignition_test.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/libvirt/resource_libvirt_coreos_ignition_test.go b/libvirt/resource_libvirt_coreos_ignition_test.go new file mode 100644 index 00000000..cbd5fe0e --- /dev/null +++ b/libvirt/resource_libvirt_coreos_ignition_test.go @@ -0,0 +1,113 @@ +package libvirt + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + libvirt "github.com/dmacvicar/libvirt-go" +) + +func TestAccLibvirtIgnition_Basic(t *testing.T) { + var volume libvirt.VirStorageVol + var config = fmt.Sprintf(` + resource "ignition_systemd_unit" "acceptance-test-systemd" { + name = "example.service" + content = "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target" + } + + resource "ignition_config" "acceptance-test-config" { + systemd = [ + "${ignition_systemd_unit.acceptance-test-systemd.id}", + ] + } + + resource "libvirt_ignition" "ignition" { + name = "ignition" + content = "${ignition_config.acceptance-test-config.rendered}" + } + `) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtIgnitionDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testAccCheckIgnitionVolumeExists("libvirt_ignition.ignition", &volume), + resource.TestCheckResourceAttr( + "libvirt_ignition.ignition", "name", "ignition"), + resource.TestCheckResourceAttr( + "libvirt_ignition.ignition", "pool", "default"), + ), + }, + }, + }) +} + +func testAccCheckIgnitionVolumeExists(n string, volume *libvirt.VirStorageVol) resource.TestCheckFunc { + return func(s *terraform.State) error { + virConn := testAccProvider.Meta().(*Client).libvirt + + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No libvirt ignition key ID is set") + } + + ignKey, err := getIgnitionVolumeKeyFromTerraformID(rs.Primary.ID) + if err != nil { + return err + } + + retrievedVol, err := virConn.LookupStorageVolByKey(ignKey) + if err != nil { + return err + } + fmt.Printf("The ID is %s", rs.Primary.ID) + + realId, err := retrievedVol.GetKey() + if err != nil { + return err + } + + if realId != ignKey { + return fmt.Errorf("Resource ID and volume key does not match") + } + + *volume = retrievedVol + return nil + } +} + +func testAccCheckLibvirtIgnitionDestroy(s *terraform.State) error { + virtConn := testAccProvider.Meta().(*Client).libvirt + + for _, rs := range s.RootModule().Resources { + if rs.Type != "libvirt_ignition" { + continue + } + + // Try to find the Ignition Volume + + ignKey, errKey := getIgnitionVolumeKeyFromTerraformID(rs.Primary.ID) + if errKey != nil { + return errKey + } + + _, err := virtConn.LookupStorageVolByKey(ignKey) + if err == nil { + return fmt.Errorf( + "Error waiting for IgnitionVolume (%s) to be destroyed: %s", + ignKey, err) + } + } + + return nil +}
\ No newline at end of file |