Skip to content

Commit ef5d68c

Browse files
committed
fix: added more test coverage
Signed-off-by: Kyle M. Tarplee <[email protected]>
1 parent 75b9a9f commit ef5d68c

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

copy.go

+2
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ func mountOrCopyNode(ctx context.Context, src content.ReadOnlyStorage, dst conte
284284

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

copy_test.go

+132-4
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ func TestCopyGraph_WithOptions(t *testing.T) {
14561456
},
14571457
}
14581458
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
1459-
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit)
1459+
t.Fatalf("CopyGraph() error = %v", err)
14601460
}
14611461

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

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

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

16851685
if got, expected := dst.numExists.Load(), int64(7); got != expected {
@@ -1709,6 +1709,134 @@ func TestCopyGraph_WithOptions(t *testing.T) {
17091709
t.Errorf("count(PostCopy()) = %d, want %d", got, expected)
17101710
}
17111711
})
1712+
1713+
t.Run("MountFrom empty sourceRepositories", func(t *testing.T) {
1714+
root = descs[6]
1715+
dst := &countingStorage{storage: cas.NewMemory()}
1716+
opts = oras.CopyGraphOptions{}
1717+
var numMountFrom atomic.Int64
1718+
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
1719+
numMountFrom.Add(1)
1720+
return nil, nil
1721+
}
1722+
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil {
1723+
t.Fatalf("CopyGraph() error = %v", err)
1724+
}
1725+
1726+
if got, expected := dst.numExists.Load(), int64(7); got != expected {
1727+
t.Errorf("count(Exists()) = %d, want %d", got, expected)
1728+
}
1729+
if got, expected := dst.numFetch.Load(), int64(0); got != expected {
1730+
t.Errorf("count(Fetch()) = %d, want %d", got, expected)
1731+
}
1732+
if got, expected := dst.numPush.Load(), int64(7); got != expected {
1733+
t.Errorf("count(Push()) = %d, want %d", got, expected)
1734+
}
1735+
if got, expected := numMountFrom.Load(), int64(4); got != expected {
1736+
t.Errorf("count(MountFrom()) = %d, want %d", got, expected)
1737+
}
1738+
})
1739+
1740+
t.Run("MountFrom error", func(t *testing.T) {
1741+
root = descs[6]
1742+
dst := &countingStorage{storage: cas.NewMemory()}
1743+
opts = oras.CopyGraphOptions{}
1744+
var numMountFrom atomic.Int64
1745+
e := errors.New("mountFrom error")
1746+
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
1747+
numMountFrom.Add(1)
1748+
return nil, e
1749+
}
1750+
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, e) {
1751+
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, e)
1752+
}
1753+
1754+
if got, expected := dst.numExists.Load(), int64(7); got != expected {
1755+
t.Errorf("count(Exists()) = %d, want %d", got, expected)
1756+
}
1757+
if got, expected := dst.numFetch.Load(), int64(0); got != expected {
1758+
t.Errorf("count(Fetch()) = %d, want %d", got, expected)
1759+
}
1760+
if got, expected := dst.numPush.Load(), int64(0); got != expected {
1761+
t.Errorf("count(Push()) = %d, want %d", got, expected)
1762+
}
1763+
if got, expected := numMountFrom.Load(), int64(4); got != expected {
1764+
t.Errorf("count(MountFrom()) = %d, want %d", got, expected)
1765+
}
1766+
})
1767+
1768+
t.Run("MountFrom OnMounted error", func(t *testing.T) {
1769+
root = descs[6]
1770+
dst := &countingStorage{storage: cas.NewMemory()}
1771+
var numMount atomic.Int64
1772+
dst.mount = func(ctx context.Context,
1773+
desc ocispec.Descriptor,
1774+
fromRepo string,
1775+
getContent func() (io.ReadCloser, error),
1776+
) error {
1777+
numMount.Add(1)
1778+
if expected := "source"; fromRepo != expected {
1779+
t.Fatalf("fromRepo = %v, want %v", fromRepo, expected)
1780+
}
1781+
rc, err := src.Fetch(ctx, desc)
1782+
if err != nil {
1783+
t.Fatalf("Failed to fetch content: %v", err)
1784+
}
1785+
defer rc.Close()
1786+
err = dst.storage.Push(ctx, desc, rc) // bypass the counters
1787+
if err != nil {
1788+
t.Fatalf("Failed to push content: %v", err)
1789+
}
1790+
return nil
1791+
}
1792+
opts = oras.CopyGraphOptions{}
1793+
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64
1794+
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
1795+
numPreCopy.Add(1)
1796+
return nil
1797+
}
1798+
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
1799+
numPostCopy.Add(1)
1800+
return nil
1801+
}
1802+
e := errors.New("onMounted error")
1803+
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error {
1804+
numOnMounted.Add(1)
1805+
return e
1806+
}
1807+
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
1808+
numMountFrom.Add(1)
1809+
return []string{"source"}, nil
1810+
}
1811+
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, e) {
1812+
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, e)
1813+
}
1814+
1815+
if got, expected := dst.numExists.Load(), int64(7); got != expected {
1816+
t.Errorf("count(Exists()) = %d, want %d", got, expected)
1817+
}
1818+
if got, expected := dst.numFetch.Load(), int64(0); got != expected {
1819+
t.Errorf("count(Fetch()) = %d, want %d", got, expected)
1820+
}
1821+
if got, expected := dst.numPush.Load(), int64(0); got != expected {
1822+
t.Errorf("count(Push()) = %d, want %d", got, expected)
1823+
}
1824+
if got, expected := numMount.Load(), int64(4); got != expected {
1825+
t.Errorf("count(Mount()) = %d, want %d", got, expected)
1826+
}
1827+
if got, expected := numOnMounted.Load(), int64(4); got != expected {
1828+
t.Errorf("count(OnMounted()) = %d, want %d", got, expected)
1829+
}
1830+
if got, expected := numMountFrom.Load(), int64(4); got != expected {
1831+
t.Errorf("count(MountFrom()) = %d, want %d", got, expected)
1832+
}
1833+
if got, expected := numPreCopy.Load(), int64(0); got != expected {
1834+
t.Errorf("count(PreCopy()) = %d, want %d", got, expected)
1835+
}
1836+
if got, expected := numPostCopy.Load(), int64(0); got != expected {
1837+
t.Errorf("count(PostCopy()) = %d, want %d", got, expected)
1838+
}
1839+
})
17121840
}
17131841

17141842
// countingStorage counts the calls to its content.Storage methods

0 commit comments

Comments
 (0)