1- // Copyright 2023 Sylabs Inc. All rights reserved.
1+ // Copyright 2023-2025 Sylabs Inc. All rights reserved.
22//
33// SPDX-License-Identifier: Apache-2.0
44
@@ -10,6 +10,7 @@ import (
1010
1111 v1 "github.com/google/go-containerregistry/pkg/v1"
1212 "github.com/google/go-containerregistry/pkg/v1/layout"
13+ "github.com/google/go-containerregistry/pkg/v1/partial"
1314 "github.com/sylabs/oci-tools/pkg/sif"
1415)
1516
@@ -18,9 +19,9 @@ func (c *Corpus) ImagePath(name string) string {
1819 return filepath .Join (c .dir , "images" , name )
1920}
2021
21- // ImageIndex returns a v1.ImageIndex corresponding to the OCI Image Layout with the specified name
22- // in the corpus.
23- func (c * Corpus ) ImageIndex (tb testing.TB , name string ) v1.ImageIndex {
22+ // rootIndex returns a v1.ImageIndex corresponding to the index.json from the OCI Image Layout
23+ // with the specified name in the corpus.
24+ func (c * Corpus ) rootIndex (tb testing.TB , name string ) v1.ImageIndex {
2425 tb .Helper ()
2526
2627 ii , err := layout .ImageIndexFromPath (c .ImagePath (name ))
@@ -31,19 +32,40 @@ func (c *Corpus) ImageIndex(tb testing.TB, name string) v1.ImageIndex {
3132 return ii
3233}
3334
35+ // ImageIndex returns a v1.ImageIndex corresponding to the OCI Image Layout with the specified name
36+ // in the corpus.
37+ func (c * Corpus ) ImageIndex (tb testing.TB , name string ) v1.ImageIndex {
38+ tb .Helper ()
39+
40+ iis , err := partial .FindIndexes (c .rootIndex (tb , name ), matchAll )
41+ if err != nil {
42+ tb .Fatalf ("failed to find indexes: %v" , err )
43+ }
44+
45+ if got := len (iis ); got != 1 {
46+ tb .Fatalf ("got %v manifests, expected 1" , got )
47+ }
48+
49+ return iis [0 ]
50+ }
51+
52+ func matchAll (v1.Descriptor ) bool { return true }
53+
3454// Image returns a v1.Image corresponding to the OCI Image Layout with the specified name in the
3555// corpus.
3656func (c * Corpus ) Image (tb testing.TB , name string ) v1.Image {
3757 tb .Helper ()
3858
39- ii := c .ImageIndex (tb , name )
40-
41- img , err := ii .Image (v1.Hash {})
59+ ims , err := partial .FindImages (c .rootIndex (tb , name ), matchAll )
4260 if err != nil {
43- tb .Fatalf ("failed to get image from index: %v" , err )
61+ tb .Fatalf ("failed to find images: %v" , err )
62+ }
63+
64+ if got := len (ims ); got != 1 {
65+ tb .Fatalf ("got %v manifests, expected 1" , got )
4466 }
4567
46- return img
68+ return ims [ 0 ]
4769}
4870
4971// OCILayout returns a temporary OCI Image Layout for the test to use, populated from the OCI Image
@@ -68,7 +90,7 @@ func (c *Corpus) SIF(tb testing.TB, name string, opt ...sif.WriteOpt) string {
6890
6991 path := filepath .Join (tb .TempDir (), "image.sif" )
7092
71- if err := sif .Write (path , c .ImageIndex (tb , name ), opt ... ); err != nil {
93+ if err := sif .Write (path , c .rootIndex (tb , name ), opt ... ); err != nil {
7294 tb .Fatalf ("failed to write SIF: %v" , err )
7395 }
7496
0 commit comments