func (s *IsolatedWorkerSuite) TestConfigValidation(c *gc.C) { tests := []struct { cfg meterstatus.IsolatedConfig expected string }{{ cfg: meterstatus.IsolatedConfig{ Runner: &stubRunner{stub: s.stub}, StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), }, expected: "clock not provided", }, { cfg: meterstatus.IsolatedConfig{ Clock: testing.NewClock(time.Now()), StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), }, expected: "hook runner not provided", }, { cfg: meterstatus.IsolatedConfig{ Clock: testing.NewClock(time.Now()), Runner: &stubRunner{stub: s.stub}, }, expected: "state file not provided", }} for i, test := range tests { c.Logf("running test %d", i) err := test.cfg.Validate() c.Assert(err, gc.ErrorMatches, test.expected) } }
// TestStatusHandlerDoesNotRerunAfterRestart tests that the status handler will not rerun a meter-status-changed // hook if it is restarted, but no actual changes are recorded. func (s *ConnectedWorkerSuite) TestStatusHandlerDoesNotRerunAfterRestart(c *gc.C) { handler, err := meterstatus.NewConnectedStatusHandler( meterstatus.ConnectedConfig{ Runner: &stubRunner{stub: s.stub}, StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), Status: s.msClient, }) c.Assert(err, jc.ErrorIsNil) c.Assert(handler, gc.NotNil) _, err = handler.SetUp() c.Assert(err, jc.ErrorIsNil) err = handler.Handle(nil) c.Assert(err, jc.ErrorIsNil) s.stub.CheckCallNames(c, "WatchMeterStatus", "MeterStatus", "RunHook") s.stub.ResetCalls() // Create a new handler (imitating worker restart). handler, err = meterstatus.NewConnectedStatusHandler( meterstatus.ConnectedConfig{ Runner: &stubRunner{stub: s.stub}, StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), Status: s.msClient}) c.Assert(err, jc.ErrorIsNil) c.Assert(handler, gc.NotNil) _, err = handler.SetUp() c.Assert(err, jc.ErrorIsNil) err = handler.Handle(nil) c.Assert(err, jc.ErrorIsNil) s.stub.CheckCallNames(c, "WatchMeterStatus", "MeterStatus") }
func (s *IsolatedWorkerSuite) SetUpTest(c *gc.C) { s.BaseSuite.SetUpTest(c) s.stub = &testing.Stub{} s.dataDir = c.MkDir() s.hookRan = make(chan struct{}) s.triggersCreated = make(chan struct{}) triggerFactory := func(state meterstatus.WorkerState, status string, disconectedAt time.Time, clk clock.Clock, amber time.Duration, red time.Duration) (<-chan time.Time, <-chan time.Time) { select { case s.triggersCreated <- struct{}{}: case <-time.After(coretesting.LongWait): c.Fatalf("failed to signal trigger creation") } return meterstatus.GetTriggers(state, status, disconectedAt, clk, amber, red) } s.clk = testing.NewClock(time.Now()) wrk, err := meterstatus.NewIsolatedStatusWorker( meterstatus.IsolatedConfig{ Runner: &stubRunner{stub: s.stub, ran: s.hookRan}, StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), Clock: s.clk, AmberGracePeriod: AmberGracePeriod, RedGracePeriod: RedGracePeriod, TriggerFactory: triggerFactory, }) c.Assert(err, jc.ErrorIsNil) c.Assert(wrk, gc.NotNil) s.worker = wrk }
// TestStatusHandlerHandlesHookMissingError tests that the handler does not report errors // caused by a missing meter-status-changed hook. func (s *ConnectedWorkerSuite) TestStatusHandlerHandlesHookMissingError(c *gc.C) { s.stub.SetErrors(context.NewMissingHookError("meter-status-changed")) handler, err := meterstatus.NewConnectedStatusHandler( meterstatus.ConnectedConfig{ Runner: &stubRunner{stub: s.stub}, StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), Status: s.msClient, }) c.Assert(err, jc.ErrorIsNil) c.Assert(handler, gc.NotNil) _, err = handler.SetUp() c.Assert(err, jc.ErrorIsNil) err = handler.Handle(nil) c.Assert(err, jc.ErrorIsNil) s.stub.CheckCallNames(c, "WatchMeterStatus", "MeterStatus", "RunHook") }
// TestStatusHandlerRunsHookOnChanges ensures that the handler runs the meter-status-changed hook // if an actual meter status change is detected. func (s *ConnectedWorkerSuite) TestStatusHandlerRunsHookOnChanges(c *gc.C) { handler, err := meterstatus.NewConnectedStatusHandler( meterstatus.ConnectedConfig{ Runner: &stubRunner{stub: s.stub}, StateFile: meterstatus.NewStateFile(path.Join(s.dataDir, "meter-status.yaml")), Status: s.msClient, }) c.Assert(err, jc.ErrorIsNil) c.Assert(handler, gc.NotNil) _, err = handler.SetUp() c.Assert(err, jc.ErrorIsNil) handler.Handle(nil) s.msClient.SetStatus("RED") handler.Handle(nil) c.Assert(err, jc.ErrorIsNil) s.stub.CheckCallNames(c, "WatchMeterStatus", "MeterStatus", "RunHook", "MeterStatus", "RunHook") }
func (t *StateFileSuite) SetUpTest(c *gc.C) { t.path = path.Join(c.MkDir(), "state.yaml") t.state = meterstatus.NewStateFile(t.path) }