-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimumunits_test.go
422 lines (374 loc) · 11.6 KB
/
minimumunits_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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"labix.org/v2/mgo"
gc "launchpad.net/gocheck"
"github.com/juju/core/state"
)
type MinUnitsSuite struct {
ConnSuite
service *state.Service
}
var _ = gc.Suite(&MinUnitsSuite{})
func (s *MinUnitsSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
s.service = s.AddTestingService(c, "dummy-service", s.AddTestingCharm(c, "dummy"))
}
func (s *MinUnitsSuite) assertRevno(c *gc.C, expectedRevno int, expectedErr error) {
revno, err := state.MinUnitsRevno(s.State, s.service.Name())
c.Assert(err, gc.Equals, expectedErr)
c.Assert(revno, gc.Equals, expectedRevno)
}
func (s *MinUnitsSuite) addUnits(c *gc.C, count int) {
for i := 0; i < count; i++ {
_, err := s.service.AddUnit()
c.Assert(err, gc.IsNil)
}
}
func (s *MinUnitsSuite) TestSetMinUnits(c *gc.C) {
service := s.service
for i, t := range []struct {
about string
initial int
changes []int
revno int
err error
}{{
// Revno is set to zero on creation.
about: "setting minimum units",
changes: []int{42},
}, {
// Revno is increased by the update operation.
about: "updating minimum units",
initial: 1,
changes: []int{42},
revno: 1,
}, {
// Revno does not change.
about: "updating minimum units with the same value",
initial: 42,
changes: []int{42},
}, {
// Revno is increased by each update.
about: "increasing minimum units multiple times",
initial: 1,
changes: []int{2, 3, 4},
revno: 3,
}, {
// Revno does not change.
about: "decreasing minimum units multiple times",
initial: 5,
changes: []int{3, 2, 1},
}, {
// No-op.
about: "removing not existent minimum units",
changes: []int{0},
err: mgo.ErrNotFound,
}, {
// The document is deleted.
about: "removing existing minimum units",
initial: 42,
changes: []int{0},
err: mgo.ErrNotFound,
}} {
c.Logf("test %d. %s", i, t.about)
// Set up initial minimum units if required.
if t.initial > 0 {
err := service.SetMinUnits(t.initial)
c.Assert(err, gc.IsNil)
}
// Insert/update minimum units.
for _, input := range t.changes {
err := service.SetMinUnits(input)
c.Assert(err, gc.IsNil)
c.Assert(service.MinUnits(), gc.Equals, input)
c.Assert(service.Refresh(), gc.IsNil)
c.Assert(service.MinUnits(), gc.Equals, input)
}
// Check the document existence and revno.
s.assertRevno(c, t.revno, t.err)
// Clean up, if required, the minUnits document.
err := service.SetMinUnits(0)
c.Assert(err, gc.IsNil)
}
}
func (s *MinUnitsSuite) TestInvalidMinUnits(c *gc.C) {
err := s.service.SetMinUnits(-1)
c.Assert(err, gc.ErrorMatches, `cannot set minimum units for service "dummy-service": cannot set a negative minimum number of units`)
}
func (s *MinUnitsSuite) TestMinUnitsInsertRetry(c *gc.C) {
defer state.SetRetryHooks(c, s.State, func() {
err := s.service.SetMinUnits(41)
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, nil)
}, func() {
s.assertRevno(c, 1, nil)
}).Check()
err := s.service.SetMinUnits(42)
c.Assert(err, gc.IsNil)
c.Assert(s.service.MinUnits(), gc.Equals, 42)
}
func (s *MinUnitsSuite) TestMinUnitsUpdateRetry(c *gc.C) {
err := s.service.SetMinUnits(41)
c.Assert(err, gc.IsNil)
defer state.SetRetryHooks(c, s.State, func() {
err := s.service.SetMinUnits(0)
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, mgo.ErrNotFound)
}, func() {
s.assertRevno(c, 0, nil)
}).Check()
err = s.service.SetMinUnits(42)
c.Assert(err, gc.IsNil)
c.Assert(s.service.MinUnits(), gc.Equals, 42)
}
func (s *MinUnitsSuite) TestMinUnitsRemoveBefore(c *gc.C) {
err := s.service.SetMinUnits(41)
c.Assert(err, gc.IsNil)
defer state.SetBeforeHooks(c, s.State, func() {
err := s.service.SetMinUnits(0)
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, mgo.ErrNotFound)
}).Check()
err = s.service.SetMinUnits(0)
c.Assert(err, gc.IsNil)
c.Assert(s.service.MinUnits(), gc.Equals, 0)
}
func (s *MinUnitsSuite) testDestroyOrRemoveServiceBefore(c *gc.C, initial, input int, preventRemoval bool) {
err := s.service.SetMinUnits(initial)
c.Assert(err, gc.IsNil)
expectedErr := `cannot set minimum units for service "dummy-service": service "dummy-service" not found`
if preventRemoval {
expectedErr = `cannot set minimum units for service "dummy-service": service is no longer alive`
s.addUnits(c, 1)
}
defer state.SetBeforeHooks(c, s.State, func() {
err := s.service.Destroy()
c.Assert(err, gc.IsNil)
}).Check()
err = s.service.SetMinUnits(input)
c.Assert(err, gc.ErrorMatches, expectedErr)
s.assertRevno(c, 0, mgo.ErrNotFound)
}
func (s *MinUnitsSuite) TestMinUnitsInsertDestroyServiceBefore(c *gc.C) {
s.testDestroyOrRemoveServiceBefore(c, 0, 42, true)
}
func (s *MinUnitsSuite) TestMinUnitsUpdateDestroyServiceBefore(c *gc.C) {
s.testDestroyOrRemoveServiceBefore(c, 1, 42, true)
}
func (s *MinUnitsSuite) TestMinUnitsRemoveDestroyServiceBefore(c *gc.C) {
s.testDestroyOrRemoveServiceBefore(c, 1, 0, true)
}
func (s *MinUnitsSuite) TestMinUnitsInsertRemoveServiceBefore(c *gc.C) {
s.testDestroyOrRemoveServiceBefore(c, 0, 42, false)
}
func (s *MinUnitsSuite) TestMinUnitsUpdateRemoveServiceBefore(c *gc.C) {
s.testDestroyOrRemoveServiceBefore(c, 1, 42, false)
}
func (s *MinUnitsSuite) TestMinUnitsRemoveRemoveServiceBefore(c *gc.C) {
s.testDestroyOrRemoveServiceBefore(c, 1, 0, false)
}
func (s *MinUnitsSuite) TestMinUnitsSetDestroyEntities(c *gc.C) {
err := s.service.SetMinUnits(1)
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, nil)
// Add two units to the service for later use.
unit1, err := s.service.AddUnit()
c.Assert(err, gc.IsNil)
unit2, err := s.service.AddUnit()
c.Assert(err, gc.IsNil)
// Destroy a unit and ensure the revno has been increased.
preventUnitDestroyRemove(c, unit1)
err = unit1.Destroy()
c.Assert(err, gc.IsNil)
s.assertRevno(c, 1, nil)
// Remove a unit and ensure the revno has been increased..
err = unit2.Destroy()
c.Assert(err, gc.IsNil)
s.assertRevno(c, 2, nil)
// Destroy the service and ensure the minUnits document has been removed.
err = s.service.Destroy()
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, mgo.ErrNotFound)
}
func (s *MinUnitsSuite) TestMinUnitsNotSetDestroyEntities(c *gc.C) {
// Add two units to the service for later use.
unit1, err := s.service.AddUnit()
c.Assert(err, gc.IsNil)
unit2, err := s.service.AddUnit()
c.Assert(err, gc.IsNil)
// Destroy a unit and ensure the minUnits document has not been created.
preventUnitDestroyRemove(c, unit1)
err = unit1.Destroy()
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, mgo.ErrNotFound)
// Remove a unit and ensure the minUnits document has not been created.
err = unit2.Destroy()
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, mgo.ErrNotFound)
// Destroy the service and ensure the minUnits document is still missing.
err = s.service.Destroy()
c.Assert(err, gc.IsNil)
s.assertRevno(c, 0, mgo.ErrNotFound)
}
func assertAllUnits(c *gc.C, service *state.Service, expected int) {
units, err := service.AllUnits()
c.Assert(err, gc.IsNil)
c.Assert(units, gc.HasLen, expected)
}
func (s *MinUnitsSuite) TestEnsureMinUnits(c *gc.C) {
service := s.service
for i, t := range []struct {
about string // Test description.
initial int // Initial number of units.
minimum int // Minimum number of units for the service.
destroy int // Number of units to be destroyed before calling EnsureMinUnits.
expected int // Expected number of units after calling EnsureMinUnits.
}{{
about: "no minimum units set",
}, {
about: "initial units > minimum units",
initial: 2,
minimum: 1,
expected: 2,
}, {
about: "initial units == minimum units",
initial: 2,
minimum: 2,
expected: 2,
}, {
about: "initial units < minimum units",
initial: 1,
minimum: 2,
expected: 2,
}, {
about: "alive units < minimum units",
initial: 2,
minimum: 2,
destroy: 1,
expected: 3,
}, {
about: "add multiple units",
initial: 6,
minimum: 5,
destroy: 4,
expected: 9,
}} {
c.Logf("test %d. %s", i, t.about)
// Set up initial units if required.
s.addUnits(c, t.initial)
// Set up minimum units if required.
err := service.SetMinUnits(t.minimum)
c.Assert(err, gc.IsNil)
// Destroy units if required.
allUnits, err := service.AllUnits()
c.Assert(err, gc.IsNil)
for i := 0; i < t.destroy; i++ {
preventUnitDestroyRemove(c, allUnits[i])
err = allUnits[i].Destroy()
c.Assert(err, gc.IsNil)
}
// Ensure the minimum number of units is correctly restored.
c.Assert(service.Refresh(), gc.IsNil)
err = service.EnsureMinUnits()
c.Assert(err, gc.IsNil)
assertAllUnits(c, service, t.expected)
// Clean up the minUnits document and the units.
err = service.SetMinUnits(0)
c.Assert(err, gc.IsNil)
removeAllUnits(c, service)
}
}
func (s *MinUnitsSuite) TestEnsureMinUnitsServiceNotAlive(c *gc.C) {
err := s.service.SetMinUnits(2)
c.Assert(err, gc.IsNil)
s.addUnits(c, 1)
err = s.service.Destroy()
c.Assert(err, gc.IsNil)
expectedErr := `cannot ensure minimum units for service "dummy-service": service is not alive`
// An error is returned if the service is not alive.
c.Assert(s.service.EnsureMinUnits(), gc.ErrorMatches, expectedErr)
// An error is returned if the service was removed.
err = s.State.Cleanup()
c.Assert(err, gc.IsNil)
c.Assert(s.service.EnsureMinUnits(), gc.ErrorMatches, expectedErr)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsUpdateMinUnitsRetry(c *gc.C) {
s.addUnits(c, 1)
err := s.service.SetMinUnits(4)
c.Assert(err, gc.IsNil)
defer state.SetRetryHooks(c, s.State, func() {
err := s.service.SetMinUnits(2)
c.Assert(err, gc.IsNil)
}, func() {
assertAllUnits(c, s.service, 2)
}).Check()
err = s.service.EnsureMinUnits()
c.Assert(err, gc.IsNil)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsAddUnitsRetry(c *gc.C) {
err := s.service.SetMinUnits(3)
c.Assert(err, gc.IsNil)
defer state.SetRetryHooks(c, s.State, func() {
s.addUnits(c, 2)
}, func() {
assertAllUnits(c, s.service, 3)
}).Check()
err = s.service.EnsureMinUnits()
c.Assert(err, gc.IsNil)
}
func (s *MinUnitsSuite) testEnsureMinUnitsBefore(c *gc.C, f func(), minUnits, expectedUnits int) {
service := s.service
err := service.SetMinUnits(minUnits)
c.Assert(err, gc.IsNil)
defer state.SetBeforeHooks(c, s.State, f).Check()
err = service.EnsureMinUnits()
c.Assert(err, gc.IsNil)
assertAllUnits(c, service, expectedUnits)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsDecreaseMinUnitsBefore(c *gc.C) {
f := func() {
err := s.service.SetMinUnits(3)
c.Assert(err, gc.IsNil)
}
s.testEnsureMinUnitsBefore(c, f, 42, 3)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsRemoveMinUnitsBefore(c *gc.C) {
f := func() {
err := s.service.SetMinUnits(0)
c.Assert(err, gc.IsNil)
}
s.testEnsureMinUnitsBefore(c, f, 2, 0)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsAddUnitsBefore(c *gc.C) {
f := func() {
s.addUnits(c, 2)
}
s.testEnsureMinUnitsBefore(c, f, 2, 2)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsDestroyServiceBefore(c *gc.C) {
s.addUnits(c, 1)
err := s.service.SetMinUnits(42)
c.Assert(err, gc.IsNil)
defer state.SetBeforeHooks(c, s.State, func() {
err := s.service.Destroy()
c.Assert(err, gc.IsNil)
}).Check()
c.Assert(s.service.EnsureMinUnits(), gc.ErrorMatches,
`cannot ensure minimum units for service "dummy-service": service is not alive`)
}
func (s *MinUnitsSuite) TestEnsureMinUnitsDecreaseMinUnitsAfter(c *gc.C) {
s.addUnits(c, 2)
service := s.service
err := service.SetMinUnits(5)
c.Assert(err, gc.IsNil)
defer state.SetAfterHooks(c, s.State, func() {
err := service.SetMinUnits(3)
c.Assert(err, gc.IsNil)
}).Check()
c.Assert(service.Refresh(), gc.IsNil)
err = service.EnsureMinUnits()
c.Assert(err, gc.IsNil)
assertAllUnits(c, service, 3)
}