func (s *deployerSuite) TestWatchUnits(c *gc.C) { c.Assert(s.resources.Count(), gc.Equals, 0) args := params.Entities{Entities: []params.Entity{ {Tag: "machine-1"}, {Tag: "machine-0"}, {Tag: "machine-42"}, }} result, err := s.deployer.WatchUnits(args) c.Assert(err, gc.IsNil) sort.Strings(result.Results[0].Changes) c.Assert(result, gc.DeepEquals, params.StringsWatchResults{ Results: []params.StringsWatchResult{ {Changes: []string{"logging/0", "mysql/0"}, StringsWatcherId: "1"}, {Error: apiservertesting.ErrUnauthorized}, {Error: apiservertesting.ErrUnauthorized}, }, }) // Verify the resource was registered and stop when done c.Assert(s.resources.Count(), gc.Equals, 1) c.Assert(result.Results[0].StringsWatcherId, gc.Equals, "1") resource := s.resources.Get("1") defer statetesting.AssertStop(c, resource) // Check that the Watch has consumed the initial event ("returned" in // the Watch call) wc := statetesting.NewStringsWatcherC(c, s.State, resource.(state.StringsWatcher)) wc.AssertNoChange() }
func (s *deployerSuite) TestWatchUnits(c *gc.C) { machine, err := s.st.Machine(s.machine.Tag()) c.Assert(err, gc.IsNil) w, err := machine.WatchUnits() c.Assert(err, gc.IsNil) defer statetesting.AssertStop(c, w) wc := statetesting.NewStringsWatcherC(c, s.BackingState, w) // Initial event. wc.AssertChange("mysql/0", "logging/0") wc.AssertNoChange() // Change something other than the lifecycle and make sure it's // not detected. err = s.subordinate.SetPassword("foo") c.Assert(err, gc.IsNil) wc.AssertNoChange() // Make the subordinate dead and check it's detected. err = s.subordinate.EnsureDead() c.Assert(err, gc.IsNil) wc.AssertChange("logging/0") wc.AssertNoChange() statetesting.AssertStop(c, w) wc.AssertClosed() }
func (s *watcherSuite) TestStringsWatcherStopsWithPendingSend(c *gc.C) { // Call the Deployer facade's WatchUnits for machine-0. var results params.StringsWatchResults args := params.Entities{Entities: []params.Entity{{Tag: s.rawMachine.Tag()}}} err := s.stateAPI.Call("Deployer", "", "WatchUnits", args, &results) c.Assert(err, gc.IsNil) c.Assert(results.Results, gc.HasLen, 1) result := results.Results[0] c.Assert(result.Error, gc.IsNil) // Start a StringsWatcher and check the initial event. w := watcher.NewStringsWatcher(s.stateAPI, result) wc := statetesting.NewStringsWatcherC(c, s.State, w) // Create a service, deploy a unit of it on the machine. mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) c.Assert(err, gc.IsNil) principal, err := mysql.AddUnit() c.Assert(err, gc.IsNil) err = principal.AssignToMachine(s.rawMachine) c.Assert(err, gc.IsNil) // Ensure the initial event is delivered. Then test the watcher // can be stopped cleanly without reading the pending change. s.BackingState.Sync() statetesting.AssertCanStopWhenSending(c, w) wc.AssertClosed() }
func (s *watcherSuite) TestWatchUnitsKeepsEvents(c *gc.C) { // Create two services, relate them, and add one unit to each - a // principal and a subordinate. mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) c.Assert(err, gc.IsNil) logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logging")) c.Assert(err, gc.IsNil) eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) c.Assert(err, gc.IsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, gc.IsNil) principal, err := mysql.AddUnit() c.Assert(err, gc.IsNil) err = principal.AssignToMachine(s.rawMachine) c.Assert(err, gc.IsNil) relUnit, err := rel.Unit(principal) c.Assert(err, gc.IsNil) err = relUnit.EnterScope(nil) c.Assert(err, gc.IsNil) subordinate, err := logging.Unit("logging/0") c.Assert(err, gc.IsNil) // Call the Deployer facade's WatchUnits for machine-0. var results params.StringsWatchResults args := params.Entities{Entities: []params.Entity{{Tag: s.rawMachine.Tag()}}} err = s.stateAPI.Call("Deployer", "", "WatchUnits", args, &results) c.Assert(err, gc.IsNil) c.Assert(results.Results, gc.HasLen, 1) result := results.Results[0] c.Assert(result.Error, gc.IsNil) // Start a StringsWatcher and check the initial event. w := watcher.NewStringsWatcher(s.stateAPI, result) wc := statetesting.NewStringsWatcherC(c, s.State, w) wc.AssertChange("mysql/0", "logging/0") wc.AssertNoChange() // Now, without reading any changes advance the lifecycle of both // units, inducing an update server-side after each two changes to // ensure they're reported as separate events over the API. err = subordinate.EnsureDead() c.Assert(err, gc.IsNil) s.BackingState.Sync() err = subordinate.Remove() c.Assert(err, gc.IsNil) err = principal.EnsureDead() c.Assert(err, gc.IsNil) s.BackingState.Sync() // Expect these changes as 2 separate events, so that // nothing gets lost. wc.AssertChange("logging/0") wc.AssertChange("mysql/0") wc.AssertNoChange() statetesting.AssertStop(c, w) wc.AssertClosed() }
func (s *ServiceSuite) TestWatchUnitsLifecycle(c *C) { // Empty initial event when no units. w := s.mysql.WatchUnits() defer testing.AssertStop(c, w) wc := testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange() wc.AssertNoChange() // Create one unit, check one change. quick, err := s.mysql.AddUnit() c.Assert(err, IsNil) wc.AssertChange(quick.Name()) wc.AssertNoChange() // Destroy that unit (short-circuited to removal), check one change. err = quick.Destroy() c.Assert(err, IsNil) wc.AssertChange(quick.Name()) wc.AssertNoChange() // Create another, check one change. slow, err := s.mysql.AddUnit() c.Assert(err, IsNil) wc.AssertChange(slow.Name()) wc.AssertNoChange() // Change unit itself, no change. preventUnitDestroyRemove(c, slow) wc.AssertNoChange() // Make unit Dying, change detected. err = slow.Destroy() c.Assert(err, IsNil) wc.AssertChange(slow.Name()) wc.AssertNoChange() // Make unit Dead, change detected. err = slow.EnsureDead() c.Assert(err, IsNil) wc.AssertChange(slow.Name()) wc.AssertNoChange() // Remove unit, final change not detected. err = slow.Remove() c.Assert(err, IsNil) wc.AssertNoChange() }
func (s *ServiceSuite) TestWatchUnitsBulkEvents(c *C) { // Alive unit... alive, err := s.mysql.AddUnit() c.Assert(err, IsNil) // Dying unit... dying, err := s.mysql.AddUnit() c.Assert(err, IsNil) preventUnitDestroyRemove(c, dying) err = dying.Destroy() c.Assert(err, IsNil) // Dead unit... dead, err := s.mysql.AddUnit() c.Assert(err, IsNil) preventUnitDestroyRemove(c, dead) err = dead.Destroy() c.Assert(err, IsNil) err = dead.EnsureDead() c.Assert(err, IsNil) // Gone unit. gone, err := s.mysql.AddUnit() c.Assert(err, IsNil) err = gone.Destroy() c.Assert(err, IsNil) // All except gone unit are reported in initial event. w := s.mysql.WatchUnits() defer testing.AssertStop(c, w) wc := testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange(alive.Name(), dying.Name(), dead.Name()) wc.AssertNoChange() // Remove them all; alive/dying changes reported; dead never mentioned again. err = alive.Destroy() c.Assert(err, IsNil) err = dying.EnsureDead() c.Assert(err, IsNil) err = dying.Remove() c.Assert(err, IsNil) err = dead.Remove() c.Assert(err, IsNil) wc.AssertChange(alive.Name(), dying.Name()) wc.AssertNoChange() }
func (s *UnitSuite) TestWatchSubordinates(c *C) { w := s.unit.WatchSubordinateUnits() defer testing.AssertStop(c, w) wc := testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange() wc.AssertNoChange() // Add a couple of subordinates, check change. subCharm := s.AddTestingCharm(c, "logging") var subUnits []*state.Unit for i := 0; i < 2; i++ { // Note: subordinate units can only be created as a side effect of a // principal entering scope; and a given principal can only have a // single subordinate unit of each service. name := "logging" + strconv.Itoa(i) subSvc, err := s.State.AddService(name, subCharm) c.Assert(err, IsNil) eps, err := s.State.InferEndpoints([]string{name, "wordpress"}) c.Assert(err, IsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, IsNil) ru, err := rel.Unit(s.unit) c.Assert(err, IsNil) err = ru.EnterScope(nil) c.Assert(err, IsNil) units, err := subSvc.AllUnits() c.Assert(err, IsNil) c.Assert(units, HasLen, 1) subUnits = append(subUnits, units[0]) } wc.AssertChange(subUnits[0].Name(), subUnits[1].Name()) wc.AssertNoChange() // Set one to Dying, check change. err := subUnits[0].Destroy() c.Assert(err, IsNil) wc.AssertChange(subUnits[0].Name()) wc.AssertNoChange() // Set both to Dead, and remove one; check change. err = subUnits[0].EnsureDead() c.Assert(err, IsNil) err = subUnits[1].EnsureDead() c.Assert(err, IsNil) err = subUnits[1].Remove() c.Assert(err, IsNil) wc.AssertChange(subUnits[0].Name(), subUnits[1].Name()) wc.AssertNoChange() // Stop watcher, check closed. testing.AssertStop(c, w) wc.AssertClosed() // Start a new watch, check Dead unit is reported. w = s.unit.WatchSubordinateUnits() defer testing.AssertStop(c, w) wc = testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange(subUnits[0].Name()) wc.AssertNoChange() // Remove the leftover, check no change. err = subUnits[0].Remove() c.Assert(err, IsNil) wc.AssertNoChange() }
func (s *ServiceSuite) TestWatchRelations(c *C) { // TODO(fwereade) split this test up a bit. w := s.mysql.WatchRelations() defer testing.AssertStop(c, w) wc := testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange() wc.AssertNoChange() // Add a relation; check change. mysqlep, err := s.mysql.Endpoint("server") c.Assert(err, IsNil) wpch := s.AddTestingCharm(c, "wordpress") wpi := 0 addRelation := func() *state.Relation { name := fmt.Sprintf("wp%d", wpi) wpi++ wp, err := s.State.AddService(name, wpch) c.Assert(err, IsNil) wpep, err := wp.Endpoint("db") c.Assert(err, IsNil) rel, err := s.State.AddRelation(mysqlep, wpep) c.Assert(err, IsNil) return rel } rel0 := addRelation() wc.AssertChange(rel0.String()) wc.AssertNoChange() // Add another relation; check change. rel1 := addRelation() wc.AssertChange(rel1.String()) wc.AssertNoChange() // Destroy a relation; check change. err = rel0.Destroy() c.Assert(err, IsNil) wc.AssertChange(rel0.String()) wc.AssertNoChange() // Stop watcher; check change chan is closed. testing.AssertStop(c, w) wc.AssertClosed() // Add a new relation; start a new watcher; check initial event. rel2 := addRelation() w = s.mysql.WatchRelations() defer testing.AssertStop(c, w) wc = testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange(rel1.String(), rel2.String()) wc.AssertNoChange() // Add a unit to the new relation; check no change. unit, err := s.mysql.AddUnit() c.Assert(err, IsNil) ru2, err := rel2.Unit(unit) c.Assert(err, IsNil) err = ru2.EnterScope(nil) c.Assert(err, IsNil) wc.AssertNoChange() // Destroy the relation with the unit in scope, and add another; check // changes. err = rel2.Destroy() c.Assert(err, IsNil) rel3 := addRelation() wc.AssertChange(rel2.String(), rel3.String()) wc.AssertNoChange() // Leave scope, destroying the relation, and check that change as well. err = ru2.LeaveScope() c.Assert(err, IsNil) wc.AssertChange(rel2.String()) wc.AssertNoChange() }
func (s *MachineSuite) TestWatchUnits(c *C) { // Start a watch on an empty machine; check no units reported. w := s.machine.WatchUnits() defer testing.AssertStop(c, w) wc := testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange() wc.AssertNoChange() // Change machine; no change. err := s.machine.SetProvisioned("cheese", "fake_nonce", nil) c.Assert(err, IsNil) wc.AssertNoChange() // Assign a unit (to a separate instance); change detected. mysql, err := s.State.AddService("mysql", s.AddTestingCharm(c, "mysql")) c.Assert(err, IsNil) mysql0, err := mysql.AddUnit() c.Assert(err, IsNil) machine, err := s.State.Machine(s.machine.Id()) c.Assert(err, IsNil) err = mysql0.AssignToMachine(machine) c.Assert(err, IsNil) wc.AssertChange("mysql/0") wc.AssertNoChange() // Change the unit; no change. err = mysql0.SetStatus(params.StatusStarted, "") c.Assert(err, IsNil) wc.AssertNoChange() // Assign another unit and make the first Dying; check both changes detected. mysql1, err := mysql.AddUnit() c.Assert(err, IsNil) err = mysql1.AssignToMachine(machine) c.Assert(err, IsNil) err = mysql0.Destroy() c.Assert(err, IsNil) wc.AssertChange("mysql/0", "mysql/1") wc.AssertNoChange() // Add a subordinate to the Alive unit; change detected. logging, err := s.State.AddService("logging", s.AddTestingCharm(c, "logging")) c.Assert(err, IsNil) eps, err := s.State.InferEndpoints([]string{"mysql", "logging"}) c.Assert(err, IsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, IsNil) mysqlru1, err := rel.Unit(mysql1) c.Assert(err, IsNil) err = mysqlru1.EnterScope(nil) c.Assert(err, IsNil) logging0, err := logging.Unit("logging/0") c.Assert(err, IsNil) wc.AssertChange("logging/0") wc.AssertNoChange() // Change the subordinate; no change. err = logging0.SetStatus(params.StatusStarted, "") c.Assert(err, IsNil) wc.AssertNoChange() // Make the Dying unit Dead; change detected. err = mysql0.EnsureDead() c.Assert(err, IsNil) wc.AssertChange("mysql/0") wc.AssertNoChange() // Stop watcher; check Changes chan closed. testing.AssertStop(c, w) wc.AssertClosed() // Start a fresh watcher; check all units reported. w = s.machine.WatchUnits() defer testing.AssertStop(c, w) wc = testing.NewStringsWatcherC(c, s.State, w) wc.AssertChange("mysql/0", "mysql/1", "logging/0") wc.AssertNoChange() // Remove the Dead unit; no change. err = mysql0.Remove() c.Assert(err, IsNil) wc.AssertNoChange() // Destroy the subordinate; change detected. err = logging0.Destroy() c.Assert(err, IsNil) wc.AssertChange("logging/0") wc.AssertNoChange() // Unassign the principal; check subordinate departure also reported. err = mysql1.UnassignFromMachine() c.Assert(err, IsNil) wc.AssertChange("mysql/1", "logging/0") wc.AssertNoChange() }