Skip to content

Commit

Permalink
[PRFix using struct instead of pointer for the unmarshal and add tests]
Browse files Browse the repository at this point in the history
Signed-off-by: Zoey Li <zoeyli@microsoft.com>
  • Loading branch information
lizMSFT committed Aug 5, 2022
1 parent 45d2c1f commit e2af366
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
9 changes: 3 additions & 6 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func getPlatformFromConfig(ctx context.Context, src content.Storage, desc ocispe
}
defer rc.Close()

var platform *ocispec.Platform
if err = json.NewDecoder(rc).Decode(&platform); err != nil {
var platform ocispec.Platform
if err = json.NewDecoder(rc).Decode(&platform); err != nil && err != io.EOF {
return nil, err
}

return platform, nil
return &platform, nil
}

// selectPlatform implements platform filter and returns the descriptor of the
Expand Down Expand Up @@ -111,9 +111,6 @@ func selectPlatform(ctx context.Context, src content.Storage, root ocispec.Descr
if err != nil {
return ocispec.Descriptor{}, err
}
if cfgPlatform == nil {
return ocispec.Descriptor{}, fmt.Errorf("%s: missing platform info", descs[0].Digest)
}

if platform.Match(cfgPlatform, p) {
return root, nil
Expand Down
74 changes: 74 additions & 0 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,80 @@ func TestCopy_WithTargetPlatformOptions(t *testing.T) {
if err.Error() != expected {
t.Fatalf("Copy() error = %v, wantErr %v", err, expected)
}

// generate test content with null config blob
blobs = nil
descs = nil
appendBlob(ocispec.MediaTypeImageConfig, []byte("null")) // Blob 0
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo2")) // Blob 1
generateManifest(arc_1, os_1, variant_1, descs[0], descs[1]) // Blob 2
generateIndex(descs[2]) // Blob 3

ctx = context.Background()
for i := range blobs {
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
t.Fatalf("failed to push test content to src: %d: %v", i, err)
}
}

dst = memory.New()
opts = oras.CopyOptions{}
targetPlatform = ocispec.Platform{
Architecture: arc_1,
OS: os_1,
}
opts.WithTargetPlatform(&targetPlatform)

// test copy with platform filter for the manifest with null config blob
// should return not found error
root = descs[2]
err = src.Tag(ctx, root, ref)
if err != nil {
t.Fatal("fail to tag root node", err)
}

_, err = oras.Copy(ctx, src, ref, dst, "", opts)
if !errors.Is(err, errdef.ErrNotFound) {
t.Fatalf("Copy() error = %v, wantErr %v", err, errdef.ErrNotFound)
}

// generate test content with empty config blob
blobs = nil
descs = nil
appendBlob(ocispec.MediaTypeImageConfig, []byte("")) // Blob 0
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo3")) // Blob 1
generateManifest(arc_1, os_1, variant_1, descs[0], descs[1]) // Blob 2
generateIndex(descs[2]) // Blob 3

ctx = context.Background()
for i := range blobs {
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
t.Fatalf("failed to push test content to src: %d: %v", i, err)
}
}

dst = memory.New()
opts = oras.CopyOptions{}
targetPlatform = ocispec.Platform{
Architecture: arc_1,
OS: os_1,
}
opts.WithTargetPlatform(&targetPlatform)

// test copy with platform filter for the manifest with empty config blob
// should return not found error
root = descs[2]
err = src.Tag(ctx, root, ref)
if err != nil {
t.Fatal("fail to tag root node", err)
}

_, err = oras.Copy(ctx, src, ref, dst, "", opts)
if !errors.Is(err, errdef.ErrNotFound) {
t.Fatalf("Copy() error = %v, wantErr %v", err, errdef.ErrNotFound)
}
}

func TestCopy_RestoreDuplicates(t *testing.T) {
Expand Down

0 comments on commit e2af366

Please sign in to comment.