Пример #1
0
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
}
Пример #2
0
func (*TriggersSuite) TestTriggerCreation(c *gc.C) {
	now := time.Now()
	tests := []struct {
		description  string
		worker       meterstatus.WorkerState
		status       string
		disconnected time.Time
		now          clock.Clock
		check        func(*gc.C, <-chan time.Time, <-chan time.Time)
	}{{
		"normal start, unit status is green",
		meterstatus.Uninitialized,
		"GREEN",
		now,
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.NotNil)
			c.Check(red, gc.NotNil)
		}}, {
		"normal start, unit status is amber",
		meterstatus.Uninitialized,
		"AMBER",
		now,
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.NotNil)
			c.Check(red, gc.NotNil)
		}}, {
		"normal start, unit status is RED",
		meterstatus.Uninitialized,
		"RED",
		now,
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is green, amber status not yet triggered",
		meterstatus.WaitingAmber,
		"GREEN",
		now,
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.NotNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is amber, amber status not yet triggered",
		meterstatus.WaitingAmber,
		"AMBER",
		now,
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.NotNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is red, amber status not yet triggered",
		meterstatus.WaitingAmber,
		"RED",
		now,
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is green, amber status trigger time passed",
		meterstatus.WaitingAmber,
		"GREEN",
		now.Add(-(testAmberGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.NotNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is amber, amber status trigger time passed",
		meterstatus.WaitingAmber,
		"AMBER",
		now.Add(-(testAmberGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.NotNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is red, amber status trigger time passed",
		meterstatus.WaitingAmber,
		"RED",
		now.Add(-(testAmberGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is amber, amber status has been triggered",
		meterstatus.WaitingRed,
		"AMBER",
		now.Add(-(testAmberGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is amber, red status trigger time has passed",
		meterstatus.WaitingRed,
		"AMBER",
		now.Add(-(testRedGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is red, red status trigger time has passed",
		meterstatus.WaitingRed,
		"RED",
		now.Add(-(testRedGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.NotNil)
		}}, {
		"restart, unit status is red, red status has been triggered",
		meterstatus.Done,
		"RED",
		now.Add(-(testRedGracePeriod + fudge)),
		testing.NewClock(now),
		func(c *gc.C, amber, red <-chan time.Time) {
			c.Check(amber, gc.IsNil)
			c.Check(red, gc.IsNil)
		}}}

	for i, test := range tests {
		c.Logf("%d: %s", i, test.description)
		signalAmber, signalRed := meterstatus.GetTriggers(test.worker, test.status, test.disconnected, test.now, testAmberGracePeriod, testRedGracePeriod)
		test.check(c, signalAmber, signalRed)
	}
}