| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package remote |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"net/http" |
| 25 |
"net/http/httptest" |
| 26 |
"net/url" |
| 27 |
"reflect" |
| 28 |
"strconv" |
| 29 |
"strings" |
| 30 |
"testing" |
| 31 |
|
| 32 |
"oras.land/oras-go/v2/errdef" |
| 33 |
"oras.land/oras-go/v2/registry" |
| 34 |
) |
| 35 |
|
| 36 |
func TestRegistryInterface(t *testing.T) { |
| 37 |
var reg any = &Registry{} |
| 38 |
if _, ok := reg.(registry.Registry); !ok { |
| 39 |
t.Error("&Registry{} does not conform registry.Registry") |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestRegistry_TLS(t *testing.T) { |
| 44 |
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 |
if r.Method != http.MethodGet || r.URL.Path != "/v2/" { |
| 46 |
t.Errorf("unexpected access: %s %s", r.Method, r.URL) |
| 47 |
w.WriteHeader(http.StatusNotFound) |
| 48 |
return |
| 49 |
} |
| 50 |
})) |
| 51 |
defer ts.Close() |
| 52 |
uri, err := url.Parse(ts.URL) |
| 53 |
if err != nil { |
| 54 |
t.Fatalf("invalid test http server: %v", err) |
| 55 |
} |
| 56 |
|
| 57 |
reg, err := NewRegistry(uri.Host) |
| 58 |
if err != nil { |
| 59 |
t.Fatalf("NewRegistry() error = %v", err) |
| 60 |
} |
| 61 |
reg.Client = ts.Client() |
| 62 |
|
| 63 |
ctx := context.Background() |
| 64 |
if err := reg.Ping(ctx); err != nil { |
| 65 |
t.Errorf("Registry.Ping() error = %v", err) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestRegistry_Ping(t *testing.T) { |
| 70 |
t.Run("Ping success", func(t *testing.T) { |
| 71 |
v2Implemented := true |
| 72 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 73 |
if r.Method != http.MethodGet || r.URL.Path != "/v2/" { |
| 74 |
t.Errorf("unexpected access: %s %s", r.Method, r.URL) |
| 75 |
w.WriteHeader(http.StatusNotFound) |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
if v2Implemented { |
| 80 |
w.WriteHeader(http.StatusOK) |
| 81 |
} else { |
| 82 |
w.WriteHeader(http.StatusNotFound) |
| 83 |
} |
| 84 |
})) |
| 85 |
defer ts.Close() |
| 86 |
uri, err := url.Parse(ts.URL) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("invalid test http server: %v", err) |
| 89 |
} |
| 90 |
|
| 91 |
reg, err := NewRegistry(uri.Host) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("NewRegistry() error = %v", err) |
| 94 |
} |
| 95 |
reg.PlainHTTP = true |
| 96 |
|
| 97 |
ctx := context.Background() |
| 98 |
if err := reg.Ping(ctx); err != nil { |
| 99 |
t.Errorf("Registry.Ping() error = %v", err) |
| 100 |
} |
| 101 |
|
| 102 |
v2Implemented = false |
| 103 |
if err := reg.Ping(ctx); err == nil { |
| 104 |
t.Errorf("Registry.Ping() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 105 |
} |
| 106 |
}) |
| 107 |
|
| 108 |
t.Run("Ping failed for server error", func(t *testing.T) { |
| 109 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 110 |
if r.Method != http.MethodGet || r.URL.Path != "/v2/" { |
| 111 |
t.Errorf("unexpected access: %s %s", r.Method, r.URL) |
| 112 |
w.WriteHeader(http.StatusNotFound) |
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
w.WriteHeader(http.StatusInternalServerError) |
| 117 |
})) |
| 118 |
defer ts.Close() |
| 119 |
uri, err := url.Parse(ts.URL) |
| 120 |
if err != nil { |
| 121 |
t.Fatalf("invalid test http server: %v", err) |
| 122 |
} |
| 123 |
|
| 124 |
reg, err := NewRegistry(uri.Host) |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("NewRegistry() error = %v", err) |
| 127 |
} |
| 128 |
reg.PlainHTTP = true |
| 129 |
|
| 130 |
ctx := context.Background() |
| 131 |
if err := reg.Ping(ctx); err == nil { |
| 132 |
t.Error("Registry.Ping() error = nil, wantErr = true") |
| 133 |
} |
| 134 |
}) |
| 135 |
|
| 136 |
t.Run("Ping failed for connection error", func(t *testing.T) { |
| 137 |
reg, err := NewRegistry("localhost:9876") |
| 138 |
if err != nil { |
| 139 |
t.Fatalf("NewRegistry() error = %v", err) |
| 140 |
} |
| 141 |
|
| 142 |
ctx := context.Background() |
| 143 |
if err := reg.Ping(ctx); err == nil { |
| 144 |
t.Error("Registry.Ping() error = nil, wantErr = true") |
| 145 |
} |
| 146 |
}) |
| 147 |
} |
| 148 |
|
| 149 |
func TestRegistry_Repositories(t *testing.T) { |
| 150 |
repoSet := [][]string{ |
| 151 |
{"the", "quick", "brown", "fox"}, |
| 152 |
{"jumps", "over", "the", "lazy"}, |
| 153 |
{"dog"}, |
| 154 |
} |
| 155 |
var ts *httptest.Server |
| 156 |
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 157 |
if r.Method != http.MethodGet || r.URL.Path != "/v2/_catalog" { |
| 158 |
t.Errorf("unexpected access: %s %s", r.Method, r.URL) |
| 159 |
w.WriteHeader(http.StatusNotFound) |
| 160 |
return |
| 161 |
} |
| 162 |
q := r.URL.Query() |
| 163 |
n, err := strconv.Atoi(q.Get("n")) |
| 164 |
if err != nil || n != 4 { |
| 165 |
t.Errorf("bad page size: %s", q.Get("n")) |
| 166 |
w.WriteHeader(http.StatusBadRequest) |
| 167 |
return |
| 168 |
} |
| 169 |
var repos []string |
| 170 |
switch q.Get("test") { |
| 171 |
case "foo": |
| 172 |
repos = repoSet[1] |
| 173 |
w.Header().Set("Link", fmt.Sprintf(`<%s/v2/_catalog?n=4&test=bar>; rel="next"`, ts.URL)) |
| 174 |
case "bar": |
| 175 |
repos = repoSet[2] |
| 176 |
default: |
| 177 |
repos = repoSet[0] |
| 178 |
w.Header().Set("Link", `</v2/_catalog?n=4&test=foo>; rel="next"`) |
| 179 |
} |
| 180 |
result := struct { |
| 181 |
Repositories []string `json:"repositories"` |
| 182 |
}{ |
| 183 |
Repositories: repos, |
| 184 |
} |
| 185 |
if err := json.NewEncoder(w).Encode(result); err != nil { |
| 186 |
t.Errorf("failed to write response: %v", err) |
| 187 |
} |
| 188 |
})) |
| 189 |
defer ts.Close() |
| 190 |
uri, err := url.Parse(ts.URL) |
| 191 |
if err != nil { |
| 192 |
t.Fatalf("invalid test http server: %v", err) |
| 193 |
} |
| 194 |
|
| 195 |
reg, err := NewRegistry(uri.Host) |
| 196 |
if err != nil { |
| 197 |
t.Fatalf("NewRegistry() error = %v", err) |
| 198 |
} |
| 199 |
reg.PlainHTTP = true |
| 200 |
reg.RepositoryListPageSize = 4 |
| 201 |
|
| 202 |
ctx := context.Background() |
| 203 |
index := 0 |
| 204 |
if err := reg.Repositories(ctx, "", func(got []string) error { |
| 205 |
if index > 2 { |
| 206 |
t.Fatalf("out of index bound: %d", index) |
| 207 |
} |
| 208 |
repos := repoSet[index] |
| 209 |
index++ |
| 210 |
if !reflect.DeepEqual(got, repos) { |
| 211 |
t.Errorf("Registry.Repositories() = %v, want %v", got, repos) |
| 212 |
} |
| 213 |
return nil |
| 214 |
}); err != nil { |
| 215 |
t.Fatalf("Registry.Repositories() error = %v", err) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func TestRegistry_Repository(t *testing.T) { |
| 220 |
reg, err := NewRegistry("localhost:5000") |
| 221 |
if err != nil { |
| 222 |
t.Fatalf("NewRegistry() error = %v", err) |
| 223 |
} |
| 224 |
reg.PlainHTTP = true |
| 225 |
reg.SkipReferrersGC = true |
| 226 |
reg.RepositoryListPageSize = 50 |
| 227 |
reg.TagListPageSize = 100 |
| 228 |
reg.ReferrerListPageSize = 10 |
| 229 |
reg.MaxMetadataBytes = 8 * 1024 * 1024 |
| 230 |
|
| 231 |
ctx := context.Background() |
| 232 |
got, err := reg.Repository(ctx, "hello-world") |
| 233 |
if err != nil { |
| 234 |
t.Fatalf("Registry.Repository() error = %v", err) |
| 235 |
} |
| 236 |
reg.Reference.Repository = "hello-world" |
| 237 |
want := (*Repository)(®.RepositoryOptions) |
| 238 |
if !reflect.DeepEqual(got, want) { |
| 239 |
t.Errorf("Registry.Repository() = %v, want %v", got, want) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// Testing `last` parameter for Repositories list |
| 244 |
func TestRegistry_Repositories_WithLastParam(t *testing.T) { |
| 245 |
repoSet := strings.Split("abcdefghijklmnopqrstuvwxyz", "") |
| 246 |
var offset int |
| 247 |
var ts *httptest.Server |
| 248 |
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 249 |
if r.Method != http.MethodGet || r.URL.Path != "/v2/_catalog" { |
| 250 |
t.Errorf("unexpected access: %s %s", r.Method, r.URL) |
| 251 |
w.WriteHeader(http.StatusNotFound) |
| 252 |
return |
| 253 |
} |
| 254 |
q := r.URL.Query() |
| 255 |
n, err := strconv.Atoi(q.Get("n")) |
| 256 |
if err != nil || n != 4 { |
| 257 |
t.Errorf("bad page size: %s", q.Get("n")) |
| 258 |
w.WriteHeader(http.StatusBadRequest) |
| 259 |
return |
| 260 |
} |
| 261 |
last := q.Get("last") |
| 262 |
if last != "" { |
| 263 |
offset = indexOf(last, repoSet) + 1 |
| 264 |
} |
| 265 |
var repos []string |
| 266 |
switch q.Get("test") { |
| 267 |
case "foo": |
| 268 |
repos = repoSet[offset : offset+n] |
| 269 |
w.Header().Set("Link", fmt.Sprintf(`<%s/v2/_catalog?n=4&last=v&test=bar>; rel="next"`, ts.URL)) |
| 270 |
case "bar": |
| 271 |
repos = repoSet[offset : offset+n] |
| 272 |
default: |
| 273 |
repos = repoSet[offset : offset+n] |
| 274 |
w.Header().Set("Link", fmt.Sprintf(`<%s/v2/_catalog?n=4&last=r&test=foo>; rel="next"`, ts.URL)) |
| 275 |
} |
| 276 |
result := struct { |
| 277 |
Repositories []string `json:"repositories"` |
| 278 |
}{ |
| 279 |
Repositories: repos, |
| 280 |
} |
| 281 |
if err := json.NewEncoder(w).Encode(result); err != nil { |
| 282 |
t.Errorf("failed to write response: %v", err) |
| 283 |
} |
| 284 |
})) |
| 285 |
defer ts.Close() |
| 286 |
uri, err := url.Parse(ts.URL) |
| 287 |
if err != nil { |
| 288 |
t.Fatalf("invalid test http server: %v", err) |
| 289 |
} |
| 290 |
|
| 291 |
reg, err := NewRegistry(uri.Host) |
| 292 |
if err != nil { |
| 293 |
t.Fatalf("NewRegistry() error = %v", err) |
| 294 |
} |
| 295 |
reg.PlainHTTP = true |
| 296 |
reg.RepositoryListPageSize = 4 |
| 297 |
last := "n" |
| 298 |
startInd := indexOf(last, repoSet) + 1 |
| 299 |
|
| 300 |
ctx := context.Background() |
| 301 |
if err := reg.Repositories(ctx, last, func(got []string) error { |
| 302 |
want := repoSet[startInd : startInd+reg.RepositoryListPageSize] |
| 303 |
startInd += reg.RepositoryListPageSize |
| 304 |
if !reflect.DeepEqual(got, want) { |
| 305 |
t.Errorf("Registry.Repositories() = %v, want %v", got, want) |
| 306 |
} |
| 307 |
return nil |
| 308 |
}); err != nil { |
| 309 |
t.Fatalf("Registry.Repositories() error = %v", err) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
func TestRegistry_do(t *testing.T) { |
| 314 |
data := []byte(`hello world!`) |
| 315 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 316 |
if r.Method != http.MethodGet || r.URL.Path != "/test" { |
| 317 |
t.Errorf("unexpected access: %s %s", r.Method, r.URL) |
| 318 |
w.WriteHeader(http.StatusNotFound) |
| 319 |
return |
| 320 |
} |
| 321 |
w.Header().Add("Warning", `299 - "Test 1: Good warning."`) |
| 322 |
w.Header().Add("Warning", `199 - "Test 2: Warning with a non-299 code."`) |
| 323 |
w.Header().Add("Warning", `299 - "Test 3: Good warning."`) |
| 324 |
w.Header().Add("Warning", `299 myregistry.example.com "Test 4: Warning with a non-unknown agent"`) |
| 325 |
w.Header().Add("Warning", `299 - "Test 5: Warning with a date." "Sat, 25 Aug 2012 23:34:45 GMT"`) |
| 326 |
w.Header().Add("wArnIng", `299 - "Test 6: Good warning."`) |
| 327 |
w.Write(data) |
| 328 |
})) |
| 329 |
defer ts.Close() |
| 330 |
uri, err := url.Parse(ts.URL) |
| 331 |
if err != nil { |
| 332 |
t.Fatalf("invalid test http server: %v", err) |
| 333 |
} |
| 334 |
testURL := ts.URL + "/test" |
| 335 |
|
| 336 |
// test do() without HandleWarning |
| 337 |
reg, err := NewRegistry(uri.Host) |
| 338 |
if err != nil { |
| 339 |
t.Fatal("NewRegistry() error =", err) |
| 340 |
} |
| 341 |
req, err := http.NewRequest(http.MethodGet, testURL, nil) |
| 342 |
if err != nil { |
| 343 |
t.Fatal("failed to create test request:", err) |
| 344 |
} |
| 345 |
resp, err := reg.do(req) |
| 346 |
if err != nil { |
| 347 |
t.Fatal("Registry.do() error =", err) |
| 348 |
} |
| 349 |
if resp.StatusCode != http.StatusOK { |
| 350 |
t.Errorf("Registry.do() status code = %v, want %v", resp.StatusCode, http.StatusOK) |
| 351 |
} |
| 352 |
if got := len(resp.Header["Warning"]); got != 6 { |
| 353 |
t.Errorf("Registry.do() warning header len = %v, want %v", got, 6) |
| 354 |
} |
| 355 |
got, err := io.ReadAll(resp.Body) |
| 356 |
if err != nil { |
| 357 |
t.Fatal("io.ReadAll() error =", err) |
| 358 |
} |
| 359 |
resp.Body.Close() |
| 360 |
if !bytes.Equal(got, data) { |
| 361 |
t.Errorf("Registry.do() = %v, want %v", got, data) |
| 362 |
} |
| 363 |
|
| 364 |
// test do() with HandleWarning |
| 365 |
reg, err = NewRegistry(uri.Host) |
| 366 |
if err != nil { |
| 367 |
t.Fatal("NewRegistry() error =", err) |
| 368 |
} |
| 369 |
var gotWarnings []Warning |
| 370 |
reg.HandleWarning = func(warning Warning) { |
| 371 |
gotWarnings = append(gotWarnings, warning) |
| 372 |
} |
| 373 |
|
| 374 |
req, err = http.NewRequest(http.MethodGet, testURL, nil) |
| 375 |
if err != nil { |
| 376 |
t.Fatal("failed to create test request:", err) |
| 377 |
} |
| 378 |
resp, err = reg.do(req) |
| 379 |
if err != nil { |
| 380 |
t.Fatal("Registry.do() error =", err) |
| 381 |
} |
| 382 |
if resp.StatusCode != http.StatusOK { |
| 383 |
t.Errorf("Registry.do() status code = %v, want %v", resp.StatusCode, http.StatusOK) |
| 384 |
} |
| 385 |
if got := len(resp.Header["Warning"]); got != 6 { |
| 386 |
t.Errorf("Registry.do() warning header len = %v, want %v", got, 6) |
| 387 |
} |
| 388 |
got, err = io.ReadAll(resp.Body) |
| 389 |
if err != nil { |
| 390 |
t.Errorf("Registry.do() = %v, want %v", got, data) |
| 391 |
} |
| 392 |
resp.Body.Close() |
| 393 |
if !bytes.Equal(got, data) { |
| 394 |
t.Errorf("Registry.do() = %v, want %v", got, data) |
| 395 |
} |
| 396 |
|
| 397 |
wantWarnings := []Warning{ |
| 398 |
{ |
| 399 |
WarningValue: WarningValue{ |
| 400 |
Code: 299, |
| 401 |
Agent: "-", |
| 402 |
Text: "Test 1: Good warning.", |
| 403 |
}, |
| 404 |
}, |
| 405 |
{ |
| 406 |
WarningValue: WarningValue{ |
| 407 |
Code: 299, |
| 408 |
Agent: "-", |
| 409 |
Text: "Test 3: Good warning.", |
| 410 |
}, |
| 411 |
}, |
| 412 |
{ |
| 413 |
WarningValue: WarningValue{ |
| 414 |
Code: 299, |
| 415 |
Agent: "-", |
| 416 |
Text: "Test 6: Good warning.", |
| 417 |
}, |
| 418 |
}, |
| 419 |
} |
| 420 |
if !reflect.DeepEqual(gotWarnings, wantWarnings) { |
| 421 |
t.Errorf("Registry.do() = %v, want %v", gotWarnings, wantWarnings) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
func TestNewRegistry(t *testing.T) { |
| 426 |
tests := []struct { |
| 427 |
name string |
| 428 |
regName string |
| 429 |
wantErr bool |
| 430 |
}{ |
| 431 |
{ |
| 432 |
name: "Valid registry name", |
| 433 |
regName: "localhost:5000", |
| 434 |
wantErr: false, |
| 435 |
}, |
| 436 |
{ |
| 437 |
name: "Invalid registry name", |
| 438 |
regName: "invalid registry name", |
| 439 |
wantErr: true, |
| 440 |
}, |
| 441 |
} |
| 442 |
|
| 443 |
for _, tt := range tests { |
| 444 |
t.Run(tt.name, func(t *testing.T) { |
| 445 |
got, err := NewRegistry(tt.regName) |
| 446 |
if (err != nil) != tt.wantErr { |
| 447 |
t.Errorf("NewRegistry() error = %v, wantErr %v", err, tt.wantErr) |
| 448 |
return |
| 449 |
} |
| 450 |
if !tt.wantErr && got == nil { |
| 451 |
t.Errorf("NewRegistry() = %v, want non-nil", got) |
| 452 |
} |
| 453 |
}) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
// indexOf returns the index of an element within a slice |
| 458 |
func indexOf(element string, data []string) int { |
| 459 |
for ind, val := range data { |
| 460 |
if element == val { |
| 461 |
return ind |
| 462 |
} |
| 463 |
} |
| 464 |
return -1 //not found. |
| 465 |
} |