Skip to content

Commit

Permalink
fix: fix the potential panic error if there is no platform in the ind…
Browse files Browse the repository at this point in the history
…ex or manifest list

Signed-off-by: Zoey Li <[email protected]>
  • Loading branch information
lizMSFT committed Jan 25, 2024
1 parent 66ea3df commit 1b64aef
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
8 changes: 8 additions & 0 deletions internal/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ import (
// Note: Variant, OSVersion and OSFeatures are optional fields, will skip
// the comparison if the target platform does not provide specific value.
func Match(got *ocispec.Platform, want *ocispec.Platform) bool {
if got == nil && want == nil {
return true
}

Check warning on line 43 in internal/platform/platform.go

View check run for this annotation

Codecov / codecov/patch

internal/platform/platform.go#L42-L43

Added lines #L42 - L43 were not covered by tests

if got == nil || want == nil {
return false
}

if got.Architecture != want.Architecture || got.OS != want.OS {
return false
}
Expand Down
93 changes: 65 additions & 28 deletions internal/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,23 @@ func TestSelectManifest(t *testing.T) {
Size: int64(len(blob)),
})
}
appendManifest := func(arc, os, variant string, mediaType string, blob []byte) {
appendManifest := func(arc, os, variant string, hasPlatform bool, mediaType string, blob []byte) {
blobs = append(blobs, blob)
descs = append(descs, ocispec.Descriptor{
desc := ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
Platform: &ocispec.Platform{
}
if hasPlatform {
desc.Platform = &ocispec.Platform{
Architecture: arc,
OS: os,
Variant: variant,
},
})
}
}
descs = append(descs, desc)
}
generateManifest := func(arc, os, variant string, subject *ocispec.Descriptor, config ocispec.Descriptor, layers ...ocispec.Descriptor) {
generateManifest := func(arc, os, variant string, hasPlatform bool, subject *ocispec.Descriptor, config ocispec.Descriptor, layers ...ocispec.Descriptor) {
manifest := ocispec.Manifest{
Subject: subject,
Config: config,
Expand All @@ -154,7 +157,7 @@ func TestSelectManifest(t *testing.T) {
if err != nil {
t.Fatal(err)
}
appendManifest(arc, os, variant, ocispec.MediaTypeImageManifest, manifestJSON)
appendManifest(arc, os, variant, hasPlatform, ocispec.MediaTypeImageManifest, manifestJSON)
}
generateIndex := func(subject *ocispec.Descriptor, manifests ...ocispec.Descriptor) {
index := ocispec.Index{
Expand All @@ -175,14 +178,14 @@ func TestSelectManifest(t *testing.T) {
"architecture":"test-arc-1",
"os":"test-os-1",
"variant":"v1"}`)) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 2
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 3
generateManifest(arc_1, os_1, variant_1, &descs[0], descs[1], descs[2:4]...) // Blob 4
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello1")) // Blob 5
generateManifest(arc_2, os_2, variant_1, nil, descs[1], descs[5]) // Blob 6
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello2")) // Blob 7
generateManifest(arc_1, os_1, variant_2, nil, descs[1], descs[7]) // Blob 8
generateIndex(&descs[0], descs[4], descs[6], descs[8]) // Blob 9
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 2
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 3
generateManifest(arc_1, os_1, variant_1, true, &descs[0], descs[1], descs[2:4]...) // Blob 4
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello1")) // Blob 5
generateManifest(arc_2, os_2, variant_1, true, nil, descs[1], descs[5]) // Blob 6
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello2")) // Blob 7
generateManifest(arc_1, os_1, variant_2, true, nil, descs[1], descs[7]) // Blob 8
generateIndex(&descs[0], descs[4], descs[6], descs[8]) // Blob 9

ctx := context.Background()
for i := range blobs {
Expand Down Expand Up @@ -264,6 +267,40 @@ func TestSelectManifest(t *testing.T) {
t.Fatalf("SelectManifest() error = %v, wantErr %v", err, errdef.ErrUnsupported)
}

// generate test content without platform
storage = cas.NewMemory()
blobs = nil
descs = nil
appendBlob("test/subject", []byte("dummy subject")) // Blob 0
appendBlob(ocispec.MediaTypeImageConfig, []byte(`{"mediaType":"application/vnd.oci.image.config.v1+json",
"created":"2022-07-29T08:13:55Z",
"author":"test author",
"architecture":"test-arc-1",
"os":"test-os-1",
"variant":"v1"}`)) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 2
generateManifest(arc_1, os_1, variant_1, false, &descs[0], descs[1], descs[2]) // Blob 3
generateIndex(&descs[0], descs[3]) // Blob 4

ctx = context.Background()
for i := range blobs {
err := storage.Push(ctx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
t.Fatalf("failed to push test content to src: %d: %v", i, err)
}
}
// test SelectManifest on image index, only one matching manifest found
root = descs[4]
targetPlatform = ocispec.Platform{
Architecture: arc_1,
OS: os_1,
}
_, err = SelectManifest(ctx, storage, root, &targetPlatform)
expected = fmt.Sprintf("%s: %v: no matching manifest was found in the manifest list", root.Digest, errdef.ErrNotFound)
if err.Error() != expected {
t.Fatalf("SelectManifest() error = %v, wantErr %v", err, expected)
}

// generate incorrect test content
storage = cas.NewMemory()
blobs = nil
Expand All @@ -275,9 +312,9 @@ func TestSelectManifest(t *testing.T) {
"architecture":"test-arc-1",
"os":"test-os-1",
"variant":"v1"}`)) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo1")) // Blob 2
generateManifest(arc_1, os_1, variant_1, &descs[0], descs[1], descs[2]) // Blob 3
generateIndex(&descs[0], descs[3]) // Blob 4
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo1")) // Blob 2
generateManifest(arc_1, os_1, variant_1, true, &descs[0], descs[1], descs[2]) // Blob 3
generateIndex(&descs[0], descs[3]) // Blob 4

ctx = context.Background()
for i := range blobs {
Expand Down Expand Up @@ -305,11 +342,11 @@ func TestSelectManifest(t *testing.T) {
storage = cas.NewMemory()
blobs = nil
descs = nil
appendBlob("test/subject", []byte("dummy subject")) // Blob 0
appendBlob(ocispec.MediaTypeImageConfig, []byte("null")) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo2")) // Blob 2
generateManifest(arc_1, os_1, variant_1, &descs[0], descs[1], descs[2]) // Blob 3
generateIndex(nil, descs[3]) // Blob 4
appendBlob("test/subject", []byte("dummy subject")) // Blob 0
appendBlob(ocispec.MediaTypeImageConfig, []byte("null")) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo2")) // Blob 2
generateManifest(arc_1, os_1, variant_1, true, &descs[0], descs[1], descs[2]) // Blob 3
generateIndex(nil, descs[3]) // Blob 4

ctx = context.Background()
for i := range blobs {
Expand All @@ -336,11 +373,11 @@ func TestSelectManifest(t *testing.T) {
storage = cas.NewMemory()
blobs = nil
descs = nil
appendBlob("test/subject", []byte("dummy subject")) // Blob 0
appendBlob(ocispec.MediaTypeImageConfig, []byte("")) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo3")) // Blob 2
generateManifest(arc_1, os_1, variant_1, nil, descs[1], descs[2]) // Blob 3
generateIndex(&descs[0], descs[3]) // Blob 4
appendBlob("test/subject", []byte("dummy subject")) // Blob 0
appendBlob(ocispec.MediaTypeImageConfig, []byte("")) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo3")) // Blob 2
generateManifest(arc_1, os_1, variant_1, true, nil, descs[1], descs[2]) // Blob 3
generateIndex(&descs[0], descs[3]) // Blob 4

ctx = context.Background()
for i := range blobs {
Expand Down

0 comments on commit 1b64aef

Please sign in to comment.