~fwereade/juju-core/uniter-relations-working-but-somewhat-too-big

« back to all changes in this revision

Viewing changes to state/relation_test.go

  • Committer: William Reade
  • Date: 2012-10-13 00:02:24 UTC
  • Revision ID: fwereade@gmail.com-20121013000224-zvf7mcg1lg1x19ah
finished relation lifecycle... I think

Show diffs side-by-side

added added

removed removed

Lines of Context:
248
248
        // Check missing settings cannot be read by any RU.
249
249
        for _, ru := range rus {
250
250
                _, err := ru.ReadSettings("peer/0")
251
 
                c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/0" in relation "peer:name": not found`)
 
251
                c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/0" in relation "peer:name": settings not found`)
252
252
        }
253
253
 
254
254
        // Add settings for one RU.
275
275
        // Check missing settings cannot be read by any RU.
276
276
        for _, ru := range rus {
277
277
                _, err := ru.ReadSettings("pro/0")
278
 
                c.Assert(err, ErrorMatches, `cannot read settings for unit "pro/0" in relation "pro:pname req:rname": not found`)
 
278
                c.Assert(err, ErrorMatches, `cannot read settings for unit "pro/0" in relation "pro:pname req:rname": settings not found`)
279
279
        }
280
280
 
281
281
        // Add settings for one RU.
302
302
        // Check missing settings cannot be read by any RU.
303
303
        for _, ru := range rus {
304
304
                _, err := ru.ReadSettings("pro/0")
305
 
                c.Assert(err, ErrorMatches, `cannot read settings for unit "pro/0" in relation "pro:pname req:rname": not found`)
 
305
                c.Assert(err, ErrorMatches, `cannot read settings for unit "pro/0" in relation "pro:pname req:rname": settings not found`)
306
306
        }
307
307
 
308
308
        // Add settings for one RU.
326
326
        rus1 := RUs{prr.pru1, prr.rru1}
327
327
        for _, ru := range rus1 {
328
328
                _, err := ru.ReadSettings("pro/0")
329
 
                c.Assert(err, ErrorMatches, `cannot read settings for unit "pro/0" in relation "pro:pname req:rname": not found`)
 
329
                c.Assert(err, ErrorMatches, `cannot read settings for unit "pro/0" in relation "pro:pname req:rname": settings not found`)
330
330
        }
331
331
}
332
332
 
 
333
func (s *RelationUnitSuite) TestScopeWithRelationLifecycle(c *C) {
 
334
        pr := NewPeerRelation(c, &s.ConnSuite)
 
335
        rel := pr.ru0.Relation()
 
336
 
 
337
        // Check the relation can't be removed while Alive.
 
338
        err := s.State.RemoveRelation(rel)
 
339
        c.Assert(err, ErrorMatches, `cannot remove relation "peer:name": relation is not dead`)
 
340
 
 
341
        // Enter a unit, and check that we can set the relation to Dying.
 
342
        err = pr.ru0.EnterScope()
 
343
        c.Assert(err, IsNil)
 
344
        err = rel.EnsureDying()
 
345
        c.Assert(err, IsNil)
 
346
 
 
347
        // Check that we can't add a new unit now.
 
348
        err = pr.ru1.EnterScope()
 
349
        c.Assert(err, Equals, state.ErrRelationDying)
 
350
 
 
351
        // Check that we created no settings for the unit we failed to add.
 
352
        _, err = pr.ru0.ReadSettings("peer/1")
 
353
        c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/1" in relation "peer:name": settings not found`)
 
354
 
 
355
        // Check that we can't set it to Dead while the scope is non-empty.
 
356
        err = rel.EnsureDead()
 
357
        c.Assert(err, ErrorMatches, `cannot finish termination of relation "peer:name": relation still has member units`)
 
358
 
 
359
        // Check that we still can't remove it while it's Dying.
 
360
        err = s.State.RemoveRelation(rel)
 
361
        c.Assert(err, ErrorMatches, `cannot remove relation "peer:name": relation is not dead`)
 
362
 
 
363
        // Leave the scope, and check that the relation can become Dead.
 
364
        err = pr.ru0.LeaveScope()
 
365
        c.Assert(err, IsNil)
 
366
        err = rel.EnsureDead()
 
367
        c.Assert(err, IsNil)
 
368
 
 
369
        // Check that unit settings for the original unit still exist.
 
370
        settings, err := pr.ru1.ReadSettings("peer/0")
 
371
        c.Assert(err, IsNil)
 
372
        c.Assert(settings, DeepEquals, map[string]interface{}{
 
373
                "private-address": "peer-0.example.com",
 
374
        })
 
375
 
 
376
        // Check the relation can be removed, and that unit settings are deleted.
 
377
        err = s.State.RemoveRelation(rel)
 
378
        c.Assert(err, IsNil)
 
379
        _, err = pr.ru1.ReadSettings("peer/0")
 
380
        c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/0" in relation "peer:name": settings not found`)
 
381
 
 
382
}
 
383
 
333
384
func (s *RelationUnitSuite) TestPeerWatchScope(c *C) {
334
385
        pr := NewPeerRelation(c, &s.ConnSuite)
335
386
 
642
693
        peerep := state.RelationEndpoint{"peer", "ifce", "baz", state.RolePeer, charm.ScopeGlobal}
643
694
        rel, err := s.State.AddRelation(peerep)
644
695
        c.Assert(err, IsNil)
645
 
        u, err := peer.AddUnit()
646
 
        c.Assert(err, IsNil)
647
 
        ru, err := rel.Unit(u)
648
 
        c.Assert(err, IsNil)
649
 
        err = u.EnsureDead()
650
 
        c.Assert(err, IsNil)
651
 
        err = peer.RemoveUnit(u)
652
 
        c.Assert(err, IsNil)
653
 
        err = ru.EnterScope()
 
696
        u0, err := peer.AddUnit()
 
697
        c.Assert(err, IsNil)
 
698
        ru0, err := rel.Unit(u0)
 
699
        c.Assert(err, IsNil)
 
700
        err = u0.EnsureDead()
 
701
        c.Assert(err, IsNil)
 
702
        err = peer.RemoveUnit(u0)
 
703
        c.Assert(err, IsNil)
 
704
        err = ru0.EnterScope()
654
705
        c.Assert(err, ErrorMatches, `cannot initialize state for unit "peer/0" in relation "peer:baz": private address of unit "peer/0" not found`)
 
706
 
 
707
        u1, err := peer.AddUnit()
 
708
        c.Assert(err, IsNil)
 
709
        err = u1.SetPrivateAddress("u1.example.com")
 
710
        c.Assert(err, IsNil)
 
711
        ru1, err := rel.Unit(u1)
 
712
        c.Assert(err, IsNil)
 
713
        err = rel.EnsureDying()
 
714
        c.Assert(err, IsNil)
 
715
        err = ru1.EnterScope()
 
716
        c.Assert(err, Equals, state.ErrRelationDying)
655
717
}
656
718
 
657
719
func (s *OriginalRelationUnitSuite) TestRelationUnitReadSettings(c *C) {
678
740
        _, err = ru0.ReadSettings("peer/pressure")
679
741
        c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/pressure" in relation "peer:baz": "peer/pressure" is not a valid unit name`)
680
742
        _, err = ru0.ReadSettings("peer/1")
681
 
        c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/1" in relation "peer:baz": not found`)
 
743
        c.Assert(err, ErrorMatches, `cannot read settings for unit "peer/1" in relation "peer:baz": settings not found`)
682
744
 
683
745
        // Put some valid settings in ru1, and check they are now accessible to
684
746
        // both RelationUnits.