-
Notifications
You must be signed in to change notification settings - Fork 3
/
guest_test.go
321 lines (281 loc) · 7.78 KB
/
guest_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
package lochness_test
import (
"encoding/json"
"errors"
"fmt"
"net"
"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 TestGuest(t *testing.T) {
suite.Run(t, new(GuestSuite))
}
type GuestSuite struct {
common.Suite
}
func (s *GuestSuite) TestJSON() {
guest := s.NewGuest()
guestBytes, err := json.Marshal(guest)
s.NoError(err)
guestFromJSON := &lochness.Guest{}
s.NoError(json.Unmarshal(guestBytes, guestFromJSON))
s.Equal(guest.MAC, guestFromJSON.MAC)
s.Equal(guest.IP, guestFromJSON.IP)
}
func (s *GuestSuite) TestNewGuest() {
guest := s.Context.NewGuest()
s.NotNil(uuid.Parse(guest.ID))
}
func (s *GuestSuite) TestGuest() {
guest := s.NewGuest()
tests := []struct {
description string
ID string
expectedErr bool
}{
{"missing id", "", true},
{"invalid ID", "adf", true},
{"nonexistant ID", uuid.New(), true},
{"real ID", guest.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
g, err := s.Context.Guest(test.ID)
if test.expectedErr {
s.Error(err, msg("lookup should fail"))
s.Nil(g, msg("failure shouldn't return a guest"))
} else {
s.NoError(err, msg("lookup should succeed"))
s.True(assert.ObjectsAreEqual(guest, g), msg("success should return correct data"))
}
}
}
func (s *GuestSuite) TestRefresh() {
guest := s.NewGuest()
guestCopy := &lochness.Guest{}
*guestCopy = *guest
_ = guest.Save()
s.NoError(guestCopy.Refresh(), "refresh existing should succeed")
s.True(assert.ObjectsAreEqual(guest, guestCopy), "refresh should pull new data")
NewGuest := s.Context.NewGuest()
s.Error(NewGuest.Refresh(), "unsaved guest refresh should fail")
}
func (s *GuestSuite) TestValidate() {
mac, _ := net.ParseMAC("4C:3F:B1:7E:54:64")
tests := []struct {
description string
id string
flavor string
network string
mac net.HardwareAddr
expectedErr bool
}{
{"missing id", "", uuid.New(), uuid.New(), mac, true},
{"non uuid id", "asdf", uuid.New(), uuid.New(), mac, true},
{"missing flavor", uuid.New(), "", uuid.New(), mac, true},
{"non uuid flavor", uuid.New(), "asdf", uuid.New(), mac, true},
{"missing subnet", uuid.New(), uuid.New(), "", mac, true},
{"non uuid subnet", uuid.New(), uuid.New(), "", mac, true},
{"missing mac", uuid.New(), uuid.New(), uuid.New(), nil, true},
{"all uuid", uuid.New(), uuid.New(), uuid.New(), mac, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
g := &lochness.Guest{
ID: test.id,
FlavorID: test.flavor,
NetworkID: test.network,
MAC: test.mac,
}
err := g.Validate()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *GuestSuite) TestSave() {
goodGuest := s.Context.NewGuest()
flavor := s.NewFlavor()
network := s.NewNetwork()
mac, _ := net.ParseMAC("4C:3F:B1:7E:54:64")
goodGuest.FlavorID = flavor.ID
goodGuest.NetworkID = network.ID
goodGuest.MAC = mac
clobberGuest := *goodGuest
tests := []struct {
description string
guest *lochness.Guest
expectedErr bool
}{
{"invalid guest", &lochness.Guest{}, true},
{"valid guest", goodGuest, false},
{"existing guest", goodGuest, false},
{"existing guest clobber", &clobberGuest, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.guest.Save()
if test.expectedErr {
s.Error(err, msg("should fail"))
} else {
s.NoError(err, msg("should succeed"))
}
}
}
func (s *GuestSuite) TestDestroy() {
blank := s.Context.NewGuest()
blank.ID = ""
hypervisor, guest := s.NewHypervisorWithGuest()
tests := []struct {
description string
g *lochness.Guest
expectedErr bool
}{
{"invalid guest", blank, true},
{"existing guest not on hypervisor", s.NewGuest(), false},
{"existing guest on hypervisor", guest, false},
{"nonexistant guest", s.Context.NewGuest(), true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.g.Destroy()
if test.expectedErr {
s.Error(err, msg("should fail"))
} else {
s.NoError(err, msg("should succeed"))
if test.g.HypervisorID != "" {
_ = hypervisor.Refresh()
s.Len(hypervisor.Guests(), 0, msg("should have been removed from hypervisor"))
}
}
}
}
func (s *GuestSuite) TestCandidates() {
guest := s.NewGuest()
subnet := s.NewSubnet()
network, _ := s.Context.Network(guest.NetworkID)
_ = network.AddSubnet(subnet)
hypervisors := lochness.Hypervisors{
s.NewHypervisor(), // Not alive
s.NewHypervisor(), // No correct subnet
s.NewHypervisor(), // Not enough resources
s.NewHypervisor(), // Everything
}
for i := 0; i < len(hypervisors); i++ {
if i != 0 {
_, _ = lochness.SetHypervisorID(hypervisors[i].ID)
_ = hypervisors[i].Heartbeat(60 * time.Second)
}
if i != 1 {
_ = hypervisors[i].AddSubnet(subnet, "mistify0")
}
if i == 2 {
hypervisors[i].AvailableResources = lochness.Resources{}
_ = hypervisors[i].Save()
}
}
candidates, err := guest.Candidates(
lochness.CandidateIsAlive,
lochness.CandidateHasResources,
lochness.CandidateHasSubnet,
)
s.NoError(err)
s.Len(candidates, 1)
s.Equal(hypervisors[3].ID, candidates[0].ID)
}
func (s *GuestSuite) TestCandidateIsAlive() {
guest := s.NewGuest()
hypervisors := lochness.Hypervisors{
s.NewHypervisor(),
s.NewHypervisor(),
}
_, err := lochness.SetHypervisorID(hypervisors[1].ID)
s.Require().NoError(err)
s.Require().NoError(hypervisors[1].Heartbeat(60 * time.Second))
candidates, err := lochness.CandidateIsAlive(guest, hypervisors)
s.NoError(err)
s.Len(candidates, 1)
s.Equal(hypervisors[1].ID, candidates[0].ID)
}
func (s *GuestSuite) TestCandidateHasResources() {
guest := s.NewGuest()
hypervisors := lochness.Hypervisors{
s.NewHypervisor(),
s.NewHypervisor(),
}
hypervisors[0].AvailableResources = lochness.Resources{}
candidates, err := lochness.CandidateHasResources(guest, hypervisors)
s.NoError(err)
s.Len(candidates, 1)
s.Equal(hypervisors[1].ID, candidates[0].ID)
}
func (s *GuestSuite) TestCandidateHasSubnet() {
hypervisor, guest := s.NewHypervisorWithGuest()
hypervisors := lochness.Hypervisors{
s.NewHypervisor(),
hypervisor,
}
candidates, err := lochness.CandidateHasSubnet(guest, hypervisors)
s.NoError(err)
s.Len(candidates, 1)
s.Equal(hypervisors[1].ID, candidates[0].ID)
}
func (s *GuestSuite) TestCandidateRandomize() {
guest := s.Context.NewGuest()
candidates := make(lochness.Hypervisors, 10)
for i := 0; i < cap(candidates); i++ {
candidates[i] = s.Context.NewHypervisor()
}
randCandidates, err := lochness.CandidateRandomize(guest, candidates)
if !s.NoError(err) {
return
}
if !s.Len(randCandidates, len(candidates)) {
return
}
different := 0
for i, candidate := range candidates {
var found bool
for j, randCandidate := range randCandidates {
if candidate.ID == randCandidate.ID {
found = true
break
}
if i != j {
different++
}
}
s.True(found, "original entry should be in randomized list")
}
if different < len(candidates)/2 {
fmt.Printf("TestCandidateRandomize: Only %d of %d elements were in different location", different, len(candidates))
}
}
func (s *GuestSuite) TestForEachGuest() {
guest := s.NewGuest()
guest2 := s.NewGuest()
expectedFound := map[string]bool{
guest.ID: true,
guest2.ID: true,
}
resultFound := make(map[string]bool)
err := s.Context.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 = s.Context.ForEachGuest(func(g *lochness.Guest) error {
return returnErr
})
s.Error(err)
s.Equal(returnErr, err)
}