summaryrefslogtreecommitdiff
path: root/libvirt/resource_libvirt_coreos_ignition_test.go
blob: 027cbd3a0b02f8eba2498e3718066c6597330e29 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
package libvirt

import (
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
	libvirt "github.com/libvirt/libvirt-go"
)

func TestAccLibvirtIgnition_Basic(t *testing.T) {
	var volume libvirt.StorageVol
	var config = fmt.Sprintf(`
	    data "ignition_systemd_unit" "acceptance-test-systemd" {
    		name = "example.service"
    		content = "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target"
	    }

	    data "ignition_config" "acceptance-test-config" {
    		systemd = [
        		"${data.ignition_systemd_unit.acceptance-test-systemd.id}",
    		]
	    }

	    resource "libvirt_ignition" "ignition" {
	    	name = "ignition"
	    	content = "${data.ignition_config.acceptance-test-config.rendered}"
	    }
	`)

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckLibvirtIgnitionDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckIgnitionVolumeExists("libvirt_ignition.ignition", &volume),
					resource.TestCheckResourceAttr(
						"libvirt_ignition.ignition", "name", "ignition"),
					resource.TestCheckResourceAttr(
						"libvirt_ignition.ignition", "pool", "default"),
				),
			},
		},
	})
}

func testAccCheckIgnitionVolumeExists(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 ignition key ID is set")
		}

		ignKey, err := getIgnitionVolumeKeyFromTerraformID(rs.Primary.ID)
		if err != nil {
			return err
		}

		retrievedVol, err := virConn.LookupStorageVolByKey(ignKey)
		if err != nil {
			return err
		}
		fmt.Printf("The ID is %s", rs.Primary.ID)

		realID, err := retrievedVol.GetKey()
		if err != nil {
			return err
		}

		if realID != ignKey {
			return fmt.Errorf("Resource ID and volume key does not match")
		}

		*volume = *retrievedVol
		return nil
	}
}

func testAccCheckLibvirtIgnitionDestroy(s *terraform.State) error {
	virtConn := testAccProvider.Meta().(*Client).libvirt

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "libvirt_ignition" {
			continue
		}

		// Try to find the Ignition Volume

		ignKey, errKey := getIgnitionVolumeKeyFromTerraformID(rs.Primary.ID)
		if errKey != nil {
			return errKey
		}

		_, err := virtConn.LookupStorageVolByKey(ignKey)
		if err == nil {
			return fmt.Errorf(
				"Error waiting for IgnitionVolume (%s) to be destroyed: %s",
				ignKey, err)
		}
	}

	return nil
}