-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathresource_libvirt_cloud_init_test.go
237 lines (216 loc) · 8.87 KB
/
resource_libvirt_cloud_init_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package libvirt
import (
"fmt"
"log"
"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 TestAccLibvirtCloudInit_CreateCloudInitDiskAndUpdate(t *testing.T) {
var volume libvirt.StorageVol
randomResourceName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
// this structs are contents values we expect.
expectedContents := Expected{UserData: "#cloud-config", NetworkConfig: "network:", MetaData: "instance-id: bamboo"}
expectedContents2 := Expected{UserData: "#cloud-config2", NetworkConfig: "network2:", MetaData: "instance-id: bamboo2"}
expectedContentsEmpty := Expected{UserData: "#cloud-config2", NetworkConfig: "", MetaData: ""}
randomIsoName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) + ".iso"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(s *terraform.State) error {
return nil
},
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_cloudinit_disk" "%s" {
name = "%s"
user_data = "#cloud-config"
meta_data = "instance-id: bamboo"
network_config = "network:"
pool = "${libvirt_pool.%s.name}"
}`, randomPoolName, randomPoolName, randomPoolPath, randomResourceName, randomIsoName, randomPoolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"libvirt_cloudinit_disk."+randomResourceName, "name", randomIsoName),
testAccCheckCloudInitVolumeExists("libvirt_cloudinit_disk."+randomResourceName, &volume),
expectedContents.testAccCheckCloudInitDiskFilesContent("libvirt_cloudinit_disk."+randomResourceName, &volume),
),
},
{
Config: fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_cloudinit_disk" "%s" {
name = "%s"
user_data = "#cloud-config2"
meta_data = "instance-id: bamboo2"
network_config = "network2:"
pool = "${libvirt_pool.%s.name}"
}`, randomPoolName, randomPoolName, randomPoolPath, randomResourceName, randomIsoName, randomPoolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"libvirt_cloudinit_disk."+randomResourceName, "name", randomIsoName),
testAccCheckCloudInitVolumeExists("libvirt_cloudinit_disk."+randomResourceName, &volume),
expectedContents2.testAccCheckCloudInitDiskFilesContent("libvirt_cloudinit_disk."+randomResourceName, &volume),
),
},
{
Config: fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_cloudinit_disk" "%s" {
name = "%s"
user_data = "#cloud-config2"
pool = "${libvirt_pool.%s.name}"
}`, randomPoolName, randomPoolName, randomPoolPath, randomResourceName, randomIsoName, randomPoolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"libvirt_cloudinit_disk."+randomResourceName, "name", randomIsoName),
testAccCheckCloudInitVolumeExists("libvirt_cloudinit_disk."+randomResourceName, &volume),
expectedContentsEmpty.testAccCheckCloudInitDiskFilesContent("libvirt_cloudinit_disk."+randomResourceName, &volume),
),
},
// when we apply 2 times with same conf, we should not have a diff. See bug:
// https://github.com/dmacvicar/terraform-provider-libvirt/issues/313
{
Config: fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_cloudinit_disk" "%s" {
name = "%s"
user_data = "#cloud-config4"
pool = "${libvirt_pool.%s.name}"
}`, randomPoolName, randomPoolName, randomPoolPath, randomResourceName, randomIsoName, randomPoolName),
ExpectNonEmptyPlan: true,
PlanOnly: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"libvirt_cloudinit_disk."+randomResourceName, "name", randomIsoName),
testAccCheckCloudInitVolumeExists("libvirt_cloudinit_disk."+randomResourceName, &volume),
expectedContentsEmpty.testAccCheckCloudInitDiskFilesContent("libvirt_cloudinit_disk."+randomResourceName, &volume),
),
},
},
})
}
// The destroy function should always handle the case where the resource might already be destroyed
// (manually, for example). If the resource is already destroyed, this should not return an error.
// This allows Terraform users to manually delete resources without breaking Terraform.
// This test should fail without a proper "Exists" implementation
func TestAccLibvirtCloudInit_ManuallyDestroyed(t *testing.T) {
var volume libvirt.StorageVol
randomResourceName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
testAccCheckLibvirtCloudInitConfigBasic := fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}
resource "libvirt_cloudinit_disk" "%s" {
name = "%s"
pool = "${libvirt_pool.%s.name}"
user_data = "#cloud-config\nssh_authorized_keys: []\n"
}`, randomPoolName, randomPoolName, randomPoolPath, randomResourceName, randomResourceName, randomPoolName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(s *terraform.State) error {
return nil
},
Steps: []resource.TestStep{
{
Config: testAccCheckLibvirtCloudInitConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudInitVolumeExists("libvirt_cloudinit_disk."+randomResourceName, &volume),
),
},
{
Config: testAccCheckLibvirtCloudInitConfigBasic,
Destroy: true,
PreConfig: func() {
client := testAccProvider.Meta().(*Client)
if volume.Key == "" {
t.Fatalf("Key is blank")
}
if err := volumeDelete(client, volume.Key); err != nil {
t.Fatal(err)
}
},
},
},
})
}
func testAccCheckCloudInitVolumeExists(volumeName string, volume *libvirt.StorageVol) resource.TestCheckFunc {
return func(state *terraform.State) error {
virConn := testAccProvider.Meta().(*Client).libvirt
rs, err := getResourceFromTerraformState(volumeName, state)
if err != nil {
return err
}
cikey, err := getCloudInitVolumeKeyFromTerraformID(rs.Primary.ID)
if err != nil {
return err
}
retrievedVol, err := virConn.StorageVolLookupByKey(cikey)
if err != nil {
return err
}
if retrievedVol.Key == "" {
return fmt.Errorf("UUID is blank")
}
if retrievedVol.Key != cikey {
log.Printf("[DEBUG]: retrievedVol.Key is: %s \ncloudinit key is %s", retrievedVol.Key, cikey)
return fmt.Errorf("Resource ID and cloudinit volume key does not match")
}
*volume = retrievedVol
return nil
}
}
// this is helper method for test expected values
type Expected struct {
UserData, NetworkConfig, MetaData string
}
func (expected *Expected) testAccCheckCloudInitDiskFilesContent(volumeName string, volume *libvirt.StorageVol) resource.TestCheckFunc {
return func(state *terraform.State) error {
virConn := testAccProvider.Meta().(*Client).libvirt
rs, err := getResourceFromTerraformState(volumeName, state)
if err != nil {
return err
}
cloudInitDiskDef, err := newCloudInitDefFromRemoteISO(virConn, rs.Primary.ID)
if err != nil {
return err
}
if cloudInitDiskDef.MetaData != expected.MetaData {
return fmt.Errorf("metadata '%s' content differs from expected Metadata %s", cloudInitDiskDef.MetaData, expected.MetaData)
}
if cloudInitDiskDef.UserData != expected.UserData {
return fmt.Errorf("userdata '%s' content differs from expected UserData %s", cloudInitDiskDef.UserData, expected.UserData)
}
if cloudInitDiskDef.NetworkConfig != expected.NetworkConfig {
return fmt.Errorf("networkconfig '%s' content differs from expected NetworkConfigData %s", cloudInitDiskDef.NetworkConfig, expected.NetworkConfig)
}
return nil
}
}