forked from UbuntuEvangelist/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife_test.go
216 lines (193 loc) · 4.71 KB
/
life_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/mgo.v2/bson"
"github.com/juju/juju/state"
)
type LifeSuite struct {
ConnSuite
charm *state.Charm
svc *state.Service
}
func (s *LifeSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
s.charm = s.AddTestingCharm(c, "dummy")
s.svc = s.AddTestingService(c, "dummysvc", s.charm)
}
var _ = gc.Suite(&LifeSuite{})
var stateChanges = []struct {
cached, desired state.Life
dbinitial, dbfinal state.Life
}{
{
state.Alive, state.Dying,
state.Alive, state.Dying,
},
{
state.Alive, state.Dying,
state.Dying, state.Dying,
},
{
state.Alive, state.Dying,
state.Dead, state.Dead,
},
{
state.Alive, state.Dead,
state.Alive, state.Dead,
},
{
state.Alive, state.Dead,
state.Dying, state.Dead,
},
{
state.Alive, state.Dead,
state.Dead, state.Dead,
},
{
state.Dying, state.Dying,
state.Dying, state.Dying,
},
{
state.Dying, state.Dying,
state.Dead, state.Dead,
},
{
state.Dying, state.Dead,
state.Dying, state.Dead,
},
{
state.Dying, state.Dead,
state.Dead, state.Dead,
},
{
state.Dead, state.Dying,
state.Dead, state.Dead,
},
{
state.Dead, state.Dead,
state.Dead, state.Dead,
},
}
type lifeFixture interface {
id() (coll string, id interface{})
setup(s *LifeSuite, c *gc.C) state.AgentLiving
}
type unitLife struct {
unit *state.Unit
st *state.State
}
func (l *unitLife) id() (coll string, id interface{}) {
return "units", state.DocID(l.st, l.unit.Name())
}
func (l *unitLife) setup(s *LifeSuite, c *gc.C) state.AgentLiving {
unit, err := s.svc.AddUnit()
c.Assert(err, jc.ErrorIsNil)
preventUnitDestroyRemove(c, unit)
l.unit = unit
return l.unit
}
type machineLife struct {
machine *state.Machine
st *state.State
}
func (l *machineLife) id() (coll string, id interface{}) {
return "machines", state.DocID(l.st, l.machine.Id())
}
func (l *machineLife) setup(s *LifeSuite, c *gc.C) state.AgentLiving {
var err error
l.machine, err = s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
return l.machine
}
func (s *LifeSuite) prepareFixture(living state.Living, lfix lifeFixture, cached, dbinitial state.Life, c *gc.C) {
collName, id := lfix.id()
coll := s.MgoSuite.Session.DB("juju").C(collName)
err := coll.UpdateId(id, bson.D{{"$set", bson.D{
{"life", cached},
}}})
c.Assert(err, jc.ErrorIsNil)
err = living.Refresh()
c.Assert(err, jc.ErrorIsNil)
err = coll.UpdateId(id, bson.D{{"$set", bson.D{
{"life", dbinitial},
}}})
c.Assert(err, jc.ErrorIsNil)
}
func (s *LifeSuite) TestLifecycleStateChanges(c *gc.C) {
for i, lfix := range []lifeFixture{&unitLife{st: s.State}, &machineLife{st: s.State}} {
c.Logf("fixture %d", i)
for j, v := range stateChanges {
c.Logf("sequence %d", j)
living := lfix.setup(s, c)
s.prepareFixture(living, lfix, v.cached, v.dbinitial, c)
switch v.desired {
case state.Dying:
err := living.Destroy()
c.Assert(err, jc.ErrorIsNil)
case state.Dead:
err := living.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
default:
panic("desired lifecycle can only be dying or dead")
}
err := living.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(living.Life(), gc.Equals, v.dbfinal)
err = living.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = living.Remove()
c.Assert(err, jc.ErrorIsNil)
}
}
}
func (s *LifeSuite) TestLifeString(c *gc.C) {
var tests = []struct {
life state.Life
want string
}{
{state.Alive, "alive"},
{state.Dying, "dying"},
{state.Dead, "dead"},
{42, "unknown"},
}
for _, test := range tests {
got := test.life.String()
c.Assert(got, gc.Equals, test.want)
}
}
const (
notAliveErr = ".*: not found or not alive"
deadErr = ".*: not found or dead"
noErr = ""
)
type lifer interface {
EnsureDead() error
Destroy() error
Life() state.Life
}
func runLifeChecks(c *gc.C, obj lifer, expectErr string, checks []func() error) {
for i, check := range checks {
c.Logf("check %d when %v", i, obj.Life())
err := check()
if expectErr == noErr {
c.Assert(err, jc.ErrorIsNil)
} else {
c.Assert(err, gc.ErrorMatches, expectErr)
}
}
}
// testWhenDying sets obj to Dying and Dead in turn, and asserts
// that the errors from the given checks match aliveErr, dyingErr and deadErr
// in each respective life state.
func testWhenDying(c *gc.C, obj lifer, dyingErr, deadErr string, checks ...func() error) {
c.Logf("checking life of %v (%T)", obj, obj)
err := obj.Destroy()
c.Assert(err, jc.ErrorIsNil)
runLifeChecks(c, obj, dyingErr, checks)
err = obj.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
runLifeChecks(c, obj, deadErr, checks)
}