~go-bot/juju-core/trunk

« back to all changes in this revision

Viewing changes to state/cleanup_test.go

  • Committer: Tarmac
  • Author(s): William Reade
  • Date: 2014-05-22 11:37:17 UTC
  • mfrom: (1679.1.11 juju-core)
  • Revision ID: tarmac-20140522113717-99dhnikcznpjpwrw
[r=fwereade],[bug=1192433] state: set departed on dying units' relations

This ended up including:

  * stopping using magic strings in cleanup
  * drawing a clear distinction between "joined" and "in scope" (which
    accounts for most of the lines of changes)

...but did *not* include:

  * fixing the name of the Uniter.JoinedRelations API, because I want to
    wait for API versioning

https://codereview.appspot.com/94540043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
        // Run the cleanup, and check that units are all destroyed as appropriate.
46
46
        s.assertCleanupRuns(c)
47
 
        s.assertDoesNotNeedCleanup(c)
48
47
        err = units[0].Refresh()
49
48
        c.Assert(err, gc.IsNil)
50
49
        c.Assert(units[0].Life(), gc.Equals, state.Dying)
52
51
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
53
52
        err = units[2].Refresh()
54
53
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
 
54
 
 
55
        // Run a final cleanup to clear the cleanup scheduled for the unit that
 
56
        // became dying.
 
57
        s.assertCleanupCount(c, 1)
55
58
}
56
59
 
57
60
func (s *CleanupSuite) TestCleanupEnvironmentServices(c *gc.C) {
108
111
        // Destroy the service, check the relation's still around.
109
112
        err = pr.svc.Destroy()
110
113
        c.Assert(err, gc.IsNil)
111
 
        s.assertNeedsCleanup(c)
112
 
        s.assertCleanupRuns(c)
 
114
        s.assertCleanupCount(c, 2)
113
115
        err = rel.Refresh()
114
116
        c.Assert(err, gc.IsNil)
115
117
        c.Assert(rel.Life(), gc.Equals, state.Dying)
116
 
        s.assertDoesNotNeedCleanup(c)
117
118
 
118
119
        // The unit leaves scope, triggering relation removal.
119
120
        err = pr.ru0.LeaveScope()
126
127
        c.Assert(settings, gc.DeepEquals, map[string]interface{}{"some": "settings"})
127
128
 
128
129
        // ...but they are on cleanup.
129
 
        s.assertCleanupRuns(c)
130
 
        s.assertDoesNotNeedCleanup(c)
 
130
        s.assertCleanupCount(c, 1)
131
131
        _, err = pr.ru1.ReadSettings("riak/0")
132
132
        c.Assert(err, gc.ErrorMatches, `cannot read settings for unit "riak/0" in relation "riak:ring": settings not found`)
133
133
}
164
164
        s.assertNeedsCleanup(c)
165
165
 
166
166
        // Clean up, and check that the unit has been removed...
167
 
        s.assertCleanupRuns(c)
168
 
        s.assertDoesNotNeedCleanup(c)
 
167
        s.assertCleanupCount(c, 2)
169
168
        assertRemoved(c, pr.u0)
170
169
 
171
170
        // ...and the unit has departed relation scope...
172
 
        assertNotInScope(c, pr.ru0)
 
171
        assertNotJoined(c, pr.ru0)
173
172
 
174
173
        // ...but that the machine remains, and is Dead, ready for removal by the
175
174
        // provisioner.
218
217
        s.assertNeedsCleanup(c)
219
218
 
220
219
        // Clean up, and check that the container has been removed...
221
 
        s.assertCleanupRuns(c)
222
 
        s.assertDoesNotNeedCleanup(c)
 
220
        s.assertCleanupCount(c, 2)
223
221
        err = container.Refresh()
224
222
        c.Assert(err, jc.Satisfies, errors.IsNotFound)
225
223
 
240
238
        assertLife(c, machine, state.Dead)
241
239
}
242
240
 
 
241
func (s *CleanupSuite) TestCleanupDyingUnit(c *gc.C) {
 
242
        // Create active unit, in a relation.
 
243
        prr := NewProReqRelation(c, &s.ConnSuite, charm.ScopeGlobal)
 
244
        err := prr.pru0.EnterScope(nil)
 
245
        c.Assert(err, gc.IsNil)
 
246
 
 
247
        // Destroy provider unit 0; check it's Dying, and a cleanup has been scheduled.
 
248
        err = prr.pu0.Destroy()
 
249
        c.Assert(err, gc.IsNil)
 
250
        err = prr.pu0.Refresh()
 
251
        c.Assert(err, gc.IsNil)
 
252
        assertLife(c, prr.pu0, state.Dying)
 
253
        s.assertNeedsCleanup(c)
 
254
 
 
255
        // Check it's reported in scope until cleaned up.
 
256
        assertJoined(c, prr.pru0)
 
257
        s.assertCleanupCount(c, 1)
 
258
        assertInScope(c, prr.pru0)
 
259
        assertNotJoined(c, prr.pru0)
 
260
 
 
261
        // Destroy the relation, and check it sticks around...
 
262
        err = prr.rel.Destroy()
 
263
        c.Assert(err, gc.IsNil)
 
264
        assertLife(c, prr.rel, state.Dying)
 
265
 
 
266
        // ...until the unit is removed, and really leaves scope.
 
267
        err = prr.pu0.EnsureDead()
 
268
        c.Assert(err, gc.IsNil)
 
269
        err = prr.pu0.Remove()
 
270
        c.Assert(err, gc.IsNil)
 
271
        assertNotInScope(c, prr.pru0)
 
272
        assertRemoved(c, prr.rel)
 
273
}
 
274
 
 
275
func (s *CleanupSuite) TestCleanupDyingUnitAlreadyRemoved(c *gc.C) {
 
276
        // Create active unit, in a relation.
 
277
        prr := NewProReqRelation(c, &s.ConnSuite, charm.ScopeGlobal)
 
278
        err := prr.pru0.EnterScope(nil)
 
279
        c.Assert(err, gc.IsNil)
 
280
 
 
281
        // Destroy provider unit 0; check it's Dying, and a cleanup has been scheduled.
 
282
        err = prr.pu0.Destroy()
 
283
        c.Assert(err, gc.IsNil)
 
284
        err = prr.pu0.Refresh()
 
285
        c.Assert(err, gc.IsNil)
 
286
        assertLife(c, prr.pu0, state.Dying)
 
287
        s.assertNeedsCleanup(c)
 
288
 
 
289
        // Remove the unit, and the relation.
 
290
        err = prr.pu0.EnsureDead()
 
291
        c.Assert(err, gc.IsNil)
 
292
        err = prr.pu0.Remove()
 
293
        c.Assert(err, gc.IsNil)
 
294
        err = prr.rel.Destroy()
 
295
        c.Assert(err, gc.IsNil)
 
296
        assertRemoved(c, prr.rel)
 
297
 
 
298
        // Check the cleanup still runs happily.
 
299
        s.assertCleanupCount(c, 1)
 
300
}
 
301
 
243
302
func (s *CleanupSuite) TestNothingToCleanup(c *gc.C) {
244
303
        s.assertDoesNotNeedCleanup(c)
245
304
        s.assertCleanupRuns(c)
262
321
        c.Assert(err, gc.IsNil)
263
322
        c.Assert(actual, jc.IsFalse)
264
323
}
 
324
 
 
325
// assertCleanupCount is useful because certain cleanups cause other cleanups
 
326
// to be queued; it makes more sense to just run cleanup again than to unpick
 
327
// object destruction so that we run the cleanups inline while running cleanups.
 
328
func (s *CleanupSuite) assertCleanupCount(c *gc.C, count int) {
 
329
        for i := 0; i < count; i++ {
 
330
                c.Logf("checking cleanups %d", i)
 
331
                s.assertNeedsCleanup(c)
 
332
                s.assertCleanupRuns(c)
 
333
        }
 
334
        s.assertDoesNotNeedCleanup(c)
 
335
}