Debian Codegraph @

ociregistry/internal/conformance/conformance_test.go

raw symbols SCIP
1 // Copyright 2023 CUE Labs AG
2 //
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 package conformance
16
17 import (
18 "bufio"
19 "bytes"
20 "context"
21 "encoding/json"
22 "fmt"
23 "io"
24 "log"
25 "net/http"
26 "net/http/httptest"
27 "net/url"
28 "os"
29 "os/exec"
30 "slices"
31 "sort"
32 "strings"
33 "testing"
34
35 "github.com/go-quicktest/qt"
36
37 digest "github.com/opencontainers/go-digest"
38 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
39 "oras.land/oras-go/v2/content"
40 "oras.land/oras-go/v2/registry/remote"
41
42 "cuelabs.dev/go/oci/ociregistry"
43 "cuelabs.dev/go/oci/ociregistry/ociclient"
44 "cuelabs.dev/go/oci/ociregistry/ocidebug"
45 "cuelabs.dev/go/oci/ociregistry/ocifilter"
46 "cuelabs.dev/go/oci/ociregistry/ocimem"
47 "cuelabs.dev/go/oci/ociregistry/ociserver"
48 "cuelabs.dev/go/oci/ociregistry/ociunify"
49 )
50
51 func init() {
52 log.SetFlags(log.Lmicroseconds)
53 }
54
55 func TestMem(t *testing.T) {
56 runTests(t, func(t *testing.T) string {
57 srv := httptest.NewServer(ociserver.New(ocidebug.New(ocimem.New(), t.Logf), nil))
58 t.Cleanup(srv.Close)
59 return srv.URL
60 })
61 }
62
63 func TestClientAsProxy(t *testing.T) {
64 runTests(t, func(t *testing.T) string {
65 direct := httptest.NewServer(ociserver.New(ocimem.New(), &ociserver.Options{
66 DebugID: "direct",
67 }))
68 t.Cleanup(direct.Close)
69 proxy := httptest.NewServer(ociserver.New(mustNewOCIClient(direct.URL, &ociclient.Options{
70 // Make sure that we exercise the paging functionality.
71 ListPageSize: 2,
72 DebugID: "proxyClient",
73 }), &ociserver.Options{
74 DebugID: "proxy",
75 }))
76 t.Cleanup(proxy.Close)
77 return proxy.URL
78 })
79 }
80
81 func TestClientAsProxyWithNoLinkHeaders(t *testing.T) {
82 runTests(t, func(t *testing.T) string {
83 direct := httptest.NewServer(ociserver.New(ocimem.New(), &ociserver.Options{
84 DebugID: "direct",
85 OmitLinkHeaderFromResponses: true,
86 }))
87 t.Cleanup(direct.Close)
88 proxy := httptest.NewServer(ociserver.New(mustNewOCIClient(direct.URL, &ociclient.Options{
89 // Make sure that we exercise the paging functionality.
90 ListPageSize: 2,
91 DebugID: "proxyClient",
92 }), &ociserver.Options{
93 DebugID: "proxy",
94 }))
95 t.Cleanup(proxy.Close)
96 return proxy.URL
97 })
98 }
99
100 func TestClientAsProxyWithMaxPageSize(t *testing.T) {
101 runTests(t, func(t *testing.T) string {
102 direct := httptest.NewServer(ociserver.New(ocimem.New(), &ociserver.Options{
103 DebugID: "direct",
104 MaxListPageSize: 1000,
105 }))
106 t.Cleanup(direct.Close)
107 proxy := httptest.NewServer(ociserver.New(mustNewOCIClient(direct.URL, nil), &ociserver.Options{
108 DebugID: "proxy",
109 MaxListPageSize: 1000,
110 }))
111 t.Cleanup(proxy.Close)
112 return proxy.URL
113 })
114 }
115
116 func TestClientAsProxyWithOmittedTagGetDigest(t *testing.T) {
117 runTests(t, func(t *testing.T) string {
118 direct := httptest.NewServer(ociserver.New(ocimem.New(), &ociserver.Options{
119 DebugID: "direct",
120 OmitDigestFromTagGetResponse: true,
121 }))
122 t.Cleanup(direct.Close)
123 proxy := httptest.NewServer(ociserver.New(mustNewOCIClient(direct.URL, nil), &ociserver.Options{
124 DebugID: "proxy",
125 }))
126 t.Cleanup(proxy.Close)
127 return proxy.URL
128 })
129 }
130
131 func TestSelectAll(t *testing.T) {
132 // Test that the Select filter works OK as a no-op when
133 // all repositories are allowed.
134 runTests(t, func(t *testing.T) string {
135 r := ocifilter.Select(ocimem.New(), func(string) bool {
136 return true
137 })
138 srv := httptest.NewServer(ociserver.New(r, nil))
139 t.Cleanup(srv.Close)
140 return srv.URL
141 })
142 }
143
144 func TestUnifyingProxySequential(t *testing.T) {
145 testUnifyingProxy(t, &ociunify.Options{
146 ReadPolicy: ociunify.ReadSequential,
147 })
148 }
149
150 func TestUnifyingProxyConcurrent(t *testing.T) {
151 testUnifyingProxy(t, &ociunify.Options{
152 ReadPolicy: ociunify.ReadConcurrent,
153 })
154 }
155
156 func testUnifyingProxy(t *testing.T, opts *ociunify.Options) {
157 debugWrap := func(what string, r ociregistry.Interface) ociregistry.Interface {
158 return ocidebug.New(r, func(f string, a ...any) {
159 t.Logf("%s: %s", what, fmt.Sprintf(f, a...))
160 })
161 }
162 runTests(t, func(t *testing.T) string {
163 direct0 := httptest.NewServer(ociserver.New(debugWrap("direct0mem", ocimem.New()), &ociserver.Options{
164 DebugID: "direct0srv",
165 }))
166 t.Cleanup(direct0.Close)
167 direct1 := httptest.NewServer(ociserver.New(debugWrap("direct1mem", ocimem.New()), &ociserver.Options{
168 DebugID: "direct1srv",
169 }))
170 t.Cleanup(direct1.Close)
171 proxy := httptest.NewServer(ociserver.New(debugWrap("proxy", ociunify.New(
172 debugWrap("proxy0", mustNewOCIClient(direct0.URL, &ociclient.Options{
173 DebugID: "client0",
174 })),
175 debugWrap("proxy1", mustNewOCIClient(direct1.URL, &ociclient.Options{
176 DebugID: "client1",
177 })),
178 opts,
179 )), &ociserver.Options{
180 DebugID: "proxysrv",
181 }))
182 t.Cleanup(proxy.Close)
183 return proxy.URL
184 })
185 }
186
187 func runTests(t *testing.T, startSrv func(t *testing.T) string) {
188 t.Run("distribution", func(t *testing.T) {
189 testDistribution(t, startSrv)
190 })
191 t.Run("extra", func(t *testing.T) {
192 testExtra(t, startSrv)
193 })
194 t.Run("extraWithLocalClient", func(t *testing.T) {
195 testExtraWithLocalClient(t, startSrv)
196 })
197 }
198
199 var extraTests = []struct {
200 testName string
201 run func(t *testing.T, client *remote.Registry)
202 }{{
203 testName: "catalog",
204 run: testCatalog,
205 }, {
206 testName: "referrers",
207 run: testReferrers,
208 }, {
209 testName: "largeManifest",
210 run: testLargeManifest,
211 }, {
212 testName: "manyTags",
213 run: testManyTags,
214 }, {
215 testName: "manyRepos",
216 run: testManyRepos,
217 }}
218
219 // testExtra runs a bunch of extra tests of functionality that isn't
220 // covered by the distribution's conformance tests.
221 //
222 // We use the oras-go client to keep us honest.
223 func testExtra(t *testing.T, startSrv func(*testing.T) string) {
224 for _, test := range extraTests {
225 t.Run(test.testName, func(t *testing.T) {
226 srvURL := startSrv(t)
227 u, err := url.Parse(srvURL)
228 qt.Assert(t, qt.IsNil(err))
229 client, err := remote.NewRegistry(u.Host)
230 qt.Assert(t, qt.IsNil(err))
231 client.PlainHTTP = true
232 test.run(t, client)
233 })
234 }
235 }
236
237 func testManyTags(t *testing.T, client *remote.Registry) {
238 ctx := context.Background()
239 client.TagListPageSize = 5
240 repo, err := client.Repository(ctx, "some-repo")
241 qt.Assert(t, qt.IsNil(err))
242 configDesc := push(t, repo.Blobs(), "application/json", []byte("{}"))
243 layer0Desc := push(t, repo.Blobs(), "", []byte("some content"))
244 manifestDesc := pushJSON(t, repo.Manifests(), ocispec.MediaTypeImageManifest, ociregistry.Manifest{
245 MediaType: ocispec.MediaTypeImageManifest,
246 Config: withMediaType(configDesc, "artifact1"),
247 Layers: []ociregistry.Descriptor{layer0Desc},
248 })
249 var tags []string
250 for i := 0; i < 37; i++ {
251 tag := fmt.Sprintf("tag%d", i)
252 err = repo.Manifests().Tag(ctx, manifestDesc, tag)
253 qt.Assert(t, qt.IsNil(err))
254 tags = append(tags, tag)
255 }
256 t.Logf("XXXX pushed all content and tags")
257 slices.Sort(tags)
258 var gotTags []string
259 err = repo.Tags(ctx, "", func(tags []string) error {
260 gotTags = append(gotTags, tags...)
261 return nil
262 })
263 qt.Assert(t, qt.IsNil(err))
264 qt.Assert(t, qt.DeepEquals(gotTags, tags))
265 }
266
267 func testManyRepos(t *testing.T, client *remote.Registry) {
268 ctx := context.Background()
269 var repos []string
270 for i := 0; i < 37; i++ {
271 repoName := fmt.Sprintf("repo%d", i)
272 repo, err := client.Repository(ctx, repoName)
273 qt.Assert(t, qt.IsNil(err))
274 push(t, repo.Blobs(), "", []byte("hello "+repoName))
275 repos = append(repos, repoName)
276 }
277 t.Logf("XXXX pushed all repos")
278 slices.Sort(repos)
279 var gotRepos []string
280 err := client.Repositories(ctx, "", func(repos []string) error {
281 gotRepos = append(gotRepos, repos...)
282 return nil
283 })
284 qt.Assert(t, qt.IsNil(err))
285 qt.Assert(t, qt.DeepEquals(gotRepos, repos))
286 }
287
288 func testCatalog(t *testing.T, client *remote.Registry) {
289 repos := []string{
290 "foo/bar",
291 "zaphod",
292 "something123/longer/xx",
293 }
294 ctx := context.Background()
295 for _, repoName := range repos {
296 repo, err := client.Repository(ctx, repoName)
297 qt.Assert(t, qt.IsNil(err))
298 push(t, repo.Blobs(), "", []byte("hello "+repoName))
299 }
300
301 var gotRepos []string
302 err := client.Repositories(ctx, "", func(repos []string) error {
303 gotRepos = append(gotRepos, repos...)
304 return nil
305 })
306 qt.Assert(t, qt.IsNil(err))
307 slices.Sort(repos)
308 qt.Assert(t, qt.DeepEquals(gotRepos, repos))
309 }
310
311 func testReferrers(t *testing.T, client *remote.Registry) {
312 ctx := context.Background()
313 repo, err := client.Repository(ctx, "some/repo")
314 qt.Assert(t, qt.IsNil(err))
315 configDesc := push(t, repo.Blobs(), "application/json", []byte("{}"))
316 layer0Desc := push(t, repo.Blobs(), "", []byte("some content"))
317 manifestDesc := pushJSON(t, repo.Manifests(), ocispec.MediaTypeImageManifest, ociregistry.Manifest{
318 MediaType: ocispec.MediaTypeImageManifest,
319 Config: withMediaType(configDesc, "artifact1"),
320 Layers: []ociregistry.Descriptor{layer0Desc},
321 })
322 referrer0 := pushJSON(t, repo.Manifests(), ocispec.MediaTypeImageManifest, ociregistry.Manifest{
323 MediaType: ocispec.MediaTypeImageManifest,
324 Config: withMediaType(configDesc, "referrer0"),
325 Subject: &manifestDesc,
326 })
327 referrer1 := pushJSON(t, repo.Manifests(), ocispec.MediaTypeImageManifest, ociregistry.Manifest{
328 MediaType: ocispec.MediaTypeImageManifest,
329 Config: withMediaType(configDesc, "referrer1"),
330 Subject: &manifestDesc,
331 })
332 referrer2 := pushJSON(t, repo.Manifests(), ocispec.MediaTypeImageManifest, ociregistry.Manifest{
333 MediaType: ocispec.MediaTypeImageManifest,
334 Config: withMediaType(configDesc, "referrer2"),
335 Subject: &referrer1,
336 })
337 var gotReferrers []ocispec.Descriptor
338 err = repo.Referrers(ctx, manifestDesc, "", func(referrers []ocispec.Descriptor) error {
339 gotReferrers = append(gotReferrers, referrers...)
340 return nil
341 })
342 qt.Assert(t, qt.IsNil(err))
343 wantReferrers := []ociregistry.Descriptor{referrer0, referrer1}
344 sortDescriptors(gotReferrers)
345 sortDescriptors(wantReferrers)
346 qt.Assert(t, qt.DeepEquals(gotReferrers, wantReferrers))
347
348 gotReferrers = nil
349 err = repo.Referrers(ctx, referrer1, "", func(referrers []ocispec.Descriptor) error {
350 gotReferrers = append(gotReferrers, referrers...)
351 return nil
352 })
353 qt.Assert(t, qt.IsNil(err))
354 wantReferrers = []ociregistry.Descriptor{referrer2}
355 sortDescriptors(gotReferrers)
356 sortDescriptors(wantReferrers)
357 qt.Assert(t, qt.DeepEquals(gotReferrers, wantReferrers))
358
359 }
360
361 func testLargeManifest(t *testing.T, client *remote.Registry) {
362 ctx := context.Background()
363 repo, err := client.Repository(ctx, "some/repo")
364 qt.Assert(t, qt.IsNil(err))
365 configDesc := push(t, repo.Blobs(), "application/json", []byte("{}"))
366 layer0Desc := push(t, repo.Blobs(), "", []byte("some content"))
367 manifestDesc := pushJSON(t, repo.Manifests(), ocispec.MediaTypeImageManifest, ociregistry.Manifest{
368 MediaType: ocispec.MediaTypeImageManifest,
369 Config: withMediaType(configDesc, "artifact1"),
370 Layers: []ociregistry.Descriptor{layer0Desc},
371 Annotations: map[string]string{
372 // Note: this is larger than ociclient.inMemThreshold
373 // to force that code to hit the non-buffered code path.
374 "somethingLarge": strings.Repeat("a", 256*1024),
375 },
376 })
377 err = repo.Manifests().Tag(ctx, manifestDesc, "someTag")
378 qt.Assert(t, qt.IsNil(err))
379
380 // The ORAS API doesn't seem to provide any API to fetch
381 // a manifest directly by tag, so use the HTTP client directly.
382 req, err := http.NewRequest("GET", "http://"+client.Reference.Registry+"/v2/some/repo/manifests/someTag", nil)
383 qt.Assert(t, qt.IsNil(err))
384 resp, err := http.DefaultClient.Do(req)
385 qt.Assert(t, qt.IsNil(err))
386 data, err := io.ReadAll(resp.Body)
387 qt.Assert(t, qt.IsNil(err))
388 qt.Assert(t, qt.Equals(resp.StatusCode, http.StatusOK), qt.Commentf("response body: %q", data))
389 qt.Assert(t, qt.Equals(digest.FromBytes(data), manifestDesc.Digest))
390 qt.Assert(t, qt.Equals(int64(len(data)), manifestDesc.Size))
391 }
392
393 var extraWithLocalClientTests = []struct {
394 testName string
395 run func(t *testing.T, reg ociregistry.Interface)
396 }{{
397 testName: "rangeInBounds",
398 run: testRangeInBounds,
399 }, {
400 testName: "rangeToEnd",
401 run: testRangeToEnd,
402 }, {
403 testName: "rangeBeyondEnd",
404 run: testRangeBeyondEnd,
405 }}
406
407 // testExtraWithLocalClient defines extra tests that can't use to oras-go client
408 // because they exercise functionality that it doesn't support.
409 func testExtraWithLocalClient(t *testing.T, startSrv func(*testing.T) string) {
410 for _, test := range extraWithLocalClientTests {
411 t.Run(test.testName, func(t *testing.T) {
412 srvURL := startSrv(t)
413 client := mustNewOCIClient(srvURL, nil)
414 test.run(t, client)
415 })
416 }
417 }
418
419 func mustNewOCIClient(srvURL string, opts *ociclient.Options) ociregistry.Interface {
420 if opts == nil {
421 opts = new(ociclient.Options)
422 }
423 u, err := url.Parse(srvURL)
424 if err != nil {
425 panic(err)
426 }
427 opts.Insecure = u.Scheme == "http"
428 client, err := ociclient.New(u.Host, opts)
429 if err != nil {
430 panic(err)
431 }
432 return client
433 }
434
435 func testRangeInBounds(t *testing.T, reg ociregistry.Interface) {
436 ctx := context.Background()
437 data := []byte("hello world")
438 desc, err := reg.PushBlob(ctx, "foo/bar", ocispec.Descriptor{
439 MediaType: "application/octet-stream",
440 Size: int64(len(data)),
441 Digest: digest.FromBytes(data),
442 }, bytes.NewReader(data))
443 qt.Assert(t, qt.IsNil(err))
444 r, err := reg.GetBlobRange(ctx, "foo/bar", desc.Digest, 6, 8)
445 qt.Assert(t, qt.IsNil(err))
446 qt.Assert(t, qt.DeepEquals(r.Descriptor(), desc))
447 gotData, err := io.ReadAll(r)
448 qt.Assert(t, qt.IsNil(err))
449 qt.Assert(t, qt.Equals(string(gotData), "wo"))
450 }
451
452 func testRangeToEnd(t *testing.T, reg ociregistry.Interface) {
453 ctx := context.Background()
454 data := []byte("hello world")
455 desc, err := reg.PushBlob(ctx, "foo/bar", ocispec.Descriptor{
456 MediaType: "application/octet-stream",
457 Size: int64(len(data)),
458 Digest: digest.FromBytes(data),
459 }, bytes.NewReader(data))
460 qt.Assert(t, qt.IsNil(err))
461 r, err := reg.GetBlobRange(ctx, "foo/bar", desc.Digest, 1, -1)
462 qt.Assert(t, qt.IsNil(err))
463 qt.Assert(t, qt.DeepEquals(r.Descriptor(), desc))
464 gotData, err := io.ReadAll(r)
465 qt.Assert(t, qt.IsNil(err))
466 qt.Assert(t, qt.Equals(string(gotData), "ello world"))
467 }
468
469 func testRangeBeyondEnd(t *testing.T, reg ociregistry.Interface) {
470 ctx := context.Background()
471 data := []byte("hello world")
472 desc, err := reg.PushBlob(ctx, "foo/bar", ocispec.Descriptor{
473 MediaType: "application/octet-stream",
474 Size: int64(len(data)),
475 Digest: digest.FromBytes(data),
476 }, bytes.NewReader(data))
477 qt.Assert(t, qt.IsNil(err))
478 r, err := reg.GetBlobRange(ctx, "foo/bar", desc.Digest, 1, 5000)
479 qt.Assert(t, qt.IsNil(err))
480 qt.Assert(t, qt.DeepEquals(r.Descriptor(), desc))
481 gotData, err := io.ReadAll(r)
482 qt.Assert(t, qt.IsNil(err))
483 qt.Assert(t, qt.Equals(string(gotData), "ello world"))
484 }
485
486 func sortDescriptors(ds []ociregistry.Descriptor) {
487 sort.Slice(ds, func(i, j int) bool {
488 return ds[i].Digest < ds[j].Digest
489 })
490 }
491
492 func withMediaType(desc ociregistry.Descriptor, mt string) ociregistry.Descriptor {
493 desc.MediaType = mt
494 return desc
495 }
496
497 func pushJSON(t *testing.T, dst content.Pusher, mediaType string, content any) ociregistry.Descriptor {
498 data, err := json.Marshal(content)
499 qt.Assert(t, qt.IsNil(err))
500 return push(t, dst, mediaType, data)
501 }
502
503 func push(t *testing.T, dst content.Pusher, mediaType string, content []byte) ociregistry.Descriptor {
504 desc := newDescriptor(mediaType, content)
505 err := dst.Push(context.Background(), desc, bytes.NewReader(content))
506 qt.Assert(t, qt.IsNil(err))
507 return desc
508 }
509
510 func newDescriptor(mediaType string, content []byte) ociregistry.Descriptor {
511 if mediaType == "" {
512 mediaType = "application/octet-stream"
513 }
514 return ociregistry.Descriptor{
515 MediaType: mediaType,
516 Size: int64(len(content)),
517 Digest: digest.FromBytes(content),
518 }
519 }
520
521 // testDistribution exercises the distribution-spec conformance tests.
522 func testDistribution(t *testing.T, startSrv func(*testing.T) string) {
523 srvURL := startSrv(t)
524 // The conformance tests aren't available to run directly, so we
525 // run `go test` on them.
526 t.Setenv("OCI_ROOT_URL", srvURL)
527 t.Setenv("OCI_NAMESPACE", "myorg/myrepo")
528 t.Setenv("OCI_CROSSMOUNT_NAMESPACE", "myorg/other")
529 t.Setenv("OCI_USERNAME", "myuser")
530 t.Setenv("OCI_PASSWORD", "mypass'")
531 t.Setenv("OCI_TEST_PULL", "1")
532 t.Setenv("OCI_TEST_PUSH", "1")
533 t.Setenv("OCI_TEST_CONTENT_DISCOVERY", "1")
534 t.Setenv("OCI_TEST_CONTENT_MANAGEMENT", "1")
535 t.Setenv("OCI_HIDE_SKIPPED_WORKFLOWS", "0")
536 t.Setenv("OCI_DEBUG", "1")
537 t.Setenv("OCI_DELETE_MANIFEST_BEFORE_BLOBS", "0")
538 t.Setenv("ACK_GINKGO_DEPRECATIONS", "1.16.5")
539 if os.Getenv("OCI_REPORT_DIR") == "" {
540 t.Setenv("OCI_REPORT_DIR", "none")
541 }
542
543 // Note: if we don't have `-count=1` here, re-running the outer
544 // test suite with `-count=1` won't actually force a re-run of the inner tests.
545 args := []string{"test", "-count=1"}
546 if testing.Verbose() {
547 args = append(args, "-v")
548 }
549 args = append(args, "github.com/opencontainers/distribution-spec/conformance")
550
551 cmd := exec.Command("go", args...)
552 r, err := cmd.StdoutPipe()
553 if err != nil {
554 t.Fatal(err)
555 }
556 cmd.Stderr = cmd.Stdout
557 if err := cmd.Start(); err != nil {
558 t.Fatal(err)
559 }
560 for scan := bufio.NewScanner(r); scan.Scan(); {
561 t.Log(scan.Text())
562 }
563 if err := cmd.Wait(); err != nil {
564 if _, ok := err.(*exec.ExitError); ok {
565 t.Fail()
566 } else {
567 t.Fatalf("unexpected error running command: %v", err)
568 }
569 }
570 }