/
docker_image_src_test.go
185 lines (164 loc) · 5.57 KB
/
docker_image_src_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
package docker
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"regexp"
"strings"
"testing"
"github.com/containers/image/v5/internal/private"
"github.com/containers/image/v5/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var _ private.ImageSource = (*dockerImageSource)(nil)
func TestDockerImageSourceReference(t *testing.T) {
manifestPathRegex := regexp.MustCompile("^/v2/.*/manifests/latest$")
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/v2/":
rw.WriteHeader(http.StatusOK)
case r.Method == http.MethodGet && manifestPathRegex.MatchString(r.URL.Path):
rw.WriteHeader(http.StatusOK)
// Empty body is good enough for this test
default:
require.FailNowf(t, "Unexpected request", "%v %v", r.Method, r.URL.Path)
}
}))
defer server.Close()
registryURL, err := url.Parse(server.URL)
require.NoError(t, err)
registry := registryURL.Host
mirrorConfiguration := strings.Replace(
`[[registry]]
prefix = "primary-override.example.com"
location = "@REGISTRY@/primary-override"
[[registry]]
location = "with-mirror.example.com"
[[registry.mirror]]
location = "@REGISTRY@/with-mirror"
`, "@REGISTRY@", registry, -1)
registriesConf, err := os.CreateTemp("", "docker-image-src")
require.NoError(t, err)
defer registriesConf.Close()
defer os.Remove(registriesConf.Name())
err = os.WriteFile(registriesConf.Name(), []byte(mirrorConfiguration), 0600)
require.NoError(t, err)
for _, c := range []struct{ input, physical string }{
{registry + "/no-redirection/busybox:latest", registry + "/no-redirection/busybox:latest"},
{"primary-override.example.com/busybox:latest", registry + "/primary-override/busybox:latest"},
{"with-mirror.example.com/busybox:latest", registry + "/with-mirror/busybox:latest"},
} {
ref, err := ParseReference("//" + c.input)
require.NoError(t, err, c.input)
src, err := ref.NewImageSource(context.Background(), &types.SystemContext{
RegistriesDirPath: "/this/does/not/exist",
DockerPerHostCertDirPath: "/this/does/not/exist",
SystemRegistriesConfPath: registriesConf.Name(),
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
})
require.NoError(t, err, c.input)
// The observable behavior
assert.Equal(t, "//"+c.input, src.Reference().StringWithinTransport(), c.input)
assert.Equal(t, ref.StringWithinTransport(), src.Reference().StringWithinTransport(), c.input)
// Also peek into internal state
src2, ok := src.(*dockerImageSource)
require.True(t, ok, c.input)
assert.Equal(t, "//"+c.input, src2.logicalRef.StringWithinTransport(), c.input)
assert.Equal(t, "//"+c.physical, src2.physicalRef.StringWithinTransport(), c.input)
}
}
func TestSimplifyContentType(t *testing.T) {
for _, c := range []struct{ input, expected string }{
{"", ""},
{"application/json", "application/json"},
{"application/json;charset=utf-8", "application/json"},
{"application/json; charset=utf-8", "application/json"},
{"application/json ; charset=utf-8", "application/json"},
{"application/json\t;\tcharset=utf-8", "application/json"},
{"application/json ;charset=utf-8", "application/json"},
{`application/json; charset="utf-8"`, "application/json"},
{"completely invalid", ""},
} {
out := simplifyContentType(c.input)
assert.Equal(t, c.expected, out, c.input)
}
}
func readNextStream(streams chan io.ReadCloser, errs chan error) ([]byte, error) {
select {
case r := <-streams:
if r == nil {
return nil, nil
}
defer r.Close()
return io.ReadAll(r)
case err := <-errs:
return nil, err
}
}
type verifyGetBlobAtData struct {
expectedData []byte
expectedError error
}
func verifyGetBlobAtOutput(t *testing.T, streams chan io.ReadCloser, errs chan error, expected []verifyGetBlobAtData) {
for _, c := range expected {
data, err := readNextStream(streams, errs)
assert.Equal(t, c.expectedData, data)
assert.Equal(t, c.expectedError, err)
}
}
func TestSplitHTTP200ResponseToPartial(t *testing.T) {
body := io.NopCloser(bytes.NewReader([]byte("123456789")))
defer body.Close()
streams := make(chan io.ReadCloser)
errs := make(chan error)
chunks := []private.ImageSourceChunk{
{Offset: 1, Length: 2},
{Offset: 4, Length: 1},
}
go splitHTTP200ResponseToPartial(streams, errs, body, chunks)
expected := []verifyGetBlobAtData{
{[]byte("23"), nil},
{[]byte("5"), nil},
{[]byte(nil), nil},
}
verifyGetBlobAtOutput(t, streams, errs, expected)
}
func TestHandle206Response(t *testing.T) {
body := io.NopCloser(bytes.NewReader([]byte("--AAA\r\n\r\n23\r\n--AAA\r\n\r\n5\r\n--AAA--")))
defer body.Close()
streams := make(chan io.ReadCloser)
errs := make(chan error)
chunks := []private.ImageSourceChunk{
{Offset: 1, Length: 2},
{Offset: 4, Length: 1},
}
mediaType := "multipart/form-data"
params := map[string]string{
"boundary": "AAA",
}
go handle206Response(streams, errs, body, chunks, mediaType, params)
expected := []verifyGetBlobAtData{
{[]byte("23"), nil},
{[]byte("5"), nil},
{[]byte(nil), nil},
}
verifyGetBlobAtOutput(t, streams, errs, expected)
body = io.NopCloser(bytes.NewReader([]byte("HELLO")))
defer body.Close()
streams = make(chan io.ReadCloser)
errs = make(chan error)
chunks = []private.ImageSourceChunk{{Offset: 100, Length: 5}}
mediaType = "text/plain"
params = map[string]string{}
go handle206Response(streams, errs, body, chunks, mediaType, params)
expected = []verifyGetBlobAtData{
{[]byte("HELLO"), nil},
{[]byte(nil), nil},
}
verifyGetBlobAtOutput(t, streams, errs, expected)
}