(t *testing.T)
| 21 | var _ private.ImageSource = (*dockerImageSource)(nil) |
| 22 | |
| 23 | func TestDockerImageSourceReference(t *testing.T) { |
| 24 | manifestPathRegex := regexp.MustCompile("^/v2/.*/manifests/latest$") |
| 25 | |
| 26 | server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 27 | switch { |
| 28 | case r.Method == http.MethodGet && r.URL.Path == "/v2/": |
| 29 | rw.WriteHeader(http.StatusOK) |
| 30 | case r.Method == http.MethodGet && manifestPathRegex.MatchString(r.URL.Path): |
| 31 | rw.WriteHeader(http.StatusOK) |
| 32 | // Empty body is good enough for this test |
| 33 | default: |
| 34 | require.FailNowf(t, "Unexpected request", "%v %v", r.Method, r.URL.Path) |
| 35 | } |
| 36 | })) |
| 37 | defer server.Close() |
| 38 | registryURL, err := url.Parse(server.URL) |
| 39 | require.NoError(t, err) |
| 40 | registry := registryURL.Host |
| 41 | |
| 42 | mirrorConfiguration := strings.ReplaceAll( |
| 43 | `[[registry]] |
| 44 | prefix = "primary-override.example.com" |
| 45 | location = "@REGISTRY@/primary-override" |
| 46 | |
| 47 | [[registry]] |
| 48 | location = "with-mirror.example.com" |
| 49 | |
| 50 | [[registry.mirror]] |
| 51 | location = "@REGISTRY@/with-mirror" |
| 52 | `, "@REGISTRY@", registry) |
| 53 | registriesConf, err := os.CreateTemp("", "docker-image-src") |
| 54 | require.NoError(t, err) |
| 55 | defer registriesConf.Close() |
| 56 | defer os.Remove(registriesConf.Name()) |
| 57 | err = os.WriteFile(registriesConf.Name(), []byte(mirrorConfiguration), 0600) |
| 58 | require.NoError(t, err) |
| 59 | |
| 60 | for _, c := range []struct{ input, physical string }{ |
| 61 | {registry + "/no-redirection/busybox:latest", registry + "/no-redirection/busybox:latest"}, |
| 62 | {"primary-override.example.com/busybox:latest", registry + "/primary-override/busybox:latest"}, |
| 63 | {"with-mirror.example.com/busybox:latest", registry + "/with-mirror/busybox:latest"}, |
| 64 | } { |
| 65 | ref, err := ParseReference("//" + c.input) |
| 66 | require.NoError(t, err, c.input) |
| 67 | src, err := ref.NewImageSource(context.Background(), &types.SystemContext{ |
| 68 | RegistriesDirPath: "/this/does/not/exist", |
| 69 | DockerPerHostCertDirPath: "/this/does/not/exist", |
| 70 | SystemRegistriesConfPath: registriesConf.Name(), |
| 71 | DockerInsecureSkipTLSVerify: types.OptionalBoolTrue, |
| 72 | }) |
| 73 | require.NoError(t, err, c.input) |
| 74 | defer src.Close() |
| 75 | |
| 76 | // The observable behavior |
| 77 | assert.Equal(t, "//"+c.input, src.Reference().StringWithinTransport(), c.input) |
| 78 | assert.Equal(t, ref.StringWithinTransport(), src.Reference().StringWithinTransport(), c.input) |
| 79 | // Also peek into internal state |
| 80 | src2, ok := src.(*dockerImageSource) |
nothing calls this directly
no test coverage detected
searching dependent graphs…