Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: oras.Copy() may skip tagging if manifest blob already exists in the destination #243

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,23 @@ func prepareCopy(ctx context.Context, dst Target, dstRef string, proxy *cas.Prox
}
}

onCopySkipped := opts.OnCopySkipped
opts.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
if onCopySkipped != nil {
if err := onCopySkipped(ctx, desc); err != nil {
return err
}
}
if !descriptor.EqualOCI(desc, root) {
return nil
}
// copy reference for skipped root
m5i-work marked this conversation as resolved.
Show resolved Hide resolved
if refPusher, ok := dst.(registry.ReferencePusher); ok {
return copyCachedNodeWithReference(ctx, proxy, refPusher, desc, dstRef)
} else {
return dst.Tag(ctx, root, dstRef)
}
m5i-work marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
108 changes: 108 additions & 0 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,114 @@ func TestCopy_FullCopy(t *testing.T) {
}
}

func TestCopy_ExistedRoot(t *testing.T) {
m5i-work marked this conversation as resolved.
Show resolved Hide resolved
src := memory.New()
dst := memory.New()

// generate test content
var blobs [][]byte
var descs []ocispec.Descriptor
appendBlob := func(mediaType string, blob []byte) {
blobs = append(blobs, blob)
descs = append(descs, ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
})
}
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) {
manifest := ocispec.Manifest{
Config: config,
Layers: layers,
}
manifestJSON, err := json.Marshal(manifest)
if err != nil {
t.Fatal(err)
}
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON)
}

appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2
generateManifest(descs[0], descs[1:3]...) // 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)
}
}

root := descs[3]
ref := "foobar"
newTag := "newtag"
err := src.Tag(ctx, root, ref)
if err != nil {
t.Fatal("fail to tag root node", err)
}

var skippedCount int64
copyOpts := oras.CopyOptions{
CopyGraphOptions: oras.CopyGraphOptions{
OnCopySkipped: func(ctx context.Context, desc ocispec.Descriptor) error {
atomic.AddInt64(&skippedCount, 1)
return nil
},
},
}

// copy with src tag
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", copyOpts)
if err != nil {
t.Fatalf("Copy() error = %v, wantErr %v", err, false)
}
if !reflect.DeepEqual(gotDesc, root) {
t.Errorf("Copy() = %v, want %v", gotDesc, root)
}
// copy with new tag
gotDesc, err = oras.Copy(ctx, src, ref, dst, newTag, copyOpts)
if err != nil {
t.Fatalf("Copy() error = %v, wantErr %v", err, false)
}
if !reflect.DeepEqual(gotDesc, root) {
t.Errorf("Copy() = %v, want %v", gotDesc, root)
}

// verify contents
for i, desc := range descs {
exists, err := dst.Exists(ctx, desc)
if err != nil {
t.Fatalf("dst.Exists(%d) error = %v", i, err)
}
if !exists {
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true)
}
}

// verify src tag
gotDesc, err = dst.Resolve(ctx, ref)
if err != nil {
t.Fatal("dst.Resolve() error =", err)
}
if !reflect.DeepEqual(gotDesc, root) {
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root)
}
// verify new tag
gotDesc, err = dst.Resolve(ctx, newTag)
if err != nil {
t.Fatal("dst.Resolve() error =", err)
}
if !reflect.DeepEqual(gotDesc, root) {
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root)
}
// verify invocation of onCopySkipped()
if got, want := skippedCount, int64(1); got != want {
t.Errorf("count(OnCopySkipped()) = %v, want %v", got, want)
}
}

func TestCopyGraph_FullCopy(t *testing.T) {
src := cas.NewMemory()
dst := cas.NewMemory()
Expand Down