Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(830)

Side by Side Diff: state/unit_test.go

Issue 87560043: errors: Improve tests, unify interfaces, wrapping (Closed)
Patch Set: errors: Improve tests, unify interfaces, wrapping Created 10 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « state/unit.go ('k') | state/watcher.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012, 2013 Canonical Ltd. 1 // Copyright 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package state_test 4 package state_test
5 5
6 import ( 6 import (
7 "strconv" 7 "strconv"
8 8
9 jc "github.com/juju/testing/checkers" 9 jc "github.com/juju/testing/checkers"
10 gc "launchpad.net/gocheck" 10 gc "launchpad.net/gocheck"
(...skipping 23 matching lines...) Expand all
34 s.service = s.AddTestingService(c, "wordpress", s.charm) 34 s.service = s.AddTestingService(c, "wordpress", s.charm)
35 c.Assert(err, gc.IsNil) 35 c.Assert(err, gc.IsNil)
36 s.unit, err = s.service.AddUnit() 36 s.unit, err = s.service.AddUnit()
37 c.Assert(err, gc.IsNil) 37 c.Assert(err, gc.IsNil)
38 c.Assert(s.unit.Series(), gc.Equals, "quantal") 38 c.Assert(s.unit.Series(), gc.Equals, "quantal")
39 } 39 }
40 40
41 func (s *UnitSuite) TestUnitNotFound(c *gc.C) { 41 func (s *UnitSuite) TestUnitNotFound(c *gc.C) {
42 _, err := s.State.Unit("subway/0") 42 _, err := s.State.Unit("subway/0")
43 c.Assert(err, gc.ErrorMatches, `unit "subway/0" not found`) 43 c.Assert(err, gc.ErrorMatches, `unit "subway/0" not found`)
44 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 44 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
45 } 45 }
46 46
47 func (s *UnitSuite) TestService(c *gc.C) { 47 func (s *UnitSuite) TestService(c *gc.C) {
48 svc, err := s.unit.Service() 48 svc, err := s.unit.Service()
49 c.Assert(err, gc.IsNil) 49 c.Assert(err, gc.IsNil)
50 c.Assert(svc.Name(), gc.Equals, s.unit.ServiceName()) 50 c.Assert(svc.Name(), gc.Equals, s.unit.ServiceName())
51 } 51 }
52 52
53 func (s *UnitSuite) TestConfigSettingsNeedCharmURLSet(c *gc.C) { 53 func (s *UnitSuite) TestConfigSettingsNeedCharmURLSet(c *gc.C) {
54 _, err := s.unit.ConfigSettings() 54 _, err := s.unit.ConfigSettings()
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 err = unit1.Refresh() 289 err = unit1.Refresh()
290 c.Assert(err, gc.IsNil) 290 c.Assert(err, gc.IsNil)
291 valid = unit1.PasswordValid("arble-farble-dying-yarble") 291 valid = unit1.PasswordValid("arble-farble-dying-yarble")
292 c.Assert(valid, jc.IsTrue) 292 c.Assert(valid, jc.IsTrue)
293 293
294 err = unit1.EnsureDead() 294 err = unit1.EnsureDead()
295 c.Assert(err, gc.IsNil) 295 c.Assert(err, gc.IsNil)
296 err = unit1.Remove() 296 err = unit1.Remove()
297 c.Assert(err, gc.IsNil) 297 c.Assert(err, gc.IsNil)
298 err = unit1.Refresh() 298 err = unit1.Refresh()
299 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 299 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
300 } 300 }
301 301
302 func (s *UnitSuite) TestGetSetStatusWhileAlive(c *gc.C) { 302 func (s *UnitSuite) TestGetSetStatusWhileAlive(c *gc.C) {
303 err := s.unit.SetStatus(params.StatusError, "", nil) 303 err := s.unit.SetStatus(params.StatusError, "", nil)
304 c.Assert(err, gc.ErrorMatches, `cannot set status "error" without info`) 304 c.Assert(err, gc.ErrorMatches, `cannot set status "error" without info`)
305 err = s.unit.SetStatus(params.StatusPending, "", nil) 305 err = s.unit.SetStatus(params.StatusPending, "", nil)
306 c.Assert(err, gc.ErrorMatches, `cannot set status "pending"`) 306 c.Assert(err, gc.ErrorMatches, `cannot set status "pending"`)
307 err = s.unit.SetStatus(params.StatusDown, "", nil) 307 err = s.unit.SetStatus(params.StatusDown, "", nil)
308 c.Assert(err, gc.ErrorMatches, `cannot set status "down"`) 308 c.Assert(err, gc.ErrorMatches, `cannot set status "down"`)
309 err = s.unit.SetStatus(params.Status("vliegkat"), "orville", nil) 309 err = s.unit.SetStatus(params.Status("vliegkat"), "orville", nil)
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 assertRemoved(c, s.unit) 613 assertRemoved(c, s.unit)
614 } 614 }
615 615
616 func assertLife(c *gc.C, entity state.Living, life state.Life) { 616 func assertLife(c *gc.C, entity state.Living, life state.Life) {
617 c.Assert(entity.Refresh(), gc.IsNil) 617 c.Assert(entity.Refresh(), gc.IsNil)
618 c.Assert(entity.Life(), gc.Equals, life) 618 c.Assert(entity.Life(), gc.Equals, life)
619 } 619 }
620 620
621 func assertRemoved(c *gc.C, entity state.Living) { 621 func assertRemoved(c *gc.C, entity state.Living) {
622 err := entity.Refresh() 622 err := entity.Refresh()
623 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 623 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
624 err = entity.Destroy() 624 err = entity.Destroy()
625 c.Assert(err, gc.IsNil) 625 c.Assert(err, gc.IsNil)
626 if entity, ok := entity.(state.AgentLiving); ok { 626 if entity, ok := entity.(state.AgentLiving); ok {
627 err = entity.EnsureDead() 627 err = entity.EnsureDead()
628 c.Assert(err, gc.IsNil) 628 c.Assert(err, gc.IsNil)
629 err = entity.Remove() 629 err = entity.Remove()
630 c.Assert(err, gc.IsNil) 630 c.Assert(err, gc.IsNil)
631 } 631 }
632 } 632 }
633 633
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 err = unit.AssignToMachine(m) 678 err = unit.AssignToMachine(m)
679 c.Assert(err, gc.IsNil) 679 c.Assert(err, gc.IsNil)
680 err = m.SetMongoPassword("foo") 680 err = m.SetMongoPassword("foo")
681 c.Assert(err, gc.IsNil) 681 c.Assert(err, gc.IsNil)
682 682
683 // Sanity check that we cannot connect with the wrong 683 // Sanity check that we cannot connect with the wrong
684 // password 684 // password
685 info.Tag = m.Tag() 685 info.Tag = m.Tag()
686 info.Password = "foo1" 686 info.Password = "foo1"
687 err = tryOpenState(info) 687 err = tryOpenState(info)
688 » c.Assert(err, jc.Satisfies, errors.IsUnauthorizedError) 688 » c.Assert(err, jc.Satisfies, errors.IsUnauthorized)
689 689
690 // Connect as the machine entity. 690 // Connect as the machine entity.
691 info.Tag = m.Tag() 691 info.Tag = m.Tag()
692 info.Password = "foo" 692 info.Password = "foo"
693 st1, err := state.Open(info, state.TestingDialOpts(), state.Policy(nil)) 693 st1, err := state.Open(info, state.TestingDialOpts(), state.Policy(nil))
694 c.Assert(err, gc.IsNil) 694 c.Assert(err, gc.IsNil)
695 defer st1.Close() 695 defer st1.Close()
696 696
697 // Change the password for a unit derived from 697 // Change the password for a unit derived from
698 // the machine entity's state. 698 // the machine entity's state.
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
1060 } 1060 }
1061 1061
1062 func (s *UnitSuite) TestRemove(c *gc.C) { 1062 func (s *UnitSuite) TestRemove(c *gc.C) {
1063 err := s.unit.Remove() 1063 err := s.unit.Remove()
1064 c.Assert(err, gc.ErrorMatches, `cannot remove unit "wordpress/0": unit i s not dead`) 1064 c.Assert(err, gc.ErrorMatches, `cannot remove unit "wordpress/0": unit i s not dead`)
1065 err = s.unit.EnsureDead() 1065 err = s.unit.EnsureDead()
1066 c.Assert(err, gc.IsNil) 1066 c.Assert(err, gc.IsNil)
1067 err = s.unit.Remove() 1067 err = s.unit.Remove()
1068 c.Assert(err, gc.IsNil) 1068 c.Assert(err, gc.IsNil)
1069 err = s.unit.Refresh() 1069 err = s.unit.Refresh()
1070 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 1070 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
1071 units, err := s.service.AllUnits() 1071 units, err := s.service.AllUnits()
1072 c.Assert(err, gc.IsNil) 1072 c.Assert(err, gc.IsNil)
1073 c.Assert(units, gc.HasLen, 0) 1073 c.Assert(units, gc.HasLen, 0)
1074 err = s.unit.Remove() 1074 err = s.unit.Remove()
1075 c.Assert(err, gc.IsNil) 1075 c.Assert(err, gc.IsNil)
1076 } 1076 }
1077 1077
1078 func (s *UnitSuite) TestRemovePathological(c *gc.C) { 1078 func (s *UnitSuite) TestRemovePathological(c *gc.C) {
1079 // Add a relation between wordpress and mysql... 1079 // Add a relation between wordpress and mysql...
1080 wordpress := s.service 1080 wordpress := s.service
(...skipping 29 matching lines...) Expand all
1110 err = wordpress.Refresh() 1110 err = wordpress.Refresh()
1111 c.Assert(err, gc.IsNil) 1111 c.Assert(err, gc.IsNil)
1112 err = rel.Refresh() 1112 err = rel.Refresh()
1113 c.Assert(err, gc.IsNil) 1113 c.Assert(err, gc.IsNil)
1114 1114
1115 // ...but when the unit on the other side departs the relation, the 1115 // ...but when the unit on the other side departs the relation, the
1116 // relation and the other service are cleaned up. 1116 // relation and the other service are cleaned up.
1117 err = mysql0ru.LeaveScope() 1117 err = mysql0ru.LeaveScope()
1118 c.Assert(err, gc.IsNil) 1118 c.Assert(err, gc.IsNil)
1119 err = wordpress.Refresh() 1119 err = wordpress.Refresh()
1120 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 1120 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
1121 err = rel.Refresh() 1121 err = rel.Refresh()
1122 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 1122 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
1123 } 1123 }
1124 1124
1125 func (s *UnitSuite) TestRemovePathologicalWithBuggyUniter(c *gc.C) { 1125 func (s *UnitSuite) TestRemovePathologicalWithBuggyUniter(c *gc.C) {
1126 // Add a relation between wordpress and mysql... 1126 // Add a relation between wordpress and mysql...
1127 wordpress := s.service 1127 wordpress := s.service
1128 wordpress0 := s.unit 1128 wordpress0 := s.unit
1129 mysql := s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql")) 1129 mysql := s.AddTestingService(c, "mysql", s.AddTestingCharm(c, "mysql"))
1130 eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"}) 1130 eps, err := s.State.InferEndpoints([]string{"wordpress", "mysql"})
1131 c.Assert(err, gc.IsNil) 1131 c.Assert(err, gc.IsNil)
1132 rel, err := s.State.AddRelation(eps...) 1132 rel, err := s.State.AddRelation(eps...)
(...skipping 27 matching lines...) Expand all
1160 c.Assert(err, gc.IsNil) 1160 c.Assert(err, gc.IsNil)
1161 1161
1162 // ...and that when the malfunctioning unit agent on the other side 1162 // ...and that when the malfunctioning unit agent on the other side
1163 // sets itself to dead *without* departing the relation, the unit's 1163 // sets itself to dead *without* departing the relation, the unit's
1164 // removal causes the relation and the other service to be cleaned up. 1164 // removal causes the relation and the other service to be cleaned up.
1165 err = mysql0.EnsureDead() 1165 err = mysql0.EnsureDead()
1166 c.Assert(err, gc.IsNil) 1166 c.Assert(err, gc.IsNil)
1167 err = mysql0.Remove() 1167 err = mysql0.Remove()
1168 c.Assert(err, gc.IsNil) 1168 c.Assert(err, gc.IsNil)
1169 err = wordpress.Refresh() 1169 err = wordpress.Refresh()
1170 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 1170 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
1171 err = rel.Refresh() 1171 err = rel.Refresh()
1172 » c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 1172 » c.Assert(err, jc.Satisfies, errors.IsNotFound)
1173 } 1173 }
1174 1174
1175 func (s *UnitSuite) TestWatchSubordinates(c *gc.C) { 1175 func (s *UnitSuite) TestWatchSubordinates(c *gc.C) {
1176 w := s.unit.WatchSubordinateUnits() 1176 w := s.unit.WatchSubordinateUnits()
1177 defer testing.AssertStop(c, w) 1177 defer testing.AssertStop(c, w)
1178 wc := testing.NewStringsWatcherC(c, s.State, w) 1178 wc := testing.NewStringsWatcherC(c, s.State, w)
1179 wc.AssertChange() 1179 wc.AssertChange()
1180 wc.AssertNoChange() 1180 wc.AssertNoChange()
1181 1181
1182 // Add a couple of subordinates, check change. 1182 // Add a couple of subordinates, check change.
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1284 err := s.unit.SetAnnotations(annotations) 1284 err := s.unit.SetAnnotations(annotations)
1285 c.Assert(err, gc.IsNil) 1285 c.Assert(err, gc.IsNil)
1286 err = s.unit.EnsureDead() 1286 err = s.unit.EnsureDead()
1287 c.Assert(err, gc.IsNil) 1287 c.Assert(err, gc.IsNil)
1288 err = s.unit.Remove() 1288 err = s.unit.Remove()
1289 c.Assert(err, gc.IsNil) 1289 c.Assert(err, gc.IsNil)
1290 ann, err := s.unit.Annotations() 1290 ann, err := s.unit.Annotations()
1291 c.Assert(err, gc.IsNil) 1291 c.Assert(err, gc.IsNil)
1292 c.Assert(ann, gc.DeepEquals, make(map[string]string)) 1292 c.Assert(ann, gc.DeepEquals, make(map[string]string))
1293 } 1293 }
OLDNEW
« no previous file with comments | « state/unit.go ('k') | state/watcher.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b