Debian Codegraph @

registry/reference_test.go

raw
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 registry
17
18 import (
19 _ "crypto/sha256"
20 "fmt"
21 "reflect"
22 "testing"
23 )
24
25 const ValidDigest = "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
26 const InvalidDigest = "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde"
27
28 // For a definition of what a "valid form [ABCD]" means, see reference.go.
29 func TestParseReferenceGoodies(t *testing.T) {
30 tests := []struct {
31 name string
32 image string
33 wantTemplate Reference
34 }{
35 {
36 name: "digest reference (valid form A)",
37 image: fmt.Sprintf("hello-world@%s", ValidDigest),
38 wantTemplate: Reference{
39 Repository: "hello-world",
40 Reference: ValidDigest,
41 },
42 },
43 {
44 name: "tag with digest (valid form B)",
45 image: fmt.Sprintf("hello-world:v2@%s", ValidDigest),
46 wantTemplate: Reference{
47 Repository: "hello-world",
48 Reference: ValidDigest,
49 },
50 },
51 {
52 name: "empty tag with digest (valid form B)",
53 image: fmt.Sprintf("hello-world:@%s", ValidDigest),
54 wantTemplate: Reference{
55 Repository: "hello-world",
56 Reference: ValidDigest,
57 },
58 },
59 {
60 name: "tag reference (valid form C)",
61 image: "hello-world:v1",
62 wantTemplate: Reference{
63 Repository: "hello-world",
64 Reference: "v1",
65 },
66 },
67 {
68 name: "basic reference (valid form D)",
69 image: "hello-world",
70 wantTemplate: Reference{
71 Repository: "hello-world",
72 },
73 },
74 }
75
76 registries := []string{
77 "localhost",
78 "registry.example.com",
79 "localhost:5000",
80 "127.0.0.1:5000",
81 "[::1]:5000",
82 }
83
84 for _, tt := range tests {
85 want := tt.wantTemplate
86 for _, registry := range registries {
87 want.Registry = registry
88 t.Run(tt.name, func(t *testing.T) {
89 got, err := ParseReference(fmt.Sprintf("%s/%s", registry, tt.image))
90 if err != nil {
91 t.Errorf("ParseReference() encountered unexpected error: %v", err)
92 return
93 }
94 if !reflect.DeepEqual(got, want) {
95 t.Errorf("ParseReference() = %v, want %v", got, tt.wantTemplate)
96 }
97 })
98 }
99 }
100 }
101
102 func TestParseReferenceUglies(t *testing.T) {
103 tests := []struct {
104 name string
105 raw string
106 want Reference
107 }{
108 {
109 name: "no repo name",
110 raw: "localhost",
111 },
112 {
113 name: "missing registry",
114 raw: "hello-world:linux",
115 },
116 {
117 name: "missing registry (issue #698)",
118 raw: "/hello-world:linux",
119 },
120 {
121 name: "invalid repo name",
122 raw: "localhost/UPPERCASE/test",
123 },
124 {
125 name: "invalid port",
126 raw: "localhost:v1/hello-world",
127 },
128 {
129 name: "invalid digest",
130 raw: fmt.Sprintf("registry.example.com/foobar@%s", InvalidDigest),
131 },
132 {
133 name: "invalid digest prefix: colon instead of the at sign",
134 raw: fmt.Sprintf("registry.example.com/hello-world:foobar:%s", ValidDigest),
135 },
136 {
137 name: "invalid digest prefix: double at sign",
138 raw: fmt.Sprintf("registry.example.com/hello-world@@%s", ValidDigest),
139 },
140 {
141 name: "invalid digest prefix: space",
142 raw: fmt.Sprintf("registry.example.com/hello-world @%s", ValidDigest),
143 },
144 }
145
146 for _, tt := range tests {
147 t.Run(tt.name, func(t *testing.T) {
148 if _, err := ParseReference(tt.raw); err == nil {
149 t.Errorf("ParseReference() expected an error, but got none")
150 return
151 }
152 })
153 }
154 }
155
156 func TestReference_Validate(t *testing.T) {
157 tests := []struct {
158 name string
159 reference Reference
160 wantErr bool
161 }{
162 {
163 name: "valid reference with tag",
164 reference: Reference{
165 Registry: "registry.example.com",
166 Repository: "hello-world",
167 Reference: "v1.0.0",
168 },
169 wantErr: false,
170 },
171 {
172 name: "valid reference with digest",
173 reference: Reference{
174 Registry: "registry.example.com",
175 Repository: "hello-world",
176 Reference: ValidDigest,
177 },
178 wantErr: false,
179 },
180 {
181 name: "valid reference without tag or digest",
182 reference: Reference{
183 Registry: "registry.example.com",
184 Repository: "hello-world",
185 },
186 wantErr: false,
187 },
188 {
189 name: "invalid registry",
190 reference: Reference{
191 Registry: "invalid registry",
192 Repository: "hello-world",
193 Reference: "v1.0.0",
194 },
195 wantErr: true,
196 },
197 {
198 name: "invalid repository",
199 reference: Reference{
200 Registry: "registry.example.com",
201 Repository: "INVALID_REPO",
202 Reference: "v1.0.0",
203 },
204 wantErr: true,
205 },
206 {
207 name: "invalid tag",
208 reference: Reference{
209 Registry: "registry.example.com",
210 Repository: "hello-world",
211 Reference: "INVALID_TAG!",
212 },
213 wantErr: true,
214 },
215 {
216 name: "invalid digest",
217 reference: Reference{
218 Registry: "registry.example.com",
219 Repository: "hello-world",
220 Reference: InvalidDigest,
221 },
222 wantErr: true,
223 },
224 }
225
226 for _, tt := range tests {
227 t.Run(tt.name, func(t *testing.T) {
228 if err := tt.reference.Validate(); (err != nil) != tt.wantErr {
229 t.Errorf("Reference.Validate() error = %v, wantErr %v", err, tt.wantErr)
230 }
231 })
232 }
233 }
234
235 func TestReference_Host(t *testing.T) {
236 tests := []struct {
237 name string
238 registry string
239 want string
240 }{
241 {
242 name: "docker.io",
243 registry: "docker.io",
244 want: "registry-1.docker.io",
245 },
246 {
247 name: "other registry",
248 registry: "registry.example.com",
249 want: "registry.example.com",
250 },
251 }
252
253 for _, tt := range tests {
254 t.Run(tt.name, func(t *testing.T) {
255 ref := Reference{
256 Registry: tt.registry,
257 }
258 if got := ref.Host(); got != tt.want {
259 t.Errorf("Reference.Host() = %v, want %v", got, tt.want)
260 }
261 })
262 }
263 }
264 func TestReference_ReferenceOrDefault(t *testing.T) {
265 tests := []struct {
266 name string
267 reference Reference
268 want string
269 }{
270 {
271 name: "empty reference",
272 reference: Reference{
273 Reference: "",
274 },
275 want: "latest",
276 },
277 {
278 name: "non-empty reference",
279 reference: Reference{
280 Reference: "v1.0.0",
281 },
282 want: "v1.0.0",
283 },
284 }
285
286 for _, tt := range tests {
287 t.Run(tt.name, func(t *testing.T) {
288 if got := tt.reference.ReferenceOrDefault(); got != tt.want {
289 t.Errorf("Reference.ReferenceOrDefault() = %v, want %v", got, tt.want)
290 }
291 })
292 }
293 }
294
295 func TestReference_String(t *testing.T) {
296 tests := []struct {
297 name string
298 reference Reference
299 want string
300 }{
301 {
302 name: "only registry",
303 reference: Reference{
304 Registry: "registry.example.com",
305 },
306 want: "registry.example.com",
307 },
308 {
309 name: "registry and repository",
310 reference: Reference{
311 Registry: "registry.example.com",
312 Repository: "hello-world",
313 },
314 want: "registry.example.com/hello-world",
315 },
316 {
317 name: "registry, repository and tag",
318 reference: Reference{
319 Registry: "registry.example.com",
320 Repository: "hello-world",
321 Reference: "v1.0.0",
322 },
323 want: "registry.example.com/hello-world:v1.0.0",
324 },
325 {
326 name: "registry, repository and digest",
327 reference: Reference{
328 Registry: "registry.example.com",
329 Repository: "hello-world",
330 Reference: ValidDigest,
331 },
332 want: fmt.Sprintf("registry.example.com/hello-world@%s", ValidDigest),
333 },
334 {
335 name: "registry, repository and invalid digest",
336 reference: Reference{
337 Registry: "registry.example.com",
338 Repository: "hello-world",
339 Reference: InvalidDigest,
340 },
341 want: fmt.Sprintf("registry.example.com/hello-world:%s", InvalidDigest),
342 },
343 }
344
345 for _, tt := range tests {
346 t.Run(tt.name, func(t *testing.T) {
347 if got := tt.reference.String(); got != tt.want {
348 t.Errorf("Reference.String() = %v, want %v", got, tt.want)
349 }
350 })
351 }
352 }