LEFT | RIGHT |
(no file at all) | |
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 . "launchpad.net/gocheck" | 7 . "launchpad.net/gocheck" |
8 "launchpad.net/juju-core/constraints" | 8 "launchpad.net/juju-core/constraints" |
9 "launchpad.net/juju-core/errors" | 9 "launchpad.net/juju-core/errors" |
| 10 "launchpad.net/juju-core/instance" |
10 "launchpad.net/juju-core/state" | 11 "launchpad.net/juju-core/state" |
11 "launchpad.net/juju-core/state/api/params" | 12 "launchpad.net/juju-core/state/api/params" |
12 "launchpad.net/juju-core/version" | 13 "launchpad.net/juju-core/version" |
13 "sort" | 14 "sort" |
14 "time" | 15 "time" |
15 ) | 16 ) |
16 | 17 |
17 type MachineSuite struct { | 18 type MachineSuite struct { |
18 ConnSuite | 19 ConnSuite |
19 machine *state.Machine | 20 machine *state.Machine |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 m, err := s.State.AddMachine("series", state.JobHostUnits) | 110 m, err := s.State.AddMachine("series", state.JobHostUnits) |
110 c.Assert(err, IsNil) | 111 c.Assert(err, IsNil) |
111 err = m.Destroy() | 112 err = m.Destroy() |
112 c.Assert(err, IsNil) | 113 c.Assert(err, IsNil) |
113 c.Assert(m.Life(), Equals, state.Dying) | 114 c.Assert(m.Life(), Equals, state.Dying) |
114 err = m.EnsureDead() | 115 err = m.EnsureDead() |
115 c.Assert(err, IsNil) | 116 c.Assert(err, IsNil) |
116 c.Assert(m.Life(), Equals, state.Dead) | 117 c.Assert(m.Life(), Equals, state.Dead) |
117 } | 118 } |
118 | 119 |
| 120 func (s *MachineSuite) TestDestroyAbort(c *C) { |
| 121 defer state.SetBeforeHooks(c, s.State, func() { |
| 122 c.Assert(s.machine.Destroy(), IsNil) |
| 123 }).Check() |
| 124 err := s.machine.Destroy() |
| 125 c.Assert(err, IsNil) |
| 126 } |
| 127 |
| 128 func (s *MachineSuite) TestDestroyCancel(c *C) { |
| 129 svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpr
ess")) |
| 130 c.Assert(err, IsNil) |
| 131 unit, err := svc.AddUnit() |
| 132 c.Assert(err, IsNil) |
| 133 |
| 134 defer state.SetBeforeHooks(c, s.State, func() { |
| 135 c.Assert(unit.AssignToMachine(s.machine), IsNil) |
| 136 }).Check() |
| 137 err = s.machine.Destroy() |
| 138 c.Assert(err, FitsTypeOf, &state.HasAssignedUnitsError{}) |
| 139 } |
| 140 |
| 141 func (s *MachineSuite) TestDestroyContention(c *C) { |
| 142 svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpr
ess")) |
| 143 c.Assert(err, IsNil) |
| 144 unit, err := svc.AddUnit() |
| 145 c.Assert(err, IsNil) |
| 146 |
| 147 perturb := state.TransactionHook{ |
| 148 Before: func() { c.Assert(unit.AssignToMachine(s.machine), IsNil
) }, |
| 149 After: func() { c.Assert(unit.UnassignFromMachine(), IsNil) }, |
| 150 } |
| 151 defer state.SetTransactionHooks( |
| 152 c, s.State, perturb, perturb, perturb, |
| 153 ).Check() |
| 154 err = s.machine.Destroy() |
| 155 c.Assert(err, ErrorMatches, "machine 0 cannot advance lifecycle: state c
hanging too quickly; try again soon") |
| 156 } |
| 157 |
119 func (s *MachineSuite) TestRemove(c *C) { | 158 func (s *MachineSuite) TestRemove(c *C) { |
120 err := s.machine.Remove() | 159 err := s.machine.Remove() |
121 c.Assert(err, ErrorMatches, "cannot remove machine 0: machine is not dea
d") | 160 c.Assert(err, ErrorMatches, "cannot remove machine 0: machine is not dea
d") |
122 err = s.machine.EnsureDead() | 161 err = s.machine.EnsureDead() |
123 c.Assert(err, IsNil) | 162 c.Assert(err, IsNil) |
124 err = s.machine.Remove() | 163 err = s.machine.Remove() |
125 c.Assert(err, IsNil) | 164 c.Assert(err, IsNil) |
126 err = s.machine.Refresh() | 165 err = s.machine.Refresh() |
127 c.Assert(errors.IsNotFoundError(err), Equals, true) | 166 c.Assert(errors.IsNotFoundError(err), Equals, true) |
128 _, err = s.machine.Containers() | 167 _, err = s.machine.Containers() |
129 c.Assert(errors.IsNotFoundError(err), Equals, true) | 168 c.Assert(errors.IsNotFoundError(err), Equals, true) |
| 169 err = s.machine.Remove() |
| 170 c.Assert(err, IsNil) |
| 171 } |
| 172 |
| 173 func (s *MachineSuite) TestRemoveAbort(c *C) { |
| 174 err := s.machine.EnsureDead() |
| 175 c.Assert(err, IsNil) |
| 176 |
| 177 defer state.SetBeforeHooks(c, s.State, func() { |
| 178 c.Assert(s.machine.Remove(), IsNil) |
| 179 }).Check() |
130 err = s.machine.Remove() | 180 err = s.machine.Remove() |
131 c.Assert(err, IsNil) | 181 c.Assert(err, IsNil) |
132 } | 182 } |
133 | 183 |
134 func (s *MachineSuite) TestDestroyMachines(c *C) { | 184 func (s *MachineSuite) TestDestroyMachines(c *C) { |
135 m0 := s.machine | 185 m0 := s.machine |
136 m1, err := s.State.AddMachine("series", state.JobManageEnviron) | 186 m1, err := s.State.AddMachine("series", state.JobManageEnviron) |
137 c.Assert(err, IsNil) | 187 c.Assert(err, IsNil) |
138 m2, err := s.State.AddMachine("series", state.JobHostUnits) | 188 m2, err := s.State.AddMachine("series", state.JobHostUnits) |
139 c.Assert(err, IsNil) | 189 c.Assert(err, IsNil) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 func (s *MachineSuite) TestTag(c *C) { | 235 func (s *MachineSuite) TestTag(c *C) { |
186 c.Assert(s.machine.Tag(), Equals, "machine-0") | 236 c.Assert(s.machine.Tag(), Equals, "machine-0") |
187 } | 237 } |
188 | 238 |
189 func (s *MachineSuite) TestMachineTag(c *C) { | 239 func (s *MachineSuite) TestMachineTag(c *C) { |
190 c.Assert(state.MachineTag("10"), Equals, "machine-10") | 240 c.Assert(state.MachineTag("10"), Equals, "machine-10") |
191 // Check a container id. | 241 // Check a container id. |
192 c.Assert(state.MachineTag("10/lxc/1"), Equals, "machine-10-lxc-1") | 242 c.Assert(state.MachineTag("10/lxc/1"), Equals, "machine-10-lxc-1") |
193 } | 243 } |
194 | 244 |
| 245 func (s *MachineSuite) TestMachineIdFromTag(c *C) { |
| 246 c.Assert(state.MachineIdFromTag("machine-10"), Equals, "10") |
| 247 // Check a container id. |
| 248 c.Assert(state.MachineIdFromTag("machine-10-lxc-1"), Equals, "10/lxc/1") |
| 249 // Check reversability. |
| 250 nested := "2/kvm/0/lxc/3" |
| 251 c.Assert(state.MachineIdFromTag(state.MachineTag(nested)), Equals, neste
d) |
| 252 } |
| 253 |
195 func (s *MachineSuite) TestSetMongoPassword(c *C) { | 254 func (s *MachineSuite) TestSetMongoPassword(c *C) { |
196 testSetMongoPassword(c, func(st *state.State) (entity, error) { | 255 testSetMongoPassword(c, func(st *state.State) (entity, error) { |
197 return st.Machine(s.machine.Id()) | 256 return st.Machine(s.machine.Id()) |
198 }) | 257 }) |
199 } | 258 } |
200 | 259 |
201 func (s *MachineSuite) TestSetPassword(c *C) { | 260 func (s *MachineSuite) TestSetPassword(c *C) { |
202 testSetPassword(c, func() (state.Authenticator, error) { | 261 testSetPassword(c, func() (state.Authenticator, error) { |
203 return s.State.Machine(s.machine.Id()) | 262 return s.State.Machine(s.machine.Id()) |
204 }) | 263 }) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 c.Assert(err, IsNil) | 298 c.Assert(err, IsNil) |
240 err = s.machines.Update( | 299 err = s.machines.Update( |
241 D{{"_id", machine.Id()}}, | 300 D{{"_id", machine.Id()}}, |
242 D{{"$set", D{{"instanceid", "spaceship/0"}}}}, | 301 D{{"$set", D{{"instanceid", "spaceship/0"}}}}, |
243 ) | 302 ) |
244 c.Assert(err, IsNil) | 303 c.Assert(err, IsNil) |
245 | 304 |
246 err = machine.Refresh() | 305 err = machine.Refresh() |
247 c.Assert(err, IsNil) | 306 c.Assert(err, IsNil) |
248 iid, _ := machine.InstanceId() | 307 iid, _ := machine.InstanceId() |
249 » c.Assert(iid, Equals, state.InstanceId("spaceship/0")) | 308 » c.Assert(iid, Equals, instance.Id("spaceship/0")) |
250 } | 309 } |
251 | 310 |
252 func (s *MachineSuite) TestMachineInstanceIdCorrupt(c *C) { | 311 func (s *MachineSuite) TestMachineInstanceIdCorrupt(c *C) { |
253 machine, err := s.State.AddMachine("series", state.JobHostUnits) | 312 machine, err := s.State.AddMachine("series", state.JobHostUnits) |
254 c.Assert(err, IsNil) | 313 c.Assert(err, IsNil) |
255 err = s.machines.Update( | 314 err = s.machines.Update( |
256 D{{"_id", machine.Id()}}, | 315 D{{"_id", machine.Id()}}, |
257 D{{"$set", D{{"instanceid", D{{"foo", "bar"}}}}}}, | 316 D{{"$set", D{{"instanceid", D{{"foo", "bar"}}}}}}, |
258 ) | 317 ) |
259 c.Assert(err, IsNil) | 318 c.Assert(err, IsNil) |
260 | 319 |
261 err = machine.Refresh() | 320 err = machine.Refresh() |
262 c.Assert(err, IsNil) | 321 c.Assert(err, IsNil) |
263 iid, ok := machine.InstanceId() | 322 iid, ok := machine.InstanceId() |
264 c.Assert(ok, Equals, false) | 323 c.Assert(ok, Equals, false) |
265 » c.Assert(iid, Equals, state.InstanceId("")) | 324 » c.Assert(iid, Equals, instance.Id("")) |
266 } | 325 } |
267 | 326 |
268 func (s *MachineSuite) TestMachineInstanceIdMissing(c *C) { | 327 func (s *MachineSuite) TestMachineInstanceIdMissing(c *C) { |
269 iid, ok := s.machine.InstanceId() | 328 iid, ok := s.machine.InstanceId() |
270 c.Assert(ok, Equals, false) | 329 c.Assert(ok, Equals, false) |
271 c.Assert(string(iid), Equals, "") | 330 c.Assert(string(iid), Equals, "") |
272 } | 331 } |
273 | 332 |
274 func (s *MachineSuite) TestMachineInstanceIdBlank(c *C) { | 333 func (s *MachineSuite) TestMachineInstanceIdBlank(c *C) { |
275 machine, err := s.State.AddMachine("series", state.JobHostUnits) | 334 machine, err := s.State.AddMachine("series", state.JobHostUnits) |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 m, svc, unit := s.assertMachineDirtyAfterAddingUnit(c) | 526 m, svc, unit := s.assertMachineDirtyAfterAddingUnit(c) |
468 err := unit.EnsureDead() | 527 err := unit.EnsureDead() |
469 c.Assert(err, IsNil) | 528 c.Assert(err, IsNil) |
470 err = unit.Remove() | 529 err = unit.Remove() |
471 c.Assert(err, IsNil) | 530 c.Assert(err, IsNil) |
472 err = svc.Destroy() | 531 err = svc.Destroy() |
473 c.Assert(err, IsNil) | 532 c.Assert(err, IsNil) |
474 c.Assert(m.Clean(), Equals, false) | 533 c.Assert(m.Clean(), Equals, false) |
475 } | 534 } |
476 | 535 |
477 type machineInfo struct { | |
478 tools *state.Tools | |
479 instanceId string | |
480 } | |
481 | |
482 func tools(tools int, url string) *state.Tools { | |
483 return &state.Tools{ | |
484 URL: url, | |
485 Binary: version.Binary{ | |
486 Number: version.Number{ | |
487 Major: 0, Minor: 0, Patch: tools, | |
488 }, | |
489 Series: "series", | |
490 Arch: "arch", | |
491 }, | |
492 } | |
493 } | |
494 | |
495 var watchMachineTests = []func(m *state.Machine) error{ | |
496 func(m *state.Machine) error { | |
497 return nil | |
498 }, | |
499 func(m *state.Machine) error { | |
500 return m.SetProvisioned("m-foo", "fake_nonce") | |
501 }, | |
502 func(m *state.Machine) error { | |
503 return m.SetAgentTools(tools(3, "baz")) | |
504 }, | |
505 } | |
506 | |
507 func (s *MachineSuite) TestWatchMachine(c *C) { | 536 func (s *MachineSuite) TestWatchMachine(c *C) { |
508 w := s.machine.Watch() | 537 w := s.machine.Watch() |
509 » defer func() { | 538 » defer AssertStop(c, w) |
510 » » c.Assert(w.Stop(), IsNil) | 539 |
511 » }() | 540 » // Initial event. |
512 » for i, test := range watchMachineTests { | 541 » wc := NotifyWatcherC{c, s.State, w} |
513 » » c.Logf("test %d", i) | 542 » wc.AssertOneChange() |
514 » » err := test(s.machine) | 543 |
515 » » c.Assert(err, IsNil) | 544 » // Make one change (to a separate instance), check one event. |
516 » » s.State.StartSync() | 545 » machine, err := s.State.Machine(s.machine.Id()) |
517 » » select { | 546 » c.Assert(err, IsNil) |
518 » » case _, ok := <-w.Changes(): | 547 » err = machine.SetProvisioned("m-foo", "fake_nonce") |
519 » » » c.Assert(ok, Equals, true) | 548 » c.Assert(err, IsNil) |
520 » » case <-time.After(5 * time.Second): | 549 » wc.AssertOneChange() |
521 » » » c.Fatalf("did not get change") | 550 |
522 » » } | 551 » // Make two changes, check one event. |
523 » } | 552 » err = machine.SetAgentTools(&state.Tools{ |
524 » select { | 553 » » URL: "foo", |
525 » case got := <-w.Changes(): | 554 » » Binary: version.MustParseBinary("0.0.3-series-arch"), |
526 » » c.Fatalf("got unexpected change: %#v", got) | 555 » }) |
527 » case <-time.After(50 * time.Millisecond): | 556 » c.Assert(err, IsNil) |
528 » } | 557 » err = machine.Destroy() |
| 558 » c.Assert(err, IsNil) |
| 559 » wc.AssertOneChange() |
| 560 |
| 561 » // Stop, check closed. |
| 562 » AssertStop(c, w) |
| 563 » wc.AssertClosed() |
| 564 |
| 565 » // Remove machine, start new watch, check single event. |
| 566 » err = machine.EnsureDead() |
| 567 » c.Assert(err, IsNil) |
| 568 » err = machine.Remove() |
| 569 » c.Assert(err, IsNil) |
| 570 » w = s.machine.Watch() |
| 571 » defer AssertStop(c, w) |
| 572 » NotifyWatcherC{c, s.State, w}.AssertOneChange() |
529 } | 573 } |
530 | 574 |
531 func (s *MachineSuite) TestWatchPrincipalUnits(c *C) { | 575 func (s *MachineSuite) TestWatchPrincipalUnits(c *C) { |
532 // Start a watch on an empty machine; check no units reported. | 576 // Start a watch on an empty machine; check no units reported. |
533 w := s.machine.WatchPrincipalUnits() | 577 w := s.machine.WatchPrincipalUnits() |
534 » defer stop(c, w) | 578 » defer AssertStop(c, w) |
535 » assertNoChange := func() { | 579 » wc := StringsWatcherC{c, s.State, w} |
536 » » s.State.Sync() | 580 » wc.AssertOneChange() |
537 » » select { | 581 |
538 » » case <-time.After(50 * time.Millisecond): | 582 » // Change machine, and create a unit independently; no change. |
539 » » case got, ok := <-w.Changes(): | |
540 » » » c.Fatalf("unexpected change: %#v, %v", got, ok) | |
541 » » } | |
542 » } | |
543 » assertChange := func(expect ...string) { | |
544 » » s.State.Sync() | |
545 » » select { | |
546 » » case <-time.After(500 * time.Millisecond): | |
547 » » » c.Fatalf("timed out") | |
548 » » case got, ok := <-w.Changes(): | |
549 » » » c.Assert(ok, Equals, true) | |
550 » » » if len(expect) == 0 { | |
551 » » » » c.Assert(got, HasLen, 0) | |
552 » » » } else { | |
553 » » » » sort.Strings(expect) | |
554 » » » » sort.Strings(got) | |
555 » » » » c.Assert(expect, DeepEquals, got) | |
556 » » » } | |
557 » » } | |
558 » » assertNoChange() | |
559 » } | |
560 » assertChange() | |
561 | |
562 » // Change machine; no change. | |
563 err := s.machine.SetProvisioned("cheese", "fake_nonce") | 583 err := s.machine.SetProvisioned("cheese", "fake_nonce") |
564 c.Assert(err, IsNil) | 584 c.Assert(err, IsNil) |
565 | 585 » wc.AssertNoChange() |
566 » // Assign a unit; change detected. | |
567 mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) | 586 mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) |
568 c.Assert(err, IsNil) | 587 c.Assert(err, IsNil) |
569 mysql0, err := mysql.AddUnit() | 588 mysql0, err := mysql.AddUnit() |
570 c.Assert(err, IsNil) | 589 c.Assert(err, IsNil) |
571 » err = mysql0.AssignToMachine(s.machine) | 590 » wc.AssertNoChange() |
572 » c.Assert(err, IsNil) | 591 |
573 » assertChange("mysql/0") | 592 » // Assign that unit (to a separate machine instance); change detected. |
| 593 » machine, err := s.State.Machine(s.machine.Id()) |
| 594 » c.Assert(err, IsNil) |
| 595 » err = mysql0.AssignToMachine(machine) |
| 596 » c.Assert(err, IsNil) |
| 597 » wc.AssertOneChange("mysql/0") |
574 | 598 |
575 // Change the unit; no change. | 599 // Change the unit; no change. |
576 err = mysql0.SetStatus(params.StatusStarted, "") | 600 err = mysql0.SetStatus(params.StatusStarted, "") |
577 c.Assert(err, IsNil) | 601 c.Assert(err, IsNil) |
578 » assertNoChange() | 602 » wc.AssertNoChange() |
579 | 603 |
580 // Assign another unit and make the first Dying; check both changes dete
cted. | 604 // Assign another unit and make the first Dying; check both changes dete
cted. |
581 mysql1, err := mysql.AddUnit() | 605 mysql1, err := mysql.AddUnit() |
582 c.Assert(err, IsNil) | 606 c.Assert(err, IsNil) |
583 » err = mysql1.AssignToMachine(s.machine) | 607 » err = mysql1.AssignToMachine(machine) |
584 c.Assert(err, IsNil) | 608 c.Assert(err, IsNil) |
585 err = mysql0.Destroy() | 609 err = mysql0.Destroy() |
586 c.Assert(err, IsNil) | 610 c.Assert(err, IsNil) |
587 » assertChange("mysql/0", "mysql/1") | 611 » wc.AssertOneChange("mysql/0", "mysql/1") |
588 | 612 |
589 // Add a subordinate to the Alive unit; no change. | 613 // Add a subordinate to the Alive unit; no change. |
590 logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logg
ing")) | 614 logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logg
ing")) |
591 c.Assert(err, IsNil) | 615 c.Assert(err, IsNil) |
592 eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) | 616 eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) |
593 c.Assert(err, IsNil) | 617 c.Assert(err, IsNil) |
594 rel, err := s.State.AddRelation(eps...) | 618 rel, err := s.State.AddRelation(eps...) |
595 c.Assert(err, IsNil) | 619 c.Assert(err, IsNil) |
596 mysqlru1, err := rel.Unit(mysql1) | 620 mysqlru1, err := rel.Unit(mysql1) |
597 c.Assert(err, IsNil) | 621 c.Assert(err, IsNil) |
598 err = mysqlru1.EnterScope(nil) | 622 err = mysqlru1.EnterScope(nil) |
599 c.Assert(err, IsNil) | 623 c.Assert(err, IsNil) |
600 logging0, err := logging.Unit("logging/0") | 624 logging0, err := logging.Unit("logging/0") |
601 c.Assert(err, IsNil) | 625 c.Assert(err, IsNil) |
602 » assertNoChange() | 626 » wc.AssertNoChange() |
603 | 627 |
604 // Change the subordinate; no change. | 628 // Change the subordinate; no change. |
605 err = logging0.SetStatus(params.StatusStarted, "") | 629 err = logging0.SetStatus(params.StatusStarted, "") |
606 c.Assert(err, IsNil) | 630 c.Assert(err, IsNil) |
607 » assertNoChange() | 631 » wc.AssertNoChange() |
608 | 632 |
609 // Make the Dying unit Dead; change detected. | 633 // Make the Dying unit Dead; change detected. |
610 err = mysql0.EnsureDead() | 634 err = mysql0.EnsureDead() |
611 c.Assert(err, IsNil) | 635 c.Assert(err, IsNil) |
612 » assertChange("mysql/0") | 636 » wc.AssertOneChange("mysql/0") |
613 | 637 |
614 // Stop watcher; check Changes chan closed. | 638 // Stop watcher; check Changes chan closed. |
615 » assertClosed := func() { | 639 » AssertStop(c, w) |
616 » » select { | 640 » wc.AssertClosed() |
617 » » case <-time.After(50 * time.Millisecond): | |
618 » » » c.Fatalf("not closed") | |
619 » » case _, ok := <-w.Changes(): | |
620 » » » c.Assert(ok, Equals, false) | |
621 » » } | |
622 » } | |
623 » stop(c, w) | |
624 » assertClosed() | |
625 | 641 |
626 // Start a fresh watcher; check both principals reported. | 642 // Start a fresh watcher; check both principals reported. |
627 w = s.machine.WatchPrincipalUnits() | 643 w = s.machine.WatchPrincipalUnits() |
628 » defer stop(c, w) | 644 » defer AssertStop(c, w) |
629 » assertChange("mysql/0", "mysql/1") | 645 » wc = StringsWatcherC{c, s.State, w} |
| 646 » wc.AssertOneChange("mysql/0", "mysql/1") |
630 | 647 |
631 // Remove the Dead unit; no change. | 648 // Remove the Dead unit; no change. |
632 err = mysql0.Remove() | 649 err = mysql0.Remove() |
633 c.Assert(err, IsNil) | 650 c.Assert(err, IsNil) |
634 » assertNoChange() | 651 » wc.AssertNoChange() |
635 | 652 |
636 // Destroy the subordinate; no change. | 653 // Destroy the subordinate; no change. |
637 err = logging0.Destroy() | 654 err = logging0.Destroy() |
638 c.Assert(err, IsNil) | 655 c.Assert(err, IsNil) |
639 » assertNoChange() | 656 » wc.AssertNoChange() |
640 | 657 |
641 // Unassign the unit; check change. | 658 // Unassign the unit; check change. |
642 err = mysql1.UnassignFromMachine() | 659 err = mysql1.UnassignFromMachine() |
643 c.Assert(err, IsNil) | 660 c.Assert(err, IsNil) |
644 » assertChange("mysql/1") | 661 » wc.AssertOneChange("mysql/1") |
645 } | 662 } |
646 | 663 |
647 func (s *MachineSuite) TestWatchUnits(c *C) { | 664 func (s *MachineSuite) TestWatchUnits(c *C) { |
648 // Start a watch on an empty machine; check no units reported. | 665 // Start a watch on an empty machine; check no units reported. |
649 w := s.machine.WatchUnits() | 666 w := s.machine.WatchUnits() |
650 » defer stop(c, w) | 667 » defer AssertStop(c, w) |
651 » assertNoChange := func() { | 668 » wc := StringsWatcherC{c, s.State, w} |
652 » » s.State.Sync() | 669 » wc.AssertOneChange() |
653 » » select { | |
654 » » case <-time.After(50 * time.Millisecond): | |
655 » » case got, ok := <-w.Changes(): | |
656 » » » c.Fatalf("unexpected change: %#v, %v", got, ok) | |
657 » » } | |
658 » } | |
659 » assertChange := func(expect ...string) { | |
660 » » s.State.Sync() | |
661 » » select { | |
662 » » case <-time.After(500 * time.Millisecond): | |
663 » » » c.Fatalf("timed out") | |
664 » » case got, ok := <-w.Changes(): | |
665 » » » c.Assert(ok, Equals, true) | |
666 » » » if len(expect) == 0 { | |
667 » » » » c.Assert(got, HasLen, 0) | |
668 » » » } else { | |
669 » » » » sort.Strings(expect) | |
670 » » » » sort.Strings(got) | |
671 » » » » c.Assert(expect, DeepEquals, got) | |
672 » » » } | |
673 » » } | |
674 » » assertNoChange() | |
675 » } | |
676 » assertChange() | |
677 | 670 |
678 // Change machine; no change. | 671 // Change machine; no change. |
679 err := s.machine.SetProvisioned("cheese", "fake_nonce") | 672 err := s.machine.SetProvisioned("cheese", "fake_nonce") |
680 c.Assert(err, IsNil) | 673 c.Assert(err, IsNil) |
681 | 674 » wc.AssertNoChange() |
682 » // Assign a unit; change detected. | 675 |
| 676 » // Assign a unit (to a separate instance); change detected. |
683 mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) | 677 mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) |
684 c.Assert(err, IsNil) | 678 c.Assert(err, IsNil) |
685 mysql0, err := mysql.AddUnit() | 679 mysql0, err := mysql.AddUnit() |
686 c.Assert(err, IsNil) | 680 c.Assert(err, IsNil) |
687 » err = mysql0.AssignToMachine(s.machine) | 681 » machine, err := s.State.Machine(s.machine.Id()) |
688 » c.Assert(err, IsNil) | 682 » c.Assert(err, IsNil) |
689 » assertChange("mysql/0") | 683 » err = mysql0.AssignToMachine(machine) |
| 684 » c.Assert(err, IsNil) |
| 685 » wc.AssertOneChange("mysql/0") |
690 | 686 |
691 // Change the unit; no change. | 687 // Change the unit; no change. |
692 err = mysql0.SetStatus(params.StatusStarted, "") | 688 err = mysql0.SetStatus(params.StatusStarted, "") |
693 c.Assert(err, IsNil) | 689 c.Assert(err, IsNil) |
694 » assertNoChange() | 690 » wc.AssertNoChange() |
695 | 691 |
696 // Assign another unit and make the first Dying; check both changes dete
cted. | 692 // Assign another unit and make the first Dying; check both changes dete
cted. |
697 mysql1, err := mysql.AddUnit() | 693 mysql1, err := mysql.AddUnit() |
698 c.Assert(err, IsNil) | 694 c.Assert(err, IsNil) |
699 » err = mysql1.AssignToMachine(s.machine) | 695 » err = mysql1.AssignToMachine(machine) |
700 c.Assert(err, IsNil) | 696 c.Assert(err, IsNil) |
701 err = mysql0.Destroy() | 697 err = mysql0.Destroy() |
702 c.Assert(err, IsNil) | 698 c.Assert(err, IsNil) |
703 » assertChange("mysql/0", "mysql/1") | 699 » wc.AssertOneChange("mysql/0", "mysql/1") |
704 | 700 |
705 // Add a subordinate to the Alive unit; change detected. | 701 // Add a subordinate to the Alive unit; change detected. |
706 logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logg
ing")) | 702 logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logg
ing")) |
707 c.Assert(err, IsNil) | 703 c.Assert(err, IsNil) |
708 eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) | 704 eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) |
709 c.Assert(err, IsNil) | 705 c.Assert(err, IsNil) |
710 rel, err := s.State.AddRelation(eps...) | 706 rel, err := s.State.AddRelation(eps...) |
711 c.Assert(err, IsNil) | 707 c.Assert(err, IsNil) |
712 mysqlru1, err := rel.Unit(mysql1) | 708 mysqlru1, err := rel.Unit(mysql1) |
713 c.Assert(err, IsNil) | 709 c.Assert(err, IsNil) |
714 err = mysqlru1.EnterScope(nil) | 710 err = mysqlru1.EnterScope(nil) |
715 c.Assert(err, IsNil) | 711 c.Assert(err, IsNil) |
716 logging0, err := logging.Unit("logging/0") | 712 logging0, err := logging.Unit("logging/0") |
717 c.Assert(err, IsNil) | 713 c.Assert(err, IsNil) |
718 » assertChange("logging/0") | 714 » wc.AssertOneChange("logging/0") |
719 | 715 |
720 // Change the subordinate; no change. | 716 // Change the subordinate; no change. |
721 err = logging0.SetStatus(params.StatusStarted, "") | 717 err = logging0.SetStatus(params.StatusStarted, "") |
722 c.Assert(err, IsNil) | 718 c.Assert(err, IsNil) |
723 » assertNoChange() | 719 » wc.AssertNoChange() |
724 | 720 |
725 // Make the Dying unit Dead; change detected. | 721 // Make the Dying unit Dead; change detected. |
726 err = mysql0.EnsureDead() | 722 err = mysql0.EnsureDead() |
727 c.Assert(err, IsNil) | 723 c.Assert(err, IsNil) |
728 » assertChange("mysql/0") | 724 » wc.AssertOneChange("mysql/0") |
729 | 725 |
730 // Stop watcher; check Changes chan closed. | 726 // Stop watcher; check Changes chan closed. |
731 » assertClosed := func() { | 727 » AssertStop(c, w) |
732 » » select { | 728 » wc.AssertClosed() |
733 » » case <-time.After(50 * time.Millisecond): | |
734 » » » c.Fatalf("not closed") | |
735 » » case _, ok := <-w.Changes(): | |
736 » » » c.Assert(ok, Equals, false) | |
737 » » } | |
738 » } | |
739 » stop(c, w) | |
740 » assertClosed() | |
741 | 729 |
742 // Start a fresh watcher; check all units reported. | 730 // Start a fresh watcher; check all units reported. |
743 w = s.machine.WatchUnits() | 731 w = s.machine.WatchUnits() |
744 » defer stop(c, w) | 732 » defer AssertStop(c, w) |
745 » assertChange("mysql/0", "mysql/1", "logging/0") | 733 » wc = StringsWatcherC{c, s.State, w} |
| 734 » wc.AssertOneChange("mysql/0", "mysql/1", "logging/0") |
746 | 735 |
747 // Remove the Dead unit; no change. | 736 // Remove the Dead unit; no change. |
748 err = mysql0.Remove() | 737 err = mysql0.Remove() |
749 c.Assert(err, IsNil) | 738 c.Assert(err, IsNil) |
750 » assertNoChange() | 739 » wc.AssertNoChange() |
751 | 740 |
752 » // Destroy the subordinate; no change. | 741 » // Destroy the subordinate; change detected. |
753 err = logging0.Destroy() | 742 err = logging0.Destroy() |
754 c.Assert(err, IsNil) | 743 c.Assert(err, IsNil) |
755 » assertChange("logging/0") | 744 » wc.AssertOneChange("logging/0") |
756 | 745 |
757 // Unassign the principal; check subordinate departure also reported. | 746 // Unassign the principal; check subordinate departure also reported. |
758 err = mysql1.UnassignFromMachine() | 747 err = mysql1.UnassignFromMachine() |
759 c.Assert(err, IsNil) | 748 c.Assert(err, IsNil) |
760 » assertChange("mysql/1", "logging/0") | 749 » wc.AssertOneChange("mysql/1", "logging/0") |
761 } | 750 } |
762 | 751 |
763 func (s *MachineSuite) TestAnnotatorForMachine(c *C) { | 752 func (s *MachineSuite) TestAnnotatorForMachine(c *C) { |
764 testAnnotator(c, func() (state.Annotator, error) { | 753 testAnnotator(c, func() (state.Annotator, error) { |
765 return s.State.Machine(s.machine.Id()) | 754 return s.State.Machine(s.machine.Id()) |
766 }) | 755 }) |
767 } | 756 } |
768 | 757 |
769 func (s *MachineSuite) TestAnnotationRemovalForMachine(c *C) { | 758 func (s *MachineSuite) TestAnnotationRemovalForMachine(c *C) { |
770 annotations := map[string]string{"mykey": "myvalue"} | 759 annotations := map[string]string{"mykey": "myvalue"} |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
898 c.Assert(status, Equals, params.StatusStopped) | 887 c.Assert(status, Equals, params.StatusStopped) |
899 c.Assert(info, Equals, "") | 888 c.Assert(info, Equals, "") |
900 | 889 |
901 err = s.machine.Remove() | 890 err = s.machine.Remove() |
902 c.Assert(err, IsNil) | 891 c.Assert(err, IsNil) |
903 err = s.machine.SetStatus(params.StatusStarted, "not really") | 892 err = s.machine.SetStatus(params.StatusStarted, "not really") |
904 c.Assert(err, ErrorMatches, `cannot set status of machine "0": not found
or not alive`) | 893 c.Assert(err, ErrorMatches, `cannot set status of machine "0": not found
or not alive`) |
905 _, _, err = s.machine.Status() | 894 _, _, err = s.machine.Status() |
906 c.Assert(err, ErrorMatches, "status not found") | 895 c.Assert(err, ErrorMatches, "status not found") |
907 } | 896 } |
LEFT | RIGHT |