-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_test.go
206 lines (173 loc) · 6.11 KB
/
shared_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
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/pubsub/v2"
jc "github.com/juju/testing/checkers"
"github.com/juju/worker/v3/workertest"
"github.com/prometheus/client_golang/prometheus"
gc "gopkg.in/check.v1"
corecontroller "github.com/juju/juju/controller"
"github.com/juju/juju/core/cache"
"github.com/juju/juju/core/presence"
"github.com/juju/juju/core/raft/queue"
"github.com/juju/juju/pubsub/controller"
"github.com/juju/juju/state"
statetesting "github.com/juju/juju/state/testing"
"github.com/juju/juju/testing"
"github.com/juju/juju/worker/gate"
"github.com/juju/juju/worker/lease"
"github.com/juju/juju/worker/modelcache"
"github.com/juju/juju/worker/multiwatcher"
)
type sharedServerContextSuite struct {
statetesting.StateSuite
hub *pubsub.StructuredHub
config sharedServerConfig
}
var _ = gc.Suite(&sharedServerContextSuite{})
func (s *sharedServerContextSuite) SetUpTest(c *gc.C) {
s.StateSuite.SetUpTest(c)
multiWatcherWorker, err := multiwatcher.NewWorker(multiwatcher.Config{
Clock: clock.WallClock,
Logger: loggo.GetLogger("test"),
Backing: state.NewAllWatcherBacking(s.StatePool),
PrometheusRegisterer: noopRegisterer{},
})
c.Assert(err, jc.ErrorIsNil)
// The worker itself is a coremultiwatcher.Factory.
s.AddCleanup(func(c *gc.C) { workertest.CleanKill(c, multiWatcherWorker) })
initialized := gate.NewLock()
s.hub = pubsub.NewStructuredHub(nil)
modelCache, err := modelcache.NewWorker(modelcache.Config{
StatePool: s.StatePool,
Hub: s.hub,
InitializedGate: initialized,
Logger: loggo.GetLogger("test"),
WatcherFactory: multiWatcherWorker.WatchController,
PrometheusRegisterer: noopRegisterer{},
Cleanup: func() {},
}.WithDefaultRestartStrategy())
s.AddCleanup(func(c *gc.C) { workertest.CleanKill(c, modelCache) })
c.Assert(err, jc.ErrorIsNil)
var controller *cache.Controller
err = modelcache.ExtractCacheController(modelCache, &controller)
c.Assert(err, jc.ErrorIsNil)
controllerConfig, err := s.State.ControllerConfig()
c.Assert(err, jc.ErrorIsNil)
s.config = sharedServerConfig{
statePool: s.StatePool,
controller: controller,
multiwatcherFactory: multiWatcherWorker,
centralHub: s.hub,
presence: presence.New(clock.WallClock),
leaseManager: &lease.Manager{},
controllerConfig: controllerConfig,
raftOpQueue: queue.NewOpQueue(clock.WallClock),
logger: loggo.GetLogger("test"),
}
}
func (s *sharedServerContextSuite) TestConfigNoStatePool(c *gc.C) {
s.config.statePool = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil statePool not valid")
}
func (s *sharedServerContextSuite) TestConfigNoController(c *gc.C) {
s.config.controller = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil controller not valid")
}
func (s *sharedServerContextSuite) TestConfigNoMultiwatcherFactory(c *gc.C) {
s.config.multiwatcherFactory = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil multiwatcherFactory not valid")
}
func (s *sharedServerContextSuite) TestConfigNoHub(c *gc.C) {
s.config.centralHub = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil centralHub not valid")
}
func (s *sharedServerContextSuite) TestConfigNoPresence(c *gc.C) {
s.config.presence = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil presence not valid")
}
func (s *sharedServerContextSuite) TestConfigNoLeaseManager(c *gc.C) {
s.config.leaseManager = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil leaseManager not valid")
}
func (s *sharedServerContextSuite) TestConfigNoControllerconfig(c *gc.C) {
s.config.controllerConfig = nil
err := s.config.validate()
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil controllerConfig not valid")
}
func (s *sharedServerContextSuite) TestNewCallsConfigValidate(c *gc.C) {
s.config.statePool = nil
ctx, err := newSharedServerContext(s.config)
c.Check(err, jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, "nil statePool not valid")
c.Check(ctx, gc.IsNil)
}
func (s *sharedServerContextSuite) TestValidConfig(c *gc.C) {
ctx, err := newSharedServerContext(s.config)
c.Assert(err, jc.ErrorIsNil)
// Normally you wouldn't directly access features.
c.Assert(ctx.features, gc.HasLen, 0)
ctx.Close()
}
func (s *sharedServerContextSuite) newContext(c *gc.C) *sharedServerContext {
ctx, err := newSharedServerContext(s.config)
c.Assert(err, jc.ErrorIsNil)
s.AddCleanup(func(*gc.C) { ctx.Close() })
return ctx
}
type stubHub struct {
*pubsub.StructuredHub
published []string
}
func (s *stubHub) Publish(topic string, data interface{}) (func(), error) {
s.published = append(s.published, topic)
return func() {}, nil
}
func (s *sharedServerContextSuite) TestControllerConfigChanged(c *gc.C) {
stub := &stubHub{StructuredHub: s.hub}
s.config.centralHub = stub
ctx := s.newContext(c)
msg := controller.ConfigChangedMessage{
Config: corecontroller.Config{
corecontroller.Features: []string{"foo", "bar"},
},
}
done, err := s.hub.Publish(controller.ConfigChanged, msg)
c.Assert(err, jc.ErrorIsNil)
select {
case <-pubsub.Wait(done):
case <-time.After(testing.LongWait):
c.Fatalf("handler didn't")
}
c.Check(ctx.featureEnabled("foo"), jc.IsTrue)
c.Check(ctx.featureEnabled("bar"), jc.IsTrue)
c.Check(ctx.featureEnabled("baz"), jc.IsFalse)
c.Check(stub.published, gc.HasLen, 0)
}
type noopRegisterer struct {
prometheus.Registerer
}
func (noopRegisterer) Register(prometheus.Collector) error {
return nil
}
func (noopRegisterer) Unregister(prometheus.Collector) bool {
return true
}