MCPcopy Create free account
hub / github.com/containers/image / TestDockerImageSourceReference

Function TestDockerImageSourceReference

docker/docker_image_src_test.go:23–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21var _ private.ImageSource = (*dockerImageSource)(nil)
22
23func 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]]
44prefix = "primary-override.example.com"
45location = "@REGISTRY@/primary-override"
46
47[[registry]]
48location = "with-mirror.example.com"
49
50[[registry.mirror]]
51location = "@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)

Callers

nothing calls this directly

Calls 7

CloseMethod · 0.95
ReferenceMethod · 0.95
RemoveMethod · 0.80
ParseReferenceFunction · 0.70
NameMethod · 0.65
NewImageSourceMethod · 0.65
StringWithinTransportMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…