1
16
17 package fs
18
19 import (
20 _ "crypto/sha256"
21 "fmt"
22 "os"
23 "testing"
24 "time"
25
26 "github.com/containerd/continuity/fs/fstest"
27 )
28
29
30
31
32
33
34 func TestCopyDirectory(t *testing.T) {
35 apply := fstest.Apply(
36 fstest.CreateDir("/etc/", 0o755),
37 fstest.CreateFile("/etc/hosts", []byte("localhost 127.0.0.1"), 0o644),
38 fstest.Link("/etc/hosts", "/etc/hosts.allow"),
39 fstest.CreateDir("/usr/local/lib", 0o755),
40 fstest.CreateFile("/usr/local/lib/libnothing.so", []byte{0x00, 0x00}, 0o755),
41 fstest.Symlink("libnothing.so", "/usr/local/lib/libnothing.so.2"),
42 fstest.CreateDir("/home", 0o755),
43 )
44
45 if err := testCopy(t, apply); err != nil {
46 t.Fatalf("Copy test failed: %+v", err)
47 }
48 }
49
50
51
52
53 func TestCopyDirectoryWithLocalSymlink(t *testing.T) {
54 apply := fstest.Apply(
55 fstest.CreateFile("nothing.txt", []byte{0x00, 0x00}, 0o755),
56 fstest.Symlink("nothing.txt", "link-no-nothing.txt"),
57 )
58
59 if err := testCopy(t, apply); err != nil {
60 t.Fatalf("Copy test failed: %+v", err)
61 }
62 }
63
64
65 func TestCopyWithLargeFile(t *testing.T) {
66 if testing.Short() {
67 t.SkipNow()
68 }
69 apply := fstest.Apply(
70 fstest.CreateDir("/banana", 0o755),
71 fstest.CreateRandomFile("/banana/split", time.Now().UnixNano(), 3*1024*1024*1024, 0o644),
72 )
73
74 if err := testCopy(t, apply); err != nil {
75 t.Fatal(err)
76 }
77 }
78
79 func testCopy(t testing.TB, apply fstest.Applier) error {
80 t1 := t.TempDir()
81 t2 := t.TempDir()
82
83 if err := apply.Apply(t1); err != nil {
84 return fmt.Errorf("failed to apply changes: %w", err)
85 }
86
87 if err := CopyDir(t2, t1); err != nil {
88 return fmt.Errorf("failed to copy: %w", err)
89 }
90
91 return fstest.CheckDirectoryEqual(t1, t2)
92 }
93
94 func BenchmarkLargeCopy100MB(b *testing.B) {
95 benchmarkLargeCopyFile(b, 100*1024*1024)
96 }
97
98 func BenchmarkLargeCopy1GB(b *testing.B) {
99 benchmarkLargeCopyFile(b, 1024*1024*1024)
100 }
101
102 func benchmarkLargeCopyFile(b *testing.B, size int64) {
103 b.StopTimer()
104 base := b.TempDir()
105 apply := fstest.Apply(
106 fstest.CreateRandomFile("/large", time.Now().UnixNano(), size, 0o644),
107 )
108 if err := apply.Apply(base); err != nil {
109 b.Fatal("failed to apply changes:", err)
110 }
111
112 for i := 0; i < b.N; i++ {
113 copied := b.TempDir()
114 b.StartTimer()
115 if err := CopyDir(copied, base); err != nil {
116 b.Fatal("failed to copy:", err)
117 }
118 b.StopTimer()
119 if i == 0 {
120 if err := fstest.CheckDirectoryEqual(base, copied); err != nil {
121 b.Fatal("check failed:", err)
122 }
123 }
124 os.RemoveAll(copied)
125 }
126 }
127
View as plain text