-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_test.go
429 lines (357 loc) · 12.5 KB
/
cleanup_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
// Copyright 2014-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v4"
"github.com/juju/juju/instance"
"github.com/juju/juju/state"
)
type CleanupSuite struct {
ConnSuite
}
var _ = gc.Suite(&CleanupSuite{})
func (s *CleanupSuite) TestCleanupDyingServiceUnits(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a service with some units.
mysql := s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql"))
units := make([]*state.Unit, 3)
for i := range units {
unit, err := mysql.AddUnit()
c.Assert(err, jc.ErrorIsNil)
units[i] = unit
}
preventUnitDestroyRemove(c, units[0])
s.assertDoesNotNeedCleanup(c)
// Destroy the service and check the units are unaffected, but a cleanup
// has been scheduled.
err := mysql.Destroy()
c.Assert(err, jc.ErrorIsNil)
for _, unit := range units {
err := unit.Refresh()
c.Assert(err, jc.ErrorIsNil)
}
s.assertNeedsCleanup(c)
// Run the cleanup, and check that units are all destroyed as appropriate.
s.assertCleanupRuns(c)
err = units[0].Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(units[0].Life(), gc.Equals, state.Dying)
err = units[1].Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
err = units[2].Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// Run a final cleanup to clear the cleanup scheduled for the unit that
// became dying.
s.assertCleanupCount(c, 1)
}
func (s *CleanupSuite) TestCleanupEnvironmentServices(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a service with some units.
mysql := s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql"))
units := make([]*state.Unit, 3)
for i := range units {
unit, err := mysql.AddUnit()
c.Assert(err, jc.ErrorIsNil)
units[i] = unit
}
s.assertDoesNotNeedCleanup(c)
// Destroy the environment and check the service and units are
// unaffected, but a cleanup for the service has been scheduled.
env, err := s.State.Environment()
c.Assert(err, jc.ErrorIsNil)
err = env.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
s.assertCleanupRuns(c)
err = mysql.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(mysql.Life(), gc.Equals, state.Dying)
for _, unit := range units {
err = unit.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(unit.Life(), gc.Equals, state.Alive)
}
// The first cleanup Destroys the service, which
// schedules another cleanup to destroy the units,
// then we need another pass for the actions cleanup
// which is queued on the next pass
s.assertCleanupCount(c, 2)
for _, unit := range units {
err = unit.Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
// Now we should have all the cleanups done
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupRelationSettings(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a relation with a unit in scope.
pr := NewPeerRelation(c, s.State, s.Owner)
rel := pr.ru0.Relation()
err := pr.ru0.EnterScope(map[string]interface{}{"some": "settings"})
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Destroy the service, check the relation's still around.
err = pr.svc.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupCount(c, 2)
err = rel.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(rel.Life(), gc.Equals, state.Dying)
// The unit leaves scope, triggering relation removal.
err = pr.ru0.LeaveScope()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Settings are not destroyed yet...
settings, err := pr.ru1.ReadSettings("riak/0")
c.Assert(err, jc.ErrorIsNil)
c.Assert(settings, gc.DeepEquals, map[string]interface{}{"some": "settings"})
// ...but they are on cleanup.
s.assertCleanupCount(c, 1)
_, err = pr.ru1.ReadSettings("riak/0")
c.Assert(err, gc.ErrorMatches, `cannot read settings for unit "riak/0" in relation "riak:ring": settings not found`)
}
func (s *CleanupSuite) TestForceDestroyMachineErrors(c *gc.C) {
manager, err := s.State.AddMachine("quantal", state.JobManageEnviron)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
err = manager.ForceDestroy()
expect := fmt.Sprintf("machine %s is required by the environment", manager.Id())
c.Assert(err, gc.ErrorMatches, expect)
s.assertDoesNotNeedCleanup(c)
assertLife(c, manager, state.Alive)
}
func (s *CleanupSuite) TestCleanupForceDestroyedMachineUnit(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a machine.
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
// Create a relation with a unit in scope and assigned to the machine.
pr := NewPeerRelation(c, s.State, s.Owner)
err = pr.u0.AssignToMachine(machine)
c.Assert(err, jc.ErrorIsNil)
err = pr.ru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Force machine destruction, check cleanup queued.
err = machine.ForceDestroy()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Clean up, and check that the unit has been removed...
s.assertCleanupCount(c, 2)
assertRemoved(c, pr.u0)
// ...and the unit has departed relation scope...
assertNotJoined(c, pr.ru0)
// ...but that the machine remains, and is Dead, ready for removal by the
// provisioner.
assertLife(c, machine, state.Dead)
}
func (s *CleanupSuite) TestCleanupForceDestroyedMachineWithContainer(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a machine with a container.
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
container, err := s.State.AddMachineInsideMachine(state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
}, machine.Id(), instance.LXC)
c.Assert(err, jc.ErrorIsNil)
// Create active units (in relation scope, with subordinates).
prr := NewProReqRelation(c, &s.ConnSuite, charm.ScopeContainer)
err = prr.pru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
err = prr.pru1.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
err = prr.rru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
err = prr.rru1.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
// Assign the various units to machines.
err = prr.pu0.AssignToMachine(machine)
c.Assert(err, jc.ErrorIsNil)
err = prr.pu1.AssignToMachine(container)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Force removal of the top-level machine.
err = machine.ForceDestroy()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// And do it again, just to check that the second cleanup doc for the same
// machine doesn't cause problems down the line.
err = machine.ForceDestroy()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Clean up, and check that the container has been removed...
s.assertCleanupCount(c, 2)
err = container.Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// ...and so have all the units...
assertRemoved(c, prr.pu0)
assertRemoved(c, prr.pu1)
assertRemoved(c, prr.ru0)
assertRemoved(c, prr.ru1)
// ...and none of the units have left relation scopes occupied...
assertNotInScope(c, prr.pru0)
assertNotInScope(c, prr.pru1)
assertNotInScope(c, prr.rru0)
assertNotInScope(c, prr.rru1)
// ...but that the machine remains, and is Dead, ready for removal by the
// provisioner.
assertLife(c, machine, state.Dead)
}
func (s *CleanupSuite) TestCleanupDyingUnit(c *gc.C) {
// Create active unit, in a relation.
prr := NewProReqRelation(c, &s.ConnSuite, charm.ScopeGlobal)
err := prr.pru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
// Destroy provider unit 0; check it's Dying, and a cleanup has been scheduled.
err = prr.pu0.Destroy()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Refresh()
c.Assert(err, jc.ErrorIsNil)
assertLife(c, prr.pu0, state.Dying)
s.assertNeedsCleanup(c)
// Check it's reported in scope until cleaned up.
assertJoined(c, prr.pru0)
s.assertCleanupCount(c, 1)
assertInScope(c, prr.pru0)
assertNotJoined(c, prr.pru0)
// Destroy the relation, and check it sticks around...
err = prr.rel.Destroy()
c.Assert(err, jc.ErrorIsNil)
assertLife(c, prr.rel, state.Dying)
// ...until the unit is removed, and really leaves scope.
err = prr.pu0.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Remove()
c.Assert(err, jc.ErrorIsNil)
assertNotInScope(c, prr.pru0)
assertRemoved(c, prr.rel)
}
func (s *CleanupSuite) TestCleanupDyingUnitAlreadyRemoved(c *gc.C) {
// Create active unit, in a relation.
prr := NewProReqRelation(c, &s.ConnSuite, charm.ScopeGlobal)
err := prr.pru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
// Destroy provider unit 0; check it's Dying, and a cleanup has been scheduled.
err = prr.pu0.Destroy()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Refresh()
c.Assert(err, jc.ErrorIsNil)
assertLife(c, prr.pu0, state.Dying)
s.assertNeedsCleanup(c)
// Remove the unit, and the relation.
err = prr.pu0.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Remove()
c.Assert(err, jc.ErrorIsNil)
err = prr.rel.Destroy()
c.Assert(err, jc.ErrorIsNil)
assertRemoved(c, prr.rel)
// Check the cleanup still runs happily.
s.assertCleanupCount(c, 1)
}
func (s *CleanupSuite) TestCleanupActions(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a service with a unit.
dummy := s.AddTestingService(c, "dummy", s.AddTestingCharm(c, "dummy"))
unit, err := dummy.AddUnit()
c.Assert(err, jc.ErrorIsNil)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
// Add a couple actions to the unit
_, err = unit.AddAction("snapshot", nil)
c.Assert(err, jc.ErrorIsNil)
_, err = unit.AddAction("snapshot", nil)
c.Assert(err, jc.ErrorIsNil)
// make sure unit still has actions
actions, err := unit.PendingActions()
c.Assert(err, jc.ErrorIsNil)
c.Assert(len(actions), gc.Equals, 2)
// destroy unit and run cleanups
err = dummy.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
// make sure unit still has actions, after first cleanup pass
actions, err = unit.PendingActions()
c.Assert(err, jc.ErrorIsNil)
c.Assert(len(actions), gc.Equals, 2)
// second cleanup pass
s.assertCleanupRuns(c)
// make sure unit has no actions, after second cleanup pass
actions, err = unit.PendingActions()
c.Assert(err, jc.ErrorIsNil)
c.Assert(len(actions), gc.Equals, 0)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupStorage(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
ch := s.AddTestingCharm(c, "storage-block")
storage := map[string]state.StorageConstraints{
"data": makeStorageCons("block", 1024, 1),
}
service := s.AddTestingServiceWithStorage(c, "storage-block", ch, storage)
u, err := service.AddUnit()
c.Assert(err, jc.ErrorIsNil)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
// this tag matches the storage instance created for the unit above.
storageTag := names.NewStorageTag("data/0")
si, err := s.State.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(si.Life(), gc.Equals, state.Alive)
// destroy storage instance and run cleanups
err = s.State.DestroyStorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
si, err = s.State.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(si.Life(), gc.Equals, state.Dying)
sa, err := s.State.StorageAttachments(u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(sa, gc.HasLen, 1)
c.Assert(sa[0].Life(), gc.Equals, state.Alive)
s.assertCleanupRuns(c)
// After running the cleanup, the attachment should be dying.
sa, err = s.State.StorageAttachments(u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(sa, gc.HasLen, 1)
c.Assert(sa[0].Life(), gc.Equals, state.Dying)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestNothingToCleanup(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
s.assertCleanupRuns(c)
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) assertCleanupRuns(c *gc.C) {
err := s.State.Cleanup()
c.Assert(err, jc.ErrorIsNil)
}
func (s *CleanupSuite) assertNeedsCleanup(c *gc.C) {
actual, err := s.State.NeedsCleanup()
c.Assert(err, jc.ErrorIsNil)
c.Assert(actual, jc.IsTrue)
}
func (s *CleanupSuite) assertDoesNotNeedCleanup(c *gc.C) {
actual, err := s.State.NeedsCleanup()
c.Assert(err, jc.ErrorIsNil)
c.Assert(actual, jc.IsFalse)
}
// assertCleanupCount is useful because certain cleanups cause other cleanups
// to be queued; it makes more sense to just run cleanup again than to unpick
// object destruction so that we run the cleanups inline while running cleanups.
func (s *CleanupSuite) assertCleanupCount(c *gc.C, count int) {
for i := 0; i < count; i++ {
c.Logf("checking cleanups %d", i)
s.assertNeedsCleanup(c)
s.assertCleanupRuns(c)
}
s.assertDoesNotNeedCleanup(c)
}