forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_undertaker_test.go
193 lines (159 loc) · 6.25 KB
/
api_undertaker_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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package featuretests
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v3"
gc "gopkg.in/check.v1"
"github.com/juju/juju/api"
"github.com/juju/juju/api/controller/undertaker"
apiwatcher "github.com/juju/juju/api/watcher"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/watcher/watchertest"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/rpc"
"github.com/juju/juju/state"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
)
// TODO(fwereade) 2016-03-17 lp:1558668
// this is not a feature test; much of it is redundant, and other
// bits should be tested elsewhere.
type undertakerSuite struct {
jujutesting.JujuConnSuite
}
func (s *undertakerSuite) TestPermDenied(c *gc.C) {
nonManagerMachine, _ := s.OpenAPIAsNewMachine(c, state.JobHostUnits)
for _, conn := range []api.Connection{
nonManagerMachine,
s.APIState,
} {
undertakerClient, err := undertaker.NewClient(conn, apiwatcher.NewNotifyWatcher)
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient, gc.NotNil)
_, err = undertakerClient.ModelInfo()
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
Message: "permission denied",
Code: "unauthorized access",
})
}
}
func (s *undertakerSuite) TestStateEnvironInfo(c *gc.C) {
st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
undertakerClient, err := undertaker.NewClient(st, apiwatcher.NewNotifyWatcher)
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient, gc.NotNil)
result, err := undertakerClient.ModelInfo()
c.Assert(err, jc.ErrorIsNil)
c.Assert(result, gc.NotNil)
c.Assert(result.Error, gc.IsNil)
info := result.Result
c.Assert(info.UUID, gc.Equals, coretesting.ModelTag.Id())
c.Assert(info.Name, gc.Equals, "controller")
c.Assert(info.GlobalName, gc.Equals, "user-admin/controller")
c.Assert(info.IsSystem, jc.IsTrue)
c.Assert(info.Life, gc.Equals, life.Alive)
}
func (s *undertakerSuite) TestStateProcessDyingEnviron(c *gc.C) {
st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
undertakerClient, err := undertaker.NewClient(st, apiwatcher.NewNotifyWatcher)
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient, gc.NotNil)
err = undertakerClient.ProcessDyingModel()
c.Assert(err, gc.ErrorMatches, "model is not dying")
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
c.Assert(model.Refresh(), jc.ErrorIsNil)
c.Assert(model.Life(), gc.Equals, state.Dying)
err = undertakerClient.ProcessDyingModel()
c.Assert(err, gc.ErrorMatches, `model not empty, found 1 machine \(model not empty\)`)
}
func (s *undertakerSuite) TestStateRemoveEnvironFails(c *gc.C) {
st, _ := s.OpenAPIAsNewMachine(c, state.JobManageModel)
undertakerClient, err := undertaker.NewClient(st, apiwatcher.NewNotifyWatcher)
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient, gc.NotNil)
c.Assert(undertakerClient.RemoveModel(), gc.ErrorMatches, "can't remove model: model still alive")
}
func (s *undertakerSuite) TestHostedEnvironInfo(c *gc.C) {
undertakerClient, otherSt := s.hostedAPI(c)
defer otherSt.Close()
result, err := undertakerClient.ModelInfo()
c.Assert(err, jc.ErrorIsNil)
c.Assert(result, gc.NotNil)
c.Assert(result.Error, gc.IsNil)
envInfo := result.Result
c.Assert(envInfo.UUID, gc.Equals, otherSt.ModelUUID())
c.Assert(envInfo.Name, gc.Equals, "hosted-env")
c.Assert(envInfo.GlobalName, gc.Equals, "user-admin/hosted-env")
c.Assert(envInfo.IsSystem, jc.IsFalse)
c.Assert(envInfo.Life, gc.Equals, life.Alive)
}
func (s *undertakerSuite) TestHostedProcessDyingEnviron(c *gc.C) {
undertakerClient, otherSt := s.hostedAPI(c)
defer otherSt.Close()
err := undertakerClient.ProcessDyingModel()
c.Assert(err, gc.ErrorMatches, "model is not dying")
factory.NewFactory(otherSt, s.StatePool).MakeApplication(c, nil)
model, err := otherSt.Model()
c.Assert(err, jc.ErrorIsNil)
c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
c.Assert(model.Refresh(), jc.ErrorIsNil)
c.Assert(model.Life(), gc.Equals, state.Dying)
err = otherSt.Cleanup()
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient.ProcessDyingModel(), jc.ErrorIsNil)
c.Assert(model.Refresh(), jc.ErrorIsNil)
c.Assert(model.Life(), gc.Equals, state.Dying)
}
func (s *undertakerSuite) TestWatchModelResources(c *gc.C) {
undertakerClient, otherSt := s.hostedAPI(c)
defer otherSt.Close()
w, err := undertakerClient.WatchModelResources()
c.Assert(err, jc.ErrorIsNil)
defer w.Kill()
wc := watchertest.NewNotifyWatcherC(c, w)
wc.AssertOneChange()
wc.AssertStops()
}
func (s *undertakerSuite) TestHostedRemoveEnviron(c *gc.C) {
undertakerClient, otherSt := s.hostedAPI(c)
defer otherSt.Close()
// Aborts on alive environ.
err := undertakerClient.RemoveModel()
c.Assert(err, gc.ErrorMatches, "can't remove model: model still alive")
factory.NewFactory(otherSt, s.StatePool).MakeApplication(c, nil)
model, err := otherSt.Model()
c.Assert(err, jc.ErrorIsNil)
c.Assert(model.Destroy(state.DestroyModelParams{}), jc.ErrorIsNil)
err = otherSt.Cleanup()
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient.ProcessDyingModel(), jc.ErrorIsNil)
c.Assert(undertakerClient.RemoveModel(), jc.ErrorIsNil)
c.Assert(otherSt.EnsureModelRemoved(), jc.ErrorIsNil)
}
func (s *undertakerSuite) hostedAPI(c *gc.C) (*undertaker.Client, *state.State) {
otherState := s.Factory.MakeModel(c, &factory.ModelParams{Name: "hosted-env"})
password, err := utils.RandomPassword()
c.Assert(err, jc.ErrorIsNil)
machine := s.Factory.MakeMachine(c, &factory.MachineParams{
Jobs: []state.MachineJob{state.JobManageModel},
Password: password,
Nonce: "fake_nonce",
})
// Connect to hosted environ from controller.
info := s.APIInfo(c)
info.Tag = machine.Tag()
info.Password = password
info.Nonce = "fake_nonce"
info.ModelTag = names.NewModelTag(otherState.ModelUUID())
otherAPIState, err := api.Open(info, api.DefaultDialOpts())
c.Assert(err, jc.ErrorIsNil)
undertakerClient, err := undertaker.NewClient(otherAPIState, apiwatcher.NewNotifyWatcher)
c.Assert(err, jc.ErrorIsNil)
c.Assert(undertakerClient, gc.NotNil)
return undertakerClient, otherState
}