-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_test.go
329 lines (293 loc) · 10.2 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
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
package reggie
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var (
authUseAccessToken bool
lastCapturedRequest *http.Request
lastCapturedRequestBodyStr string
)
func TestClient(t *testing.T) {
authTestServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedAuthHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte("testuser:testpass"))
h := r.Header.Get("Authorization")
if h != expectedAuthHeader {
w.WriteHeader(http.StatusUnauthorized)
} else {
w.WriteHeader(http.StatusOK)
if authUseAccessToken {
w.Write([]byte(`{"access_token": "abc123"}`))
} else {
w.Write([]byte(`{"token": "abc123"}`))
}
}
}))
defer authTestServer.Close()
registryTestServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastCapturedRequest = r
buf := new(bytes.Buffer)
buf.ReadFrom(lastCapturedRequest.Body)
lastCapturedRequestBodyStr = buf.String()
h := r.Header.Get("Authorization")
if h == "Bearer abc123" {
w.Header().Set("Location", "https://abc123location.io/v2/blobs/uploads/e361aeb8-3181-11ea-850d-2e728ce88125")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`
{
"errors": [{
"code": "BLOB_UNKNOWN",
"message": "blob unknown to registry",
"detail": "lol"
}
]
}`))
} else {
wwwHeader := fmt.Sprintf("Bearer realm=\"%s/v2/auth\",service=\"testservice\",scope=\"testscope\"",
authTestServer.URL)
w.Header().Set("www-authenticate", wwwHeader)
w.WriteHeader(http.StatusUnauthorized)
}
}))
defer registryTestServer.Close()
// test client without Address is invalid
client, err := NewClient("")
if err == nil {
t.Fatalf("Providing empty Address should throw error: %s", err)
}
// test creating generic client
client, err = NewClient(registryTestServer.URL,
WithUsernamePassword("testuser", "testpass"),
WithDefaultName("testname"),
WithUserAgent("reggie-tests"),
WithInsecureSkipTLSVerify(true))
if err != nil {
t.Fatalf("Errors creating client: %s", err)
}
if !client.Config.InsecureSkipTLSVerify {
t.Errorf("Setting insecure skip TLS verify didn't work")
}
// test setting debug option
client2, err := NewClient(registryTestServer.URL, WithDebug(true))
if err != nil {
t.Fatalf("Errors creating client: %s", err)
}
if !client2.Config.Debug {
t.Errorf("Setting the debug flag didn't work")
}
// test setting auth scope
testScope := `realm="https://silly.com/v2/auth",service="testservice",scope="pull,push"`
client3, err := NewClient(registryTestServer.URL, WithAuthScope(testScope))
if err != nil {
t.Fatalf("Errors creating client: %s", err)
}
if s := client3.Config.AuthScope; s != testScope {
t.Errorf("Setting the auth scope didn't work: %s", s)
}
// test default name
req := client.NewRequest(GET, "/v2/<name>/tags/list")
if !strings.HasSuffix(req.URL, "/v2/testname/tags/list") {
t.Fatalf("NewRequest does not add default namespace to URL")
}
// check user agent
uaHeader := req.Header.Get("User-Agent")
if uaHeader != "reggie-tests" {
t.Fatalf("Expected User-Agent header to be \"reggie-tests\" but instead got \"%s\"", uaHeader)
}
resp, responseErr := client.Do(req)
if responseErr != nil {
t.Fatalf("Errors executing request: %s", err)
}
if status := resp.StatusCode(); status != http.StatusOK {
t.Fatalf("Expected response code 200 but was %d", status)
}
// test default name reset
client.SetDefaultName("othername")
req = client.NewRequest(GET, "/v2/<name>/tags/list")
if !strings.HasSuffix(req.URL, "/v2/othername/tags/list") {
t.Fatalf("NewRequest does not add runtime namespace to URL")
}
resp, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
if status := resp.StatusCode(); status != http.StatusOK {
t.Fatalf("Expected response code 200 but was %d", status)
}
// test custom name on request
req = client.NewRequest(GET, "/v2/<name>/tags/list", WithName("customname"))
if !strings.HasSuffix(req.URL, "/v2/customname/tags/list") {
t.Fatalf("NewRequest does not add runtime namespace to URL")
}
resp, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
if status := resp.StatusCode(); status != http.StatusOK {
t.Fatalf("Expected response code 200 but was %d", status)
}
// test Location header on request
relativeLocation := resp.GetRelativeLocation()
if strings.Contains(relativeLocation, "http://") || strings.Contains(relativeLocation, "https://") {
t.Fatalf("Relative Location contains host")
}
if relativeLocation == "" {
t.Fatalf("Location header not present")
}
// test error function on response
errorList, err := resp.Errors()
if err != nil {
t.Fatalf("Errors parsing json: %s", err)
}
if len(errorList) == 0 {
t.Fatalf("Error list is length zero")
}
e1 := errorList[0]
if e1.Code == "" {
t.Fatalf("Code not returned in response body")
}
if e1.Message == "" {
t.Fatalf("Message not returned in response body")
}
if e1.Detail == "" {
t.Fatalf("Detail not returned in response body")
}
// test absolute location as well
absoluteLocation := resp.GetAbsoluteLocation()
if absoluteLocation == "" {
t.Fatalf("Location header not present")
}
// test reference on request
req = client.NewRequest(HEAD, "/v2/<name>/manifests/<reference>", WithReference("silly"))
if !strings.HasSuffix(req.URL, "/v2/othername/manifests/silly") {
t.Fatalf("NewRequest does not add runtime reference to URL")
}
// test digest on request
digest := "6f4e69a5ff18d92e7315e3ee31c62165ebf25bfa05cad05c0d09d8f412dae401"
req = client.NewRequest(GET, "/v2/<name>/blobs/<digest>", WithDigest(digest))
if !strings.HasSuffix(req.URL, fmt.Sprintf("/v2/othername/blobs/%s", digest)) {
t.Fatalf("NewRequest does not add runtime digest to URL")
}
// test session id on request
id := "f0ca5d12-5557-4747-9c21-3d916f2fc885"
req = client.NewRequest(GET, "/v2/<name>/blobs/uploads/<session_id>", WithSessionID(id))
if !strings.HasSuffix(req.URL, fmt.Sprintf("/v2/othername/blobs/uploads/%s", id)) {
t.Fatalf("NewRequest does not add runtime digest to URL")
}
// invalid request (no ref)
req = client.NewRequest(HEAD, "/v2/<name>/manifests/<reference>")
resp, err = client.Do(req)
if err == nil {
t.Fatalf("Expected error with missing ref")
}
// invalid request (no digest)
req = client.NewRequest(GET, "/v2/<name>/blobs/<digest>")
resp, err = client.Do(req)
if err == nil {
t.Fatalf("Expected error with missing digest")
}
// invalid request (no session id)
req = client.NewRequest(GET, "/v2/<name>/blobs/uploads/<session_id>")
resp, err = client.Do(req)
if err == nil {
t.Fatalf("Expected error with missing session id")
}
// bad address on client should not create
_, err = NewClient("xwejknxw://jshnws")
if err == nil {
t.Fatalf("Should be errors creating client with bad address: %s", err)
}
// Make sure headers and body match after going through auth
req = client.NewRequest(PUT, "/a/b/c").
SetHeader("Content-Length", "3").
SetHeader("Content-Range", "0-2").
SetHeader("Content-Type", "application/octet-stream").
SetQueryParam("digest", "xyz").
SetBody([]byte("abc"))
_, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
// 5 headers expected: the ones we set plus "Authorization" and "User-Agent"
numHeaders := len(lastCapturedRequest.Header)
if numHeaders != 5 {
fmt.Println(lastCapturedRequest.Header)
t.Fatalf("Expected 5 headers total, instead got %d", numHeaders)
}
// Just to be safe, lets check each of the 5 headers are ones we expect
for _, h := range []string{
"Content-Length",
"Content-Range",
"Content-Type",
"Authorization",
"User-Agent",
} {
if lastCapturedRequest.Header.Get(h) == "" {
t.Fatalf("Missing header: %s", h)
}
}
// Check that the body did not get lost somewhere
if lastCapturedRequestBodyStr != "abc" {
t.Fatalf("Expected body to be \"abc\" but instead got %s", lastCapturedRequestBodyStr)
}
// Test that the retry callback is invoked, if configured.
newBody := "not the original body"
req = client.NewRequest(PUT, "/a/b/c", WithRetryCallback(func(r *Request) error {
r.SetBody(newBody)
return nil
})).SetBody([]byte("original body"))
_, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
// Ensure the request ended up with the new body.
if lastCapturedRequestBodyStr != newBody {
t.Fatalf("Expected body to be %q, but instead got %q", newBody, lastCapturedRequestBodyStr)
}
// Test the case where the retry callback returns an error.
req = client.NewRequest(PUT, "/a/b/c", WithRetryCallback(func(r *Request) error {
return errors.New("uh oh")
})).SetBody([]byte("original body"))
_, err = client.Do(req)
if err == nil {
t.Fatalf("Expected error from callback function, but request returned no error")
}
if !strings.Contains(err.Error(), "uh oh") {
t.Fatalf("Expected error to contain callback error \"uh oh\", but instead got %q", err)
}
// test for access_token vs. token
authUseAccessToken = true
req = client.NewRequest(GET, "/v2/<name>/tags/list")
_, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
// TODO: disable this
// See https://github.com/opencontainers/distribution-spec/issues/396
// Accept header should not be present unless explicitly set
req = client.NewRequest(PUT, "/a/b/c").
SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json")
_, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
if a := lastCapturedRequest.Header.Get("Accept"); a != "" {
t.Fatalf("Expected lastCapturedAcceptHeader to be empty string, but instead got %s", a)
}
req = client.NewRequest(PUT, "/a/b/c").
SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json").
SetHeader("Accept", "application/cha.cha.cha.v1+json")
_, err = client.Do(req)
if err != nil {
t.Fatalf("Errors executing request: %s", err)
}
if a := lastCapturedRequest.Header.Get("Accept"); a != "application/cha.cha.cha.v1+json" {
t.Fatalf("Expected lastCapturedAcceptHeader to be application/cha.cha.cha.v1+json, but instead got %s", a)
}
}