Skip to content

Commit

Permalink
fix: added more test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle M. Tarplee <[email protected]>
  • Loading branch information
ktarplee committed Jan 5, 2024
1 parent 75b9a9f commit ef5d68c
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 4 deletions.
2 changes: 2 additions & 0 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ func mountOrCopyNode(ctx context.Context, src content.ReadOnlyStorage, dst conte

// Only care to mount blobs
if descriptor.IsManifest(desc) {
// mountOrCopyNode might never be called with a Manifest based on how copyGraph() is currently implemented
// but it is safer to handle all cases so we keep this here
return copyNode(ctx, src, dst, desc, opts)
}

Check warning on line 290 in copy.go

View check run for this annotation

Codecov / codecov/patch

copy.go#L287-L290

Added lines #L287 - L290 were not covered by tests

Expand Down
136 changes: 132 additions & 4 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ func TestCopyGraph_WithOptions(t *testing.T) {
},
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
t.Fatalf("CopyGraph() error = %v", err)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
Expand Down Expand Up @@ -1515,7 +1515,7 @@ func TestCopyGraph_WithOptions(t *testing.T) {
return []string{"source"}, nil
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
t.Fatalf("CopyGraph() error = %v", err)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
Expand Down Expand Up @@ -1590,7 +1590,7 @@ func TestCopyGraph_WithOptions(t *testing.T) {
return []string{"source"}, nil
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
t.Fatalf("CopyGraph() error = %v", err)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
Expand Down Expand Up @@ -1679,7 +1679,7 @@ func TestCopyGraph_WithOptions(t *testing.T) {
return []string{"missing/the/data", "source"}, nil
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
t.Fatalf("CopyGraph() error = %v", err)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
Expand Down Expand Up @@ -1709,6 +1709,134 @@ func TestCopyGraph_WithOptions(t *testing.T) {
t.Errorf("count(PostCopy()) = %d, want %d", got, expected)
}
})

t.Run("MountFrom empty sourceRepositories", func(t *testing.T) {
root = descs[6]
dst := &countingStorage{storage: cas.NewMemory()}
opts = oras.CopyGraphOptions{}
var numMountFrom atomic.Int64
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
numMountFrom.Add(1)
return nil, nil
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
t.Fatalf("CopyGraph() error = %v", err)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
t.Errorf("count(Exists()) = %d, want %d", got, expected)
}
if got, expected := dst.numFetch.Load(), int64(0); got != expected {
t.Errorf("count(Fetch()) = %d, want %d", got, expected)
}
if got, expected := dst.numPush.Load(), int64(7); got != expected {
t.Errorf("count(Push()) = %d, want %d", got, expected)
}
if got, expected := numMountFrom.Load(), int64(4); got != expected {
t.Errorf("count(MountFrom()) = %d, want %d", got, expected)
}
})

t.Run("MountFrom error", func(t *testing.T) {
root = descs[6]
dst := &countingStorage{storage: cas.NewMemory()}
opts = oras.CopyGraphOptions{}
var numMountFrom atomic.Int64
e := errors.New("mountFrom error")
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
numMountFrom.Add(1)
return nil, e
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, e) {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, e)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
t.Errorf("count(Exists()) = %d, want %d", got, expected)
}
if got, expected := dst.numFetch.Load(), int64(0); got != expected {
t.Errorf("count(Fetch()) = %d, want %d", got, expected)
}
if got, expected := dst.numPush.Load(), int64(0); got != expected {
t.Errorf("count(Push()) = %d, want %d", got, expected)
}
if got, expected := numMountFrom.Load(), int64(4); got != expected {
t.Errorf("count(MountFrom()) = %d, want %d", got, expected)
}
})

t.Run("MountFrom OnMounted error", func(t *testing.T) {
root = descs[6]
dst := &countingStorage{storage: cas.NewMemory()}
var numMount atomic.Int64
dst.mount = func(ctx context.Context,
desc ocispec.Descriptor,
fromRepo string,
getContent func() (io.ReadCloser, error),
) error {
numMount.Add(1)
if expected := "source"; fromRepo != expected {
t.Fatalf("fromRepo = %v, want %v", fromRepo, expected)
}
rc, err := src.Fetch(ctx, desc)
if err != nil {
t.Fatalf("Failed to fetch content: %v", err)
}
defer rc.Close()
err = dst.storage.Push(ctx, desc, rc) // bypass the counters
if err != nil {
t.Fatalf("Failed to push content: %v", err)
}
return nil
}
opts = oras.CopyGraphOptions{}
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
numPreCopy.Add(1)
return nil
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
numPostCopy.Add(1)
return nil
}
e := errors.New("onMounted error")
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error {
numOnMounted.Add(1)
return e
}
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
numMountFrom.Add(1)
return []string{"source"}, nil
}
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, e) {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, e)
}

if got, expected := dst.numExists.Load(), int64(7); got != expected {
t.Errorf("count(Exists()) = %d, want %d", got, expected)
}
if got, expected := dst.numFetch.Load(), int64(0); got != expected {
t.Errorf("count(Fetch()) = %d, want %d", got, expected)
}
if got, expected := dst.numPush.Load(), int64(0); got != expected {
t.Errorf("count(Push()) = %d, want %d", got, expected)
}
if got, expected := numMount.Load(), int64(4); got != expected {
t.Errorf("count(Mount()) = %d, want %d", got, expected)
}
if got, expected := numOnMounted.Load(), int64(4); got != expected {
t.Errorf("count(OnMounted()) = %d, want %d", got, expected)
}
if got, expected := numMountFrom.Load(), int64(4); got != expected {
t.Errorf("count(MountFrom()) = %d, want %d", got, expected)
}
if got, expected := numPreCopy.Load(), int64(0); got != expected {
t.Errorf("count(PreCopy()) = %d, want %d", got, expected)
}
if got, expected := numPostCopy.Load(), int64(0); got != expected {
t.Errorf("count(PostCopy()) = %d, want %d", got, expected)
}
})
}

// countingStorage counts the calls to its content.Storage methods
Expand Down

0 comments on commit ef5d68c

Please sign in to comment.