summaryrefslogtreecommitdiff
path: root/libvirt/resource_libvirt_domain_test.go
blob: 61bab3b6d905516803fecb43d4d40f8d2f837370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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"
}
`