summaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
authorThomas Hipp <thomashipp@gmail.com>2017-11-16 18:11:20 +0100
committerGitHub <noreply@github.com>2017-11-16 18:11:20 +0100
commit3a52429e58643dad0e6122f3fb47b61be2ba2c72 (patch)
tree1774b31f9155ccd616098c27a820535b87d5532b /libvirt
parentacad5df18a9ffd03d6f6bdd2b33f4eade8e9a09d (diff)
parent38b172e306d23b844846244fd1ffd0dd55e5714e (diff)
downloadterraform-provider-libvirt-3a52429e58643dad0e6122f3fb47b61be2ba2c72.tar
terraform-provider-libvirt-3a52429e58643dad0e6122f3fb47b61be2ba2c72.tar.gz
Merge pull request #238 from MalloZup/improve_cloud_init
Add an Acceptance Test for CloudInit def
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/cloudinit_def_test.go83
1 files changed, 81 insertions, 2 deletions
diff --git a/libvirt/cloudinit_def_test.go b/libvirt/cloudinit_def_test.go
index ab01f4e7..c2202a75 100644
--- a/libvirt/cloudinit_def_test.go
+++ b/libvirt/cloudinit_def_test.go
@@ -1,12 +1,16 @@
package libvirt
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"
+ "regexp"
"strings"
"testing"
-
- "gopkg.in/yaml.v2"
)
func TestNewCloudInitDef(t *testing.T) {
@@ -161,6 +165,81 @@ ssh_authorized_keys:
}
}
+func TestCreateCloudIsoViaPlugin(t *testing.T) {
+ var volume libvirt.StorageVol
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: func(s *terraform.State) error {
+ return nil
+ },
+ Steps: []resource.TestStep{
+ {
+ Config: fmt.Sprintf(`
+ 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", "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" {
+ name = "commoninit2.iso"
+ local_hostname = "samba2"
+ pool = "default"
+ user_data = "invalidgino"
+ }`),
+ ExpectError: regexp.MustCompile("Error merging UserData with UserDataRaw: yaml: unmarshal errors"),
+ },
+ },
+ })
+}
+
+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 {