summaryrefslogtreecommitdiff
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
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
-rw-r--r--.gitignore2
-rw-r--r--Makefile2
-rw-r--r--libvirt/cloudinit_def_test.go83
-rwxr-xr-xtravis/setup-guest2
4 files changed, 85 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index c3879151..f3879e37 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,8 @@
./terraform-provider-libvirt
+.terraform/*
./*.tf
*.tfstate
*.tfstate.backup
*.test
*.cov
+terraform-provider-libvirt
diff --git a/Makefile b/Makefile
index fea63149..512d7ae0 100644
--- a/Makefile
+++ b/Makefile
@@ -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 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 {
diff --git a/travis/setup-guest b/travis/setup-guest
index 71562655..42568e81 100755
--- a/travis/setup-guest
+++ b/travis/setup-guest
@@ -14,7 +14,7 @@ done
add-apt-repository -y ppa:gophers/archive
apt-get -qq update
-apt-get install -y qemu libvirt-bin libvirt-dev golang-1.9 ovmf
+apt-get install -y qemu libvirt-bin libvirt-dev golang-1.9 ovmf genisoimage
echo -e "<pool type='dir'>\n<name>default</name>\n<target>\n<path>/pool-default</path>\n</target>\n</pool>" > pool.xml
mkdir /pool-default
chmod a+rwx /pool-default