summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--libvirt/provider_test.go35
-rw-r--r--libvirt/resource_libvirt_domain.go4
-rw-r--r--libvirt/resource_libvirt_domain_test.go100
4 files changed, 138 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 0fd7d828..7be59275 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ terraform-provider-libvirt
*.tf
*.tfstate
*.tfstate.backup
+*.test
diff --git a/libvirt/provider_test.go b/libvirt/provider_test.go
new file mode 100644
index 00000000..662b1185
--- /dev/null
+++ b/libvirt/provider_test.go
@@ -0,0 +1,35 @@
+package libvirt
+
+import (
+ "os"
+ "testing"
+
+ "github.com/hashicorp/terraform/helper/schema"
+ "github.com/hashicorp/terraform/terraform"
+)
+
+var testAccProviders map[string]terraform.ResourceProvider
+var testAccProvider *schema.Provider
+
+func init() {
+ testAccProvider = Provider().(*schema.Provider)
+ testAccProviders = map[string]terraform.ResourceProvider{
+ "libvirt": testAccProvider,
+ }
+}
+
+func TestProvider(t *testing.T) {
+ if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
+ t.Fatalf("err: %s", err)
+ }
+}
+
+func TestProvider_impl(t *testing.T) {
+ var _ terraform.ResourceProvider = Provider()
+}
+
+func testAccPreCheck(t *testing.T) {
+ if v := os.Getenv("LIBVIRT_DEFAULT_URI"); v == "" {
+ t.Fatal("LIBVIRT_DEFAULT_URI must be set for acceptance tests")
+ }
+}
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index 38df7e95..2efb6826 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -70,7 +70,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
},
}
- log.Printf("[INFO] Creating virtual machine")
+ log.Printf("[INFO] Creating libvirt domain")
data, err := xml.Marshal(domainDef)
if err != nil {
@@ -88,7 +88,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
}
d.SetId(fmt.Sprintf("%d", id))
- log.Printf("[INFO] Virtual Machine ID: %s", d.Id())
+ log.Printf("[INFO] Domain ID: %s", d.Id())
return resourceLibvirtDomainRead(d, meta)
}
diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go
new file mode 100644
index 00000000..61bab3b6
--- /dev/null
+++ b/libvirt/resource_libvirt_domain_test.go
@@ -0,0 +1,100 @@
+package libvirt
+
+import (
+ "fmt"
+ "strconv"
+ "testing"
+ "github.com/hashicorp/terraform/helper/resource"
+ "github.com/hashicorp/terraform/terraform"
+ "gopkg.in/alexzorin/libvirt-go.v2"
+)
+
+func TestAccLibvirtDomain_Basic(t *testing.T) {
+ var domain libvirt.VirDomain
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtDomainDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: testAccCheckLibvirtDomainConfig_basic,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtDomainExists("libvirt_domain.terraform-acceptance-test-1", &domain),
+ resource.TestCheckResourceAttr(
+ "libvirt_domain.terraform-acceptance-test-1", "name", "terraform-test"),
+ ),
+ },
+ },
+ })
+}
+
+func testAccCheckLibvirtDomainDestroy(s *terraform.State) error {
+ virtConn := testAccProvider.Meta().(*Client).libvirt
+
+ for _, rs := range s.RootModule().Resources {
+ if rs.Type != "libvirt_domain" {
+ continue
+ }
+
+ domainId, _ := strconv.Atoi(rs.Primary.ID)
+
+ // Try to find the server
+ _, err := virtConn.LookupDomainById(uint32(domainId))
+ if err == nil {
+ return fmt.Errorf(
+ "Error waiting for domain (%s) to be destroyed: %s",
+ rs.Primary.ID, err)
+ }
+ }
+
+ return nil
+}
+
+func testAccCheckLibvirtDomainExists(n string, domain *libvirt.VirDomain) resource.TestCheckFunc {
+ return func(s *terraform.State) error {
+ rs, ok := s.RootModule().Resources[n]
+ if !ok {
+ return fmt.Errorf("Not found: %s", n)
+ }
+
+ if rs.Primary.ID == "" {
+ return fmt.Errorf("No libvirt domain ID is set")
+ }
+
+ domainId, err := strconv.Atoi(rs.Primary.ID)
+
+ if err != nil {
+ return err
+ }
+
+ virConn := testAccProvider.Meta().(*Client).libvirt
+
+ retrieveDomain, err := virConn.LookupDomainById(uint32(domainId))
+
+ if err != nil {
+ return err
+ }
+
+ fmt.Printf("The ID is %d", domainId)
+
+ realId, err := retrieveDomain.GetID()
+ if err != nil {
+ return err
+ }
+
+ if realId != uint(domainId) {
+ return fmt.Errorf("Libvirt domain not found")
+ }
+
+ *domain = retrieveDomain
+
+ return nil
+ }
+}
+
+const testAccCheckLibvirtDomainConfig_basic = `
+resource "libvirt_domain" "terraform-acceptance-test-1" {
+ name = "terraform-test"
+}
+`