-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifold_test.go
297 lines (248 loc) · 8.11 KB
/
manifold_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"context"
"time"
"github.com/juju/errors"
mgotesting "github.com/juju/mgo/v3/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/dependency"
dt "github.com/juju/worker/v3/dependency/testing"
"github.com/juju/worker/v3/workertest"
gc "gopkg.in/check.v1"
coreagent "github.com/juju/juju/agent"
"github.com/juju/juju/state"
statetesting "github.com/juju/juju/state/testing"
coretesting "github.com/juju/juju/testing"
workerstate "github.com/juju/juju/worker/state"
)
type ManifoldSuite struct {
statetesting.StateSuite
manifold dependency.Manifold
openStateCalled bool
openStateErr error
config workerstate.ManifoldConfig
resources dt.StubResources
setStatePoolCalls []*state.StatePool
}
var _ = gc.Suite(&ManifoldSuite{})
func (s *ManifoldSuite) SetUpTest(c *gc.C) {
s.StateSuite.SetUpTest(c)
s.openStateCalled = false
s.openStateErr = nil
s.setStatePoolCalls = nil
s.config = workerstate.ManifoldConfig{
AgentName: "agent",
StateConfigWatcherName: "state-config-watcher",
OpenStatePool: s.fakeOpenState,
PingInterval: 10 * time.Millisecond,
SetStatePool: func(pool *state.StatePool) {
s.setStatePoolCalls = append(s.setStatePoolCalls, pool)
},
}
s.manifold = workerstate.Manifold(s.config)
s.resources = dt.StubResources{
"agent": dt.NewStubResource(new(mockAgent)),
"state-config-watcher": dt.NewStubResource(true),
}
}
func (s *ManifoldSuite) fakeOpenState(context.Context, coreagent.Config) (*state.StatePool, error) {
s.openStateCalled = true
if s.openStateErr != nil {
return nil, s.openStateErr
}
// Here's one we prepared earlier...
return s.StatePool, nil
}
func (s *ManifoldSuite) TestInputs(c *gc.C) {
c.Assert(s.manifold.Inputs, jc.SameContents, []string{
"agent",
"state-config-watcher",
})
}
func (s *ManifoldSuite) TestStartAgentMissing(c *gc.C) {
s.resources["agent"] = dt.StubResource{Error: dependency.ErrMissing}
w, err := s.startManifold(c)
c.Check(w, gc.IsNil)
c.Check(err, gc.Equals, dependency.ErrMissing)
}
func (s *ManifoldSuite) TestStateConfigWatcherMissing(c *gc.C) {
s.resources["state-config-watcher"] = dt.StubResource{Error: dependency.ErrMissing}
w, err := s.startManifold(c)
c.Check(w, gc.IsNil)
c.Check(err, gc.Equals, dependency.ErrMissing)
}
func (s *ManifoldSuite) TestStartOpenStateNil(c *gc.C) {
s.config.OpenStatePool = nil
s.startManifoldInvalidConfig(c, s.config, "nil OpenStatePool not valid")
}
func (s *ManifoldSuite) TestStartSetStatePoolNil(c *gc.C) {
s.config.SetStatePool = nil
s.startManifoldInvalidConfig(c, s.config, "nil SetStatePool not valid")
}
func (s *ManifoldSuite) startManifoldInvalidConfig(c *gc.C, config workerstate.ManifoldConfig, expect string) {
manifold := workerstate.Manifold(config)
w, err := manifold.Start(s.resources.Context())
c.Check(w, gc.IsNil)
c.Check(err, gc.ErrorMatches, expect)
}
func (s *ManifoldSuite) TestStartNotStateServer(c *gc.C) {
s.resources["state-config-watcher"] = dt.NewStubResource(false)
w, err := s.startManifold(c)
c.Check(w, gc.IsNil)
c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
c.Check(err, gc.ErrorMatches, "no StateServingInfo in config: dependency not available")
}
func (s *ManifoldSuite) TestStartOpenStateFails(c *gc.C) {
s.openStateErr = errors.New("boom")
w, err := s.startManifold(c)
c.Check(w, gc.IsNil)
c.Check(err, gc.ErrorMatches, "boom")
}
func (s *ManifoldSuite) TestStartSuccess(c *gc.C) {
w := s.mustStartManifold(c)
c.Check(s.openStateCalled, jc.IsTrue)
checkNotExiting(c, w)
workertest.CleanKill(c, w)
}
func (s *ManifoldSuite) TestStatePinging(c *gc.C) {
w := s.mustStartManifold(c)
checkNotExiting(c, w)
// Kill the mongod to cause pings to fail.
mgotesting.MgoServer.Destroy()
// FIXME: Ideally we'd want the "state ping failed" error here, but in reality the txn watcher will fail
// first because it is long polling.
checkExitsWithError(c, w, "(state ping failed|hub txn watcher sync error): .+")
}
func (s *ManifoldSuite) TestOutputBadWorker(c *gc.C) {
var st *state.State
err := s.manifold.Output(dummyWorker{}, &st)
c.Check(st, gc.IsNil)
c.Check(err, gc.ErrorMatches, `in should be a \*state.stateWorker; .+`)
}
func (s *ManifoldSuite) TestOutputWrongType(c *gc.C) {
w := s.mustStartManifold(c)
var wrong int
err := s.manifold.Output(w, &wrong)
c.Check(wrong, gc.Equals, 0)
c.Check(err, gc.ErrorMatches, `out should be \*StateTracker; got .+`)
}
func (s *ManifoldSuite) TestOutputSuccess(c *gc.C) {
w := s.mustStartManifold(c)
var stTracker workerstate.StateTracker
err := s.manifold.Output(w, &stTracker)
c.Assert(err, jc.ErrorIsNil)
pool, err := stTracker.Use()
c.Assert(err, jc.ErrorIsNil)
systemState, err := pool.SystemState()
c.Assert(err, jc.ErrorIsNil)
c.Check(systemState, gc.Equals, s.State)
c.Assert(stTracker.Done(), jc.ErrorIsNil)
// Ensure State is closed when the worker is done.
workertest.CleanKill(c, w)
assertStatePoolClosed(c, s.StatePool)
}
func (s *ManifoldSuite) TestStateStillInUse(c *gc.C) {
w := s.mustStartManifold(c)
var stTracker workerstate.StateTracker
err := s.manifold.Output(w, &stTracker)
c.Assert(err, jc.ErrorIsNil)
pool, err := stTracker.Use()
c.Assert(err, jc.ErrorIsNil)
// Close the worker while the State is still in use.
workertest.CleanKill(c, w)
assertStatePoolNotClosed(c, pool)
// Now signal that the State is no longer needed.
c.Assert(stTracker.Done(), jc.ErrorIsNil)
assertStatePoolClosed(c, pool)
}
func (s *ManifoldSuite) TestDeadStateRemoved(c *gc.C) {
// Create an additional state *before* we start
// the worker, so the worker's lifecycle watcher
// is guaranteed to observe it in both the Alive
// state and the Dead state.
newSt := s.Factory.MakeModel(c, nil)
defer newSt.Close()
model, err := newSt.Model()
c.Assert(err, jc.ErrorIsNil)
w := s.mustStartManifold(c)
defer workertest.CleanKill(c, w)
var stTracker workerstate.StateTracker
err = s.manifold.Output(w, &stTracker)
c.Assert(err, jc.ErrorIsNil)
pool, err := stTracker.Use()
c.Assert(err, jc.ErrorIsNil)
defer stTracker.Done()
// Get a reference to the state pool entry, so we can
// prevent it from being fully removed from the pool.
st, err := pool.Get(newSt.ModelUUID())
c.Assert(err, jc.ErrorIsNil)
defer st.Release()
// Progress the model to Dead.
c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
c.Assert(model.Refresh(), jc.ErrorIsNil)
c.Assert(model.Life(), gc.Equals, state.Dying)
c.Assert(newSt.RemoveDyingModel(), jc.ErrorIsNil)
c.Assert(model.Refresh(), jc.Satisfies, errors.IsNotFound)
for a := coretesting.LongAttempt.Start(); a.Next(); {
st, err := pool.Get(newSt.ModelUUID())
if errors.IsNotFound(err) {
c.Assert(err, gc.ErrorMatches, "model .* has been removed")
return
}
c.Assert(err, jc.ErrorIsNil)
st.Release()
}
c.Fatal("timed out waiting for model state to be removed from pool")
}
func (s *ManifoldSuite) mustStartManifold(c *gc.C) worker.Worker {
w, err := s.startManifold(c)
c.Assert(err, jc.ErrorIsNil)
return w
}
func (s *ManifoldSuite) startManifold(c *gc.C) (worker.Worker, error) {
w, err := s.manifold.Start(s.resources.Context())
if w != nil {
s.AddCleanup(func(*gc.C) { worker.Stop(w) })
}
return w, err
}
func checkNotExiting(c *gc.C, w worker.Worker) {
exited := make(chan bool)
go func() {
w.Wait()
close(exited)
}()
select {
case <-exited:
c.Fatal("worker exited unexpectedly")
case <-time.After(coretesting.ShortWait):
// Worker didn't exit (good)
}
}
func checkExitsWithError(c *gc.C, w worker.Worker, expectedErr string) {
errCh := make(chan error)
go func() {
errCh <- w.Wait()
}()
select {
case err := <-errCh:
c.Check(err, gc.ErrorMatches, expectedErr)
case <-time.After(coretesting.LongWait):
c.Fatal("timed out waiting for worker to exit")
}
}
type mockAgent struct {
coreagent.Agent
}
func (ma *mockAgent) CurrentConfig() coreagent.Config {
return new(mockConfig)
}
type mockConfig struct {
coreagent.Config
}
type dummyWorker struct {
worker.Worker
}