-
Notifications
You must be signed in to change notification settings - Fork 3
/
hypervisor_test.go
464 lines (402 loc) · 13.4 KB
/
hypervisor_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package lochness_test
import (
"encoding/json"
"errors"
"os"
"testing"
"time"
"github.com/mistifyio/lochness"
"github.com/mistifyio/lochness/internal/tests/common"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestHypervisor(t *testing.T) {
suite.Run(t, new(HypervisorSuite))
}
type HypervisorSuite struct {
common.Suite
}
func (s *HypervisorSuite) TestJSON() {
hypervisor, _ := s.NewHypervisorWithGuest()
hypervisorBytes, err := json.Marshal(hypervisor)
s.NoError(err)
hypervisorFromJSON := &lochness.Hypervisor{}
s.NoError(json.Unmarshal(hypervisorBytes, hypervisorFromJSON))
s.Equal(hypervisor.ID, hypervisorFromJSON.ID)
s.Equal(hypervisor.Metadata, hypervisorFromJSON.Metadata)
s.Equal(hypervisor.IP, hypervisorFromJSON.IP)
s.Equal(hypervisor.Netmask, hypervisorFromJSON.Netmask)
s.Equal(hypervisor.Gateway, hypervisorFromJSON.Gateway)
s.Equal(hypervisor.MAC, hypervisorFromJSON.MAC)
s.Equal(hypervisor.TotalResources, hypervisorFromJSON.TotalResources)
s.Equal(hypervisor.AvailableResources, hypervisorFromJSON.AvailableResources)
s.Equal(hypervisor.Config, hypervisorFromJSON.Config)
}
func (s *HypervisorSuite) TestNewHypervisor() {
hypervisor := s.Context.NewHypervisor()
s.NotNil(uuid.Parse(hypervisor.ID))
}
func (s *HypervisorSuite) TestHypervisor() {
hypervisor := s.NewHypervisor()
tests := []struct {
description string
ID string
expectedErr bool
}{
{"missing id", "", true},
{"invalid ID", "adf", true},
{"nonexistant ID", uuid.New(), true},
{"real ID", hypervisor.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
h, err := s.Context.Hypervisor(test.ID)
if test.expectedErr {
s.Error(err, msg("lookup should fail"))
s.Nil(h, msg("failure shouldn't return a hypervisor"))
} else {
s.NoError(err, msg("lookup should succeed"))
s.True(assert.ObjectsAreEqual(hypervisor, h), msg("success should return correct data"))
}
}
}
func (s *HypervisorSuite) TestRefresh() {
hypervisor, _ := s.NewHypervisorWithGuest()
hypervisorCopy := &lochness.Hypervisor{}
*hypervisorCopy = *hypervisor
_, _ = lochness.SetHypervisorID(hypervisor.ID)
hypervisor.TotalResources = lochness.Resources{
Memory: 42,
CPU: 42,
Disk: 42,
}
s.Require().NoError(hypervisor.Save())
s.NoError(hypervisorCopy.Refresh(), "refresh existing should succeed")
s.True(assert.ObjectsAreEqual(hypervisor, hypervisorCopy), "refresh should pull new data")
NewHypervisor := s.Context.NewHypervisor()
s.Error(NewHypervisor.Refresh(), "unsaved hypervisor refresh should fail")
}
func (s *HypervisorSuite) TestGetAndSetHypervisorID() {
// hostname only case will only pass if it's a uuid. Determine if this
// machine's hostname will pass.
hostname, _ := os.Hostname()
var hostnameExpectedErr bool
if uuid.Parse(hostname) == nil {
hostnameExpectedErr = true
hostname = ""
}
hypervisorID := lochness.GetHypervisorID()
tests := []struct {
description string
id string
env string
expectedID string
expectedErr bool
}{
{"hostname only", "", "", hostname, hostnameExpectedErr},
{"non-uuid id, no env", "asdf", "", "", true},
{"uuid id, no env", "4f2b89d8-79e1-4ee6-9ca6-4d41856b507b", "", "4f2b89d8-79e1-4ee6-9ca6-4d41856b507b", false},
{"no id, non-uuid env", "", "asdf", "", true},
{"no id, uuid id", "", "717265be-edcd-4131-9adf-1aae1852c9bd", "717265be-edcd-4131-9adf-1aae1852c9bd", false},
{"non-uuid id, non-uuid env", "asdf", "asdf", "", true},
{"uuid id, non-uuid env", "62d6b8ea-66de-4061-99ff-aa3792968c0d", "asdf", "62d6b8ea-66de-4061-99ff-aa3792968c0d", false},
{"non-uuid id, uuid env", "asdf", "ad126074-69c6-41a3-8cb6-d6b71947c74c", "", true},
{"uuid id, uuid env", "08270977-3cf2-4fb9-89e4-a7ca7435aa8c", "769bf032-2cfb-47ee-8e28-9d6aa856eca6", "08270977-3cf2-4fb9-89e4-a7ca7435aa8c", false},
}
for _, test := range tests {
msg := s.Messager(test.description)
_ = os.Setenv("HYPERVISOR_ID", test.env)
id, err := lochness.SetHypervisorID(test.id)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Empty(id, msg("should return empty id"))
s.Equal(hypervisorID, lochness.GetHypervisorID(), msg("should not update the hypervisorID"))
} else {
s.NoError(err, msg("should succeed"))
s.Equal(test.expectedID, id, msg("should return expected id"))
hypervisorID = lochness.GetHypervisorID()
s.Equal(test.expectedID, hypervisorID, msg("should update the hypervisorID"))
}
}
}
func (s *HypervisorSuite) TestVerifyOnHV() {
hypervisor := s.NewHypervisor()
s.Error(hypervisor.VerifyOnHV())
_, _ = lochness.SetHypervisorID(hypervisor.ID)
s.NoError(hypervisor.VerifyOnHV())
}
func (s *HypervisorSuite) TestUpdateResources() {
hypervisor, guest := s.NewHypervisorWithGuest()
_ = hypervisor.SetConfig("guestDiskDir", "/")
flavor, _ := s.Context.Flavor(guest.FlavorID)
_, _ = lochness.SetHypervisorID(hypervisor.ID)
// Reset Resources
hypervisor.TotalResources = lochness.Resources{}
hypervisor.AvailableResources = lochness.Resources{}
s.NoError(hypervisor.UpdateResources())
tr := hypervisor.TotalResources
ar := hypervisor.AvailableResources
s.NotEqual(0, tr.Memory)
s.NotEqual(0, tr.Disk)
s.NotEqual(0, tr.CPU)
s.Equal(tr.Memory-flavor.Memory, ar.Memory)
s.Equal(tr.Disk-flavor.Disk, ar.Disk)
// Total and available CPU are currently equal
s.Equal(tr.CPU, ar.CPU)
loadedHypervisor, _ := s.Context.Hypervisor(hypervisor.ID)
tr = loadedHypervisor.TotalResources
ar = loadedHypervisor.AvailableResources
s.True(assert.ObjectsAreEqual(hypervisor.TotalResources, loadedHypervisor.TotalResources))
s.True(assert.ObjectsAreEqual(hypervisor.AvailableResources, loadedHypervisor.AvailableResources))
}
func (s *HypervisorSuite) TestValidate() {
tests := []struct {
description string
ID string
expectedErr bool
}{
{"missing ID", "", true},
{"non uuid ID", "asdf", true},
{"uuid ID", uuid.New(), false},
}
for _, test := range tests {
msg := s.Messager(test.description)
h := &lochness.Hypervisor{ID: test.ID}
err := h.Validate()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *HypervisorSuite) TestSave() {
goodHypervisor := s.Context.NewHypervisor()
clobberHypervisor := *goodHypervisor
tests := []struct {
description string
hypervisor *lochness.Hypervisor
expectedErr bool
}{
{"invalid hypervisor", &lochness.Hypervisor{}, true},
{"valid hypervisor", goodHypervisor, false},
{"existing hypervisor", goodHypervisor, false},
{"existing hypervisor clobber", &clobberHypervisor, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.hypervisor.Save()
if test.expectedErr {
s.Error(err, msg("should fail"))
} else {
s.NoError(err, msg("should succeed"))
}
}
}
func (s *HypervisorSuite) TestAddSubnet() {
tests := []struct {
description string
Hypervisor *lochness.Hypervisor
subnet *lochness.Subnet
expectedErr bool
}{
{"nonexisting Hypervisor, nonexisting subnet", s.Context.NewHypervisor(), s.Context.NewSubnet(), true},
{"existing Hypervisor, nonexisting subnet", s.NewHypervisor(), s.Context.NewSubnet(), true},
{"nonexisting Hypervisor, existing subnet", s.Context.NewHypervisor(), s.NewSubnet(), true},
{"existing Hypervisor and subnet", s.NewHypervisor(), s.NewSubnet(), false},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.Hypervisor.AddSubnet(test.subnet, "mistify0")
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Len(test.Hypervisor.Subnets(), 0, msg("fail should not add subnet to Hypervisor"))
} else {
s.NoError(err, msg("should succeed"))
s.Len(test.Hypervisor.Subnets(), 1, msg("fail should add subnet to Hypervisor"))
}
}
}
func (s *HypervisorSuite) TestRemoveSubnet() {
subnet := s.NewSubnet()
hypervisor := s.NewHypervisor()
_ = hypervisor.AddSubnet(subnet, "mistify0")
tests := []struct {
description string
h *lochness.Hypervisor
s *lochness.Subnet
len int
}{
{"nonexisting hypervisor, nonexisting subnet", s.Context.NewHypervisor(), s.Context.NewSubnet(), 0},
{"existing hypervisor, nonexisting subnet", hypervisor, s.Context.NewSubnet(), 1},
{"nonexisting hypervisor, existing subnet", s.Context.NewHypervisor(), subnet, 0},
{"existing hypervisor and subnet", hypervisor, subnet, 0},
}
for _, test := range tests {
msg := s.Messager(test.description)
s.NoError(test.h.RemoveSubnet(test.s))
s.Len(test.h.Subnets(), test.len, msg("subnet mismatch: exp: %d got: %d", test.len, len(test.h.Subnets())))
}
}
func (s *HypervisorSuite) TestSubnets() {
hypervisor := s.NewHypervisor()
_ = hypervisor.AddSubnet(s.NewSubnet(), "mistify0")
s.Len(hypervisor.Subnets(), 1)
}
func (s *HypervisorSuite) TestHeartbeatAndIsAlive() {
hypervisor := s.NewHypervisor()
s.Error(hypervisor.Heartbeat(60 * time.Second))
s.False(hypervisor.IsAlive())
_, _ = lochness.SetHypervisorID(hypervisor.ID)
s.NoError(hypervisor.Heartbeat(60 * time.Second))
s.True(hypervisor.IsAlive())
}
func (s *HypervisorSuite) TestAddGuest() {
guest := s.NewGuest()
hypervisor := s.NewHypervisor()
subnet := s.NewSubnet()
network, _ := s.Context.Network(guest.NetworkID)
_ = network.AddSubnet(subnet)
_ = hypervisor.AddSubnet(subnet, "mistify0")
tests := []struct {
description string
guest *lochness.Guest
hypervisor *lochness.Hypervisor
expectedErr bool
}{
{"neither exist", s.Context.NewGuest(), s.Context.NewHypervisor(), true},
{"guest exist", s.NewGuest(), s.Context.NewHypervisor(), true},
{"hypervisor exist", s.Context.NewGuest(), s.NewHypervisor(), true},
{"both exist, wrong network", s.NewGuest(), s.NewHypervisor(), true},
{"both exist, right network", guest, hypervisor, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.hypervisor.AddGuest(test.guest)
if test.expectedErr {
s.Error(err, msg("should fail"))
s.Empty(test.guest.HypervisorID, msg("should not set hypervisor id"))
s.Len(test.hypervisor.Guests(), 0, msg("should not add to guest list"))
} else {
s.NoError(err, msg("should pass"))
s.Equal(test.hypervisor.ID, test.guest.HypervisorID, msg("should set hypervisor id"))
s.Len(test.hypervisor.Guests(), 1, msg("should add to guest list"))
}
}
}
func (s *HypervisorSuite) TestRemoveGuest() {
hypervisor, guest := s.NewHypervisorWithGuest()
s.Error(hypervisor.RemoveGuest(s.NewGuest()))
s.Equal(hypervisor.ID, guest.HypervisorID)
s.Len(hypervisor.Guests(), 1)
s.NoError(hypervisor.RemoveGuest(guest))
s.Empty(guest.HypervisorID)
s.Len(hypervisor.Guests(), 0)
}
func (s *HypervisorSuite) TestGuests() {
hypervisor, guest := s.NewHypervisorWithGuest()
guests := hypervisor.Guests()
s.Len(guests, 1)
s.Equal(guest.ID, guests[0])
}
func (s *HypervisorSuite) TestForEachGuest() {
hypervisor, guest1 := s.NewHypervisorWithGuest()
guest2 := s.NewGuest()
guest2.NetworkID = guest1.NetworkID
_ = hypervisor.AddGuest(guest2)
expectedFound := map[string]bool{
guest1.ID: true,
guest2.ID: true,
}
resultFound := make(map[string]bool)
err := hypervisor.ForEachGuest(func(g *lochness.Guest) error {
resultFound[g.ID] = true
return nil
})
s.NoError(err)
s.True(assert.ObjectsAreEqual(expectedFound, resultFound))
returnErr := errors.New("an error")
err = hypervisor.ForEachGuest(func(g *lochness.Guest) error {
return returnErr
})
s.Error(err)
s.Equal(returnErr, err)
}
func (s *HypervisorSuite) TestFirstHypervisor() {
_ = s.NewHypervisor()
_ = s.NewHypervisor()
h, err := s.Context.FirstHypervisor(func(h *lochness.Hypervisor) bool {
return true
})
s.NoError(err)
s.NotNil(h)
}
func (s *HypervisorSuite) TestForEachHypervisor() {
hypervisor := s.NewHypervisor()
hypervisor2 := s.NewHypervisor()
expectedFound := map[string]bool{
hypervisor.ID: true,
hypervisor2.ID: true,
}
resultFound := make(map[string]bool)
err := s.Context.ForEachHypervisor(func(h *lochness.Hypervisor) error {
resultFound[h.ID] = true
return nil
})
s.NoError(err)
s.True(assert.ObjectsAreEqual(expectedFound, resultFound))
returnErr := errors.New("an error")
err = s.Context.ForEachHypervisor(func(h *lochness.Hypervisor) error {
return returnErr
})
s.Error(err)
s.Equal(returnErr, err)
}
func (s *HypervisorSuite) TestSetConfig() {
hypervisor := s.NewHypervisor()
tests := []struct {
description string
key string
value string
expectedErr bool
}{
{"empty key", "", "bar", true},
{"empty value", "bar", "", false},
{"key and value", "foo", "bar", false},
{"already set", "foo", "baz", false},
{"nested key", "foobar/baz", "bang", false},
}
for _, test := range tests {
err := hypervisor.SetConfig(test.key, test.value)
if test.expectedErr {
s.Error(err, test.description)
} else {
s.NoError(err, test.description)
}
}
}
func (s *HypervisorSuite) TestDestroy() {
blank := s.Context.NewHypervisor()
blank.ID = ""
hypervisorWithGuest, _ := s.NewHypervisorWithGuest()
tests := []struct {
description string
h *lochness.Hypervisor
expectedErr bool
}{
{"invalid hypervisor", blank, true},
{"existing hypervisor", s.NewHypervisor(), false},
{"nonexistant hypervisor", s.Context.NewHypervisor(), true},
{"hypervisor with guest", hypervisorWithGuest, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.h.Destroy()
if test.expectedErr {
s.Error(err, msg("should fail"))
} else {
s.NoError(err, msg("should succeed"))
}
}
}