-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_test.go
327 lines (298 loc) · 10.5 KB
/
state_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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api_test
import (
stdtesting "testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"
"gopkg.in/macaroon.v1"
"github.com/juju/juju/api"
"github.com/juju/juju/api/modelmanager"
"github.com/juju/juju/api/usermanager"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/network"
coretesting "github.com/juju/juju/testing"
)
func TestAll(t *stdtesting.T) {
coretesting.MgoTestPackage(t)
}
type stateSuite struct {
jujutesting.JujuConnSuite
}
var _ = gc.Suite(&stateSuite{})
type slideSuite struct {
coretesting.BaseSuite
}
var _ = gc.Suite(&slideSuite{})
func (s *stateSuite) TestCloseMultipleOk(c *gc.C) {
c.Assert(s.APIState.Close(), gc.IsNil)
c.Assert(s.APIState.Close(), gc.IsNil)
c.Assert(s.APIState.Close(), gc.IsNil)
}
// OpenAPIWithoutLogin connects to the API and returns an api.State without
// actually calling st.Login already. The returned strings are the "tag" and
// "password" that we would have used to login.
func (s *stateSuite) OpenAPIWithoutLogin(c *gc.C) (api.Connection, names.Tag, string) {
info := s.APIInfo(c)
tag := info.Tag
password := info.Password
info.Tag = nil
info.Password = ""
info.Macaroons = nil
info.SkipLogin = true
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, jc.ErrorIsNil)
return apistate, tag, password
}
func (s *stateSuite) TestAPIHostPortsAlwaysIncludesTheConnection(c *gc.C) {
hostportslist := s.APIState.APIHostPorts()
c.Check(hostportslist, gc.HasLen, 1)
serverhostports := hostportslist[0]
c.Check(serverhostports, gc.HasLen, 1)
// the other addresses, but always see this one as well.
info := s.APIInfo(c)
// We intentionally set this to invalid values
badServer := network.NewHostPorts(1234, "0.1.2.3")
badServer[0].Scope = network.ScopeMachineLocal
s.State.SetAPIHostPorts([][]network.HostPort{badServer})
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, jc.ErrorIsNil)
defer apistate.Close()
hostports := apistate.APIHostPorts()
c.Check(hostports, gc.DeepEquals, [][]network.HostPort{
serverhostports,
badServer,
})
}
func (s *stateSuite) TestTags(c *gc.C) {
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
apistate, tag, password := s.OpenAPIWithoutLogin(c)
defer apistate.Close()
// Even though we haven't called Login, the model tag should
// still be set.
modelTag, ok := apistate.ModelTag()
c.Check(ok, jc.IsTrue)
c.Check(modelTag, gc.Equals, model.ModelTag())
err = apistate.Login(tag, password, "", nil)
c.Assert(err, jc.ErrorIsNil)
// Now that we've logged in, ModelTag should still be the same.
modelTag, ok = apistate.ModelTag()
c.Check(ok, jc.IsTrue)
c.Check(modelTag, gc.Equals, model.ModelTag())
controllerTag := apistate.ControllerTag()
c.Check(controllerTag, gc.Equals, coretesting.ControllerTag)
}
func (s *stateSuite) TestLoginMacaroon(c *gc.C) {
apistate, tag, _ := s.OpenAPIWithoutLogin(c)
defer apistate.Close()
// Use a different API connection, because we can't get at UserManager without logging in.
loggedInAPI := s.OpenControllerAPI(c)
defer loggedInAPI.Close()
mac, err := usermanager.NewClient(loggedInAPI).CreateLocalLoginMacaroon(tag.(names.UserTag))
c.Assert(err, jc.ErrorIsNil)
err = apistate.Login(tag, "", "", []macaroon.Slice{{mac}})
c.Assert(err, jc.ErrorIsNil)
c.Assert(apistate.AuthTag(), gc.Equals, tag)
}
func (s *stateSuite) TestLoginSetsModelAccess(c *gc.C) {
// The default user has admin access.
c.Assert(s.APIState.ModelAccess(), gc.Equals, "admin")
manager := usermanager.NewClient(s.OpenControllerAPI(c))
defer manager.Close()
usertag, _, err := manager.AddUser("ro", "ro", "ro-password")
c.Assert(err, jc.ErrorIsNil)
mmanager := modelmanager.NewClient(s.OpenControllerAPI(c))
defer mmanager.Close()
modeltag, ok := s.APIState.ModelTag()
c.Assert(ok, jc.IsTrue)
err = mmanager.GrantModel(usertag.Canonical(), "read", modeltag.Id())
c.Assert(err, jc.ErrorIsNil)
conn := s.OpenAPIAs(c, usertag, "ro-password")
c.Assert(conn.ModelAccess(), gc.Equals, "read")
}
func (s *stateSuite) TestLoginSetsControllerAccess(c *gc.C) {
// The default user has admin access.
c.Assert(s.APIState.ControllerAccess(), gc.Equals, "superuser")
manager := usermanager.NewClient(s.OpenControllerAPI(c))
defer manager.Close()
usertag, _, err := manager.AddUser("ro", "ro", "ro-password")
c.Assert(err, jc.ErrorIsNil)
mmanager := modelmanager.NewClient(s.OpenControllerAPI(c))
defer mmanager.Close()
modeltag, ok := s.APIState.ModelTag()
c.Assert(ok, jc.IsTrue)
err = mmanager.GrantModel(usertag.Canonical(), "read", modeltag.Id())
c.Assert(err, jc.ErrorIsNil)
conn := s.OpenAPIAs(c, usertag, "ro-password")
c.Assert(conn.ControllerAccess(), gc.Equals, "login")
}
func (s *stateSuite) TestLoginMacaroonInvalidId(c *gc.C) {
apistate, tag, _ := s.OpenAPIWithoutLogin(c)
defer apistate.Close()
mac, err := macaroon.New([]byte("root-key"), "id", "juju")
c.Assert(err, jc.ErrorIsNil)
err = apistate.Login(tag, "", "", []macaroon.Slice{{mac}})
c.Assert(err, gc.ErrorMatches, "invalid entity name or password \\(unauthorized access\\)")
}
func (s *stateSuite) TestLoginMacaroonInvalidUser(c *gc.C) {
apistate, tag, _ := s.OpenAPIWithoutLogin(c)
defer apistate.Close()
// Use a different API connection, because we can't get at UserManager without logging in.
loggedInAPI := s.OpenControllerAPI(c)
defer loggedInAPI.Close()
mac, err := usermanager.NewClient(loggedInAPI).CreateLocalLoginMacaroon(tag.(names.UserTag))
c.Assert(err, jc.ErrorIsNil)
err = apistate.Login(names.NewUserTag("bob@local"), "", "", []macaroon.Slice{{mac}})
c.Assert(err, gc.ErrorMatches, "invalid entity name or password \\(unauthorized access\\)")
}
func (s *stateSuite) TestLoginTracksFacadeVersions(c *gc.C) {
apistate, tag, password := s.OpenAPIWithoutLogin(c)
defer apistate.Close()
// We haven't called Login yet, so the Facade Versions should be empty
c.Check(apistate.AllFacadeVersions(), gc.HasLen, 0)
err := apistate.Login(tag, password, "", nil)
c.Assert(err, jc.ErrorIsNil)
// Now that we've logged in, AllFacadeVersions should be updated.
allVersions := apistate.AllFacadeVersions()
c.Check(allVersions, gc.Not(gc.HasLen), 0)
// For sanity checking, ensure that we have a v2 of the Client facade
c.Assert(allVersions["Client"], gc.Not(gc.HasLen), 0)
c.Check(allVersions["Client"][0], gc.Equals, 1)
}
func (s *stateSuite) TestAllFacadeVersionsSafeFromMutation(c *gc.C) {
allVersions := s.APIState.AllFacadeVersions()
clients := allVersions["Client"]
origClients := make([]int, len(clients))
copy(origClients, clients)
// Mutating the dict should not affect the cached versions
allVersions["Client"] = append(allVersions["Client"], 2597)
newVersions := s.APIState.AllFacadeVersions()
newClientVers := newVersions["Client"]
c.Check(newClientVers, gc.DeepEquals, origClients)
c.Check(newClientVers[len(newClientVers)-1], gc.Not(gc.Equals), 2597)
}
func (s *stateSuite) TestBestFacadeVersion(c *gc.C) {
c.Check(s.APIState.BestFacadeVersion("Client"), gc.Equals, 1)
}
func (s *stateSuite) TestAPIHostPortsMovesConnectedValueFirst(c *gc.C) {
hostportslist := s.APIState.APIHostPorts()
c.Check(hostportslist, gc.HasLen, 1)
serverhostports := hostportslist[0]
c.Check(serverhostports, gc.HasLen, 1)
goodAddress := serverhostports[0]
// the other addresses, but always see this one as well.
info := s.APIInfo(c)
// We intentionally set this to invalid values
badValue := network.HostPort{
Address: network.Address{
Value: "0.1.2.3",
Type: network.IPv4Address,
Scope: network.ScopeMachineLocal,
},
Port: 1234,
}
badServer := []network.HostPort{badValue}
extraAddress := network.HostPort{
Address: network.Address{
Value: "0.1.2.4",
Type: network.IPv4Address,
Scope: network.ScopeMachineLocal,
},
Port: 5678,
}
extraAddress2 := network.HostPort{
Address: network.Address{
Value: "0.1.2.1",
Type: network.IPv4Address,
Scope: network.ScopeMachineLocal,
},
Port: 9012,
}
serverExtra := []network.HostPort{
extraAddress, goodAddress, extraAddress2,
}
current := [][]network.HostPort{badServer, serverExtra}
s.State.SetAPIHostPorts(current)
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, jc.ErrorIsNil)
defer apistate.Close()
hostports := apistate.APIHostPorts()
// We should have rotate the server we connected to as the first item,
// and the address of that server as the first address
sortedServer := []network.HostPort{
goodAddress, extraAddress, extraAddress2,
}
expected := [][]network.HostPort{sortedServer, badServer}
c.Check(hostports, gc.DeepEquals, expected)
}
var exampleHostPorts = []network.HostPort{{
Address: network.NewAddress("0.1.2.3"),
Port: 1234,
}, {
Address: network.NewAddress("0.1.2.4"),
Port: 5678,
}, {
Address: network.NewAddress("0.1.2.1"),
Port: 9012,
}, {
Address: network.NewAddress("0.1.9.1"),
Port: 8888,
}}
func (s *slideSuite) TestSlideToFrontNoOp(c *gc.C) {
servers := [][]network.HostPort{
{exampleHostPorts[0]},
{exampleHostPorts[1]},
}
// order should not have changed
expected := [][]network.HostPort{
{exampleHostPorts[0]},
{exampleHostPorts[1]},
}
api.SlideAddressToFront(servers, 0, 0)
c.Check(servers, gc.DeepEquals, expected)
}
func (s *slideSuite) TestSlideToFrontAddress(c *gc.C) {
servers := [][]network.HostPort{
{exampleHostPorts[0], exampleHostPorts[1], exampleHostPorts[2]},
{exampleHostPorts[3]},
}
// server order should not change, but ports should be switched
expected := [][]network.HostPort{
{exampleHostPorts[1], exampleHostPorts[0], exampleHostPorts[2]},
{exampleHostPorts[3]},
}
api.SlideAddressToFront(servers, 0, 1)
c.Check(servers, gc.DeepEquals, expected)
}
func (s *slideSuite) TestSlideToFrontServer(c *gc.C) {
servers := [][]network.HostPort{
{exampleHostPorts[0], exampleHostPorts[1]},
{exampleHostPorts[2]},
{exampleHostPorts[3]},
}
// server 1 should be slid to the front
expected := [][]network.HostPort{
{exampleHostPorts[2]},
{exampleHostPorts[0], exampleHostPorts[1]},
{exampleHostPorts[3]},
}
api.SlideAddressToFront(servers, 1, 0)
c.Check(servers, gc.DeepEquals, expected)
}
func (s *slideSuite) TestSlideToFrontBoth(c *gc.C) {
servers := [][]network.HostPort{
{exampleHostPorts[0]},
{exampleHostPorts[1], exampleHostPorts[2]},
{exampleHostPorts[3]},
}
// server 1 should be slid to the front
expected := [][]network.HostPort{
{exampleHostPorts[2], exampleHostPorts[1]},
{exampleHostPorts[0]},
{exampleHostPorts[3]},
}
api.SlideAddressToFront(servers, 1, 1)
c.Check(servers, gc.DeepEquals, expected)
}