Skip to content

Commit 1c60954

Browse files
authored
Merge pull request #106 from dtrudg/test-index
test: modify corpus ImageIndex handling
2 parents 49d96fc + 784b767 commit 1c60954

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed
-194 Bytes
Binary file not shown.
-908 Bytes
Binary file not shown.
-67 Bytes
Binary file not shown.
-194 Bytes
Binary file not shown.

pkg/sif/write_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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,8 @@ import (
1010
"testing"
1111

1212
v1 "github.com/google/go-containerregistry/pkg/v1"
13+
ggcrempty "github.com/google/go-containerregistry/pkg/v1/empty"
14+
ggcrmutate "github.com/google/go-containerregistry/pkg/v1/mutate"
1315
"github.com/sebdah/goldie/v2"
1416
"github.com/sylabs/oci-tools/pkg/sif"
1517
"github.com/sylabs/oci-tools/test"
@@ -26,19 +28,25 @@ func TestWrite(t *testing.T) {
2628
}{
2729
{
2830
name: "DockerManifest",
29-
ii: corpus.ImageIndex(t, "hello-world-docker-v2-manifest"),
31+
ii: ggcrmutate.AppendManifests(
32+
ggcrempty.Index,
33+
ggcrmutate.IndexAddendum{Add: corpus.Image(t, "hello-world-docker-v2-manifest")}),
3034
},
3135
{
3236
name: "DockerManifestList",
3337
ii: corpus.ImageIndex(t, "hello-world-docker-v2-manifest-list"),
3438
},
3539
{
3640
name: "ManyLayers",
37-
ii: corpus.ImageIndex(t, "many-layers"),
41+
ii: ggcrmutate.AppendManifests(
42+
ggcrempty.Index,
43+
ggcrmutate.IndexAddendum{Add: corpus.Image(t, "many-layers")}),
3844
},
3945
{
4046
name: "SpareDescriptor",
41-
ii: corpus.ImageIndex(t, "hello-world-docker-v2-manifest"),
47+
ii: ggcrmutate.AppendManifests(
48+
ggcrempty.Index,
49+
ggcrmutate.IndexAddendum{Add: corpus.Image(t, "hello-world-docker-v2-manifest")}),
4250
opts: []sif.WriteOpt{
4351
sif.OptWriteWithSpareDescriptorCapacity(1),
4452
},

test/images.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.
3656
func (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

Comments
 (0)