-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiclient_test.go
178 lines (160 loc) · 5.22 KB
/
apiclient_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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api_test
import (
"fmt"
"io"
"net"
"strconv"
"github.com/juju/names"
"github.com/juju/utils/parallel"
gc "gopkg.in/check.v1"
"github.com/juju/juju/api"
"github.com/juju/juju/apiserver/params"
jujutesting "github.com/juju/juju/juju/testing"
coretesting "github.com/juju/juju/testing"
)
type apiclientSuite struct {
jujutesting.JujuConnSuite
}
var _ = gc.Suite(&apiclientSuite{})
type websocketSuite struct {
coretesting.BaseSuite
}
var _ = gc.Suite(&websocketSuite{})
func (s *apiclientSuite) TestOpenPrefersLocalhostIfPresent(c *gc.C) {
// Create a socket that proxies to the API server though our localhost address.
info := s.APIInfo(c)
serverAddr := info.Addrs[0]
server, err := net.Dial("tcp", serverAddr)
c.Assert(err, gc.IsNil)
defer server.Close()
listener, err := net.Listen("tcp", "localhost:0")
c.Assert(err, gc.IsNil)
defer listener.Close()
go func() {
for {
client, err := listener.Accept()
if err != nil {
return
}
go io.Copy(client, server)
go io.Copy(server, client)
}
}()
// Check that we are using our working address to connect
listenerAddress := listener.Addr().String()
// listenAddress contains the actual IP address, but APIHostPorts
// is going to report localhost, so just find the port
_, port, err := net.SplitHostPort(listenerAddress)
c.Check(err, gc.IsNil)
portNum, err := strconv.Atoi(port)
c.Check(err, gc.IsNil)
expectedHostPort := fmt.Sprintf("localhost:%d", portNum)
info.Addrs = []string{"fakeAddress:1", "fakeAddress:1", expectedHostPort}
st, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
defer st.Close()
c.Assert(st.Addr(), gc.Equals, expectedHostPort)
}
func (s *apiclientSuite) TestOpenMultiple(c *gc.C) {
// Create a socket that proxies to the API server.
info := s.APIInfo(c)
serverAddr := info.Addrs[0]
server, err := net.Dial("tcp", serverAddr)
c.Assert(err, gc.IsNil)
defer server.Close()
listener, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, gc.IsNil)
defer listener.Close()
go func() {
for {
client, err := listener.Accept()
if err != nil {
return
}
go io.Copy(client, server)
go io.Copy(server, client)
}
}()
// Check that we can use the proxy to connect.
proxyAddr := listener.Addr().String()
info.Addrs = []string{proxyAddr}
st, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
defer st.Close()
c.Assert(st.Addr(), gc.Equals, proxyAddr)
// Now break Addrs[0], and ensure that Addrs[1]
// is successfully connected to.
info.Addrs = []string{proxyAddr, serverAddr}
listener.Close()
st, err = api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
defer st.Close()
c.Assert(st.Addr(), gc.Equals, serverAddr)
}
func (s *apiclientSuite) TestOpenMultipleError(c *gc.C) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, gc.IsNil)
defer listener.Close()
go func() {
for {
client, err := listener.Accept()
if err != nil {
return
}
client.Close()
}
}()
info := s.APIInfo(c)
addr := listener.Addr().String()
info.Addrs = []string{addr, addr, addr}
_, err = api.Open(info, api.DialOpts{})
c.Assert(err, gc.ErrorMatches, `unable to connect to "wss://.*/environment/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/api"`)
}
func (s *apiclientSuite) TestOpenPassesEnvironTag(c *gc.C) {
info := s.APIInfo(c)
env, err := s.State.Environment()
c.Assert(err, gc.IsNil)
// TODO(jam): 2014-06-05 http://pad.lv/1326802
// we want to test this eventually, but for now s.APIInfo uses
// conn.StateInfo() which doesn't know about EnvironTag.
// c.Check(info.EnvironTag, gc.Equals, env.Tag())
// c.Assert(info.EnvironTag, gc.Not(gc.Equals), "")
// We start by ensuring we have an invalid tag, and Open should fail.
info.EnvironTag = names.NewEnvironTag("bad-tag")
_, err = api.Open(info, api.DialOpts{})
c.Check(err, gc.ErrorMatches, `unknown environment: "bad-tag"`)
c.Check(params.ErrCode(err), gc.Equals, params.CodeNotFound)
// Now set it to the right tag, and we should succeed.
info.EnvironTag = env.EnvironTag()
st, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
st.Close()
// Backwards compatibility, we should succeed if we do not set an
// environ tag
info.EnvironTag = names.NewEnvironTag("")
st, err = api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
st.Close()
}
func (s *apiclientSuite) TestDialWebsocketStopped(c *gc.C) {
stopped := make(chan struct{})
f := api.NewWebsocketDialer(nil, api.DialOpts{})
close(stopped)
result, err := f(stopped)
c.Assert(err, gc.Equals, parallel.ErrStopped)
c.Assert(result, gc.IsNil)
}
func (*websocketSuite) TestSetUpWebsocketConfig(c *gc.C) {
conf, err := api.SetUpWebsocket("0.1.2.3:1234", "", nil)
c.Assert(err, gc.IsNil)
c.Check(conf.Location.String(), gc.Equals, "wss://0.1.2.3:1234/")
c.Check(conf.Origin.String(), gc.Equals, "http://localhost/")
}
func (*websocketSuite) TestSetUpWebsocketConfigHandlesEnvironUUID(c *gc.C) {
conf, err := api.SetUpWebsocket("0.1.2.3:1234", "dead-beef-1234", nil)
c.Assert(err, gc.IsNil)
c.Check(conf.Location.String(), gc.Equals, "wss://0.1.2.3:1234/environment/dead-beef-1234/api")
c.Check(conf.Origin.String(), gc.Equals, "http://localhost/")
}