-
Notifications
You must be signed in to change notification settings - Fork 459
/
resource_libvirt_coreos_ignition_test.go
115 lines (100 loc) · 3.26 KB
/
resource_libvirt_coreos_ignition_test.go
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
114
115
package libvirt
import (
"fmt"
"testing"
libvirt "github.com/digitalocean/go-libvirt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccLibvirtIgnition_Basic(t *testing.T) {
var volume libvirt.StorageVol
randomServiceName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) + ".service"
randomIgnitionName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
var config = fmt.Sprintf(`
data "ignition_systemd_unit" "acceptance-test-systemd" {
name = "%s"
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.rendered}",
]
}
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_ignition" "ignition" {
name = "%s"
content = "${data.ignition_config.acceptance-test-config.rendered}"
pool = "${libvirt_pool.%s.name}"
}
`, randomServiceName, randomPoolName, randomPoolName, randomPoolPath, randomIgnitionName, randomPoolName)
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", randomIgnitionName),
resource.TestCheckResourceAttr(
"libvirt_ignition.ignition", "pool", randomPoolName),
),
},
},
})
}
func testAccCheckIgnitionVolumeExists(name string, volume *libvirt.StorageVol) resource.TestCheckFunc {
return func(state *terraform.State) error {
virConn := testAccProvider.Meta().(*Client).libvirt
rs, err := getResourceFromTerraformState(name, state)
if err != nil {
return err
}
ignKey, err := getIgnitionVolumeKeyFromTerraformID(rs.Primary.ID)
if err != nil {
return err
}
retrievedVol, err := virConn.StorageVolLookupByKey(ignKey)
if err != nil {
return err
}
fmt.Printf("The ID is %s", rs.Primary.ID)
if retrievedVol.Key == "" {
return fmt.Errorf("Resource key is blank")
}
if retrievedVol.Key != 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.StorageVolLookupByKey(ignKey)
if err == nil {
return fmt.Errorf(
"Error waiting for IgnitionVolume (%s) to be destroyed: %s",
ignKey, err)
}
}
return nil
}