-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold_test.go
205 lines (171 loc) · 5.24 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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package undertaker_test
import (
"github.com/juju/errors"
"github.com/juju/juju/caas"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/worker.v1"
"gopkg.in/juju/worker.v1/dependency"
dt "gopkg.in/juju/worker.v1/dependency/testing"
"github.com/juju/juju/api/base"
"github.com/juju/juju/environs"
"github.com/juju/juju/worker/common"
"github.com/juju/juju/worker/undertaker"
)
type manifoldSuite struct {
testing.IsolationSuite
modelType string
}
type CAASManifoldSuite struct {
manifoldSuite
}
type IAASManifoldSuite struct {
manifoldSuite
}
var (
_ = gc.Suite(&IAASManifoldSuite{})
_ = gc.Suite(&CAASManifoldSuite{})
)
func (s *CAASManifoldSuite) SetUpTest(c *gc.C) {
s.modelType = "caas"
}
func (s *IAASManifoldSuite) SetUpTest(c *gc.C) {
s.modelType = "iaas"
}
func (s *manifoldSuite) destroyerName() string {
if s.modelType == "iaas" {
return "environ"
}
return "broker"
}
func (s *manifoldSuite) namesConfig() undertaker.ManifoldConfig {
destroyerName := "environ"
if s.modelType == "caas" {
destroyerName = "broker"
}
return undertaker.ManifoldConfig{
APICallerName: "api-caller",
CloudDestroyerName: destroyerName,
NewCredentialValidatorFacade: func(base.APICaller) (common.CredentialAPI, error) {
return &fakeCredentialAPI{}, nil
},
}
}
func (s *manifoldSuite) TestInputs(c *gc.C) {
manifold := undertaker.Manifold(s.namesConfig())
c.Check(manifold.Inputs, jc.DeepEquals, []string{
"api-caller", s.destroyerName(),
})
}
func (s *manifoldSuite) TestOutput(c *gc.C) {
manifold := undertaker.Manifold(s.namesConfig())
c.Check(manifold.Output, gc.IsNil)
}
func (s *manifoldSuite) TestAPICallerMissing(c *gc.C) {
resources := resourcesMissing("api-caller")
manifold := undertaker.Manifold(s.namesConfig())
worker, err := manifold.Start(resources.Context())
c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
c.Check(worker, gc.IsNil)
}
func (s *manifoldSuite) TestEnvironMissing(c *gc.C) {
resources := resourcesMissing("environ", "broker")
manifold := undertaker.Manifold(s.namesConfig())
worker, err := manifold.Start(resources.Context())
c.Check(errors.Cause(err), gc.Equals, dependency.ErrMissing)
c.Check(worker, gc.IsNil)
}
func (s *manifoldSuite) TestNewFacadeError(c *gc.C) {
resources := resourcesMissing()
config := s.namesConfig()
config.NewFacade = func(apiCaller base.APICaller) (undertaker.Facade, error) {
checkResource(c, apiCaller, resources, "api-caller")
return nil, errors.New("blort")
}
manifold := undertaker.Manifold(config)
worker, err := manifold.Start(resources.Context())
c.Check(err, gc.ErrorMatches, "blort")
c.Check(worker, gc.IsNil)
}
func (s *manifoldSuite) TestNewCredentialAPIError(c *gc.C) {
config := s.namesConfig()
config.NewFacade = func(_ base.APICaller) (undertaker.Facade, error) {
return &fakeFacade{}, nil
}
config.NewCredentialValidatorFacade = func(apiCaller base.APICaller) (common.CredentialAPI, error) {
return nil, errors.New("blort")
}
manifold := undertaker.Manifold(config)
resources := resourcesMissing()
worker, err := manifold.Start(resources.Context())
c.Check(err, gc.ErrorMatches, "blort")
c.Check(worker, gc.IsNil)
}
func (s *manifoldSuite) TestNewWorkerError(c *gc.C) {
resources := resourcesMissing()
expectFacade := &fakeFacade{}
config := s.namesConfig()
config.NewFacade = func(_ base.APICaller) (undertaker.Facade, error) {
return expectFacade, nil
}
config.NewWorker = func(cfg undertaker.Config) (worker.Worker, error) {
c.Check(cfg.Facade, gc.Equals, expectFacade)
checkResource(c, cfg.Destroyer, resources, s.destroyerName())
return nil, errors.New("lhiis")
}
manifold := undertaker.Manifold(config)
worker, err := manifold.Start(resources.Context())
c.Check(err, gc.ErrorMatches, "lhiis")
c.Check(worker, gc.IsNil)
}
func (s *manifoldSuite) TestNewWorkerSuccess(c *gc.C) {
expectWorker := &fakeWorker{}
config := s.namesConfig()
config.NewFacade = func(_ base.APICaller) (undertaker.Facade, error) {
return &fakeFacade{}, nil
}
config.NewWorker = func(_ undertaker.Config) (worker.Worker, error) {
return expectWorker, nil
}
manifold := undertaker.Manifold(config)
resources := resourcesMissing()
worker, err := manifold.Start(resources.Context())
c.Check(err, jc.ErrorIsNil)
c.Check(worker, gc.Equals, expectWorker)
}
func resourcesMissing(missing ...string) dt.StubResources {
resources := dt.StubResources{
"api-caller": dt.NewStubResource(&fakeAPICaller{}),
"environ": dt.NewStubResource(&fakeEnviron{}),
"broker": dt.NewStubResource(&fakeBroker{}),
}
for _, name := range missing {
resources[name] = dt.StubResource{Error: dependency.ErrMissing}
}
return resources
}
func checkResource(c *gc.C, actual interface{}, resources dt.StubResources, name string) {
c.Check(actual, gc.Equals, resources[name].Outputs[0])
}
type fakeAPICaller struct {
base.APICaller
}
type fakeEnviron struct {
environs.Environ
}
type fakeBroker struct {
caas.Broker
}
type fakeFacade struct {
undertaker.Facade
}
type fakeWorker struct {
worker.Worker
}
type fakeCredentialAPI struct{}
func (*fakeCredentialAPI) InvalidateModelCredential(reason string) error {
return nil
}