-
Notifications
You must be signed in to change notification settings - Fork 13
/
api_test.go
383 lines (352 loc) · 10.5 KB
/
api_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright © 2019 - 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goscaleio
import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"sync"
"testing"
v1 "github.com/dell/goscaleio/types/v1"
)
func setupClient(t *testing.T, hostAddr string) *Client {
os.Setenv("GOSCALEIO_ENDPOINT", hostAddr+"/api")
client, err := NewClient()
if err != nil {
t.Fatal(err)
}
// test ok
_, err = client.Authenticate(&ConfigConnect{
Username: "ScaleIOUser",
Password: "password",
Version: "2.0",
})
if err != nil {
t.Fatal(err)
}
return client
}
func requestAuthOK(resp http.ResponseWriter, req *http.Request) bool {
_, pwd, _ := req.BasicAuth()
if pwd == "" {
resp.WriteHeader(http.StatusUnauthorized)
resp.Write([]byte(`{"message":"Unauthorized","httpStatusCode":401,"errorCode":0}`))
return false
}
return true
}
func handleAuthToken(resp http.ResponseWriter, req *http.Request) {
if !requestAuthOK(resp, req) {
return
}
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`"012345678901234567890123456789"`))
}
func TestClientVersion(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
switch req.RequestURI {
case "/api/version":
// Check for valid authentication token
authHeader := req.Header.Get("Authorization")
if authHeader != "Bearer valid_token" {
// Respond with 401 Unauthorized only if the token is missing or invalid
if authHeader == "" {
resp.WriteHeader(http.StatusUnauthorized)
resp.Write([]byte(`Unauthorized 401`))
return
}
// For any other case, respond with version 4.0
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`"4.0"`))
return
}
// Respond with version 4.0
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`"4.0"`))
case "/api/login":
// Check basic authentication
uname, pwd, basic := req.BasicAuth()
if !basic {
// Respond with 401 Unauthorized if basic auth is not provided
resp.WriteHeader(http.StatusUnauthorized)
resp.Write([]byte(`{"message":"Unauthorized","httpStatusCode":401,"errorCode":0}`))
return
}
if uname != "ScaleIOUser" || pwd != "password" {
// Respond with 401 Unauthorized if credentials are invalid
resp.WriteHeader(http.StatusUnauthorized)
resp.Write([]byte(`{"message":"Unauthorized","httpStatusCode":401,"errorCode":0}`))
return
}
// Respond with a valid token
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`"012345678901234567890123456789"`))
default:
// Respond with 404 Not Found for any other endpoint
http.Error(resp, "Expecting endpoint /api/login got "+req.RequestURI, http.StatusNotFound)
}
},
))
defer server.Close()
// Set the environment variable for the endpoint
os.Setenv("GOSCALEIO_ENDPOINT", server.URL+"/api")
// Initialize the client
client, err := NewClient()
if err != nil {
t.Fatal(err)
}
// Test successful authentication
_, err = client.Authenticate(&ConfigConnect{
Username: "ScaleIOUser",
Password: "password",
Endpoint: "",
Version: "4.0",
})
if err != nil {
t.Fatal(err)
}
// Test for version retrieval
ver, err := client.GetVersion()
if err != nil {
// Check if the error is due to unauthorized access
if strings.Contains(err.Error(), "Unauthorized") {
// If unauthorized, test passes
return
}
// If error is not due to unauthorized access, fail the test
t.Fatal(err)
}
if ver != "4.0" {
t.Fatal("Expecting version string \"4.0\", got ", ver)
}
}
func TestClientLogin(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
switch req.RequestURI {
case "/api/version":
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`"2.0"`))
case "/api/login":
//accept := req.Header.Get("Accept")
// check Accept header
//if ver := strings.Split(accept, ";"); len(ver) != 2 {
// t.Fatal("Expecting Accept header to include version")
//} else {
// if !strings.HasPrefix(ver[1], "version=") {
// t.Fatal("Header Accept must include version")
// }
//}
uname, pwd, basic := req.BasicAuth()
if !basic {
t.Fatal("Client only support basic auth")
}
if uname != "ScaleIOUser" || pwd != "password" {
resp.WriteHeader(http.StatusUnauthorized)
resp.Write([]byte(`{"message":"Unauthorized","httpStatusCode":401,"errorCode":0}`))
return
}
resp.WriteHeader(http.StatusOK)
resp.Write([]byte(`"012345678901234567890123456789"`))
default:
t.Fatal("Expecting endpoint /api/login got", req.RequestURI)
}
},
))
defer server.Close()
hostAddr := server.URL
os.Setenv("GOSCALEIO_ENDPOINT", hostAddr+"/api")
client, err := NewClient()
if err != nil {
t.Fatal(err)
}
// test ok
_, err = client.Authenticate(&ConfigConnect{
Username: "ScaleIOUser",
Password: "password",
Endpoint: "",
Version: "2.0",
})
if err != nil {
t.Fatal(err)
}
if client.GetToken() != "012345678901234567890123456789" {
t.Fatal("Expecting token 012345678901234567890123456789, got", client.GetToken())
}
// test bad login
_, err = client.Authenticate(&ConfigConnect{
Username: "ScaleIOUser",
Password: "badPassWord",
Endpoint: "",
Version: "2.0",
})
if err == nil {
t.Fatal("Expecting an error for bad Login, but did not")
}
}
type stubTypeWithMetaData struct{}
func (s stubTypeWithMetaData) MetaData() http.Header {
h := make(http.Header)
h.Set("foo", "bar")
return h
}
func Test_addMetaData(t *testing.T) {
tests := []struct {
name string
givenHeader map[string]string
expectedHeader map[string]string
body interface{}
}{
{"nil header is a noop", nil, nil, nil},
{"nil body is a noop", nil, nil, nil},
{"header is updated", make(map[string]string), map[string]string{"Foo": "bar"}, stubTypeWithMetaData{}},
{"header is not updated", make(map[string]string), map[string]string{}, struct{}{}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
addMetaData(tt.givenHeader, tt.body)
switch {
case tt.givenHeader == nil:
if tt.givenHeader != nil {
t.Errorf("(%s): expected %s, actual %s", tt.body, tt.expectedHeader, tt.givenHeader)
}
case tt.body == nil:
if len(tt.givenHeader) != 0 {
t.Errorf("(%s): expected %s, actual %s", tt.body, tt.expectedHeader, tt.givenHeader)
}
default:
if !reflect.DeepEqual(tt.expectedHeader, tt.givenHeader) {
t.Errorf("(%s): expected %s, actual %s", tt.body, tt.expectedHeader, tt.givenHeader)
}
}
})
}
}
func Test_updateHeaders(_ *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func() {
defer wg.Done()
updateHeaders("3.5")
}()
}
wg.Wait()
}
func Test_getJSONWithRetry(t *testing.T) {
t.Run("retried request is similar to the original", func(t *testing.T) {
var (
paths []string // record the requested paths in order.
bodies []string // record the request bodies in order.
headers []http.Header // record the headers in order.
callCount int // how many times our endpoint was requested.
)
checkHeaders := []string{"Accept"} // only check these headers.
// mock a PowerFlex endpoint.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Record the requested paths in order.
paths = append(paths, fmt.Sprintf("%s %s", r.Method, r.URL.Path))
switch r.URL.Path {
case "/testing":
callCount++
b := testReadAll(t, r.Body)
bodies = append(bodies, string(b))
headers = append(headers, testFilterHeaders(r.Header, checkHeaders))
// First request to error with HTTP 401 and trigger a login request.
if callCount == 1 {
w.WriteHeader(http.StatusUnauthorized)
testjsonEncode(t, w, testBuildError(http.StatusUnauthorized))
}
case "/api/login":
fmt.Fprintf(w, `"fakesessiontoken"`)
default:
t.Fatalf("unexpected path: %q", r.URL.Path)
}
}))
defer ts.Close()
c, err := NewClientWithArgs(ts.URL, "3.5", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}
// Call getJSONWithRetry with a dummy request and some
// map as the request body. We don't care about the
// response so pass in nil.
m := map[string]string{"foo": "bar"}
wantBody, err := json.Marshal(&m)
if err != nil {
t.Fatal(err)
}
c.getJSONWithRetry(http.MethodPost, "/testing", wantBody, nil)
// Assert the call order was as expected.
wantPaths := []string{"POST /testing", "GET /api/login", "POST /testing"}
if !reflect.DeepEqual(paths, wantPaths) {
t.Errorf("paths: got %+v, want %+v", paths, wantPaths)
}
// Assert the second body was the same as the first.
gotBodies, wantBodies := bodies[1], bodies[0]
if !reflect.DeepEqual(gotBodies, wantBodies) {
t.Errorf("retried body: got %q, want %q", gotBodies, wantBodies)
}
// Assert the headers for both requests were the same.
gotHeaders, wantHeaders := headers[1], headers[0]
if !reflect.DeepEqual(gotHeaders, wantHeaders) {
t.Errorf("retried headers: got %q, want %q", gotHeaders, wantHeaders)
}
})
}
// testFilterHeaders accepts a header and a list of header names
// to filter on (inclusive). The returned http.Header will include only
// header fields with these names.
func testFilterHeaders(h http.Header, filter []string) http.Header {
result := make(http.Header)
for _, v := range filter {
if _, ok := h[v]; !ok {
continue
}
result.Set(v, h.Get(v))
}
return result
}
func testBuildError(code int) error {
return &v1.Error{
Message: "test message",
HTTPStatusCode: code,
ErrorCode: 0,
ErrorDetails: nil,
}
}
func testReadAll(t *testing.T, rc io.ReadCloser) []byte {
t.Helper()
b, err := io.ReadAll(rc)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
rc.Close()
})
return b
}
func testjsonEncode(t *testing.T, w io.Writer, v interface{}) {
t.Helper()
err := json.NewEncoder(w).Encode(&v)
if err != nil {
t.Fatal(err)
}
}