github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/meterstatus/triggers_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package meterstatus_test 5 6 import ( 7 "time" 8 9 "github.com/juju/testing" 10 "github.com/juju/utils/clock" 11 gc "gopkg.in/check.v1" 12 13 coretesting "github.com/juju/juju/testing" 14 "github.com/juju/juju/worker/meterstatus" 15 ) 16 17 type TriggersSuite struct { 18 coretesting.BaseSuite 19 } 20 21 var _ = gc.Suite(&TriggersSuite{}) 22 23 var fudge = time.Second 24 25 const ( 26 testAmberGracePeriod = time.Minute * 10 27 testRedGracePeriod = time.Minute * 30 28 ) 29 30 func (*TriggersSuite) TestTriggerCreation(c *gc.C) { 31 now := time.Now() 32 tests := []struct { 33 description string 34 worker meterstatus.WorkerState 35 status string 36 disconnected time.Time 37 now clock.Clock 38 check func(*gc.C, <-chan time.Time, <-chan time.Time) 39 }{{ 40 "normal start, unit status is green", 41 meterstatus.Uninitialized, 42 "GREEN", 43 now, 44 testing.NewClock(now), 45 func(c *gc.C, amber, red <-chan time.Time) { 46 c.Check(amber, gc.NotNil) 47 c.Check(red, gc.NotNil) 48 }}, { 49 "normal start, unit status is amber", 50 meterstatus.Uninitialized, 51 "AMBER", 52 now, 53 testing.NewClock(now), 54 func(c *gc.C, amber, red <-chan time.Time) { 55 c.Check(amber, gc.NotNil) 56 c.Check(red, gc.NotNil) 57 }}, { 58 "normal start, unit status is RED", 59 meterstatus.Uninitialized, 60 "RED", 61 now, 62 testing.NewClock(now), 63 func(c *gc.C, amber, red <-chan time.Time) { 64 c.Check(amber, gc.IsNil) 65 c.Check(red, gc.NotNil) 66 }}, { 67 "restart, unit status is green, amber status not yet triggered", 68 meterstatus.WaitingAmber, 69 "GREEN", 70 now, 71 testing.NewClock(now), 72 func(c *gc.C, amber, red <-chan time.Time) { 73 c.Check(amber, gc.NotNil) 74 c.Check(red, gc.NotNil) 75 }}, { 76 "restart, unit status is amber, amber status not yet triggered", 77 meterstatus.WaitingAmber, 78 "AMBER", 79 now, 80 testing.NewClock(now), 81 func(c *gc.C, amber, red <-chan time.Time) { 82 c.Check(amber, gc.NotNil) 83 c.Check(red, gc.NotNil) 84 }}, { 85 "restart, unit status is red, amber status not yet triggered", 86 meterstatus.WaitingAmber, 87 "RED", 88 now, 89 testing.NewClock(now), 90 func(c *gc.C, amber, red <-chan time.Time) { 91 c.Check(amber, gc.IsNil) 92 c.Check(red, gc.NotNil) 93 }}, { 94 "restart, unit status is green, amber status trigger time passed", 95 meterstatus.WaitingAmber, 96 "GREEN", 97 now.Add(-(testAmberGracePeriod + fudge)), 98 testing.NewClock(now), 99 func(c *gc.C, amber, red <-chan time.Time) { 100 c.Check(amber, gc.NotNil) 101 c.Check(red, gc.NotNil) 102 }}, { 103 "restart, unit status is amber, amber status trigger time passed", 104 meterstatus.WaitingAmber, 105 "AMBER", 106 now.Add(-(testAmberGracePeriod + fudge)), 107 testing.NewClock(now), 108 func(c *gc.C, amber, red <-chan time.Time) { 109 c.Check(amber, gc.NotNil) 110 c.Check(red, gc.NotNil) 111 }}, { 112 "restart, unit status is red, amber status trigger time passed", 113 meterstatus.WaitingAmber, 114 "RED", 115 now.Add(-(testAmberGracePeriod + fudge)), 116 testing.NewClock(now), 117 func(c *gc.C, amber, red <-chan time.Time) { 118 c.Check(amber, gc.IsNil) 119 c.Check(red, gc.NotNil) 120 }}, { 121 "restart, unit status is amber, amber status has been triggered", 122 meterstatus.WaitingRed, 123 "AMBER", 124 now.Add(-(testAmberGracePeriod + fudge)), 125 testing.NewClock(now), 126 func(c *gc.C, amber, red <-chan time.Time) { 127 c.Check(amber, gc.IsNil) 128 c.Check(red, gc.NotNil) 129 }}, { 130 "restart, unit status is amber, red status trigger time has passed", 131 meterstatus.WaitingRed, 132 "AMBER", 133 now.Add(-(testRedGracePeriod + fudge)), 134 testing.NewClock(now), 135 func(c *gc.C, amber, red <-chan time.Time) { 136 c.Check(amber, gc.IsNil) 137 c.Check(red, gc.NotNil) 138 }}, { 139 "restart, unit status is red, red status trigger time has passed", 140 meterstatus.WaitingRed, 141 "RED", 142 now.Add(-(testRedGracePeriod + fudge)), 143 testing.NewClock(now), 144 func(c *gc.C, amber, red <-chan time.Time) { 145 c.Check(amber, gc.IsNil) 146 c.Check(red, gc.NotNil) 147 }}, { 148 "restart, unit status is red, red status has been triggered", 149 meterstatus.Done, 150 "RED", 151 now.Add(-(testRedGracePeriod + fudge)), 152 testing.NewClock(now), 153 func(c *gc.C, amber, red <-chan time.Time) { 154 c.Check(amber, gc.IsNil) 155 c.Check(red, gc.IsNil) 156 }}} 157 158 for i, test := range tests { 159 c.Logf("%d: %s", i, test.description) 160 signalAmber, signalRed := meterstatus.GetTriggers(test.worker, test.status, test.disconnected, test.now, testAmberGracePeriod, testRedGracePeriod) 161 test.check(c, signalAmber, signalRed) 162 } 163 }