-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_test.go
454 lines (429 loc) · 12.3 KB
/
collection_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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"github.com/juju/mgo/v3"
"github.com/juju/mgo/v3/bson"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/state"
"github.com/juju/juju/testing/factory"
)
type collectionSuite struct {
ConnSuite
}
var _ = gc.Suite(&collectionSuite{})
type collectionTestCase struct {
label string
test func() (int, error)
expectedCount int
expectedPanic string
expectedError string
}
func (s *collectionSuite) TestGenericStateCollection(c *gc.C) {
// The users collection does not require filtering by model UUID.
coll, closer := state.GetCollection(s.State, state.UsersC)
defer closer()
c.Check(coll.Name(), gc.Equals, state.UsersC)
s.Factory.MakeUser(c, &factory.UserParams{Name: "foo", DisplayName: "Ms Foo"})
s.Factory.MakeUser(c, &factory.UserParams{Name: "bar"})
collSnapshot := newCollectionSnapshot(c, coll.Writeable().Underlying())
for i, t := range []collectionTestCase{
{
label: "Count",
test: func() (int, error) {
return coll.Count()
},
expectedCount: 3,
},
{
label: "FindId",
test: func() (int, error) {
return coll.FindId("foo").Count()
},
expectedCount: 1,
},
{
label: "Find with one result",
test: func() (int, error) {
return coll.Find(bson.D{{"displayname", "Ms Foo"}}).Count()
},
expectedCount: 1,
},
{
label: "Find with nil",
test: func() (int, error) {
return coll.Find(nil).Count()
},
expectedCount: 3,
},
{
label: "Insert",
test: func() (int, error) {
err := coll.Writeable().Insert(bson.D{{"_id", "more"}})
c.Assert(err, jc.ErrorIsNil)
return coll.Count()
},
expectedCount: 4,
},
{
label: "RemoveId",
test: func() (int, error) {
err := coll.Writeable().RemoveId("bar")
c.Assert(err, jc.ErrorIsNil)
return coll.Count()
},
expectedCount: 2,
},
{
label: "Remove",
test: func() (int, error) {
err := coll.Writeable().Remove(bson.D{{"displayname", "Ms Foo"}})
c.Assert(err, jc.ErrorIsNil)
return coll.Count()
},
expectedCount: 2,
},
{
label: "RemoveAll",
test: func() (int, error) {
_, err := coll.Writeable().RemoveAll(bson.D{{"createdby", s.Owner.Name()}})
c.Assert(err, jc.ErrorIsNil)
return coll.Count()
},
expectedCount: 0,
},
{
label: "Update",
test: func() (int, error) {
err := coll.Writeable().Update(bson.D{{"_id", "bar"}},
bson.D{{"$set", bson.D{{"displayname", "Updated Bar"}}}})
c.Assert(err, jc.ErrorIsNil)
return coll.Find(bson.D{{"displayname", "Updated Bar"}}).Count()
},
expectedCount: 1,
},
{
label: "UpdateId",
test: func() (int, error) {
err := coll.Writeable().UpdateId("bar",
bson.D{{"$set", bson.D{{"displayname", "Updated Bar"}}}})
c.Assert(err, jc.ErrorIsNil)
return coll.Find(bson.D{{"displayname", "Updated Bar"}}).Count()
},
expectedCount: 1,
},
} {
c.Logf("test %d: %s", i, t.label)
collSnapshot.restore(c)
count, err := t.test()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, t.expectedCount)
}
}
func (s *collectionSuite) TestModelStateCollection(c *gc.C) {
// The machines collection requires filtering by model UUID. Set up
// 2 models with machines in each.
m0 := s.Factory.MakeMachine(c, nil)
s.Factory.MakeMachine(c, nil)
st1 := s.Factory.MakeModel(c, nil)
defer st1.Close()
f1 := factory.NewFactory(st1, s.StatePool)
otherM0 := f1.MakeMachine(c, &factory.MachineParams{Base: state.UbuntuBase("22.04")})
// Ensure that the first machine in each model have overlapping ids
// (otherwise tests may not fail when they should)
c.Assert(m0.Id(), gc.Equals, otherM0.Id())
machines0, closer := state.GetCollection(s.State, state.MachinesC)
defer closer()
machines1, closer := state.GetCollection(st1, state.MachinesC)
defer closer()
machinesSnapshot := newCollectionSnapshot(c, machines0.Writeable().Underlying())
c.Assert(machines0.Name(), gc.Equals, state.MachinesC)
for i, t := range []collectionTestCase{
{
label: "Count filters by model",
test: func() (int, error) {
return machines0.Count()
},
expectedCount: 2,
},
{
label: "Find filters by model",
test: func() (int, error) {
return machines0.Find(bson.D{{"base.channel", m0.Base().Channel}}).Count()
},
expectedCount: 2,
},
{
label: "Find adds model UUID when _id is provided",
test: func() (int, error) {
return machines0.Find(bson.D{{"_id", m0.Id()}}).Count()
},
expectedCount: 1,
},
{
label: "Find tolerates model UUID prefix already being present",
test: func() (int, error) {
return machines0.Find(bson.D{
{"_id", state.DocID(s.State, m0.Id())},
}).Count()
},
expectedCount: 1,
},
{
label: "Find with no selector still filters by model",
test: func() (int, error) {
return machines0.Find(nil).Count()
},
expectedCount: 2,
},
{
label: "Find leaves _id alone if used with operator",
test: func() (int, error) {
return machines0.Find(bson.D{
{"_id", bson.D{{"$regex", ":" + m0.Id() + "$"}}},
}).Count()
},
expectedCount: 1, // not 2 because model-uuid filter is still added
},
{
label: "Find works with maps",
test: func() (int, error) {
return machines0.Find(map[string]string{"_id": m0.Id()}).Count()
},
expectedCount: 1,
},
{
label: "Find panics if model-uuid is included",
test: func() (int, error) {
machines0.Find(bson.D{{"model-uuid", "whatever"}})
return 0, nil
},
expectedPanic: "model-uuid is added automatically and should not be provided",
},
{
label: "FindId adds model UUID prefix",
test: func() (int, error) {
return machines0.FindId(m0.Id()).Count()
},
expectedCount: 1,
},
{
label: "FindId tolerates model UUID prefix already being there",
test: func() (int, error) {
return machines0.FindId(state.DocID(s.State, m0.Id())).Count()
},
expectedCount: 1,
},
{
label: "Insert adds model-uuid",
test: func() (int, error) {
err := machines0.Writeable().Insert(bson.D{
{"_id", state.DocID(s.State, "99")},
{"machineid", 99},
})
c.Assert(err, jc.ErrorIsNil)
return machines0.Count()
},
expectedCount: 3,
},
{
label: "Insert populates model-uuid if blank",
test: func() (int, error) {
err := machines0.Writeable().Insert(bson.D{
{"_id", state.DocID(s.State, "99")},
{"machineid", 99},
{"model-uuid", ""},
})
c.Assert(err, jc.ErrorIsNil)
return machines0.Count()
},
expectedCount: 3,
},
{
label: "Insert prefixes _id",
test: func() (int, error) {
err := machines0.Writeable().Insert(bson.D{
{"_id", "99"},
{"machineid", 99},
})
c.Assert(err, jc.ErrorIsNil)
return machines0.FindId("99").Count()
},
expectedCount: 1,
},
{
label: "Insert tolerates prefixed _id and correct model-uuid if provided",
test: func() (int, error) {
err := machines0.Writeable().Insert(bson.D{
{"_id", state.DocID(s.State, "99")},
{"machineid", 99},
{"model-uuid", s.State.ModelUUID()},
})
c.Assert(err, jc.ErrorIsNil)
return machines0.Count()
},
expectedCount: 3,
},
{
label: "Insert fails if model-uuid doesn't match",
test: func() (int, error) {
err := machines0.Writeable().Insert(bson.D{
{"_id", "99"},
{"machineid", 99},
{"model-uuid", "something-else"},
})
return 0, err
},
expectedError: "bad \"model-uuid\" value: .+",
},
{
label: "Remove adds model UUID prefix to _id",
test: func() (int, error) {
err := machines0.Writeable().Remove(bson.D{{"_id", "0"}})
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 2, // Expect machine-1 in first model and machine-0 in second model
},
{
label: "Remove filters by model",
test: func() (int, error) {
// Attempt to remove the trusty machine in the second
// model with the collection that's filtering for the
// first model - nothing should get removed.
err := machines0.Writeable().Remove(bson.D{{"base.channel", state.UbuntuBase("22.04").Channel}})
c.Assert(err, gc.ErrorMatches, "not found")
return s.machines.Count()
},
expectedCount: 3, // Expect all machines to still be there.
},
{
label: "Remove filters by model 2",
test: func() (int, error) {
err := machines0.Writeable().Remove(bson.D{{"machineid", "0"}})
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 2, // Expect machine 1 in first model and machine-0 in second model
},
{
label: "RemoveId adds model UUID prefix",
test: func() (int, error) {
err := machines0.Writeable().RemoveId(m0.Id())
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 2, // Expect machine-1 in first model and machine-0 in second model
},
{
label: "RemoveId tolerates model UUID prefix already being there",
test: func() (int, error) {
err := machines0.Writeable().RemoveId(state.DocID(s.State, m0.Id()))
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 2, // Expect machine-1 in first model and machine-0 in second model
},
{
label: "RemoveAll filters by model",
test: func() (int, error) {
_, err := machines0.Writeable().RemoveAll(bson.D{{"base.channel", m0.Base().Channel}})
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 1, // Expect machine-1 in second model
},
{
label: "RemoveAll adds model UUID when _id is provided",
test: func() (int, error) {
_, err := machines0.Writeable().RemoveAll(bson.D{{"_id", m0.Id()}})
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 2, // Expect machine-1 in first model and machine-0 in second model
},
{
label: "RemoveAll tolerates model UUID prefix already being present",
test: func() (int, error) {
_, err := machines0.Writeable().RemoveAll(bson.D{
{"_id", state.DocID(s.State, m0.Id())},
})
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 2, // Expect machine-1 in first model and machine-0 in second model
},
{
label: "RemoveAll with no selector still filters by model",
test: func() (int, error) {
_, err := machines0.Writeable().RemoveAll(nil)
c.Assert(err, jc.ErrorIsNil)
return s.machines.Count()
},
expectedCount: 1, // Expect machine-0 in second model
},
{
label: "RemoveAll panics if model-uuid is included",
test: func() (int, error) {
machines0.Writeable().RemoveAll(bson.D{{"model-uuid", "whatever"}})
return 0, nil
},
expectedPanic: "model-uuid is added automatically and should not be provided",
},
{
label: "Update",
test: func() (int, error) {
err := machines0.Writeable().Update(bson.D{{"_id", m0.Id()}},
bson.D{{"$set", bson.D{{"update-field", "field value"}}}})
c.Assert(err, jc.ErrorIsNil)
return machines0.Find(bson.D{{"update-field", "field value"}}).Count()
},
expectedCount: 1,
},
{
label: "UpdateId",
test: func() (int, error) {
err := machines0.Writeable().UpdateId(m0.Id(),
bson.D{{"$set", bson.D{{"update-field", "field value"}}}})
c.Assert(err, jc.ErrorIsNil)
return machines0.Find(bson.D{{"update-field", "field value"}}).Count()
},
expectedCount: 1,
},
} {
c.Logf("test %d: %s", i, t.label)
machinesSnapshot.restore(c)
if t.expectedPanic == "" {
count, err := t.test()
if t.expectedError != "" {
c.Assert(err, gc.ErrorMatches, t.expectedError)
} else {
c.Assert(err, jc.ErrorIsNil)
}
c.Check(count, gc.Equals, t.expectedCount)
} else {
c.Check(func() { t.test() }, gc.PanicMatches, t.expectedPanic)
}
// Check that other model is untouched after each test
count, err := machines1.Count()
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
}
}
type collectionSnapshot struct {
coll *mgo.Collection
origDocs []interface{}
}
func newCollectionSnapshot(c *gc.C, coll *mgo.Collection) *collectionSnapshot {
ss := &collectionSnapshot{coll: coll}
err := coll.Find(nil).All(&ss.origDocs)
c.Assert(err, jc.ErrorIsNil)
return ss
}
func (ss *collectionSnapshot) restore(c *gc.C) {
_, err := ss.coll.RemoveAll(nil)
c.Assert(err, jc.ErrorIsNil)
err = ss.coll.Insert(ss.origDocs...)
c.Assert(err, jc.ErrorIsNil)
}