LEFT | RIGHT |
1 // launchpad.net/juju/go/state | |
2 // | |
3 // Copyright (c) 2011-2012 Canonical Ltd. | |
4 package state_test | 1 package state_test |
5 | 2 |
6 import ( | 3 import ( |
7 "fmt" | 4 "fmt" |
8 . "launchpad.net/gocheck" | 5 . "launchpad.net/gocheck" |
9 "launchpad.net/gozk/zookeeper" | 6 "launchpad.net/gozk/zookeeper" |
10 "launchpad.net/juju/go/charm" | 7 "launchpad.net/juju/go/charm" |
11 "launchpad.net/juju/go/state" | 8 "launchpad.net/juju/go/state" |
12 "launchpad.net/juju/go/testing" | 9 "launchpad.net/juju/go/testing" |
13 "net/url" | 10 "net/url" |
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 | 797 |
801 wordpressService, err := s.st.AddService("wordpress", dummy) | 798 wordpressService, err := s.st.AddService("wordpress", dummy) |
802 c.Assert(err, IsNil) | 799 c.Assert(err, IsNil) |
803 wordpressUnit, err := wordpressService.AddUnit() | 800 wordpressUnit, err := wordpressService.AddUnit() |
804 c.Assert(err, IsNil) | 801 c.Assert(err, IsNil) |
805 | 802 |
806 _, err = wordpressUnit.AssignToUnusedMachine() | 803 _, err = wordpressUnit.AssignToUnusedMachine() |
807 c.Assert(err, ErrorMatches, "no unused machine found") | 804 c.Assert(err, ErrorMatches, "no unused machine found") |
808 } | 805 } |
809 | 806 |
810 func (s *StateSuite) TestPlaceUnit(c *C) { | 807 func (s *StateSuite) TestAssignUnit(c *C) { |
811 _, err := s.st.AddMachine() | 808 _, err := s.st.AddMachine() |
812 c.Assert(err, IsNil) | 809 c.Assert(err, IsNil) |
813 dummy := s.addDummyCharm(c) | 810 dummy := s.addDummyCharm(c) |
814 serv, err := s.st.AddService("minecraft", dummy) | 811 serv, err := s.st.AddService("minecraft", dummy) |
815 c.Assert(err, IsNil) | 812 c.Assert(err, IsNil) |
816 unit0, err := serv.AddUnit() | 813 unit0, err := serv.AddUnit() |
817 c.Assert(err, IsNil) | 814 c.Assert(err, IsNil) |
818 | 815 |
819 // Check nonsensical policy | 816 // Check nonsensical policy |
820 » fail := func() { unit0.Place(state.PlacementPolicy("random")) } | 817 » fail := func() { state.AssignUnit(s.st, unit0, state.AssignmentPolicy("r
andom")) } |
821 » c.Assert(fail, PanicMatches, `unknown unit placement policy: "random"`) | 818 » c.Assert(fail, PanicMatches, `unknown unit assignment policy: "random"`) |
822 _, err = unit0.AssignedMachineId() | 819 _, err = unit0.AssignedMachineId() |
823 c.Assert(err, NotNil) | 820 c.Assert(err, NotNil) |
824 s.assertMachineCount(c, 1) | 821 s.assertMachineCount(c, 1) |
825 | 822 |
826 // Check local placement | 823 // Check local placement |
827 » err = unit0.Place(state.PlaceLocal) | 824 » err = state.AssignUnit(s.st, unit0, state.AssignLocal) |
828 c.Assert(err, IsNil) | 825 c.Assert(err, IsNil) |
829 mid, err := unit0.AssignedMachineId() | 826 mid, err := unit0.AssignedMachineId() |
830 c.Assert(err, IsNil) | 827 c.Assert(err, IsNil) |
831 c.Assert(mid, Equals, 0) | 828 c.Assert(mid, Equals, 0) |
832 s.assertMachineCount(c, 1) | 829 s.assertMachineCount(c, 1) |
833 | 830 |
834 // Check unassigned placement with no unused machines | 831 // Check unassigned placement with no unused machines |
835 unit1, err := serv.AddUnit() | 832 unit1, err := serv.AddUnit() |
836 c.Assert(err, IsNil) | 833 c.Assert(err, IsNil) |
837 » err = unit1.Place(state.PlaceUnassigned) | 834 » err = state.AssignUnit(s.st, unit1, state.AssignUnused) |
838 c.Assert(err, IsNil) | 835 c.Assert(err, IsNil) |
839 mid, err = unit1.AssignedMachineId() | 836 mid, err = unit1.AssignedMachineId() |
840 c.Assert(err, IsNil) | 837 c.Assert(err, IsNil) |
841 c.Assert(mid, Equals, 1) | 838 c.Assert(mid, Equals, 1) |
842 s.assertMachineCount(c, 2) | 839 s.assertMachineCount(c, 2) |
843 | 840 |
844 // Check unassigned placement on an unused machine | 841 // Check unassigned placement on an unused machine |
845 _, err = s.st.AddMachine() | 842 _, err = s.st.AddMachine() |
846 unit2, err := serv.AddUnit() | 843 unit2, err := serv.AddUnit() |
847 c.Assert(err, IsNil) | 844 c.Assert(err, IsNil) |
848 » err = unit2.Place(state.PlaceUnassigned) | 845 » err = state.AssignUnit(s.st, unit2, state.AssignUnused) |
849 c.Assert(err, IsNil) | 846 c.Assert(err, IsNil) |
850 mid, err = unit2.AssignedMachineId() | 847 mid, err = unit2.AssignedMachineId() |
851 c.Assert(err, IsNil) | 848 c.Assert(err, IsNil) |
852 c.Assert(mid, Equals, 2) | 849 c.Assert(mid, Equals, 2) |
853 s.assertMachineCount(c, 3) | 850 s.assertMachineCount(c, 3) |
854 } | 851 } |
855 | 852 |
856 func (s *StateSuite) TestGetSetClearUnitUpgrade(c *C) { | 853 func (s *StateSuite) TestGetSetClearUnitUpgrade(c *C) { |
857 // Check that setting and clearing an upgrade flag on a unit works. | 854 // Check that setting and clearing an upgrade flag on a unit works. |
858 dummy := s.addDummyCharm(c) | 855 dummy := s.addDummyCharm(c) |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 alive, err = unit.AgentAlive() | 1051 alive, err = unit.AgentAlive() |
1055 c.Assert(err, IsNil) | 1052 c.Assert(err, IsNil) |
1056 c.Assert(alive, Equals, true) | 1053 c.Assert(alive, Equals, true) |
1057 | 1054 |
1058 pinger.Kill() | 1055 pinger.Kill() |
1059 | 1056 |
1060 alive, err = unit.AgentAlive() | 1057 alive, err = unit.AgentAlive() |
1061 c.Assert(err, IsNil) | 1058 c.Assert(err, IsNil) |
1062 c.Assert(alive, Equals, false) | 1059 c.Assert(alive, Equals, false) |
1063 } | 1060 } |
| 1061 |
| 1062 func (s *StateSuite) TestAddRelation(c *C) { |
| 1063 dummy := s.addDummyCharm(c) |
| 1064 // Provider and requirer. |
| 1065 s.st.AddService("mysqldb", dummy) |
| 1066 s.st.AddService("wordpress", dummy) |
| 1067 mysqlep := state.RelationEndpoint{"mysqldb", "blog", "db", state.RolePro
vider, state.ScopeGlobal} |
| 1068 blogep := state.RelationEndpoint{"wordpress", "blog", "db", state.RoleRe
quirer, state.ScopeGlobal} |
| 1069 relation, serviceRelations, err := s.st.AddRelation(blogep, mysqlep) |
| 1070 c.Assert(err, IsNil) |
| 1071 c.Assert(relation, NotNil) |
| 1072 c.Assert(serviceRelations, HasLen, 2) |
| 1073 c.Assert(serviceRelations[0].RelationScope(), Equals, state.ScopeGlobal) |
| 1074 c.Assert(serviceRelations[0].RelationRole(), Equals, state.RoleRequirer) |
| 1075 c.Assert(serviceRelations[1].RelationScope(), Equals, state.ScopeGlobal) |
| 1076 c.Assert(serviceRelations[1].RelationRole(), Equals, state.RoleProvider) |
| 1077 c.Assert(serviceRelations[0].RelationName(), Equals, serviceRelations[1]
.RelationName()) |
| 1078 // Peer. |
| 1079 s.st.AddService("riak", dummy) |
| 1080 riakep := state.RelationEndpoint{"riak", "ring", "cache", state.RolePeer
, state.ScopeGlobal} |
| 1081 relation, serviceRelations, err = s.st.AddRelation(riakep) |
| 1082 c.Assert(err, IsNil) |
| 1083 c.Assert(relation, NotNil) |
| 1084 c.Assert(serviceRelations, HasLen, 1) |
| 1085 c.Assert(serviceRelations[0].RelationScope(), Equals, state.ScopeGlobal) |
| 1086 c.Assert(serviceRelations[0].RelationRole(), Equals, state.RolePeer) |
| 1087 c.Assert(serviceRelations[0].RelationName(), Equals, "cache") |
| 1088 |
| 1089 } |
| 1090 |
| 1091 func (s *StateSuite) TestAddRelationMissingService(c *C) { |
| 1092 dummy := s.addDummyCharm(c) |
| 1093 s.st.AddService("mysqldb", dummy) |
| 1094 mysqlep := state.RelationEndpoint{"mysqldb", "blog", "db", state.RolePro
vider, state.ScopeGlobal} |
| 1095 blogep := state.RelationEndpoint{"wordpress", "blog", "db", state.RoleRe
quirer, state.ScopeGlobal} |
| 1096 _, _, err := s.st.AddRelation(blogep, mysqlep) |
| 1097 c.Assert(err, ErrorMatches, `service with name "wordpress" not found`) |
| 1098 } |
| 1099 |
| 1100 func (s *StateSuite) TestAddRelationMissingEndpoint(c *C) { |
| 1101 dummy := s.addDummyCharm(c) |
| 1102 s.st.AddService("mysqldb", dummy) |
| 1103 mysqlep := state.RelationEndpoint{"mysqldb", "blog", "db", state.RolePro
vider, state.ScopeGlobal} |
| 1104 _, _, err := s.st.AddRelation(mysqlep) |
| 1105 c.Assert(err, ErrorMatches, `can't add non-peer relation with a single s
ervice`) |
| 1106 } |
| 1107 |
| 1108 func (s *StateSuite) TestAddClientServerDifferentRoles(c *C) { |
| 1109 dummy := s.addDummyCharm(c) |
| 1110 s.st.AddService("mysqldb", dummy) |
| 1111 s.st.AddService("riak", dummy) |
| 1112 mysqlep := state.RelationEndpoint{"mysqldb", "ifce", "db", state.RoleReq
uirer, state.ScopeGlobal} |
| 1113 riakep := state.RelationEndpoint{"riak", "ring", "cache", state.RolePeer
, state.ScopeGlobal} |
| 1114 _, _, err := s.st.AddRelation(mysqlep, riakep) |
| 1115 c.Assert(err, ErrorMatches, `can't add relation between mysqldb:db and r
iak:cache`) |
| 1116 } |
| 1117 |
| 1118 func (s *StateSuite) TestAddRelationDifferentInterfaces(c *C) { |
| 1119 dummy := s.addDummyCharm(c) |
| 1120 s.st.AddService("mysqldb", dummy) |
| 1121 s.st.AddService("wordpress", dummy) |
| 1122 mysqlep := state.RelationEndpoint{"mysqldb", "ifce-a", "db", state.RoleP
rovider, state.ScopeGlobal} |
| 1123 blogep := state.RelationEndpoint{"wordpress", "ifce-b", "db", state.Role
Requirer, state.ScopeGlobal} |
| 1124 _, _, err := s.st.AddRelation(blogep, mysqlep) |
| 1125 c.Assert(err, ErrorMatches, `can't add relation between wordpress:db and
mysqldb:db`) |
| 1126 } |
| 1127 |
| 1128 func (s *StateSuite) TestAddClientServerRelationTwice(c *C) { |
| 1129 dummy := s.addDummyCharm(c) |
| 1130 // Provider and requirer. |
| 1131 s.st.AddService("mysqldb", dummy) |
| 1132 s.st.AddService("wordpress", dummy) |
| 1133 mysqlep := state.RelationEndpoint{"mysqldb", "blog", "db", state.RolePro
vider, state.ScopeGlobal} |
| 1134 blogep := state.RelationEndpoint{"wordpress", "blog", "db", state.RoleRe
quirer, state.ScopeGlobal} |
| 1135 _, _, err := s.st.AddRelation(blogep, mysqlep) |
| 1136 c.Assert(err, IsNil) |
| 1137 _, _, err = s.st.AddRelation(blogep, mysqlep) |
| 1138 c.Assert(err, ErrorMatches, `relation already exists`) |
| 1139 // Peer. |
| 1140 s.st.AddService("riak", dummy) |
| 1141 riakep := state.RelationEndpoint{"riak", "ring", "cache", state.RolePeer
, state.ScopeGlobal} |
| 1142 _, _, err = s.st.AddRelation(riakep) |
| 1143 c.Assert(err, IsNil) |
| 1144 _, _, err = s.st.AddRelation(riakep) |
| 1145 c.Assert(err, ErrorMatches, `relation already exists`) |
| 1146 } |
| 1147 |
| 1148 func (s *StateSuite) TestAddPeerRelationIllegalEndpointNumber(c *C) { |
| 1149 dummy := s.addDummyCharm(c) |
| 1150 s.st.AddService("mysqldb", dummy) |
| 1151 s.st.AddService("wordpress", dummy) |
| 1152 s.st.AddService("riak", dummy) |
| 1153 mysqlep := state.RelationEndpoint{"mysqldb", "ifce", "cache", state.Role
Provider, state.ScopeGlobal} |
| 1154 blogep := state.RelationEndpoint{"wordpress", "ifce", "cache", state.Rol
eRequirer, state.ScopeGlobal} |
| 1155 riakep := state.RelationEndpoint{"riak", "blog", "cache", state.RolePeer
, state.ScopeGlobal} |
| 1156 _, _, err := s.st.AddRelation() |
| 1157 c.Assert(err, ErrorMatches, `can't add relations between 0 services`) |
| 1158 _, _, err = s.st.AddRelation(mysqlep, blogep, riakep) |
| 1159 c.Assert(err, ErrorMatches, `can't add relations between 3 services`) |
| 1160 } |
| 1161 |
| 1162 func (s *StateSuite) TestEnvironment(c *C) { |
| 1163 path, err := s.zkConn.Create("/environment", "type: dummy\nname: foo\n",
0, zookeeper.WorldACL(zookeeper.PERM_ALL)) |
| 1164 c.Assert(err, IsNil) |
| 1165 c.Assert(path, Equals, "/environment") |
| 1166 |
| 1167 env, err := s.st.Environment() |
| 1168 env.Read() |
| 1169 c.Assert(err, IsNil) |
| 1170 c.Assert(env.Map(), DeepEquals, map[string]interface{}{"type": "dummy",
"name": "foo"}) |
| 1171 } |
LEFT | RIGHT |