OLD | NEW |
1 // launchpad.net/juju/go/state | 1 // launchpad.net/juju/go/state |
2 // | 2 // |
3 // Copyright (c) 2011-2012 Canonical Ltd. | 3 // Copyright (c) 2011-2012 Canonical Ltd. |
4 | |
5 package state_test | 4 package state_test |
6 | 5 |
7 import ( | 6 import ( |
8 "fmt" | 7 "fmt" |
9 . "launchpad.net/gocheck" | 8 . "launchpad.net/gocheck" |
10 "launchpad.net/gozk/zookeeper" | 9 "launchpad.net/gozk/zookeeper" |
11 "launchpad.net/juju/go/charm" | 10 "launchpad.net/juju/go/charm" |
12 "launchpad.net/juju/go/state" | 11 "launchpad.net/juju/go/state" |
| 12 "net/url" |
| 13 "path/filepath" |
13 "testing" | 14 "testing" |
14 ) | 15 ) |
15 | 16 |
16 // zkCreate is a simple helper to create a node with a value based | 17 // TestPackage integrates the tests into gotest. |
17 // on the path. It uses standard parameters for ZooKeeper and the | 18 func TestPackage(t *testing.T) { |
18 // test fails when the node can't be created. | 19 » srv, dir := state.ZkSetUpEnvironment(t) |
19 func zkCreate(c *C, zk *zookeeper.Conn, path, value string) { | 20 » defer state.ZkTearDownEnvironment(t, srv, dir) |
20 » if _, err := zk.Create(path, value, 0, zookeeper.WorldACL(zookeeper.PERM
_ALL)); err != nil { | 21 |
21 » » c.Fatal("Cannot set path '"+path+"' in ZooKeeper: ", err) | 22 » TestingT(t) |
22 » } | |
23 } | 23 } |
24 | 24 |
25 // TestPackage integrates the tests into gotest. | 25 // charmDir returns a directory containing the given test charm. |
26 func TestPackage(t *testing.T) { | 26 func charmDir(name string) string { |
27 » TestingT(t) | 27 » return filepath.Join("..", "charm", "testrepo", "series", name) |
| 28 } |
| 29 |
| 30 // readCharm returns a test charm by its name. |
| 31 func readCharm(c *C, name string) charm.Charm { |
| 32 » ch, err := charm.ReadDir(charmDir(name)) |
| 33 » c.Assert(err, IsNil) |
| 34 » return ch |
| 35 } |
| 36 |
| 37 // localCharmURL returns the local URL of a charm. |
| 38 func localCharmURL(ch charm.Charm) *charm.URL { |
| 39 » url := fmt.Sprintf("local:series/%s-%d", ch.Meta().Name, ch.Revision()) |
| 40 » return charm.MustParseURL(url) |
| 41 } |
| 42 |
| 43 // addDummyCharm adds the 'dummy' charm state to st. |
| 44 func addDummyCharm(c *C, st *state.State) (*state.Charm, *charm.URL) { |
| 45 » ch := readCharm(c, "dummy") |
| 46 » curl := localCharmURL(ch) |
| 47 » bundleURL, err := url.Parse("http://bundle.url") |
| 48 » c.Assert(err, IsNil) |
| 49 » dummy, err := st.AddCharm(ch, curl, bundleURL) |
| 50 » c.Assert(err, IsNil) |
| 51 » return dummy, curl |
28 } | 52 } |
29 | 53 |
30 type StateSuite struct { | 54 type StateSuite struct { |
31 zkServer *zookeeper.Server | 55 zkServer *zookeeper.Server |
32 zkTestRoot string | 56 zkTestRoot string |
33 zkTestPort int | 57 zkTestPort int |
34 zkAddr string | 58 zkAddr string |
35 zkConn *zookeeper.Conn | 59 zkConn *zookeeper.Conn |
36 st *state.State | 60 st *state.State |
37 } | 61 } |
38 | 62 |
39 var _ = Suite(&StateSuite{}) | 63 var _ = Suite(&StateSuite{}) |
40 | 64 |
41 func (s *StateSuite) SetUpSuite(c *C) { | 65 func (s *StateSuite) SetUpTest(c *C) { |
42 var err error | 66 var err error |
43 » s.zkTestRoot = c.MkDir() + "/zookeeper" | 67 » s.st, err = state.Open(&state.Info{ |
44 » s.zkTestPort = 21812 | 68 » » Addrs: []string{state.ZkAddr}, |
45 » s.zkAddr = fmt.Sprint("localhost:", s.zkTestPort) | 69 » }) |
46 | |
47 » s.zkServer, err = zookeeper.CreateServer(s.zkTestPort, s.zkTestRoot, "") | |
48 » if err != nil { | |
49 » » c.Fatal("Cannot set up ZooKeeper server environment: ", err) | |
50 » } | |
51 » err = s.zkServer.Start() | |
52 » if err != nil { | |
53 » » c.Fatal("Cannot start ZooKeeper server: ", err) | |
54 » } | |
55 } | |
56 | |
57 func (s *StateSuite) TearDownSuite(c *C) { | |
58 » if s.zkServer != nil { | |
59 » » s.zkServer.Destroy() | |
60 » } | |
61 } | |
62 | |
63 func (s *StateSuite) SetUpTest(c *C) { | |
64 » s.st, s.zkConn = state.OpenAddr(c, s.zkAddr) | |
65 » err := s.st.Initialize() | |
66 c.Assert(err, IsNil) | 70 c.Assert(err, IsNil) |
| 71 err = s.st.Initialize() |
| 72 c.Assert(err, IsNil) |
| 73 s.zkConn = state.ZkConn(s.st) |
67 } | 74 } |
68 | 75 |
69 func (s *StateSuite) TearDownTest(c *C) { | 76 func (s *StateSuite) TearDownTest(c *C) { |
70 // Delete possible nodes, ignore errors. | 77 // Delete possible nodes, ignore errors. |
71 zkRemoveTree(s.zkConn, "/topology") | 78 zkRemoveTree(s.zkConn, "/topology") |
72 zkRemoveTree(s.zkConn, "/charms") | 79 zkRemoveTree(s.zkConn, "/charms") |
73 zkRemoveTree(s.zkConn, "/services") | 80 zkRemoveTree(s.zkConn, "/services") |
74 zkRemoveTree(s.zkConn, "/machines") | 81 zkRemoveTree(s.zkConn, "/machines") |
75 zkRemoveTree(s.zkConn, "/units") | 82 zkRemoveTree(s.zkConn, "/units") |
76 zkRemoveTree(s.zkConn, "/relations") | 83 zkRemoveTree(s.zkConn, "/relations") |
77 zkRemoveTree(s.zkConn, "/initialized") | 84 zkRemoveTree(s.zkConn, "/initialized") |
78 s.zkConn.Close() | 85 s.zkConn.Close() |
79 } | 86 } |
80 | 87 |
| 88 func (s StateSuite) TestAddCharm(c *C) { |
| 89 // Check that adding charms works correctly. |
| 90 dummyCharm := readCharm(c, "dummy") |
| 91 curl := localCharmURL(dummyCharm) |
| 92 bundleURL, err := url.Parse("http://bundle.url") |
| 93 c.Assert(err, IsNil) |
| 94 dummy, err := s.st.AddCharm(dummyCharm, curl, bundleURL) |
| 95 c.Assert(err, IsNil) |
| 96 c.Assert(dummy.URL().String(), Equals, curl.String()) |
| 97 _, _, err = s.zkConn.Children("/charms") |
| 98 c.Assert(err, IsNil) |
| 99 } |
| 100 |
| 101 func (s StateSuite) TestCharmAttributes(c *C) { |
| 102 // Check that the basic (invariant) fields of the charm |
| 103 // are correctly in place. |
| 104 _, curl := addDummyCharm(c, s.st) |
| 105 |
| 106 dummy, err := s.st.Charm(curl) |
| 107 c.Assert(err, IsNil) |
| 108 c.Assert(dummy.URL().String(), Equals, curl.String()) |
| 109 c.Assert(dummy.Revision(), Equals, 1) |
| 110 bundleURL, err := url.Parse("http://bundle.url") |
| 111 c.Assert(err, IsNil) |
| 112 c.Assert(dummy.BundleURL(), DeepEquals, bundleURL) |
| 113 meta := dummy.Meta() |
| 114 c.Assert(meta.Name, Equals, "dummy") |
| 115 config := dummy.Config() |
| 116 c.Assert(config.Options["title"], Equals, |
| 117 charm.Option{ |
| 118 Default: "My Title", |
| 119 Description: "A descriptive title used for the service."
, |
| 120 Type: "string", |
| 121 }, |
| 122 ) |
| 123 } |
| 124 |
| 125 func (s StateSuite) TestNonExistentCharmPriorToInitialization(c *C) { |
| 126 // Check that getting a charm before any other charm has been added fail
s nicely. |
| 127 curl, err := charm.ParseURL("local:series/dummy-1") |
| 128 c.Assert(err, IsNil) |
| 129 _, err = s.st.Charm(curl) |
| 130 c.Assert(err, ErrorMatches, `charm not found: "local:series/dummy-1"`) |
| 131 } |
| 132 |
| 133 func (s StateSuite) TestGetNonExistentCharm(c *C) { |
| 134 // Check that getting a non-existent charm fails nicely. |
| 135 addDummyCharm(c, s.st) |
| 136 |
| 137 curl := charm.MustParseURL("local:anotherseries/dummy-1") |
| 138 _, err := s.st.Charm(curl) |
| 139 c.Assert(err, ErrorMatches, `charm not found: "local:anotherseries/dummy
-1"`) |
| 140 } |
| 141 |
81 func (s StateSuite) TestAddService(c *C) { | 142 func (s StateSuite) TestAddService(c *C) { |
82 // Check that adding services works correctly. | 143 // Check that adding services works correctly. |
83 » charm := state.CharmMock("local:myseries/mytest-1") | 144 » dummy, curl := addDummyCharm(c, s.st) |
84 » wordpress, err := s.st.AddService("wordpress", charm) | 145 » wordpress, err := s.st.AddService("wordpress", dummy) |
85 c.Assert(err, IsNil) | 146 c.Assert(err, IsNil) |
86 c.Assert(wordpress.Name(), Equals, "wordpress") | 147 c.Assert(wordpress.Name(), Equals, "wordpress") |
87 » mysql, err := s.st.AddService("mysql", charm) | 148 » mysql, err := s.st.AddService("mysql", dummy) |
88 c.Assert(err, IsNil) | 149 c.Assert(err, IsNil) |
89 c.Assert(mysql.Name(), Equals, "mysql") | 150 c.Assert(mysql.Name(), Equals, "mysql") |
90 | 151 |
91 // Check that retrieving the new created services works correctly. | 152 // Check that retrieving the new created services works correctly. |
92 wordpress, err = s.st.Service("wordpress") | 153 wordpress, err = s.st.Service("wordpress") |
93 c.Assert(err, IsNil) | 154 c.Assert(err, IsNil) |
94 c.Assert(wordpress.Name(), Equals, "wordpress") | 155 c.Assert(wordpress.Name(), Equals, "wordpress") |
95 url, err := wordpress.CharmURL() | 156 url, err := wordpress.CharmURL() |
96 c.Assert(err, IsNil) | 157 c.Assert(err, IsNil) |
97 » c.Assert(url.String(), Equals, "local:myseries/mytest-1") | 158 » c.Assert(url.String(), Equals, curl.String()) |
98 mysql, err = s.st.Service("mysql") | 159 mysql, err = s.st.Service("mysql") |
99 c.Assert(err, IsNil) | 160 c.Assert(err, IsNil) |
100 c.Assert(mysql.Name(), Equals, "mysql") | 161 c.Assert(mysql.Name(), Equals, "mysql") |
101 url, err = mysql.CharmURL() | 162 url, err = mysql.CharmURL() |
102 c.Assert(err, IsNil) | 163 c.Assert(err, IsNil) |
103 » c.Assert(url.String(), Equals, "local:myseries/mytest-1") | 164 » c.Assert(url.String(), Equals, curl.String()) |
104 } | 165 } |
105 | 166 |
106 func (s StateSuite) TestRemoveService(c *C) { | 167 func (s StateSuite) TestRemoveService(c *C) { |
107 » service, err := s.st.AddService("wordpress", state.CharmMock("local:myse
ries/mytest-1")) | 168 » dummy, _ := addDummyCharm(c, s.st) |
| 169 » service, err := s.st.AddService("wordpress", dummy) |
108 c.Assert(err, IsNil) | 170 c.Assert(err, IsNil) |
109 | 171 |
110 // Check that removing the service works correctly. | 172 // Check that removing the service works correctly. |
111 err = s.st.RemoveService(service) | 173 err = s.st.RemoveService(service) |
112 c.Assert(err, IsNil) | 174 c.Assert(err, IsNil) |
113 service, err = s.st.Service("wordpress") | 175 service, err = s.st.Service("wordpress") |
114 c.Assert(err, ErrorMatches, `service with name "wordpress" not found`) | 176 c.Assert(err, ErrorMatches, `service with name "wordpress" not found`) |
115 } | 177 } |
116 | 178 |
117 func (s StateSuite) TestReadNonExistentService(c *C) { | 179 func (s StateSuite) TestReadNonExistentService(c *C) { |
118 _, err := s.st.Service("pressword") | 180 _, err := s.st.Service("pressword") |
119 c.Assert(err, ErrorMatches, `service with name "pressword" not found`) | 181 c.Assert(err, ErrorMatches, `service with name "pressword" not found`) |
120 } | 182 } |
121 | 183 |
122 func (s StateSuite) TestAllServices(c *C) { | 184 func (s StateSuite) TestAllServices(c *C) { |
123 // Check without existing services. | 185 // Check without existing services. |
124 services, err := s.st.AllServices() | 186 services, err := s.st.AllServices() |
125 c.Assert(err, IsNil) | 187 c.Assert(err, IsNil) |
126 c.Assert(len(services), Equals, 0) | 188 c.Assert(len(services), Equals, 0) |
127 | 189 |
128 // Check that after adding services the result is ok. | 190 // Check that after adding services the result is ok. |
129 » charm := state.CharmMock("local:myseries/mytest-1") | 191 » dummy, _ := addDummyCharm(c, s.st) |
130 » _, err = s.st.AddService("wordpress", charm) | 192 » _, err = s.st.AddService("wordpress", dummy) |
131 c.Assert(err, IsNil) | 193 c.Assert(err, IsNil) |
132 services, err = s.st.AllServices() | 194 services, err = s.st.AllServices() |
133 c.Assert(err, IsNil) | 195 c.Assert(err, IsNil) |
134 c.Assert(len(services), Equals, 1) | 196 c.Assert(len(services), Equals, 1) |
135 | 197 |
136 » _, err = s.st.AddService("mysql", charm) | 198 » _, err = s.st.AddService("mysql", dummy) |
137 c.Assert(err, IsNil) | 199 c.Assert(err, IsNil) |
138 services, err = s.st.AllServices() | 200 services, err = s.st.AllServices() |
139 c.Assert(err, IsNil) | 201 c.Assert(err, IsNil) |
140 c.Assert(len(services), Equals, 2) | 202 c.Assert(len(services), Equals, 2) |
141 | 203 |
142 // Check the returned service, order is defined by sorted keys. | 204 // Check the returned service, order is defined by sorted keys. |
143 c.Assert(services[0].Name(), Equals, "wordpress") | 205 c.Assert(services[0].Name(), Equals, "wordpress") |
144 c.Assert(services[1].Name(), Equals, "mysql") | 206 c.Assert(services[1].Name(), Equals, "mysql") |
145 } | 207 } |
146 | 208 |
147 func (s StateSuite) TestServiceCharm(c *C) { | 209 func (s StateSuite) TestServiceCharm(c *C) { |
148 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 210 » dummy, curl := addDummyCharm(c, s.st) |
| 211 » wordpress, err := s.st.AddService("wordpress", dummy) |
149 c.Assert(err, IsNil) | 212 c.Assert(err, IsNil) |
150 | 213 |
151 // Check that getting and setting the service charm URL works correctly. | 214 // Check that getting and setting the service charm URL works correctly. |
152 » url, err := wordpress.CharmURL() | 215 » testcurl, err := wordpress.CharmURL() |
153 c.Assert(err, IsNil) | 216 c.Assert(err, IsNil) |
154 » c.Assert(url.String(), Equals, "local:myseries/mytest-1") | 217 » c.Assert(testcurl.String(), Equals, curl.String()) |
155 » url, err = charm.ParseURL("local:myseries/myprod-1") | 218 » testcurl, err = charm.ParseURL("local:myseries/mydummy-1") |
156 c.Assert(err, IsNil) | 219 c.Assert(err, IsNil) |
157 » err = wordpress.SetCharmURL(url) | 220 » err = wordpress.SetCharmURL(testcurl) |
158 c.Assert(err, IsNil) | 221 c.Assert(err, IsNil) |
159 » url, err = wordpress.CharmURL() | 222 » testcurl, err = wordpress.CharmURL() |
160 c.Assert(err, IsNil) | 223 c.Assert(err, IsNil) |
161 » c.Assert(url.String(), Equals, "local:myseries/myprod-1") | 224 » c.Assert(testcurl.String(), Equals, "local:myseries/mydummy-1") |
162 } | 225 } |
163 | 226 |
164 func (s StateSuite) TestServiceExposed(c *C) { | 227 func (s StateSuite) TestServiceExposed(c *C) { |
165 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 228 » dummy, _ := addDummyCharm(c, s.st) |
| 229 » wordpress, err := s.st.AddService("wordpress", dummy) |
166 c.Assert(err, IsNil) | 230 c.Assert(err, IsNil) |
167 | 231 |
168 // Check that querying for the exposed flag works correctly. | 232 // Check that querying for the exposed flag works correctly. |
169 exposed, err := wordpress.IsExposed() | 233 exposed, err := wordpress.IsExposed() |
170 c.Assert(err, IsNil) | 234 c.Assert(err, IsNil) |
171 c.Assert(exposed, Equals, false) | 235 c.Assert(exposed, Equals, false) |
172 | 236 |
173 // Check that setting and clearing the exposed flag works correctly. | 237 // Check that setting and clearing the exposed flag works correctly. |
174 err = wordpress.SetExposed() | 238 err = wordpress.SetExposed() |
175 c.Assert(err, IsNil) | 239 c.Assert(err, IsNil) |
(...skipping 17 matching lines...) Expand all Loading... |
193 c.Assert(err, IsNil) | 257 c.Assert(err, IsNil) |
194 | 258 |
195 // Check that setting and clearing the exposed flag on removed services
also doesn't fail. | 259 // Check that setting and clearing the exposed flag on removed services
also doesn't fail. |
196 err = s.st.RemoveService(wordpress) | 260 err = s.st.RemoveService(wordpress) |
197 c.Assert(err, IsNil) | 261 c.Assert(err, IsNil) |
198 err = wordpress.ClearExposed() | 262 err = wordpress.ClearExposed() |
199 c.Assert(err, IsNil) | 263 c.Assert(err, IsNil) |
200 } | 264 } |
201 | 265 |
202 func (s StateSuite) TestAddUnit(c *C) { | 266 func (s StateSuite) TestAddUnit(c *C) { |
203 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 267 » dummy, _ := addDummyCharm(c, s.st) |
| 268 » wordpress, err := s.st.AddService("wordpress", dummy) |
204 c.Assert(err, IsNil) | 269 c.Assert(err, IsNil) |
205 | 270 |
206 // Check that adding units works. | 271 // Check that adding units works. |
207 unitZero, err := wordpress.AddUnit() | 272 unitZero, err := wordpress.AddUnit() |
208 c.Assert(err, IsNil) | 273 c.Assert(err, IsNil) |
209 c.Assert(unitZero.Name(), Equals, "wordpress/0") | 274 c.Assert(unitZero.Name(), Equals, "wordpress/0") |
210 unitOne, err := wordpress.AddUnit() | 275 unitOne, err := wordpress.AddUnit() |
211 c.Assert(err, IsNil) | 276 c.Assert(err, IsNil) |
212 c.Assert(unitOne.Name(), Equals, "wordpress/1") | 277 c.Assert(unitOne.Name(), Equals, "wordpress/1") |
213 } | 278 } |
214 | 279 |
215 func (s StateSuite) TestReadUnit(c *C) { | 280 func (s StateSuite) TestReadUnit(c *C) { |
216 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 281 » dummy, _ := addDummyCharm(c, s.st) |
| 282 » wordpress, err := s.st.AddService("wordpress", dummy) |
217 c.Assert(err, IsNil) | 283 c.Assert(err, IsNil) |
218 _, err = wordpress.AddUnit() | 284 _, err = wordpress.AddUnit() |
219 c.Assert(err, IsNil) | 285 c.Assert(err, IsNil) |
220 _, err = wordpress.AddUnit() | 286 _, err = wordpress.AddUnit() |
221 c.Assert(err, IsNil) | 287 c.Assert(err, IsNil) |
222 » mysql, err := s.st.AddService("mysql", state.CharmMock("local:myseries/m
ytest-1")) | 288 » mysql, err := s.st.AddService("mysql", dummy) |
223 c.Assert(err, IsNil) | 289 c.Assert(err, IsNil) |
224 _, err = mysql.AddUnit() | 290 _, err = mysql.AddUnit() |
225 c.Assert(err, IsNil) | 291 c.Assert(err, IsNil) |
226 | 292 |
227 // Check that retrieving a unit works correctly. | 293 // Check that retrieving a unit works correctly. |
228 unit, err := wordpress.Unit("wordpress/0") | 294 unit, err := wordpress.Unit("wordpress/0") |
229 c.Assert(err, IsNil) | 295 c.Assert(err, IsNil) |
230 c.Assert(unit.Name(), Equals, "wordpress/0") | 296 c.Assert(unit.Name(), Equals, "wordpress/0") |
231 | 297 |
232 // Check that retrieving a non-existent or an invalidly | 298 // Check that retrieving a non-existent or an invalidly |
233 // named unit fail nicely. | 299 // named unit fail nicely. |
234 unit, err = wordpress.Unit("wordpress") | 300 unit, err = wordpress.Unit("wordpress") |
235 c.Assert(err, ErrorMatches, `"wordpress" is not a valid unit name`) | 301 c.Assert(err, ErrorMatches, `"wordpress" is not a valid unit name`) |
236 unit, err = wordpress.Unit("wordpress/0/0") | 302 unit, err = wordpress.Unit("wordpress/0/0") |
237 c.Assert(err, ErrorMatches, `"wordpress/0/0" is not a valid unit name`) | 303 c.Assert(err, ErrorMatches, `"wordpress/0/0" is not a valid unit name`) |
238 unit, err = wordpress.Unit("pressword/0") | 304 unit, err = wordpress.Unit("pressword/0") |
239 c.Assert(err, ErrorMatches, `can't find unit "pressword/0" on service "w
ordpress"`) | 305 c.Assert(err, ErrorMatches, `can't find unit "pressword/0" on service "w
ordpress"`) |
240 unit, err = wordpress.Unit("mysql/0") | 306 unit, err = wordpress.Unit("mysql/0") |
241 c.Assert(err, ErrorMatches, `can't find unit "mysql/0" on service "wordp
ress"`) | 307 c.Assert(err, ErrorMatches, `can't find unit "mysql/0" on service "wordp
ress"`) |
242 | 308 |
243 // Check that retrieving unit names works. | 309 // Check that retrieving unit names works. |
244 unitNames, err := wordpress.UnitNames() | 310 unitNames, err := wordpress.UnitNames() |
245 c.Assert(err, IsNil) | 311 c.Assert(err, IsNil) |
246 » c.Assert(unitNames, Equals, []string{"wordpress/0", "wordpress/1"}) | 312 » c.Assert(unitNames, DeepEquals, []string{"wordpress/0", "wordpress/1"}) |
247 | 313 |
248 // Check that retrieving all units works. | 314 // Check that retrieving all units works. |
249 units, err := wordpress.AllUnits() | 315 units, err := wordpress.AllUnits() |
250 c.Assert(err, IsNil) | 316 c.Assert(err, IsNil) |
251 c.Assert(len(units), Equals, 2) | 317 c.Assert(len(units), Equals, 2) |
252 c.Assert(units[0].Name(), Equals, "wordpress/0") | 318 c.Assert(units[0].Name(), Equals, "wordpress/0") |
253 c.Assert(units[1].Name(), Equals, "wordpress/1") | 319 c.Assert(units[1].Name(), Equals, "wordpress/1") |
254 } | 320 } |
255 | 321 |
256 func (s StateSuite) TestReadUnitWithChangingState(c *C) { | 322 func (s StateSuite) TestReadUnitWithChangingState(c *C) { |
257 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 323 » dummy, _ := addDummyCharm(c, s.st) |
| 324 » wordpress, err := s.st.AddService("wordpress", dummy) |
258 c.Assert(err, IsNil) | 325 c.Assert(err, IsNil) |
259 | 326 |
260 // Check that reading a unit after removing the service | 327 // Check that reading a unit after removing the service |
261 // fails nicely. | 328 // fails nicely. |
262 err = s.st.RemoveService(wordpress) | 329 err = s.st.RemoveService(wordpress) |
263 c.Assert(err, IsNil) | 330 c.Assert(err, IsNil) |
264 _, err = s.st.Unit("wordpress/0") | 331 _, err = s.st.Unit("wordpress/0") |
265 c.Assert(err, ErrorMatches, `service with name "wordpress" not found`) | 332 c.Assert(err, ErrorMatches, `service with name "wordpress" not found`) |
266 } | 333 } |
267 | 334 |
268 func (s StateSuite) TestRemoveUnit(c *C) { | 335 func (s StateSuite) TestRemoveUnit(c *C) { |
269 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 336 » dummy, _ := addDummyCharm(c, s.st) |
| 337 » wordpress, err := s.st.AddService("wordpress", dummy) |
270 c.Assert(err, IsNil) | 338 c.Assert(err, IsNil) |
271 _, err = wordpress.AddUnit() | 339 _, err = wordpress.AddUnit() |
272 c.Assert(err, IsNil) | 340 c.Assert(err, IsNil) |
273 _, err = wordpress.AddUnit() | 341 _, err = wordpress.AddUnit() |
274 c.Assert(err, IsNil) | 342 c.Assert(err, IsNil) |
275 | 343 |
276 // Check that removing a unit works. | 344 // Check that removing a unit works. |
277 unit, err := wordpress.Unit("wordpress/0") | 345 unit, err := wordpress.Unit("wordpress/0") |
278 c.Assert(err, IsNil) | 346 c.Assert(err, IsNil) |
279 err = wordpress.RemoveUnit(unit) | 347 err = wordpress.RemoveUnit(unit) |
280 c.Assert(err, IsNil) | 348 c.Assert(err, IsNil) |
281 unitNames, err := wordpress.UnitNames() | 349 unitNames, err := wordpress.UnitNames() |
282 c.Assert(err, IsNil) | 350 c.Assert(err, IsNil) |
283 » c.Assert(unitNames, Equals, []string{"wordpress/1"}) | 351 » c.Assert(unitNames, DeepEquals, []string{"wordpress/1"}) |
284 | 352 |
285 // Check that removing a non-existent unit fails nicely. | 353 // Check that removing a non-existent unit fails nicely. |
286 err = wordpress.RemoveUnit(unit) | 354 err = wordpress.RemoveUnit(unit) |
287 c.Assert(err, ErrorMatches, "environment state has changed") | 355 c.Assert(err, ErrorMatches, "environment state has changed") |
288 } | 356 } |
289 | 357 |
290 func (s StateSuite) TestGetSetPublicAddress(c *C) { | 358 func (s StateSuite) TestGetSetPublicAddress(c *C) { |
291 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 359 » dummy, _ := addDummyCharm(c, s.st) |
| 360 » wordpress, err := s.st.AddService("wordpress", dummy) |
292 c.Assert(err, IsNil) | 361 c.Assert(err, IsNil) |
293 unit, err := wordpress.AddUnit() | 362 unit, err := wordpress.AddUnit() |
294 c.Assert(err, IsNil) | 363 c.Assert(err, IsNil) |
295 | 364 |
296 // Check that retrieving and setting of a public address works. | 365 // Check that retrieving and setting of a public address works. |
297 address, err := unit.PublicAddress() | 366 address, err := unit.PublicAddress() |
298 c.Assert(err, ErrorMatches, "unit has no public address") | 367 c.Assert(err, ErrorMatches, "unit has no public address") |
299 err = unit.SetPublicAddress("example.foobar.com") | 368 err = unit.SetPublicAddress("example.foobar.com") |
300 c.Assert(err, IsNil) | 369 c.Assert(err, IsNil) |
301 address, err = unit.PublicAddress() | 370 address, err = unit.PublicAddress() |
302 c.Assert(err, IsNil) | 371 c.Assert(err, IsNil) |
303 c.Assert(address, Equals, "example.foobar.com") | 372 c.Assert(address, Equals, "example.foobar.com") |
304 } | 373 } |
305 | 374 |
306 func (s StateSuite) TestGetSetPrivateAddress(c *C) { | 375 func (s StateSuite) TestGetSetPrivateAddress(c *C) { |
307 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 376 » dummy, _ := addDummyCharm(c, s.st) |
| 377 » wordpress, err := s.st.AddService("wordpress", dummy) |
308 c.Assert(err, IsNil) | 378 c.Assert(err, IsNil) |
309 unit, err := wordpress.AddUnit() | 379 unit, err := wordpress.AddUnit() |
310 c.Assert(err, IsNil) | 380 c.Assert(err, IsNil) |
311 | 381 |
312 // Check that retrieving and setting of a private address works. | 382 // Check that retrieving and setting of a private address works. |
313 address, err := unit.PrivateAddress() | 383 address, err := unit.PrivateAddress() |
314 c.Assert(err, ErrorMatches, "unit has no private address") | 384 c.Assert(err, ErrorMatches, "unit has no private address") |
315 err = unit.SetPrivateAddress("example.local") | 385 err = unit.SetPrivateAddress("example.local") |
316 c.Assert(err, IsNil) | 386 c.Assert(err, IsNil) |
317 address, err = unit.PrivateAddress() | 387 address, err = unit.PrivateAddress() |
318 c.Assert(err, IsNil) | 388 c.Assert(err, IsNil) |
319 c.Assert(address, Equals, "example.local") | 389 c.Assert(address, Equals, "example.local") |
320 } | 390 } |
321 | 391 |
322 func (s StateSuite) TestUnitCharm(c *C) { | 392 func (s StateSuite) TestUnitCharm(c *C) { |
323 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 393 » dummy, curl := addDummyCharm(c, s.st) |
| 394 » wordpress, err := s.st.AddService("wordpress", dummy) |
324 c.Assert(err, IsNil) | 395 c.Assert(err, IsNil) |
325 unit, err := wordpress.AddUnit() | 396 unit, err := wordpress.AddUnit() |
326 c.Assert(err, IsNil) | 397 c.Assert(err, IsNil) |
327 | 398 |
328 // Check that getting and setting the unit charm URL works correctly. | 399 // Check that getting and setting the unit charm URL works correctly. |
329 » url, err := unit.CharmURL() | 400 » testcurl, err := unit.CharmURL() |
330 c.Assert(err, IsNil) | 401 c.Assert(err, IsNil) |
331 » c.Assert(url.String(), Equals, "local:myseries/mytest-1") | 402 » c.Assert(testcurl.String(), Equals, curl.String()) |
332 » url, err = charm.ParseURL("local:myseries/myprod-1") | 403 » testcurl, err = charm.ParseURL("local:myseries/mydummy-1") |
333 c.Assert(err, IsNil) | 404 c.Assert(err, IsNil) |
334 » err = unit.SetCharmURL(url) | 405 » err = unit.SetCharmURL(testcurl) |
335 c.Assert(err, IsNil) | 406 c.Assert(err, IsNil) |
336 » url, err = unit.CharmURL() | 407 » testcurl, err = unit.CharmURL() |
337 c.Assert(err, IsNil) | 408 c.Assert(err, IsNil) |
338 » c.Assert(url.String(), Equals, "local:myseries/myprod-1") | 409 » c.Assert(testcurl.String(), Equals, "local:myseries/mydummy-1") |
339 } | 410 } |
340 | 411 |
341 func (s StateSuite) TestUnassignUnitFromMachineWithoutBeingAssigned(c *C) { | 412 func (s StateSuite) TestUnassignUnitFromMachineWithoutBeingAssigned(c *C) { |
342 // When unassigning a machine from a unit, it is possible that | 413 // When unassigning a machine from a unit, it is possible that |
343 // the machine has not been previously assigned, or that it | 414 // the machine has not been previously assigned, or that it |
344 // was assigned but the state changed beneath us. In either | 415 // was assigned but the state changed beneath us. In either |
345 // case, the end state is the intended state, so we simply | 416 // case, the end state is the intended state, so we simply |
346 // move forward without any errors here, to avoid having to | 417 // move forward without any errors here, to avoid having to |
347 // handle the extra complexity of dealing with the concurrency | 418 // handle the extra complexity of dealing with the concurrency |
348 // problems. | 419 // problems. |
349 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 420 » dummy, _ := addDummyCharm(c, s.st) |
| 421 » wordpress, err := s.st.AddService("wordpress", dummy) |
350 c.Assert(err, IsNil) | 422 c.Assert(err, IsNil) |
351 unit, err := wordpress.AddUnit() | 423 unit, err := wordpress.AddUnit() |
352 c.Assert(err, IsNil) | 424 c.Assert(err, IsNil) |
353 | 425 |
354 err = unit.UnassignFromMachine() | 426 err = unit.UnassignFromMachine() |
355 c.Assert(err, IsNil) | 427 c.Assert(err, IsNil) |
356 | 428 |
357 // Check that the unit has no machine assigned. | 429 // Check that the unit has no machine assigned. |
358 wordpress, err = s.st.Service("wordpress") | 430 wordpress, err = s.st.Service("wordpress") |
359 c.Assert(err, IsNil) | 431 c.Assert(err, IsNil) |
360 units, err := wordpress.AllUnits() | 432 units, err := wordpress.AllUnits() |
361 c.Assert(err, IsNil) | 433 c.Assert(err, IsNil) |
362 unit = units[0] | 434 unit = units[0] |
363 _, err = unit.AssignedMachineId() | 435 _, err = unit.AssignedMachineId() |
364 c.Assert(err, ErrorMatches, `unit not assigned to machine`) | 436 c.Assert(err, ErrorMatches, `unit not assigned to machine`) |
365 } | 437 } |
366 | 438 |
367 func (s StateSuite) TestAssignUnitToMachineAgainFails(c *C) { | 439 func (s StateSuite) TestAssignUnitToMachineAgainFails(c *C) { |
368 // Check that assigning an already assigned unit to | 440 // Check that assigning an already assigned unit to |
369 // a machine fails if it isn't precisely the same | 441 // a machine fails if it isn't precisely the same |
370 // machine.· | 442 // machine.· |
371 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 443 » dummy, _ := addDummyCharm(c, s.st) |
| 444 » wordpress, err := s.st.AddService("wordpress", dummy) |
372 c.Assert(err, IsNil) | 445 c.Assert(err, IsNil) |
373 unit, err := wordpress.AddUnit() | 446 unit, err := wordpress.AddUnit() |
374 c.Assert(err, IsNil) | 447 c.Assert(err, IsNil) |
375 machineOne, err := s.st.AddMachine() | 448 machineOne, err := s.st.AddMachine() |
376 c.Assert(err, IsNil) | 449 c.Assert(err, IsNil) |
377 machineTwo, err := s.st.AddMachine() | 450 machineTwo, err := s.st.AddMachine() |
378 c.Assert(err, IsNil) | 451 c.Assert(err, IsNil) |
379 | 452 |
380 err = unit.AssignToMachine(machineOne) | 453 err = unit.AssignToMachine(machineOne) |
381 c.Assert(err, IsNil) | 454 c.Assert(err, IsNil) |
382 | 455 |
383 // Assigning the unit to the same machine should return no error. | 456 // Assigning the unit to the same machine should return no error. |
384 err = unit.AssignToMachine(machineOne) | 457 err = unit.AssignToMachine(machineOne) |
385 c.Assert(err, IsNil) | 458 c.Assert(err, IsNil) |
386 | 459 |
387 // Assigning the unit to a different machine should fail. | 460 // Assigning the unit to a different machine should fail. |
388 err = unit.AssignToMachine(machineTwo) | 461 err = unit.AssignToMachine(machineTwo) |
389 c.Assert(err, ErrorMatches, `unit "wordpress/0" already assigned to mach
ine 0`) | 462 c.Assert(err, ErrorMatches, `unit "wordpress/0" already assigned to mach
ine 0`) |
390 | 463 |
391 machineId, err := unit.AssignedMachineId() | 464 machineId, err := unit.AssignedMachineId() |
392 c.Assert(err, IsNil) | 465 c.Assert(err, IsNil) |
393 c.Assert(machineId, Equals, 0) | 466 c.Assert(machineId, Equals, 0) |
394 } | 467 } |
395 | 468 |
396 func (s StateSuite) TestUnassignUnitFromMachineWithChangingState(c *C) { | 469 func (s StateSuite) TestUnassignUnitFromMachineWithChangingState(c *C) { |
397 » // Check | 470 » // Check that unassigning while the state changes fails nicely. |
398 » wordpress, err := s.st.AddService("wordpress", state.CharmMock("local:my
series/mytest-1")) | 471 » dummy, _ := addDummyCharm(c, s.st) |
| 472 » wordpress, err := s.st.AddService("wordpress", dummy) |
399 c.Assert(err, IsNil) | 473 c.Assert(err, IsNil) |
400 unit, err := wordpress.AddUnit() | 474 unit, err := wordpress.AddUnit() |
401 c.Assert(err, IsNil) | 475 c.Assert(err, IsNil) |
402 | 476 |
403 // Remove the unit for the tests. | 477 // Remove the unit for the tests. |
404 wordpress, err = s.st.Service("wordpress") | 478 wordpress, err = s.st.Service("wordpress") |
405 c.Assert(err, IsNil) | 479 c.Assert(err, IsNil) |
406 units, err := wordpress.AllUnits() | 480 units, err := wordpress.AllUnits() |
407 c.Assert(err, IsNil) | 481 c.Assert(err, IsNil) |
408 unit = units[0] | 482 unit = units[0] |
(...skipping 12 matching lines...) Expand all Loading... |
421 c.Assert(err, ErrorMatches, "environment state has changed") | 495 c.Assert(err, ErrorMatches, "environment state has changed") |
422 _, err = unit.AssignedMachineId() | 496 _, err = unit.AssignedMachineId() |
423 c.Assert(err, ErrorMatches, "environment state has changed") | 497 c.Assert(err, ErrorMatches, "environment state has changed") |
424 } | 498 } |
425 | 499 |
426 func (s StateSuite) TestAssignUnitToUnusedMachine(c *C) { | 500 func (s StateSuite) TestAssignUnitToUnusedMachine(c *C) { |
427 // Create root machine that shouldn't be useds. | 501 // Create root machine that shouldn't be useds. |
428 _, err := s.st.AddMachine() | 502 _, err := s.st.AddMachine() |
429 c.Assert(err, IsNil) | 503 c.Assert(err, IsNil) |
430 // Check that a unit can be assigned to an unused machine. | 504 // Check that a unit can be assigned to an unused machine. |
431 » mysqlService, err := s.st.AddService("mysql", state.CharmMock("local:mys
eries/mytest-1")) | 505 » dummy, _ := addDummyCharm(c, s.st) |
| 506 » mysqlService, err := s.st.AddService("mysql", dummy) |
432 c.Assert(err, IsNil) | 507 c.Assert(err, IsNil) |
433 mysqlUnit, err := mysqlService.AddUnit() | 508 mysqlUnit, err := mysqlService.AddUnit() |
434 c.Assert(err, IsNil) | 509 c.Assert(err, IsNil) |
435 mysqlMachine, err := s.st.AddMachine() | 510 mysqlMachine, err := s.st.AddMachine() |
436 c.Assert(err, IsNil) | 511 c.Assert(err, IsNil) |
437 err = mysqlUnit.AssignToMachine(mysqlMachine) | 512 err = mysqlUnit.AssignToMachine(mysqlMachine) |
438 c.Assert(err, IsNil) | 513 c.Assert(err, IsNil) |
439 err = s.st.RemoveService(mysqlService) | 514 err = s.st.RemoveService(mysqlService) |
440 c.Assert(err, IsNil) | 515 c.Assert(err, IsNil) |
441 | 516 |
442 » wordpressService, err := s.st.AddService("wordpress", state.CharmMock("l
ocal:myseries/mytest-1")) | 517 » wordpressService, err := s.st.AddService("wordpress", dummy) |
443 c.Assert(err, IsNil) | 518 c.Assert(err, IsNil) |
444 wordpressUnit, err := wordpressService.AddUnit() | 519 wordpressUnit, err := wordpressService.AddUnit() |
445 c.Assert(err, IsNil) | 520 c.Assert(err, IsNil) |
446 wordpressMachine, err := wordpressUnit.AssignToUnusedMachine() | 521 wordpressMachine, err := wordpressUnit.AssignToUnusedMachine() |
447 c.Assert(err, IsNil) | 522 c.Assert(err, IsNil) |
448 | 523 |
449 c.Assert(wordpressMachine.Id(), Equals, mysqlMachine.Id()) | 524 c.Assert(wordpressMachine.Id(), Equals, mysqlMachine.Id()) |
450 } | 525 } |
451 | 526 |
452 func (s StateSuite) TestAssignUnitToUnusedMachineWithChangingService(c *C) { | 527 func (s StateSuite) TestAssignUnitToUnusedMachineWithChangingService(c *C) { |
453 // Create root machine that shouldn't be useds. | 528 // Create root machine that shouldn't be useds. |
454 _, err := s.st.AddMachine() | 529 _, err := s.st.AddMachine() |
455 c.Assert(err, IsNil) | 530 c.Assert(err, IsNil) |
456 // Check for a 'state changed' error if a service is manipulated | 531 // Check for a 'state changed' error if a service is manipulated |
457 // during reuse. | 532 // during reuse. |
458 » mysqlService, err := s.st.AddService("mysql", state.CharmMock("local:mys
eries/mytest-1")) | 533 » dummy, _ := addDummyCharm(c, s.st) |
| 534 » mysqlService, err := s.st.AddService("mysql", dummy) |
459 c.Assert(err, IsNil) | 535 c.Assert(err, IsNil) |
460 mysqlUnit, err := mysqlService.AddUnit() | 536 mysqlUnit, err := mysqlService.AddUnit() |
461 c.Assert(err, IsNil) | 537 c.Assert(err, IsNil) |
462 mysqlMachine, err := s.st.AddMachine() | 538 mysqlMachine, err := s.st.AddMachine() |
463 c.Assert(err, IsNil) | 539 c.Assert(err, IsNil) |
464 err = mysqlUnit.AssignToMachine(mysqlMachine) | 540 err = mysqlUnit.AssignToMachine(mysqlMachine) |
465 c.Assert(err, IsNil) | 541 c.Assert(err, IsNil) |
466 err = s.st.RemoveService(mysqlService) | 542 err = s.st.RemoveService(mysqlService) |
467 c.Assert(err, IsNil) | 543 c.Assert(err, IsNil) |
468 | 544 |
469 » wordpressService, err := s.st.AddService("wordpress", state.CharmMock("l
ocal:myseries/mytest-1")) | 545 » wordpressService, err := s.st.AddService("wordpress", dummy) |
470 c.Assert(err, IsNil) | 546 c.Assert(err, IsNil) |
471 wordpressUnit, err := wordpressService.AddUnit() | 547 wordpressUnit, err := wordpressService.AddUnit() |
472 c.Assert(err, IsNil) | 548 c.Assert(err, IsNil) |
473 err = s.st.RemoveService(wordpressService) | 549 err = s.st.RemoveService(wordpressService) |
474 c.Assert(err, IsNil) | 550 c.Assert(err, IsNil) |
475 | 551 |
476 _, err = wordpressUnit.AssignToUnusedMachine() | 552 _, err = wordpressUnit.AssignToUnusedMachine() |
477 c.Assert(err, ErrorMatches, "environment state has changed") | 553 c.Assert(err, ErrorMatches, "environment state has changed") |
478 } | 554 } |
479 | 555 |
480 func (s StateSuite) TestAssignUniToUnusedMachineWithChangingUnit(c *C) { | 556 func (s StateSuite) TestAssignUnitToUnusedMachineWithChangingUnit(c *C) { |
481 // Create root machine that shouldn't be useds. | 557 // Create root machine that shouldn't be useds. |
482 _, err := s.st.AddMachine() | 558 _, err := s.st.AddMachine() |
483 c.Assert(err, IsNil) | 559 c.Assert(err, IsNil) |
484 // Check for a 'state changed' error if a unit is manipulated | 560 // Check for a 'state changed' error if a unit is manipulated |
485 // during reuse. | 561 // during reuse. |
486 » mysqlService, err := s.st.AddService("mysql", state.CharmMock("local:mys
eries/mytest-1")) | 562 » dummy, _ := addDummyCharm(c, s.st) |
| 563 » mysqlService, err := s.st.AddService("mysql", dummy) |
487 c.Assert(err, IsNil) | 564 c.Assert(err, IsNil) |
488 mysqlUnit, err := mysqlService.AddUnit() | 565 mysqlUnit, err := mysqlService.AddUnit() |
489 c.Assert(err, IsNil) | 566 c.Assert(err, IsNil) |
490 mysqlMachine, err := s.st.AddMachine() | 567 mysqlMachine, err := s.st.AddMachine() |
491 c.Assert(err, IsNil) | 568 c.Assert(err, IsNil) |
492 err = mysqlUnit.AssignToMachine(mysqlMachine) | 569 err = mysqlUnit.AssignToMachine(mysqlMachine) |
493 c.Assert(err, IsNil) | 570 c.Assert(err, IsNil) |
494 err = s.st.RemoveService(mysqlService) | 571 err = s.st.RemoveService(mysqlService) |
495 c.Assert(err, IsNil) | 572 c.Assert(err, IsNil) |
496 | 573 |
497 » wordpressService, err := s.st.AddService("wordpress", state.CharmMock("l
ocal:myseries/mytest-1")) | 574 » wordpressService, err := s.st.AddService("wordpress", dummy) |
498 c.Assert(err, IsNil) | 575 c.Assert(err, IsNil) |
499 wordpressUnit, err := wordpressService.AddUnit() | 576 wordpressUnit, err := wordpressService.AddUnit() |
500 c.Assert(err, IsNil) | 577 c.Assert(err, IsNil) |
501 err = wordpressService.RemoveUnit(wordpressUnit) | 578 err = wordpressService.RemoveUnit(wordpressUnit) |
502 c.Assert(err, IsNil) | 579 c.Assert(err, IsNil) |
503 | 580 |
504 _, err = wordpressUnit.AssignToUnusedMachine() | 581 _, err = wordpressUnit.AssignToUnusedMachine() |
505 c.Assert(err, ErrorMatches, "environment state has changed") | 582 c.Assert(err, ErrorMatches, "environment state has changed") |
506 } | 583 } |
507 | 584 |
508 func (s StateSuite) TestAssignUnitToUnusedMachineOnlyZero(c *C) { | 585 func (s StateSuite) TestAssignUnitToUnusedMachineOnlyZero(c *C) { |
509 // Create root machine that shouldn't be useds. | 586 // Create root machine that shouldn't be useds. |
510 _, err := s.st.AddMachine() | 587 _, err := s.st.AddMachine() |
511 c.Assert(err, IsNil) | 588 c.Assert(err, IsNil) |
512 // Check that the unit can't be assigned to machine zero. | 589 // Check that the unit can't be assigned to machine zero. |
513 » wordpressService, err := s.st.AddService("wordpress", state.CharmMock("l
ocal:myseries/mytest-1")) | 590 » dummy, _ := addDummyCharm(c, s.st) |
| 591 » wordpressService, err := s.st.AddService("wordpress", dummy) |
514 c.Assert(err, IsNil) | 592 c.Assert(err, IsNil) |
515 wordpressUnit, err := wordpressService.AddUnit() | 593 wordpressUnit, err := wordpressService.AddUnit() |
516 c.Assert(err, IsNil) | 594 c.Assert(err, IsNil) |
517 | 595 |
518 _, err = wordpressUnit.AssignToUnusedMachine() | 596 _, err = wordpressUnit.AssignToUnusedMachine() |
519 c.Assert(err, ErrorMatches, "no unused machine found") | 597 c.Assert(err, ErrorMatches, "no unused machine found") |
520 } | 598 } |
521 | 599 |
522 func (s StateSuite) TestAssignUnitToUnusedMachineNoneAvailable(c *C) { | 600 func (s StateSuite) TestAssignUnitToUnusedMachineNoneAvailable(c *C) { |
523 // Create machine 0, that shouldn't be used. | 601 // Create machine 0, that shouldn't be used. |
524 _, err := s.st.AddMachine() | 602 _, err := s.st.AddMachine() |
525 c.Assert(err, IsNil) | 603 c.Assert(err, IsNil) |
526 // Check that assigning without unused machine fails.··· | 604 // Check that assigning without unused machine fails.··· |
527 » mysqlService, err := s.st.AddService("mysql", state.CharmMock("local:mys
eries/mytest-1")) | 605 » dummy, _ := addDummyCharm(c, s.st) |
| 606 » mysqlService, err := s.st.AddService("mysql", dummy) |
528 c.Assert(err, IsNil) | 607 c.Assert(err, IsNil) |
529 mysqlUnit, err := mysqlService.AddUnit() | 608 mysqlUnit, err := mysqlService.AddUnit() |
530 c.Assert(err, IsNil) | 609 c.Assert(err, IsNil) |
531 mysqlMachine, err := s.st.AddMachine() | 610 mysqlMachine, err := s.st.AddMachine() |
532 c.Assert(err, IsNil) | 611 c.Assert(err, IsNil) |
533 err = mysqlUnit.AssignToMachine(mysqlMachine) | 612 err = mysqlUnit.AssignToMachine(mysqlMachine) |
534 c.Assert(err, IsNil) | 613 c.Assert(err, IsNil) |
535 | 614 |
536 » wordpressService, err := s.st.AddService("wordpress", state.CharmMock("l
ocal:myseries/mytest-1")) | 615 » wordpressService, err := s.st.AddService("wordpress", dummy) |
537 c.Assert(err, IsNil) | 616 c.Assert(err, IsNil) |
538 wordpressUnit, err := wordpressService.AddUnit() | 617 wordpressUnit, err := wordpressService.AddUnit() |
539 c.Assert(err, IsNil) | 618 c.Assert(err, IsNil) |
540 | 619 |
541 _, err = wordpressUnit.AssignToUnusedMachine() | 620 _, err = wordpressUnit.AssignToUnusedMachine() |
542 c.Assert(err, ErrorMatches, "no unused machine found") | 621 c.Assert(err, ErrorMatches, "no unused machine found") |
543 } | 622 } |
544 | 623 |
545 // zkRemoveTree recursively removes a tree. | 624 // zkRemoveTree recursively removes a tree. |
546 func zkRemoveTree(zk *zookeeper.Conn, path string) error { | 625 func zkRemoveTree(zk *zookeeper.Conn, path string) error { |
547 // First recursively delete the children. | 626 // First recursively delete the children. |
548 children, _, err := zk.Children(path) | 627 children, _, err := zk.Children(path) |
549 if err != nil { | 628 if err != nil { |
550 return err | 629 return err |
551 } | 630 } |
552 for _, child := range children { | 631 for _, child := range children { |
553 if err = zkRemoveTree(zk, fmt.Sprintf("%s/%s", path, child)); er
r != nil { | 632 if err = zkRemoveTree(zk, fmt.Sprintf("%s/%s", path, child)); er
r != nil { |
554 return err | 633 return err |
555 } | 634 } |
556 } | 635 } |
557 // Now delete the path itself. | 636 // Now delete the path itself. |
558 return zk.Delete(path, -1) | 637 return zk.Delete(path, -1) |
559 } | 638 } |
OLD | NEW |