/
client_test.go
292 lines (253 loc) · 9.16 KB
/
client_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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api_test
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"code.google.com/p/go.net/websocket"
"github.com/juju/loggo"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"gopkg.in/juju/charm.v3"
charmtesting "gopkg.in/juju/charm.v3/testing"
gc "launchpad.net/gocheck"
"github.com/juju/juju/api"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/constraints"
jujutesting "github.com/juju/juju/juju/testing"
)
type clientSuite struct {
jujutesting.JujuConnSuite
}
var _ = gc.Suite(&clientSuite{})
// TODO(jam) 2013-08-27 http://pad.lv/1217282
// Right now most of the direct tests for api.Client behavior are in
// apiserver/client/*_test.go
func (s *clientSuite) TestCloseMultipleOk(c *gc.C) {
client := s.APIState.Client()
c.Assert(client.Close(), gc.IsNil)
c.Assert(client.Close(), gc.IsNil)
c.Assert(client.Close(), gc.IsNil)
}
func (s *clientSuite) TestAddLocalCharm(c *gc.C) {
charmArchive := charmtesting.Charms.CharmArchive(c.MkDir(), "dummy")
curl := charm.MustParseURL(
fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
)
client := s.APIState.Client()
// Test the sanity checks first.
_, err := client.AddLocalCharm(charm.MustParseURL("cs:quantal/wordpress-1"), nil)
c.Assert(err, gc.ErrorMatches, `expected charm URL with local: schema, got "cs:quantal/wordpress-1"`)
// Upload an archive with its original revision.
savedURL, err := client.AddLocalCharm(curl, charmArchive)
c.Assert(err, gc.IsNil)
c.Assert(savedURL.String(), gc.Equals, curl.String())
// Upload a charm directory with changed revision.
charmDir := charmtesting.Charms.ClonedDir(c.MkDir(), "dummy")
charmDir.SetDiskRevision(42)
savedURL, err = client.AddLocalCharm(curl, charmDir)
c.Assert(err, gc.IsNil)
c.Assert(savedURL.Revision, gc.Equals, 42)
// Upload a charm directory again, revision should be bumped.
savedURL, err = client.AddLocalCharm(curl, charmDir)
c.Assert(err, gc.IsNil)
c.Assert(savedURL.String(), gc.Equals, curl.WithRevision(43).String())
}
func (s *clientSuite) TestAddLocalCharmError(c *gc.C) {
lis, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, gc.IsNil)
defer lis.Close()
url := fmt.Sprintf("http://%v", lis.Addr())
http.HandleFunc("/charms", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
})
go func() {
http.Serve(lis, nil)
}()
client := s.APIState.Client()
api.SetServerRoot(client, url)
charmArchive := charmtesting.Charms.CharmArchive(c.MkDir(), "dummy")
curl := charm.MustParseURL(
fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
)
_, err = client.AddLocalCharm(curl, charmArchive)
c.Assert(err, gc.ErrorMatches, "charm upload failed: 405 \\(Method Not Allowed\\)")
}
func (s *clientSuite) TestClientEnvironmentUUID(c *gc.C) {
environ, err := s.State.Environment()
c.Assert(err, gc.IsNil)
client := s.APIState.Client()
c.Assert(client.EnvironmentUUID(), gc.Equals, environ.Tag().Id())
}
func (s *clientSuite) TestWatchDebugLogConnected(c *gc.C) {
// Shows both the unmarshalling of a real error, and
// that the api server is connected.
client := s.APIState.Client()
reader, err := client.WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.ErrorMatches, "cannot open log file: .*")
c.Assert(reader, gc.IsNil)
}
func (s *clientSuite) TestConnectionErrorBadConnection(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, func(_ *websocket.Config) (io.ReadCloser, error) {
return nil, fmt.Errorf("bad connection")
})
client := s.APIState.Client()
reader, err := client.WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.ErrorMatches, "bad connection")
c.Assert(reader, gc.IsNil)
}
func (s *clientSuite) TestConnectionErrorNoData(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, func(_ *websocket.Config) (io.ReadCloser, error) {
return ioutil.NopCloser(&bytes.Buffer{}), nil
})
client := s.APIState.Client()
reader, err := client.WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.ErrorMatches, "unable to read initial response: EOF")
c.Assert(reader, gc.IsNil)
}
func (s *clientSuite) TestConnectionErrorBadData(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, func(_ *websocket.Config) (io.ReadCloser, error) {
junk := strings.NewReader("junk\n")
return ioutil.NopCloser(junk), nil
})
client := s.APIState.Client()
reader, err := client.WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.ErrorMatches, "unable to unmarshal initial response: .*")
c.Assert(reader, gc.IsNil)
}
func (s *clientSuite) TestConnectionErrorReadError(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, func(_ *websocket.Config) (io.ReadCloser, error) {
err := fmt.Errorf("bad read")
return ioutil.NopCloser(&badReader{err}), nil
})
client := s.APIState.Client()
reader, err := client.WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.ErrorMatches, "unable to read initial response: bad read")
c.Assert(reader, gc.IsNil)
}
func (s *clientSuite) TestParamsEncoded(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, echoURL(c))
params := api.DebugLogParams{
IncludeEntity: []string{"a", "b"},
IncludeModule: []string{"c", "d"},
ExcludeEntity: []string{"e", "f"},
ExcludeModule: []string{"g", "h"},
Limit: 100,
Backlog: 200,
Level: loggo.ERROR,
Replay: true,
}
client := s.APIState.Client()
reader, err := client.WatchDebugLog(params)
c.Assert(err, gc.IsNil)
connectURL := connectURLFromReader(c, reader)
c.Assert(connectURL.Path, gc.Matches, "/log")
values := connectURL.Query()
c.Assert(values, jc.DeepEquals, url.Values{
"includeEntity": params.IncludeEntity,
"includeModule": params.IncludeModule,
"excludeEntity": params.ExcludeEntity,
"excludeModule": params.ExcludeModule,
"maxLines": {"100"},
"backlog": {"200"},
"level": {"ERROR"},
"replay": {"true"},
})
}
func (s *clientSuite) TestDebugLogRootPath(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, echoURL(c))
// If the server is old, we log at "/log"
info := s.APIInfo(c)
info.EnvironTag = nil
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
defer apistate.Close()
reader, err := apistate.Client().WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.IsNil)
connectURL := connectURLFromReader(c, reader)
c.Assert(connectURL.Path, gc.Matches, "/log")
}
func (s *clientSuite) TestDebugLogAtUUIDLogPath(c *gc.C) {
s.PatchValue(api.WebsocketDialConfig, echoURL(c))
// If the server supports it, we should log at "/environment/UUID/log"
environ, err := s.State.Environment()
c.Assert(err, gc.IsNil)
info := s.APIInfo(c)
info.EnvironTag = environ.Tag()
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
defer apistate.Close()
reader, err := apistate.Client().WatchDebugLog(api.DebugLogParams{})
c.Assert(err, gc.IsNil)
connectURL := connectURLFromReader(c, reader)
c.ExpectFailure("debug log always goes to /log for compatibility http://pad.lv/1326799")
c.Assert(connectURL.Path, gc.Matches, fmt.Sprintf("/%s/log", environ.UUID()))
}
func (s *clientSuite) TestOpenUsesEnvironUUIDPaths(c *gc.C) {
info := s.APIInfo(c)
// Backwards compatibility, passing EnvironTag = "" should just work
info.EnvironTag = nil
apistate, err := api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
apistate.Close()
// Passing in the correct environment UUID should also work
environ, err := s.State.Environment()
c.Assert(err, gc.IsNil)
info.EnvironTag = environ.Tag()
apistate, err = api.Open(info, api.DialOpts{})
c.Assert(err, gc.IsNil)
apistate.Close()
// Passing in a bad environment UUID should fail with a known error
info.EnvironTag = names.NewEnvironTag("dead-beef-123456")
apistate, err = api.Open(info, api.DialOpts{})
c.Check(err, gc.ErrorMatches, `unknown environment: "dead-beef-123456"`)
c.Check(err, jc.Satisfies, params.IsCodeNotFound)
c.Assert(apistate, gc.IsNil)
}
func (s *clientSuite) TestClientEnsureAvailabilityFailsBadEnvTag(c *gc.C) {
defer api.PatchEnvironTag(s.APIState, "bad-env-uuid")()
emptyCons := constraints.Value{}
defaultSeries := ""
_, err := s.APIState.Client().EnsureAvailability(3, emptyCons, defaultSeries)
c.Assert(err, gc.ErrorMatches,
`invalid environment tag: "bad-env-uuid" is not a valid tag`)
}
// badReader raises err when Read is called.
type badReader struct {
err error
}
func (r *badReader) Read(p []byte) (n int, err error) {
return 0, r.err
}
func echoURL(c *gc.C) func(*websocket.Config) (io.ReadCloser, error) {
response := ¶ms.ErrorResult{}
message, err := json.Marshal(response)
c.Assert(err, gc.IsNil)
return func(config *websocket.Config) (io.ReadCloser, error) {
pr, pw := io.Pipe()
go func() {
fmt.Fprintf(pw, "%s\n", message)
fmt.Fprintf(pw, "%s\n", config.Location)
}()
return pr, nil
}
}
func connectURLFromReader(c *gc.C, rc io.ReadCloser) *url.URL {
bufReader := bufio.NewReader(rc)
location, err := bufReader.ReadString('\n')
c.Assert(err, gc.IsNil)
connectURL, err := url.Parse(strings.TrimSpace(location))
c.Assert(err, gc.IsNil)
rc.Close()
return connectURL
}