From 9d3bfb5aaf0b51695a2dae54ede7845660d8eb70 Mon Sep 17 00:00:00 2001 From: Dario Maiocchi Date: Tue, 14 Nov 2017 20:22:55 +0100 Subject: Add AcceptTest for CloudInit --- libvirt/cloudinit_def_test.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/libvirt/cloudinit_def_test.go b/libvirt/cloudinit_def_test.go index ab01f4e7..18c63a07 100644 --- a/libvirt/cloudinit_def_test.go +++ b/libvirt/cloudinit_def_test.go @@ -1,11 +1,12 @@ package libvirt import ( + "fmt" + "github.com/hashicorp/terraform/helper/resource" "os" "path/filepath" "strings" "testing" - "gopkg.in/yaml.v2" ) @@ -161,6 +162,34 @@ ssh_authorized_keys: } } +func TestCreateCloudIsoViaPlugin(t *testing.T) { + var config = fmt.Sprintf(` + + resource "libvirt_cloudinit" "test" { + name = "commoninit.iso" + local_hostname = "tango" + pool = "default" + user_data = "#cloud-config\nssh_authorized_keys: []\n" + } + `) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtIgnitionDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "libvirt_cloudinit.test", "name", "commoninit.iso"), + resource.TestCheckResourceAttr( + "libvirt_cloudinit.test", "local_hostname", "tango"), + ), + }, + }, + }) +} + func exists(path string) (bool, error) { _, err := os.Stat(path) if err == nil { -- cgit v1.2.3 From 4ae842bbeb3b9d72659d4c78f6bf1c75a7881965 Mon Sep 17 00:00:00 2001 From: Dario Maiocchi Date: Tue, 14 Nov 2017 21:20:54 +0100 Subject: add geniso to travis_guest --- libvirt/cloudinit_def_test.go | 62 ++++++++++++++++++++++++++++++++----------- travis/setup-guest | 2 +- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/libvirt/cloudinit_def_test.go b/libvirt/cloudinit_def_test.go index 18c63a07..1604c667 100644 --- a/libvirt/cloudinit_def_test.go +++ b/libvirt/cloudinit_def_test.go @@ -3,11 +3,13 @@ package libvirt import ( "fmt" "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "gopkg.in/yaml.v2" "os" "path/filepath" + "regexp" "strings" "testing" - "gopkg.in/yaml.v2" ) func TestNewCloudInitDef(t *testing.T) { @@ -163,27 +165,57 @@ ssh_authorized_keys: } func TestCreateCloudIsoViaPlugin(t *testing.T) { - var config = fmt.Sprintf(` - - resource "libvirt_cloudinit" "test" { - name = "commoninit.iso" - local_hostname = "tango" - pool = "default" - user_data = "#cloud-config\nssh_authorized_keys: []\n" - } - `) + + // FIXME: check for existence of the cloud-init volume + resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckLibvirtIgnitionDestroy, + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: func(s *terraform.State) error { + return nil + }, Steps: []resource.TestStep{ { - Config: config, + Config: fmt.Sprintf(` + resource "libvirt_cloudinit" "test" { + name = "commoninit.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"), resource.TestCheckResourceAttr( - "libvirt_cloudinit.test", "local_hostname", "tango"), + "libvirt_cloudinit.test", "local_hostname", "tango1"), + ), + }, + // 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"), + }, + // 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"), ), }, }, 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 "\ndefault\n\n/pool-default\n\n" > pool.xml mkdir /pool-default chmod a+rwx /pool-default -- cgit v1.2.3 From 38b172e306d23b844846244fd1ffd0dd55e5714e Mon Sep 17 00:00:00 2001 From: Dario Maiocchi Date: Wed, 15 Nov 2017 23:48:49 +0100 Subject: add volume cloud_init test check --- .gitignore | 2 ++ Makefile | 2 +- libvirt/cloudinit_def_test.go | 72 +++++++++++++++++++++++++++---------------- 3 files changed, 48 insertions(+), 28 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 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 { -- cgit v1.2.3