diff options
author | Dario Maiocchi <dmaiocchi@suse.com> | 2017-11-15 23:48:49 +0100 |
---|---|---|
committer | Dario Maiocchi <dmaiocchi@suse.com> | 2017-11-16 16:47:40 +0100 |
commit | 38b172e306d23b844846244fd1ffd0dd55e5714e (patch) | |
tree | 1774b31f9155ccd616098c27a820535b87d5532b | |
parent | 4ae842bbeb3b9d72659d4c78f6bf1c75a7881965 (diff) | |
download | terraform-provider-libvirt-38b172e306d23b844846244fd1ffd0dd55e5714e.tar terraform-provider-libvirt-38b172e306d23b844846244fd1ffd0dd55e5714e.tar.gz |
add volume cloud_init test check
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libvirt/cloudinit_def_test.go | 72 |
3 files changed, 48 insertions, 28 deletions
@@ -1,6 +1,8 @@ ./terraform-provider-libvirt +.terraform/* ./*.tf *.tfstate *.tfstate.backup *.test *.cov +terraform-provider-libvirt @@ -1,6 +1,6 @@ default: build -build: gofmtcheck +build: gofmtcheck golint go build install: diff --git a/libvirt/cloudinit_def_test.go b/libvirt/cloudinit_def_test.go index 1604c667..c2202a75 100644 --- a/libvirt/cloudinit_def_test.go +++ b/libvirt/cloudinit_def_test.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + libvirt "github.com/libvirt/libvirt-go" "gopkg.in/yaml.v2" "os" "path/filepath" @@ -165,9 +166,7 @@ ssh_authorized_keys: } func TestCreateCloudIsoViaPlugin(t *testing.T) { - - // FIXME: check for existence of the cloud-init volume - + var volume libvirt.StorageVol resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -177,51 +176,70 @@ func TestCreateCloudIsoViaPlugin(t *testing.T) { Steps: []resource.TestStep{ { Config: fmt.Sprintf(` - resource "libvirt_cloudinit" "test" { - name = "commoninit.iso" - local_hostname = "tango1" - pool = "default" - user_data = "#cloud-config\nssh_authorized_keys: []\n" + resource "libvirt_cloudinit" "test" { + name = "test.iso" + local_hostname = "tango1" + pool = "default" + user_data = "#cloud-config\nssh_authorized_keys: []\n" }`), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( - "libvirt_cloudinit.test", "name", "commoninit.iso"), + "libvirt_cloudinit.test", "name", "test.iso"), resource.TestCheckResourceAttr( "libvirt_cloudinit.test", "local_hostname", "tango1"), + testAccCheckCloudInitVolumeExists("libvirt_cloudinit.test", &volume), ), }, // 2nd tests Invalid userdata { Config: fmt.Sprintf(` - resource "libvirt_cloudinit" "test" { + resource "libvirt_cloudinit" "test" { name = "commoninit2.iso" local_hostname = "samba2" pool = "default" user_data = "invalidgino" - }`), + }`), ExpectError: regexp.MustCompile("Error merging UserData with UserDataRaw: yaml: unmarshal errors"), }, - // 3nd test explicetely don'tuse user_data - { - Config: fmt.Sprintf(` - resource "libvirt_cloudinit" "test3" { - name = "commoninit3.iso" - local_hostname = "nouserdata" - pool = "default" - }`), - - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "libvirt_cloudinit.test3", "name", "commoninit3.iso"), - resource.TestCheckResourceAttr( - "libvirt_cloudinit.test3", "local_hostname", "nouserdata"), - ), - }, }, }) } +func testAccCheckCloudInitVolumeExists(n string, volume *libvirt.StorageVol) 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 volume key ID is set") + } + + cikey, err := getCloudInitVolumeKeyFromTerraformID(rs.Primary.ID) + retrievedVol, err := virConn.LookupStorageVolByKey(cikey) + if err != nil { + return err + } + realID, err := retrievedVol.GetKey() + if err != nil { + return err + } + + if realID != cikey { + fmt.Printf("realID is: %s \ncloudinit key is %s", realID, cikey) + return fmt.Errorf("Resource ID and cloudinit volume key does not match") + } + + *volume = *retrievedVol + + return nil + } +} + func exists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { |