LEFT | RIGHT |
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 package state_test | 4 package state_test |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | 7 "fmt" |
8 . "launchpad.net/gocheck" | 8 . "launchpad.net/gocheck" |
9 "launchpad.net/gozk/zookeeper" | 9 "launchpad.net/gozk/zookeeper" |
10 "launchpad.net/juju/go/charm" | 10 "launchpad.net/juju/go/charm" |
11 "launchpad.net/juju/go/state" | 11 "launchpad.net/juju/go/state" |
| 12 "net/url" |
12 "path/filepath" | 13 "path/filepath" |
13 "testing" | 14 "testing" |
14 ) | 15 ) |
15 | 16 |
16 // TestPackage integrates the tests into gotest. | 17 // TestPackage integrates the tests into gotest. |
17 func TestPackage(t *testing.T) { | 18 func TestPackage(t *testing.T) { |
18 srv, dir := state.ZkSetUpEnvironment(t) | 19 srv, dir := state.ZkSetUpEnvironment(t) |
19 defer state.ZkTearDownEnvironment(t, srv, dir) | 20 defer state.ZkTearDownEnvironment(t, srv, dir) |
20 | 21 |
21 TestingT(t) | 22 TestingT(t) |
(...skipping 14 matching lines...) Expand all Loading... |
36 // localCharmURL returns the local URL of a charm. | 37 // localCharmURL returns the local URL of a charm. |
37 func localCharmURL(ch charm.Charm) *charm.URL { | 38 func localCharmURL(ch charm.Charm) *charm.URL { |
38 url := fmt.Sprintf("local:series/%s-%d", ch.Meta().Name, ch.Revision()) | 39 url := fmt.Sprintf("local:series/%s-%d", ch.Meta().Name, ch.Revision()) |
39 return charm.MustParseURL(url) | 40 return charm.MustParseURL(url) |
40 } | 41 } |
41 | 42 |
42 // addDummyCharm adds the 'dummy' charm state to st. | 43 // addDummyCharm adds the 'dummy' charm state to st. |
43 func addDummyCharm(c *C, st *state.State) (*state.Charm, *charm.URL) { | 44 func addDummyCharm(c *C, st *state.State) (*state.Charm, *charm.URL) { |
44 ch := readCharm(c, "dummy") | 45 ch := readCharm(c, "dummy") |
45 curl := localCharmURL(ch) | 46 curl := localCharmURL(ch) |
46 » dummy, err := st.AddCharm(ch, curl, "http://bundle.url") | 47 » bundleURL, err := url.Parse("http://bundle.url") |
| 48 » c.Assert(err, IsNil) |
| 49 » dummy, err := st.AddCharm(ch, curl, bundleURL) |
47 c.Assert(err, IsNil) | 50 c.Assert(err, IsNil) |
48 return dummy, curl | 51 return dummy, curl |
49 } | 52 } |
50 | 53 |
51 type StateSuite struct { | 54 type StateSuite struct { |
52 zkServer *zookeeper.Server | 55 zkServer *zookeeper.Server |
53 zkTestRoot string | 56 zkTestRoot string |
54 zkTestPort int | 57 zkTestPort int |
55 zkAddr string | 58 zkAddr string |
56 zkConn *zookeeper.Conn | 59 zkConn *zookeeper.Conn |
(...skipping 22 matching lines...) Expand all Loading... |
79 zkRemoveTree(s.zkConn, "/units") | 82 zkRemoveTree(s.zkConn, "/units") |
80 zkRemoveTree(s.zkConn, "/relations") | 83 zkRemoveTree(s.zkConn, "/relations") |
81 zkRemoveTree(s.zkConn, "/initialized") | 84 zkRemoveTree(s.zkConn, "/initialized") |
82 s.zkConn.Close() | 85 s.zkConn.Close() |
83 } | 86 } |
84 | 87 |
85 func (s StateSuite) TestAddCharm(c *C) { | 88 func (s StateSuite) TestAddCharm(c *C) { |
86 // Check that adding charms works correctly. | 89 // Check that adding charms works correctly. |
87 dummyCharm := readCharm(c, "dummy") | 90 dummyCharm := readCharm(c, "dummy") |
88 curl := localCharmURL(dummyCharm) | 91 curl := localCharmURL(dummyCharm) |
89 » dummy, err := s.st.AddCharm(dummyCharm, curl, "http://bundle.url") | 92 » bundleURL, err := url.Parse("http://bundle.url") |
| 93 » c.Assert(err, IsNil) |
| 94 » dummy, err := s.st.AddCharm(dummyCharm, curl, bundleURL) |
90 c.Assert(err, IsNil) | 95 c.Assert(err, IsNil) |
91 c.Assert(dummy.URL().String(), Equals, curl.String()) | 96 c.Assert(dummy.URL().String(), Equals, curl.String()) |
92 | 97 » _, _, err = s.zkConn.Children("/charms") |
93 » children, _, err := s.zkConn.Children("/charms") | 98 » c.Assert(err, IsNil) |
94 » c.Assert(err, IsNil) | |
95 » c.Assert(children, Equals, []string{"local_3a_series_2f_dummy-1"}) | |
96 } | |
97 | |
98 func (s StateSuite) TestCharm(c *C) { | |
99 » // Check that reading a previously added charm works correctly. | |
100 » dummy, curl := addDummyCharm(c, s.st) | |
101 | |
102 » dummy, err := s.st.Charm(curl) | |
103 » c.Assert(err, IsNil) | |
104 » c.Assert(dummy.URL().String(), Equals, curl.String()) | |
105 } | 99 } |
106 | 100 |
107 func (s StateSuite) TestCharmAttributes(c *C) { | 101 func (s StateSuite) TestCharmAttributes(c *C) { |
108 // Check that the basic (invariant) fields of the charm | 102 // Check that the basic (invariant) fields of the charm |
109 // are correctly in place. | 103 // are correctly in place. |
110 » dummy, curl := addDummyCharm(c, s.st) | 104 » _, curl := addDummyCharm(c, s.st) |
111 | 105 |
112 dummy, err := s.st.Charm(curl) | 106 dummy, err := s.st.Charm(curl) |
113 c.Assert(err, IsNil) | 107 c.Assert(err, IsNil) |
114 c.Assert(dummy.URL().String(), Equals, curl.String()) | 108 c.Assert(dummy.URL().String(), Equals, curl.String()) |
115 c.Assert(dummy.Revision(), Equals, 1) | 109 c.Assert(dummy.Revision(), Equals, 1) |
116 » c.Assert(dummy.BundleURL(), Equals, "http://bundle.url") | 110 » bundleURL, err := url.Parse("http://bundle.url") |
117 } | 111 » c.Assert(err, IsNil) |
118 | 112 » c.Assert(dummy.BundleURL(), DeepEquals, bundleURL) |
119 func (s StateSuite) TestCharmMetadata(c *C) { | |
120 » // Check that the charm metadata was correctly saved and loaded. | |
121 » dummy, curl := addDummyCharm(c, s.st) | |
122 | |
123 » dummy, err := s.st.Charm(curl) | |
124 » c.Assert(err, IsNil) | |
125 meta := dummy.Meta() | 113 meta := dummy.Meta() |
126 c.Assert(meta.Name, Equals, "dummy") | 114 c.Assert(meta.Name, Equals, "dummy") |
127 } | |
128 | |
129 func (s StateSuite) TestCharmConfig(c *C) { | |
130 // Verify that the charm config is present and correct. | |
131 dummy, curl := addDummyCharm(c, s.st) | |
132 | |
133 dummy, err := s.st.Charm(curl) | |
134 c.Assert(err, IsNil) | |
135 config := dummy.Config() | 115 config := dummy.Config() |
136 c.Assert(config.Options["title"], Equals, | 116 c.Assert(config.Options["title"], Equals, |
137 charm.Option{ | 117 charm.Option{ |
138 Default: "My Title", | 118 Default: "My Title", |
139 Description: "A descriptive title used for the service."
, | 119 Description: "A descriptive title used for the service."
, |
140 Type: "string", | 120 Type: "string", |
141 }, | 121 }, |
142 ) | 122 ) |
143 } | 123 } |
144 | 124 |
145 func (s StateSuite) TestNonExistentCharmPriorToInitialization(c *C) { | 125 func (s StateSuite) TestNonExistentCharmPriorToInitialization(c *C) { |
146 » // Check that getting a charm before anyone has been added fails nicely. | 126 » // Check that getting a charm before any other charm has been added fail
s nicely. |
147 curl, err := charm.ParseURL("local:series/dummy-1") | 127 curl, err := charm.ParseURL("local:series/dummy-1") |
148 c.Assert(err, IsNil) | 128 c.Assert(err, IsNil) |
149 _, err = s.st.Charm(curl) | 129 _, err = s.st.Charm(curl) |
150 c.Assert(err, ErrorMatches, `charm not found: "local:series/dummy-1"`) | 130 c.Assert(err, ErrorMatches, `charm not found: "local:series/dummy-1"`) |
151 } | 131 } |
152 | 132 |
153 func (s StateSuite) TestGetNonExistentCharm(c *C) { | 133 func (s StateSuite) TestGetNonExistentCharm(c *C) { |
154 // Check that getting a non-existent charm fails nicely. | 134 // Check that getting a non-existent charm fails nicely. |
155 addDummyCharm(c, s.st) | 135 addDummyCharm(c, s.st) |
156 | 136 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 unit, err = wordpress.Unit("wordpress/0/0") | 302 unit, err = wordpress.Unit("wordpress/0/0") |
323 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`) |
324 unit, err = wordpress.Unit("pressword/0") | 304 unit, err = wordpress.Unit("pressword/0") |
325 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"`) |
326 unit, err = wordpress.Unit("mysql/0") | 306 unit, err = wordpress.Unit("mysql/0") |
327 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"`) |
328 | 308 |
329 // Check that retrieving unit names works. | 309 // Check that retrieving unit names works. |
330 unitNames, err := wordpress.UnitNames() | 310 unitNames, err := wordpress.UnitNames() |
331 c.Assert(err, IsNil) | 311 c.Assert(err, IsNil) |
332 » c.Assert(unitNames, Equals, []string{"wordpress/0", "wordpress/1"}) | 312 » c.Assert(unitNames, DeepEquals, []string{"wordpress/0", "wordpress/1"}) |
333 | 313 |
334 // Check that retrieving all units works. | 314 // Check that retrieving all units works. |
335 units, err := wordpress.AllUnits() | 315 units, err := wordpress.AllUnits() |
336 c.Assert(err, IsNil) | 316 c.Assert(err, IsNil) |
337 c.Assert(len(units), Equals, 2) | 317 c.Assert(len(units), Equals, 2) |
338 c.Assert(units[0].Name(), Equals, "wordpress/0") | 318 c.Assert(units[0].Name(), Equals, "wordpress/0") |
339 c.Assert(units[1].Name(), Equals, "wordpress/1") | 319 c.Assert(units[1].Name(), Equals, "wordpress/1") |
340 } | 320 } |
341 | 321 |
342 func (s StateSuite) TestReadUnitWithChangingState(c *C) { | 322 func (s StateSuite) TestReadUnitWithChangingState(c *C) { |
(...skipping 18 matching lines...) Expand all Loading... |
361 _, err = wordpress.AddUnit() | 341 _, err = wordpress.AddUnit() |
362 c.Assert(err, IsNil) | 342 c.Assert(err, IsNil) |
363 | 343 |
364 // Check that removing a unit works. | 344 // Check that removing a unit works. |
365 unit, err := wordpress.Unit("wordpress/0") | 345 unit, err := wordpress.Unit("wordpress/0") |
366 c.Assert(err, IsNil) | 346 c.Assert(err, IsNil) |
367 err = wordpress.RemoveUnit(unit) | 347 err = wordpress.RemoveUnit(unit) |
368 c.Assert(err, IsNil) | 348 c.Assert(err, IsNil) |
369 unitNames, err := wordpress.UnitNames() | 349 unitNames, err := wordpress.UnitNames() |
370 c.Assert(err, IsNil) | 350 c.Assert(err, IsNil) |
371 » c.Assert(unitNames, Equals, []string{"wordpress/1"}) | 351 » c.Assert(unitNames, DeepEquals, []string{"wordpress/1"}) |
372 | 352 |
373 // Check that removing a non-existent unit fails nicely. | 353 // Check that removing a non-existent unit fails nicely. |
374 err = wordpress.RemoveUnit(unit) | 354 err = wordpress.RemoveUnit(unit) |
375 c.Assert(err, ErrorMatches, "environment state has changed") | 355 c.Assert(err, ErrorMatches, "environment state has changed") |
376 } | 356 } |
377 | 357 |
378 func (s StateSuite) TestGetSetPublicAddress(c *C) { | 358 func (s StateSuite) TestGetSetPublicAddress(c *C) { |
379 dummy, _ := addDummyCharm(c, s.st) | 359 dummy, _ := addDummyCharm(c, s.st) |
380 wordpress, err := s.st.AddService("wordpress", dummy) | 360 wordpress, err := s.st.AddService("wordpress", dummy) |
381 c.Assert(err, IsNil) | 361 c.Assert(err, IsNil) |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 c.Assert(err, IsNil) | 546 c.Assert(err, IsNil) |
567 wordpressUnit, err := wordpressService.AddUnit() | 547 wordpressUnit, err := wordpressService.AddUnit() |
568 c.Assert(err, IsNil) | 548 c.Assert(err, IsNil) |
569 err = s.st.RemoveService(wordpressService) | 549 err = s.st.RemoveService(wordpressService) |
570 c.Assert(err, IsNil) | 550 c.Assert(err, IsNil) |
571 | 551 |
572 _, err = wordpressUnit.AssignToUnusedMachine() | 552 _, err = wordpressUnit.AssignToUnusedMachine() |
573 c.Assert(err, ErrorMatches, "environment state has changed") | 553 c.Assert(err, ErrorMatches, "environment state has changed") |
574 } | 554 } |
575 | 555 |
576 func (s StateSuite) TestAssignUniToUnusedMachineWithChangingUnit(c *C) { | 556 func (s StateSuite) TestAssignUnitToUnusedMachineWithChangingUnit(c *C) { |
577 // Create root machine that shouldn't be useds. | 557 // Create root machine that shouldn't be useds. |
578 _, err := s.st.AddMachine() | 558 _, err := s.st.AddMachine() |
579 c.Assert(err, IsNil) | 559 c.Assert(err, IsNil) |
580 // Check for a 'state changed' error if a unit is manipulated | 560 // Check for a 'state changed' error if a unit is manipulated |
581 // during reuse. | 561 // during reuse. |
582 dummy, _ := addDummyCharm(c, s.st) | 562 dummy, _ := addDummyCharm(c, s.st) |
583 mysqlService, err := s.st.AddService("mysql", dummy) | 563 mysqlService, err := s.st.AddService("mysql", dummy) |
584 c.Assert(err, IsNil) | 564 c.Assert(err, IsNil) |
585 mysqlUnit, err := mysqlService.AddUnit() | 565 mysqlUnit, err := mysqlService.AddUnit() |
586 c.Assert(err, IsNil) | 566 c.Assert(err, IsNil) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 return err | 629 return err |
650 } | 630 } |
651 for _, child := range children { | 631 for _, child := range children { |
652 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 { |
653 return err | 633 return err |
654 } | 634 } |
655 } | 635 } |
656 // Now delete the path itself. | 636 // Now delete the path itself. |
657 return zk.Delete(path, -1) | 637 return zk.Delete(path, -1) |
658 } | 638 } |
LEFT | RIGHT |