Skip to content

Commit

Permalink
Merge pull request #92 from tri-adam/improve-corpus
Browse files Browse the repository at this point in the history
refactor: improve test corpus
  • Loading branch information
tri-adam authored Oct 7, 2024
2 parents b3d301b + 2e928c0 commit aa3f634
Show file tree
Hide file tree
Showing 29 changed files with 248 additions and 47 deletions.
49 changes: 44 additions & 5 deletions pkg/sif/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,64 @@ func Test_imageIndex_Image(t *testing.T) {
hash: imageDigest,
wantDescriptor: imageDescriptor,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
img, err := tt.ii.Image(tt.hash)
if err != nil {
t.Fatal(err)
}

if err := validate.Image(img); err != nil {
t.Error(err)
}

if d, err := img.(withDescriptor).Descriptor(); err != nil {
t.Error(err)
} else if got, want := d, tt.wantDescriptor; !reflect.DeepEqual(got, want) {
t.Errorf("got descriptor %+v, want %+v", got, want)
}
})
}
}

func Test_imageIndex_Index(t *testing.T) {
indexDigest := v1.Hash{
Algorithm: "sha256",
Hex: "00e1ee7c898a2c393ea2fe7680938f8dcbe55e51fbf08032cf37326a677f92ed",
}
indexDescriptor := &v1.Descriptor{
MediaType: "application/vnd.docker.distribution.manifest.list.v2+json",
Size: 2069,
Digest: indexDigest,
}

tests := []struct {
name string
ii v1.ImageIndex
hash v1.Hash
wantErr bool
wantDescriptor *v1.Descriptor
}{
{
name: "DockerManifestList",
ii: imageIndexFromPath(t, "hello-world-docker-v2-manifest-list"),
hash: imageDigest,
wantDescriptor: imageDescriptor,
hash: indexDigest,
wantDescriptor: indexDescriptor,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
img, err := tt.ii.Image(tt.hash)
ii, err := tt.ii.ImageIndex(tt.hash)
if err != nil {
t.Fatal(err)
}

if err := validate.Image(img); err != nil {
if err := validate.Index(ii); err != nil {
t.Error(err)
}

if d, err := img.(withDescriptor).Descriptor(); err != nil {
if d, err := ii.(withDescriptor).Descriptor(); err != nil {
t.Error(err)
} else if got, want := d, tt.wantDescriptor; !reflect.DeepEqual(got, want) {
t.Errorf("got descriptor %+v, want %+v", got, want)
Expand Down
8 changes: 4 additions & 4 deletions pkg/sif/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func TestImageIndexFromFileImage(t *testing.T) {
f: fileImageFromPath(t, "hello-world-docker-v2-manifest"),
wantDescriptor: &v1.Descriptor{
MediaType: "application/vnd.oci.image.index.v1+json",
Size: 314,
Size: 444,
Digest: v1.Hash{
Algorithm: "sha256",
Hex: "a2ca8d2eb29d4b32cabd3f2ca67c14c8ae178b93c3000da3ec63faca49a688e4",
Hex: "61a28aa82cdc48531d7dc366298507a4e896cebce1e01ff391626edd968f6d58",
},
},
},
Expand All @@ -63,10 +63,10 @@ func TestImageIndexFromFileImage(t *testing.T) {
f: fileImageFromPath(t, "hello-world-docker-v2-manifest-list"),
wantDescriptor: &v1.Descriptor{
MediaType: "application/vnd.oci.image.index.v1+json",
Size: 2069,
Size: 323,
Digest: v1.Hash{
Algorithm: "sha256",
Hex: "00e1ee7c898a2c393ea2fe7680938f8dcbe55e51fbf08032cf37326a677f92ed",
Hex: "18102125ef60453edd3543a30fd87469b3703999c8be7b58de67575f09ea74b2",
},
},
},
Expand Down
Binary file modified pkg/sif/testdata/TestRemoveBlob/Valid.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestRemoveManifests/NoMatch.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestRemoveManifests/Valid.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestReplace/ReplaceImageManifestList.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestReplace/ReplaceIndexManifestList.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestWrite/DockerManifest.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestWrite/DockerManifestList.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestWrite/ManyLayers.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestWrite/SpareDescriptor.golden
Binary file not shown.
40 changes: 18 additions & 22 deletions test/images/gen_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
ocimutate "github.com/sylabs/oci-tools/pkg/mutate"
Expand Down Expand Up @@ -56,17 +55,12 @@ func generateImages(path string) error {
return err
}

desc, err := partial.Descriptor(img)
lp, err := layout.Write(im.destination, empty.Index)
if err != nil {
return err
}

ii := mutate.AppendManifests(empty.Index, mutate.IndexAddendum{
Add: img,
Descriptor: *desc,
})

if _, err := layout.Write(im.destination, ii); err != nil {
if err := lp.AppendImage(img); err != nil {
return err
}
}
Expand All @@ -92,7 +86,12 @@ func generateIndexes(path string) error {
return err
}

if _, err := layout.Write(im.destination, ii); err != nil {
lp, err := layout.Write(im.destination, empty.Index)
if err != nil {
return err
}

if err := lp.AppendIndex(ii); err != nil {
return err
}
}
Expand Down Expand Up @@ -418,9 +417,12 @@ func generateTARImages(path string) error {
}
}

ii := mutate.AppendManifests(empty.Index, mutate.IndexAddendum{Add: img})
lp, err := layout.Write(im.destination, empty.Index)
if err != nil {
return err
}

if _, err := layout.Write(im.destination, ii); err != nil {
if err := lp.AppendImage(img); err != nil {
return err
}
}
Expand Down Expand Up @@ -458,13 +460,12 @@ func generateManyLayerImage(path string) error {
return err
}

ii := mutate.AppendManifests(empty.Index, mutate.IndexAddendum{Add: img})

if _, err := layout.Write(filepath.Join(path, "many-layers"), ii); err != nil {
lp, err := layout.Write(filepath.Join(path, "many-layers"), empty.Index)
if err != nil {
return err
}

return nil
return lp.AppendImage(img)
}

var errMultipleImages = errors.New("multiple images found in index")
Expand Down Expand Up @@ -528,17 +529,12 @@ func generateSquashFSImages(path string) error {
return err
}

desc, err := partial.Descriptor(img)
lp, err := layout.Write(im.destination, empty.Index)
if err != nil {
return err
}

ii = mutate.AppendManifests(empty.Index, mutate.IndexAddendum{
Add: img,
Descriptor: *desc,
})

if _, err := layout.Write(im.destination, ii); err != nil {
if err := lp.AppendImage(img); err != nil {
return err
}
}
Expand Down
12 changes: 11 additions & 1 deletion test/images/hard-link-1/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":423,"digest":"sha256:eacde4cbd0221fb6527f3fc01918db6a494a05c68f3e220f33c1dad9e174cd2c"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 423,
"digest": "sha256:eacde4cbd0221fb6527f3fc01918db6a494a05c68f3e220f33c1dad9e174cd2c"
}
]
}
12 changes: 11 additions & 1 deletion test/images/hard-link-2/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":583,"digest":"sha256:f7209ae85e747ac5b1a7ba4ff73b24698a2868ce123632e8beb4813b46cf2d63"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 583,
"digest": "sha256:f7209ae85e747ac5b1a7ba4ff73b24698a2868ce123632e8beb4813b46cf2d63"
}
]
}
12 changes: 11 additions & 1 deletion test/images/hard-link-delete-1/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":583,"digest":"sha256:e343c65727b572acd09c1f505861e2165a3d62ff3e334d0e1377912ce2e41862"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 583,
"digest": "sha256:e343c65727b572acd09c1f505861e2165a3d62ff3e334d0e1377912ce2e41862"
}
]
}
12 changes: 11 additions & 1 deletion test/images/hard-link-delete-2/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":583,"digest":"sha256:0e0b27a1f4af2f833ea9c4c04c0603a08aa3562fd5b0fbfba0d9167cf745504d"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 583,
"digest": "sha256:0e0b27a1f4af2f833ea9c4c04c0603a08aa3562fd5b0fbfba0d9167cf745504d"
}
]
}
12 changes: 11 additions & 1 deletion test/images/hard-link-delete-3/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":583,"digest":"sha256:02662923e132c5ad7a530347b39c7a6736a22251be7c680b03a4259df63ceb52"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 583,
"digest": "sha256:02662923e132c5ad7a530347b39c7a6736a22251be7c680b03a4259df63ceb52"
}
]
}
12 changes: 11 additions & 1 deletion test/images/hard-link-delete-4/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":743,"digest":"sha256:040b72ebd80ec8421c2f6ebd71fe99dd7a9ad967e7c7e438e19fc2a3dd93a77b"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 743,
"digest": "sha256:040b72ebd80ec8421c2f6ebd71fe99dd7a9ad967e7c7e438e19fc2a3dd93a77b"
}
]
}
12 changes: 11 additions & 1 deletion test/images/hard-link-delete-xattr/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":583,"digest":"sha256:1d67b918d609474d3158051f62273b90cb4333a9ca5d422105d15151955588f7"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 583,
"digest": "sha256:1d67b918d609474d3158051f62273b90cb4333a9ca5d422105d15151955588f7"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"manifests":[{"digest":"sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"amd64","os":"linux"},"size":525},{"digest":"sha256:6253ef1af25aabd67777a01c686e7c69ee612961db34c8b90da079e5473be83b","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"arm","os":"linux","variant":"v5"},"size":525},{"digest":"sha256:40d0cfd0861719208ff9f7747ab3f97844eeca509df705db44a736df863b76af","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"arm","os":"linux","variant":"v7"},"size":525},{"digest":"sha256:432f982638b3aefab73cc58ab28f5c16e96fdb504e8c134fc58dff4bae8bf338","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"arm64","os":"linux","variant":"v8"},"size":525},{"digest":"sha256:995efde2e81b21d1ea7066aa77a59298a62a9e9fbb4b77f36c189774ec9b1089","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"386","os":"linux"},"size":525},{"digest":"sha256:eb11b1a194ff8e236a01eff392c4e1296a53b0fb4780d8b0382f7996a15d5392","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"mips64le","os":"linux"},"size":525},{"digest":"sha256:3209b9aec056b296ea55b2af7757d078bf92e55a3ea29c5fdef5c785bcef09c4","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"ppc64le","os":"linux"},"size":525},{"digest":"sha256:98c9722322be649df94780d3fbe594fce7996234b259f27eac9428b84050c849","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"riscv64","os":"linux"},"size":525},{"digest":"sha256:c7b6944911848ce39b44ed660d95fb54d69bbd531de724c7ce6fc9f743c0b861","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"s390x","os":"linux"},"size":525}],"mediaType":"application\/vnd.docker.distribution.manifest.list.v2+json","schemaVersion":2}
12 changes: 11 additions & 1 deletion test/images/hello-world-docker-v2-manifest-list/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"manifests":[{"digest":"sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"amd64","os":"linux"},"size":525},{"digest":"sha256:6253ef1af25aabd67777a01c686e7c69ee612961db34c8b90da079e5473be83b","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"arm","os":"linux","variant":"v5"},"size":525},{"digest":"sha256:40d0cfd0861719208ff9f7747ab3f97844eeca509df705db44a736df863b76af","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"arm","os":"linux","variant":"v7"},"size":525},{"digest":"sha256:432f982638b3aefab73cc58ab28f5c16e96fdb504e8c134fc58dff4bae8bf338","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"arm64","os":"linux","variant":"v8"},"size":525},{"digest":"sha256:995efde2e81b21d1ea7066aa77a59298a62a9e9fbb4b77f36c189774ec9b1089","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"386","os":"linux"},"size":525},{"digest":"sha256:eb11b1a194ff8e236a01eff392c4e1296a53b0fb4780d8b0382f7996a15d5392","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"mips64le","os":"linux"},"size":525},{"digest":"sha256:3209b9aec056b296ea55b2af7757d078bf92e55a3ea29c5fdef5c785bcef09c4","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"ppc64le","os":"linux"},"size":525},{"digest":"sha256:98c9722322be649df94780d3fbe594fce7996234b259f27eac9428b84050c849","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"riscv64","os":"linux"},"size":525},{"digest":"sha256:c7b6944911848ce39b44ed660d95fb54d69bbd531de724c7ce6fc9f743c0b861","mediaType":"application\/vnd.docker.distribution.manifest.v2+json","platform":{"architecture":"s390x","os":"linux"},"size":525}],"mediaType":"application\/vnd.docker.distribution.manifest.list.v2+json","schemaVersion":2}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"size": 2069,
"digest": "sha256:00e1ee7c898a2c393ea2fe7680938f8dcbe55e51fbf08032cf37326a677f92ed"
}
]
}
17 changes: 16 additions & 1 deletion test/images/hello-world-docker-v2-manifest/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":525,"digest":"sha256:432f982638b3aefab73cc58ab28f5c16e96fdb504e8c134fc58dff4bae8bf338","platform":{"architecture":"arm64","os":"linux","variant":"v8"}}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 525,
"digest": "sha256:432f982638b3aefab73cc58ab28f5c16e96fdb504e8c134fc58dff4bae8bf338",
"platform": {
"architecture": "arm64",
"os": "linux",
"variant": "v8"
}
}
]
}
12 changes: 11 additions & 1 deletion test/images/many-layers/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":8214,"digest":"sha256:7c000de5bc837f29d1c9a5e76bba79922d860e5c0f448df3b6fc38431a067c9a"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 8214,
"digest": "sha256:7c000de5bc837f29d1c9a5e76bba79922d860e5c0f448df3b6fc38431a067c9a"
}
]
}
12 changes: 11 additions & 1 deletion test/images/overlayfs-docker-v2-manifest/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":421,"digest":"sha256:c6ec7891ab06148a4097f8d3d638671a2ca03945c8f400b4d50061c2560cb11a"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 421,
"digest": "sha256:c6ec7891ab06148a4097f8d3d638671a2ca03945c8f400b4d50061c2560cb11a"
}
]
}
12 changes: 11 additions & 1 deletion test/images/root-dir-entry/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":422,"digest":"sha256:933a741d95d28f288d212aeb08754d92a7a5d88116d4425309e1e88a9de150f4"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 422,
"digest": "sha256:933a741d95d28f288d212aeb08754d92a7a5d88116d4425309e1e88a9de150f4"
}
]
}
12 changes: 11 additions & 1 deletion test/images/whiteout-explicit-dir/index.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{"schemaVersion":2,"mediaType":"application/vnd.oci.image.index.v1+json","manifests":[{"mediaType":"application/vnd.docker.distribution.manifest.v2+json","size":583,"digest":"sha256:f79caaccbff85dadc4f638985af54679c3e56f563c0e1fd1ec6a95e6a61dbad1"}]}
{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [
{
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"size": 583,
"digest": "sha256:f79caaccbff85dadc4f638985af54679c3e56f563c0e1fd1ec6a95e6a61dbad1"
}
]
}
Loading

0 comments on commit aa3f634

Please sign in to comment.