|
1 | 1 | package distribution // import "github.com/docker/docker/distribution" |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "net/url" |
7 | 11 | "reflect" |
8 | 12 | "regexp" |
9 | 13 | "runtime" |
10 | 14 | "strings" |
| 15 | + "sync/atomic" |
11 | 16 | "testing" |
12 | 17 |
|
13 | 18 | "github.com/docker/distribution/manifest/schema1" |
14 | 19 | "github.com/docker/distribution/reference" |
| 20 | + "github.com/docker/docker/api/types" |
| 21 | + registrytypes "github.com/docker/docker/api/types/registry" |
| 22 | + "github.com/docker/docker/image" |
| 23 | + "github.com/docker/docker/registry" |
15 | 24 | digest "github.com/opencontainers/go-digest" |
16 | 25 | specs "github.com/opencontainers/image-spec/specs-go/v1" |
17 | 26 | "gotest.tools/v3/assert" |
@@ -205,3 +214,160 @@ func TestFormatPlatform(t *testing.T) { |
205 | 214 | } |
206 | 215 | } |
207 | 216 | } |
| 217 | + |
| 218 | +func TestPullSchema2Config(t *testing.T) { |
| 219 | + ctx := context.Background() |
| 220 | + |
| 221 | + const imageJSON = `{ |
| 222 | + "architecture": "amd64", |
| 223 | + "os": "linux", |
| 224 | + "config": {}, |
| 225 | + "rootfs": { |
| 226 | + "type": "layers", |
| 227 | + "diff_ids": [] |
| 228 | + } |
| 229 | +}` |
| 230 | + expectedDigest := digest.Digest("sha256:66ad98165d38f53ee73868f82bd4eed60556ddfee824810a4062c4f777b20a5b") |
| 231 | + |
| 232 | + tests := []struct { |
| 233 | + name string |
| 234 | + handler func(callCount int, w http.ResponseWriter) |
| 235 | + expectError string |
| 236 | + expectAttempts int64 |
| 237 | + }{ |
| 238 | + { |
| 239 | + name: "success first time", |
| 240 | + handler: func(callCount int, w http.ResponseWriter) { |
| 241 | + w.WriteHeader(http.StatusOK) |
| 242 | + _, _ = w.Write([]byte(imageJSON)) |
| 243 | + }, |
| 244 | + expectAttempts: 1, |
| 245 | + }, |
| 246 | + { |
| 247 | + name: "500 status", |
| 248 | + handler: func(callCount int, w http.ResponseWriter) { |
| 249 | + if callCount == 1 { |
| 250 | + w.WriteHeader(http.StatusInternalServerError) |
| 251 | + return |
| 252 | + } |
| 253 | + w.WriteHeader(http.StatusOK) |
| 254 | + _, _ = w.Write([]byte(imageJSON)) |
| 255 | + }, |
| 256 | + expectAttempts: 2, |
| 257 | + }, |
| 258 | + { |
| 259 | + name: "EOF", |
| 260 | + handler: func(callCount int, w http.ResponseWriter) { |
| 261 | + if callCount == 1 { |
| 262 | + panic("intentional panic") |
| 263 | + } |
| 264 | + w.WriteHeader(http.StatusOK) |
| 265 | + _, _ = w.Write([]byte(imageJSON)) |
| 266 | + }, |
| 267 | + expectAttempts: 2, |
| 268 | + }, |
| 269 | + { |
| 270 | + name: "unauthorized", |
| 271 | + handler: func(callCount int, w http.ResponseWriter) { |
| 272 | + w.WriteHeader(http.StatusUnauthorized) |
| 273 | + }, |
| 274 | + expectError: "unauthorized: authentication required", |
| 275 | + expectAttempts: 1, |
| 276 | + }, |
| 277 | + } |
| 278 | + |
| 279 | + for _, tt := range tests { |
| 280 | + tt := tt |
| 281 | + t.Run(tt.name, func(t *testing.T) { |
| 282 | + var callCount int64 |
| 283 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 284 | + t.Logf("HTTP %s %s", r.Method, r.URL.Path) |
| 285 | + defer r.Body.Close() |
| 286 | + switch { |
| 287 | + case r.Method == "GET" && r.URL.Path == "/v2": |
| 288 | + w.WriteHeader(http.StatusOK) |
| 289 | + case r.Method == "GET" && r.URL.Path == "/v2/docker.io/library/testremotename/blobs/"+expectedDigest.String(): |
| 290 | + tt.handler(int(atomic.AddInt64(&callCount, 1)), w) |
| 291 | + default: |
| 292 | + w.WriteHeader(http.StatusNotFound) |
| 293 | + } |
| 294 | + })) |
| 295 | + defer ts.Close() |
| 296 | + |
| 297 | + p := testNewPuller(t, ts.URL) |
| 298 | + |
| 299 | + config, err := p.pullSchema2Config(ctx, expectedDigest) |
| 300 | + if tt.expectError == "" { |
| 301 | + if err != nil { |
| 302 | + t.Fatal(err) |
| 303 | + } |
| 304 | + |
| 305 | + _, err = image.NewFromJSON(config) |
| 306 | + if err != nil { |
| 307 | + t.Fatal(err) |
| 308 | + } |
| 309 | + } else { |
| 310 | + if err == nil { |
| 311 | + t.Fatalf("expected error to contain %q", tt.expectError) |
| 312 | + } |
| 313 | + if !strings.Contains(err.Error(), tt.expectError) { |
| 314 | + t.Fatalf("expected error=%q to contain %q", err, tt.expectError) |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + if callCount != tt.expectAttempts { |
| 319 | + t.Fatalf("got callCount=%d but expected=%d", callCount, tt.expectAttempts) |
| 320 | + } |
| 321 | + }) |
| 322 | + } |
| 323 | +} |
| 324 | + |
| 325 | +func testNewPuller(t *testing.T, rawurl string) *v2Puller { |
| 326 | + t.Helper() |
| 327 | + |
| 328 | + uri, err := url.Parse(rawurl) |
| 329 | + if err != nil { |
| 330 | + t.Fatalf("could not parse url from test server: %v", err) |
| 331 | + } |
| 332 | + |
| 333 | + endpoint := registry.APIEndpoint{ |
| 334 | + Mirror: false, |
| 335 | + URL: uri, |
| 336 | + Version: 2, |
| 337 | + Official: false, |
| 338 | + TrimHostname: false, |
| 339 | + TLSConfig: nil, |
| 340 | + } |
| 341 | + n, _ := reference.ParseNormalizedNamed("testremotename") |
| 342 | + repoInfo := ®istry.RepositoryInfo{ |
| 343 | + Name: n, |
| 344 | + Index: ®istrytypes.IndexInfo{ |
| 345 | + Name: "testrepo", |
| 346 | + Mirrors: nil, |
| 347 | + Secure: false, |
| 348 | + Official: false, |
| 349 | + }, |
| 350 | + Official: false, |
| 351 | + } |
| 352 | + imagePullConfig := &ImagePullConfig{ |
| 353 | + Config: Config{ |
| 354 | + MetaHeaders: http.Header{}, |
| 355 | + AuthConfig: &types.AuthConfig{ |
| 356 | + RegistryToken: secretRegistryToken, |
| 357 | + }, |
| 358 | + }, |
| 359 | + Schema2Types: ImageTypes, |
| 360 | + } |
| 361 | + |
| 362 | + puller, err := newPuller(endpoint, repoInfo, imagePullConfig, nil) |
| 363 | + if err != nil { |
| 364 | + t.Fatal(err) |
| 365 | + } |
| 366 | + p := puller.(*v2Puller) |
| 367 | + |
| 368 | + p.repo, _, err = NewV2Repository(context.Background(), p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull") |
| 369 | + if err != nil { |
| 370 | + t.Fatal(err) |
| 371 | + } |
| 372 | + return p |
| 373 | +} |
0 commit comments