forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basesuite_test.go
121 lines (101 loc) · 3.31 KB
/
basesuite_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
// Copyright 2019 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver_test
import (
"time"
"github.com/juju/clock"
"github.com/juju/loggo"
"github.com/juju/pubsub/v2"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/worker/v3/workertest"
"github.com/prometheus/client_golang/prometheus"
gc "gopkg.in/check.v1"
"github.com/juju/juju/api"
"github.com/juju/juju/apiserver"
"github.com/juju/juju/apiserver/testserver"
"github.com/juju/juju/core/cache"
"github.com/juju/juju/state"
statetesting "github.com/juju/juju/state/testing"
"github.com/juju/juju/worker/gate"
"github.com/juju/juju/worker/modelcache"
"github.com/juju/juju/worker/multiwatcher"
)
type baseSuite struct {
statetesting.StateSuite
controller *cache.Controller
cfg apiserver.ServerConfig
}
func (s *baseSuite) SetUpTest(c *gc.C) {
s.StateSuite.SetUpTest(c)
loggo.GetLogger("juju.apiserver").SetLogLevel(loggo.TRACE)
allWatcherBacking, err := state.NewAllWatcherBacking(s.StatePool)
c.Assert(err, jc.ErrorIsNil)
multiWatcherWorker, err := multiwatcher.NewWorker(multiwatcher.Config{
Clock: clock.WallClock,
Logger: loggo.GetLogger("test"),
Backing: allWatcherBacking,
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()
modelCache, err := modelcache.NewWorker(modelcache.Config{
StatePool: s.StatePool,
Hub: pubsub.NewStructuredHub(nil),
InitializedGate: initialized,
Logger: loggo.GetLogger("modelcache"),
WatcherFactory: multiWatcherWorker.WatchController,
PrometheusRegisterer: noopRegisterer{},
Cleanup: func() {},
}.WithDefaultRestartStrategy())
c.Assert(err, jc.ErrorIsNil)
s.AddCleanup(func(c *gc.C) { workertest.CleanKill(c, modelCache) })
select {
case <-initialized.Unlocked():
case <-time.After(testing.LongWait):
c.Fatalf("model cache not initialized after %s", testing.LongWait)
}
err = modelcache.ExtractCacheController(modelCache, &s.controller)
c.Assert(err, jc.ErrorIsNil)
s.cfg = testserver.DefaultServerConfig(c, s.Clock)
s.cfg.Controller = s.controller
}
func (s *baseSuite) newServer(c *gc.C) *testserver.Server {
server := testserver.NewServerWithConfig(c, s.StatePool, s.cfg)
s.AddCleanup(func(c *gc.C) {
workertest.CleanKill(c, server.APIServer)
server.HTTPServer.Close()
})
server.Info.ModelTag = s.Model.ModelTag()
return server
}
func (s *baseSuite) openAPIWithoutLogin(c *gc.C, info0 *api.Info) api.Connection {
info := *info0
info.Tag = nil
info.Password = ""
info.SkipLogin = true
info.Macaroons = nil
st, err := api.Open(&info, fastDialOpts)
c.Assert(err, jc.ErrorIsNil)
s.AddCleanup(func(*gc.C) { _ = st.Close() })
return st
}
// derivedSuite is just here to test newServer is clean.
type derivedSuite struct {
baseSuite
}
var _ = gc.Suite(&derivedSuite{})
func (s *derivedSuite) TestNewServer(c *gc.C) {
_ = s.newServer(c)
}
type noopRegisterer struct {
prometheus.Registerer
}
func (noopRegisterer) Register(prometheus.Collector) error {
return nil
}
func (noopRegisterer) Unregister(prometheus.Collector) bool {
return true
}