1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
jc "github.com/juju/testing/checkers"
10
"github.com/juju/juju/state"
11
statetesting "github.com/juju/juju/state/testing"
12
"github.com/juju/juju/testing/factory"
15
type StatusHistorySuite struct {
16
statetesting.StateSuite
19
var _ = gc.Suite(&StatusHistorySuite{})
21
func (s *StatusHistorySuite) TestPruneStatusHistory(c *gc.C) {
23
// NOTE: the behaviour is bad, and the test is ugly. I'm just verifying
24
// the existing logic here.
26
// If you get the opportunity to fix this, you'll want a better shape of
27
// test (that injects a usable clock dependency, apart from anything else,
28
// and checks that we do our best to maintain a usable span of history
29
// rather than an arbitrary limit per entity. And isn't O(N) on status
30
// count in the environment).
33
units := make([]*state.Unit, count)
34
agents := make([]*state.UnitAgent, count)
35
service := s.Factory.MakeService(c, nil)
36
for i := 0; i < count; i++ {
37
units[i] = s.Factory.MakeUnit(c, &factory.UnitParams{Service: service})
38
agents[i] = units[i].Agent()
41
primeUnitStatusHistory(c, units[0], 10)
42
primeUnitStatusHistory(c, units[1], 50)
43
primeUnitStatusHistory(c, units[2], 100)
44
primeUnitAgentStatusHistory(c, agents[0], 100)
45
primeUnitAgentStatusHistory(c, agents[1], 50)
46
primeUnitAgentStatusHistory(c, agents[2], 10)
48
err := state.PruneStatusHistory(s.State, 30)
49
c.Assert(err, jc.ErrorIsNil)
51
history, err := units[0].StatusHistory(50)
52
c.Assert(err, jc.ErrorIsNil)
53
c.Assert(history, gc.HasLen, 11)
54
checkInitialWorkloadStatus(c, history[10])
55
for i, statusInfo := range history[:10] {
56
checkPrimedUnitStatus(c, statusInfo, 9-i)
59
history, err = units[1].StatusHistory(50)
60
c.Assert(err, jc.ErrorIsNil)
61
c.Assert(history, gc.HasLen, 30)
62
for i, statusInfo := range history {
63
checkPrimedUnitStatus(c, statusInfo, 49-i)
66
history, err = units[2].StatusHistory(50)
67
c.Assert(err, jc.ErrorIsNil)
68
c.Assert(history, gc.HasLen, 30)
69
for i, statusInfo := range history {
70
checkPrimedUnitStatus(c, statusInfo, 99-i)
73
history, err = agents[0].StatusHistory(50)
74
c.Assert(err, jc.ErrorIsNil)
75
c.Assert(history, gc.HasLen, 30)
76
for i, statusInfo := range history {
77
checkPrimedUnitAgentStatus(c, statusInfo, 99-i)
80
history, err = agents[1].StatusHistory(50)
81
c.Assert(err, jc.ErrorIsNil)
82
c.Assert(history, gc.HasLen, 30)
83
for i, statusInfo := range history {
84
checkPrimedUnitAgentStatus(c, statusInfo, 49-i)
87
history, err = agents[2].StatusHistory(50)
88
c.Assert(err, jc.ErrorIsNil)
89
c.Assert(history, gc.HasLen, 11)
90
checkInitialUnitAgentStatus(c, history[10])
91
for i, statusInfo := range history[:10] {
92
checkPrimedUnitAgentStatus(c, statusInfo, 9-i)