This repository was archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathinstance_test.go
296 lines (240 loc) · 7.69 KB
/
instance_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package instance // import "github.com/docker/infrakit/pkg/provider/libvirt/plugin/instance"
import (
"testing"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
"github.com/libvirt/libvirt-go"
"github.com/libvirt/libvirt-go-xml"
"github.com/stretchr/testify/require"
)
const libvirtDrvTest = "test:///default"
// checks if a domain with the given name exists from libvirt's PoV
func domainExists(t *testing.T, conn *libvirt.Connect, id string) bool {
doms, err := conn.ListAllDomains(libvirt.CONNECT_LIST_DOMAINS_ACTIVE)
require.NoError(t, err)
for _, d := range doms {
info, err := d.GetInfo()
require.NoError(t, err)
if info.State != libvirt.DOMAIN_RUNNING {
continue
}
name, err := d.GetName()
require.NoError(t, err)
if name == id {
return true
}
}
return false
}
func findInstance(t *testing.T, plugin instance.Plugin, id instance.ID) *instance.Description {
descrs, err := plugin.DescribeInstances(map[string]string{}, true)
require.NoError(t, err)
for _, d := range descrs {
if d.ID == id {
return &d
}
}
return nil
}
// checks if an instance exists from the plugin's PoV
func instanceExists(t *testing.T, plugin instance.Plugin, id instance.ID) bool {
return findInstance(t, plugin, id) != nil
}
func TestInvalidProperties(t *testing.T) {
conn, err := libvirt.NewConnect(libvirtDrvTest)
require.NoError(t, err)
defer conn.Close()
plugin := NewLibvirtPlugin(libvirtDrvTest)
require.NotNil(t, plugin)
props := types.Any(`this is not valid JSON`)
logicalID := instance.LogicalID("")
_, err = plugin.Provision(instance.Spec{
Properties: &props,
Tags: map[string]string{},
Init: "",
LogicalID: &logicalID,
Attachments: []instance.Attachment{},
})
require.Error(t, err)
}
func TestBasicLifecycle(t *testing.T) {
conn, err := libvirt.NewConnect(libvirtDrvTest)
require.NoError(t, err)
defer conn.Close()
plugin := NewLibvirtPlugin(libvirtDrvTest)
require.NotNil(t, plugin)
props := types.Any(`{
"Domain": {
"Type": "test",
"VCPU": {
"Value": 1
},
"Memory" : {
"Unit": "MiB",
"Value": 1024
},
"OS": {
"Type": {
"Type": "hvm"
},
"Kernel": "/foo/bar/Image"
},
"Devices": {
"Interfaces": [
{
"Type": "bridge",
"Source": {
"Bridge": "virbr0"
}
}
]
}
}
}`)
logicalID := instance.LogicalID("da:37:60:0c:e2:fe")
id, err := plugin.Provision(instance.Spec{
Properties: &props,
Tags: map[string]string{},
Init: "",
LogicalID: &logicalID,
Attachments: []instance.Attachment{},
})
require.NoError(t, err)
require.NotNil(t, id)
require.True(t, domainExists(t, conn, string(*id)), "domain was not found in libvirt domains")
require.True(t, instanceExists(t, plugin, *id), "domain was not found in plugin instances")
inst := findInstance(t, plugin, *id)
require.NotNil(t, inst)
require.Equal(t, logicalID, *inst.LogicalID)
err = plugin.Destroy(*id, instance.Termination)
require.NoError(t, err)
require.False(t, domainExists(t, conn, string(*id)), "domain was found in libvirt domains after destroy")
require.False(t, instanceExists(t, plugin, *id), "domain was found in plugin instances after destroy")
}
// TestNoDevices checks that we do not crash if props.Domain.Devices is not populated.
func TestNoDevices(t *testing.T) {
conn, err := libvirt.NewConnect(libvirtDrvTest)
require.NoError(t, err)
defer conn.Close()
plugin := NewLibvirtPlugin(libvirtDrvTest)
require.NotNil(t, plugin)
props := types.Any(`{
"Domain": {
"Type": "test",
"VCPU": {
"Value": 1
},
"Memory" : {
"Unit": "MiB",
"Value": 1024
},
"OS": {
"Type": {
"Type": "hvm"
},
"Kernel": "/foo/bar/Image"
}
}
}`)
logicalID := instance.LogicalID("d2:60:fb:f5:bd:44")
id, err := plugin.Provision(instance.Spec{
Properties: &props,
Tags: map[string]string{},
Init: "",
LogicalID: &logicalID,
Attachments: []instance.Attachment{},
})
require.NoError(t, err)
require.NotNil(t, id)
require.True(t, domainExists(t, conn, string(*id)), "domain was not found in libvirt domains")
require.True(t, instanceExists(t, plugin, *id), "domain was not found in plugin instances")
inst := findInstance(t, plugin, *id)
require.NotNil(t, inst)
require.Equal(t, logicalID, *inst.LogicalID)
err = plugin.Destroy(*id, instance.Termination)
require.NoError(t, err)
require.False(t, domainExists(t, conn, string(*id)), "domain was found in libvirt domains after destroy")
require.False(t, instanceExists(t, plugin, *id), "domain was found in plugin instances after destroy")
}
// TestLogicalIDNotMAC checks that we do not fail if LogicalID happens to not be a MAC address
func TestLogicalIDNotMAC(t *testing.T) {
conn, err := libvirt.NewConnect(libvirtDrvTest)
require.NoError(t, err)
defer conn.Close()
plugin := NewLibvirtPlugin(libvirtDrvTest)
require.NotNil(t, plugin)
props := types.Any(`{
"Domain": {
"Type": "test",
"VCPU": {
"Value": 1
},
"Memory" : {
"Unit": "MiB",
"Value": 1024
},
"OS": {
"Type": {
"Type": "hvm"
},
"Kernel": "/foo/bar/Image"
}
}
}`)
logicalID := instance.LogicalID("da:37:60:0c:e2:fe")
id, err := plugin.Provision(instance.Spec{
Properties: &props,
Tags: map[string]string{},
Init: "",
LogicalID: &logicalID,
Attachments: []instance.Attachment{},
})
require.NoError(t, err)
require.NotNil(t, id)
require.True(t, domainExists(t, conn, string(*id)), "domain was not found in libvirt domains")
require.True(t, instanceExists(t, plugin, *id), "domain was not found in plugin instances")
inst := findInstance(t, plugin, *id)
require.NotNil(t, inst)
require.Equal(t, logicalID, *inst.LogicalID)
err = plugin.Destroy(*id, instance.Termination)
require.NoError(t, err)
require.False(t, domainExists(t, conn, string(*id)), "domain was found in libvirt domains after destroy")
require.False(t, instanceExists(t, plugin, *id), "domain was found in plugin instances after destroy")
}
func TestLabel(t *testing.T) {
conn, err := libvirt.NewConnect(libvirtDrvTest)
require.NoError(t, err)
defer conn.Close()
domname := "test-instanceXXXX"
domcfg := libvirtxml.Domain{
Type: "test",
VCPU: &libvirtxml.DomainVCPU{Value: 1},
Memory: &libvirtxml.DomainMemory{Value: 1024, Unit: "MiB"},
Name: "test-instanceXXXX",
OS: &libvirtxml.DomainOS{
Type: &libvirtxml.DomainOSType{
Type: "hvm",
},
Kernel: "/foo/bar/Kernel",
Initrd: "/foo/bar/Ramdisk",
KernelArgs: "root=/dev/sda2",
},
}
xmldoc, err := domcfg.Marshal()
require.NoError(t, err)
_, err = conn.DomainCreateXML(string(xmldoc), 0)
require.NoError(t, err)
require.True(t, domainExists(t, conn, domname), "domain was not found in libvirt domains")
plugin := NewLibvirtPlugin(libvirtDrvTest)
require.NotNil(t, plugin)
require.False(t, instanceExists(t, plugin, instance.ID(domname)), "domain was found in plugin instances before label")
labels := map[string]string{
"foo": "bar",
"foo2": "bar",
}
err = plugin.Label(instance.ID(domname), labels)
require.NoError(t, err)
require.True(t, instanceExists(t, plugin, instance.ID(domname)), "domain was not found in plugin instances after label")
inst := findInstance(t, plugin, instance.ID(domname))
require.Equal(t, labels, inst.Tags)
}