forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_meterstatus_test.go
99 lines (77 loc) · 2.64 KB
/
api_meterstatus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package featuretests
import (
"time"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v3"
gc "gopkg.in/check.v1"
"github.com/juju/juju/api/agent/meterstatus"
"github.com/juju/juju/core/watcher/watchertest"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/state"
)
type meterStatusIntegrationSuite struct {
jujutesting.JujuConnSuite
status meterstatus.MeterStatusClient
unit *state.Unit
}
func (s *meterStatusIntegrationSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
s.unit = s.Factory.MakeUnit(c, nil)
password, err := utils.RandomPassword()
c.Assert(err, jc.ErrorIsNil)
err = s.unit.SetPassword(password)
c.Assert(err, jc.ErrorIsNil)
state := s.OpenAPIAs(c, s.unit.UnitTag(), password)
s.status = meterstatus.NewClient(state, s.unit.UnitTag())
c.Assert(s.status, gc.NotNil)
// Ask for the MetricsManager as part of setup, so the metrics
// document is created before any of the tests care.
_, err = s.State.MetricsManager()
c.Assert(err, jc.ErrorIsNil)
// Ensure that all the creation events have flowed through the system.
s.WaitForModelWatchersIdle(c, s.Model.UUID())
}
func (s *meterStatusIntegrationSuite) TestMeterStatus(c *gc.C) {
code, info, err := s.status.MeterStatus()
c.Assert(err, jc.ErrorIsNil)
c.Assert(code, gc.Equals, "AMBER")
c.Assert(info, gc.Equals, "not set")
err = s.unit.SetMeterStatus("RED", "some status")
c.Assert(err, jc.ErrorIsNil)
code, info, err = s.status.MeterStatus()
c.Assert(err, jc.ErrorIsNil)
c.Assert(code, gc.Equals, "RED")
c.Assert(info, gc.Equals, "some status")
}
func (s *meterStatusIntegrationSuite) TestWatchMeterStatus(c *gc.C) {
w, err := s.status.WatchMeterStatus()
c.Assert(err, jc.ErrorIsNil)
wc := watchertest.NewNotifyWatcherC(c, w)
defer wc.AssertStops()
// Initial event.
wc.AssertOneChange()
err = s.unit.SetMeterStatus("AMBER", "ok")
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
// Non-change is not reported.
err = s.unit.SetMeterStatus("AMBER", "ok")
c.Assert(err, jc.ErrorIsNil)
wc.AssertNoChange()
mm, err := s.State.MetricsManager()
c.Assert(err, jc.ErrorIsNil)
err = mm.SetLastSuccessfulSend(time.Now())
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
// meter status does not change on every failed
// attempt to send metrics - on three consecutive
// fails, we get a meter status change
err = mm.IncrementConsecutiveErrors()
c.Assert(err, jc.ErrorIsNil)
err = mm.IncrementConsecutiveErrors()
c.Assert(err, jc.ErrorIsNil)
err = mm.IncrementConsecutiveErrors()
c.Assert(err, jc.ErrorIsNil)
wc.AssertOneChange()
}