-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_test.go
257 lines (235 loc) · 7.97 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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api_test
import (
stdtesting "testing"
gc "launchpad.net/gocheck"
"github.com/juju/juju/api"
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.State, string, string) {
info := s.APIInfo(c)
tag := info.Tag
password := info.Password
info.Tag = nil
info.Password = ""
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
return apistate, tag.String(), 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
badValue := network.HostPort{network.Address{
Value: "0.1.2.3",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeMachineLocal,
}, 1234}
badServer := []network.HostPort{badValue}
s.State.SetAPIHostPorts([][]network.HostPort{badServer})
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
defer apistate.Close()
hostports := apistate.APIHostPorts()
c.Check(hostports, gc.DeepEquals, [][]network.HostPort{serverhostports, badServer})
}
func (s *stateSuite) TestLoginSetsEnvironTag(c *gc.C) {
env, err := s.State.Environment()
c.Assert(err, gc.IsNil)
apistate, tag, password := s.OpenAPIWithoutLogin(c)
defer apistate.Close()
// We haven't called Login yet, so the EnvironTag shouldn't be set.
envTag, err := apistate.EnvironTag()
c.Check(err, gc.ErrorMatches, `"" is not a valid tag`)
c.Check(envTag.String(), gc.Equals, "environment-")
err = apistate.Login(tag, password, "")
c.Assert(err, gc.IsNil)
// Now that we've logged in, EnvironTag should be updated correctly.
envTag, err = apistate.EnvironTag()
c.Check(err, gc.IsNil)
c.Check(envTag, gc.Equals, env.EnvironTag())
}
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, "")
c.Assert(err, gc.IsNil)
// 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 v0 of the Client facade
c.Assert(allVersions["Client"], gc.Not(gc.HasLen), 0)
c.Check(allVersions["Client"][0], gc.Equals, 0)
}
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, 0)
}
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{network.Address{
Value: "0.1.2.3",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeMachineLocal,
}, 1234}
badServer := []network.HostPort{badValue}
extraAddress := network.HostPort{network.Address{
Value: "0.1.2.4",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeMachineLocal,
}, 5678}
extraAddress2 := network.HostPort{network.Address{
Value: "0.1.2.1",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeMachineLocal,
}, 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, gc.IsNil)
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.Address{
Value: "0.1.2.3",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeUnknown,
}, Port: 1234,
}, {
Address: network.Address{
Value: "0.1.2.4",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeUnknown,
}, Port: 5678,
}, {
Address: network.Address{
Value: "0.1.2.1",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeUnknown,
}, Port: 9012,
}, {
Address: network.Address{
Value: "0.1.9.1",
Type: network.IPv4Address,
NetworkName: "",
Scope: network.ScopeUnknown,
}, 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)
}