Debian Codegraph @

integration/fsfs/fsfs_test.go

raw symbols SCIP
1 package fsfs_test
2
3 import (
4 "log"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "testing"
9 "testing/fstest"
10
11 "github.com/gokrazy/rsync/internal/rsynctest"
12 "github.com/gokrazy/rsync/internal/testlogger"
13 "github.com/gokrazy/rsync/rsyncd"
14 "github.com/google/go-cmp/cmp"
15 )
16
17 func TestMain(m *testing.M) {
18 if err := rsynctest.CommandMain(m); err != nil {
19 log.Fatal(err)
20 }
21 }
22
23 func TestMapFS(t *testing.T) {
24 t.Parallel()
25
26 tmp := t.TempDir()
27 dest := filepath.Join(tmp, "dest")
28
29 memfs := fstest.MapFS{
30 "hello.txt": &fstest.MapFile{
31 Data: []byte("world"),
32 Mode: 0o644,
33 ModTime: rsynctest.GosPublicRelease,
34 },
35 "bye.txt": &fstest.MapFile{
36 Data: []byte("moon"),
37 Mode: 0o644,
38 ModTime: rsynctest.GosPublicRelease,
39 },
40 }
41
42 srv := rsynctest.NewInMemory(t, rsyncd.Module{
43 Name: "memfs",
44 FS: memfs,
45 })
46 args := []string{"-av"}
47 srv.RunClient(t, args, "./", []string{dest + "/"})
48
49 {
50 want := []byte("world")
51 got, err := os.ReadFile(filepath.Join(dest, "hello.txt"))
52 if err != nil {
53 t.Fatal(err)
54 }
55 if diff := cmp.Diff(want, got); diff != "" {
56 t.Fatalf("hello.txt: unexpected file contents: diff (-want +got):\n%s", diff)
57 }
58 }
59 {
60 want := []byte("moon")
61 got, err := os.ReadFile(filepath.Join(dest, "bye.txt"))
62 if err != nil {
63 t.Fatal(err)
64 }
65 if diff := cmp.Diff(want, got); diff != "" {
66 t.Fatalf("world.txt: unexpected file contents: diff (-want +got):\n%s", diff)
67 }
68 }
69
70 // Restore write permission so that t.TempDir() cleanup succeeds
71 if err := os.Chmod(dest, 0755); err != nil {
72 t.Fatal(err)
73 }
74 }
75
76 func TestMapFSSubdirNested(t *testing.T) {
77 t.Parallel()
78
79 tmp := t.TempDir()
80 dest := filepath.Join(tmp, "dest")
81
82 memfs := fstest.MapFS{
83 "treasure/expensive/hello.txt": &fstest.MapFile{
84 Data: []byte("world"),
85 Mode: 0o644,
86 ModTime: rsynctest.GosPublicRelease,
87 },
88 }
89
90 srv := rsynctest.New(t, []rsyncd.Module{
91 {
92 Name: "memfs",
93 FS: memfs,
94 },
95 })
96 // sync into dest dir
97 rsync := exec.Command(rsynctest.AnyRsync(t),
98 append(
99 []string{
100 // "--debug=all4",
101 "--archive",
102 "-v", "-v", "-v", "-v",
103 "--port=" + srv.Port,
104 "rsync://localhost/memfs/treasure/expensive/", // copy contents of expensive
105 },
106 filepath.Base(dest))...)
107 rsync.Dir = filepath.Dir(dest)
108 rsync.Stdout = testlogger.New(t)
109 rsync.Stderr = testlogger.New(t)
110 if err := rsync.Run(); err != nil {
111 t.Fatalf("%v: %v", rsync.Args, err)
112 }
113
114 {
115 want := []byte("world")
116 got, err := os.ReadFile(filepath.Join(dest, "hello.txt"))
117 if err != nil {
118 t.Fatal(err)
119 }
120 if diff := cmp.Diff(want, got); diff != "" {
121 t.Fatalf("hello.txt: unexpected file contents: diff (-want +got):\n%s", diff)
122 }
123 }
124
125 // Restore write permission so that t.TempDir() cleanup succeeds
126 if err := os.Chmod(dest, 0755); err != nil {
127 t.Fatal(err)
128 }
129 }
130
131 func TestMapFSSubdirNestedNoSlash(t *testing.T) {
132 t.Parallel()
133
134 tmp := t.TempDir()
135 dest := filepath.Join(tmp, "dest")
136
137 memfs := fstest.MapFS{
138 "treasure/expensive/hello.txt": &fstest.MapFile{
139 Data: []byte("world"),
140 Mode: 0o644,
141 ModTime: rsynctest.GosPublicRelease,
142 },
143 }
144
145 srv := rsynctest.New(t, []rsyncd.Module{
146 {
147 Name: "memfs",
148 FS: memfs,
149 },
150 })
151 // sync into dest dir
152 rsync := exec.Command(rsynctest.AnyRsync(t),
153 append(
154 []string{
155 // "--debug=all4",
156 "--archive",
157 "-v", "-v", "-v", "-v",
158 "--port=" + srv.Port,
159 "rsync://localhost/memfs/treasure/expensive", // copy expensive
160 },
161 filepath.Base(dest))...)
162 rsync.Dir = filepath.Dir(dest)
163 rsync.Stdout = testlogger.New(t)
164 rsync.Stderr = testlogger.New(t)
165 if err := rsync.Run(); err != nil {
166 t.Fatalf("%v: %v", rsync.Args, err)
167 }
168
169 {
170 want := []byte("world")
171 got, err := os.ReadFile(filepath.Join(dest, "expensive", "hello.txt"))
172 if err != nil {
173 t.Fatal(err)
174 }
175 if diff := cmp.Diff(want, got); diff != "" {
176 t.Fatalf("hello.txt: unexpected file contents: diff (-want +got):\n%s", diff)
177 }
178 }
179
180 // Restore write permission so that t.TempDir() cleanup succeeds
181 if err := os.Chmod(dest, 0755); err != nil {
182 t.Fatal(err)
183 }
184 if err := os.Chmod(filepath.Join(dest, "expensive"), 0755); err != nil {
185 t.Fatal(err)
186 }
187 }
188
189 func TestMapFSSingleFile(t *testing.T) {
190 t.Parallel()
191
192 tmp := t.TempDir()
193 dest := filepath.Join(tmp, "dest")
194
195 memfs := fstest.MapFS{
196 "expensive/hello.txt": &fstest.MapFile{
197 Data: []byte("world"),
198 Mode: 0o644,
199 ModTime: rsynctest.GosPublicRelease,
200 },
201 }
202
203 srv := rsynctest.NewInMemory(t, rsyncd.Module{
204 Name: "memfs",
205 FS: memfs,
206 })
207 args := []string{"-av"}
208 srv.RunClient(t, args, "./expensive/hello.txt", []string{dest + "/"})
209
210 {
211 want := []byte("world")
212 got, err := os.ReadFile(filepath.Join(dest, "hello.txt"))
213 if err != nil {
214 t.Fatal(err)
215 }
216 if diff := cmp.Diff(want, got); diff != "" {
217 t.Fatalf("hello.txt: unexpected file contents: diff (-want +got):\n%s", diff)
218 }
219 }
220
221 // Restore write permission so that t.TempDir() cleanup succeeds
222 if err := os.Chmod(dest, 0755); err != nil {
223 t.Fatal(err)
224 }
225 }
226
227 func TestMapFSErrors(t *testing.T) {
228 t.Parallel()
229
230 memfs := fstest.MapFS{
231 "hello.txt": &fstest.MapFile{
232 Data: []byte("world"),
233 Mode: 0o644,
234 ModTime: rsynctest.GosPublicRelease,
235 },
236 }
237 _, err := rsyncd.NewServer([]rsyncd.Module{
238 {
239 Name: "test",
240 FS: memfs,
241 Writable: true,
242 },
243 })
244 if err == nil {
245 t.Fatal("expected error for writable FS module, got nil")
246 }
247
248 _, err = rsyncd.NewServer([]rsyncd.Module{
249 {
250 Name: "test",
251 Path: "/",
252 FS: memfs,
253 },
254 })
255 if err == nil {
256 t.Fatal("expected error for module with Path and FS, got nil")
257 }
258 }
259
260 func TestMapFSLargeFile(t *testing.T) {
261 t.Parallel()
262
263 tmp := t.TempDir()
264 dest := filepath.Join(tmp, "dest")
265 destLarge := filepath.Join(dest, "large.bin")
266
267 headPattern := []byte{0x11}
268 bodyPattern := []byte{0xbb}
269 endPattern := []byte{0xee}
270 large := rsynctest.ConstructLargeDataFile(headPattern, bodyPattern, endPattern)
271
272 memfs := fstest.MapFS{
273 "hello.txt": &fstest.MapFile{
274 Data: []byte("world"),
275 Mode: 0o644,
276 ModTime: rsynctest.GosPublicRelease,
277 },
278 "large.bin": &fstest.MapFile{
279 Data: large,
280 Mode: 0o644,
281 ModTime: rsynctest.GosPublicRelease,
282 },
283 }
284
285 srv := rsynctest.NewInMemory(t, rsyncd.Module{
286 Name: "memfs",
287 FS: memfs,
288 })
289 args := []string{"-av"}
290 firstStats := srv.RunClient(t, args, "./", []string{dest})
291 t.Logf("firstStats: %+v", firstStats)
292
293 {
294 want := []byte("world")
295 got, err := os.ReadFile(filepath.Join(dest, "hello.txt"))
296 if err != nil {
297 t.Fatal(err)
298 }
299 if diff := cmp.Diff(want, got); diff != "" {
300 t.Fatalf("hello.txt: unexpected file contents: diff (-want +got):\n%s", diff)
301 }
302 }
303
304 if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil {
305 t.Fatal(err)
306 }
307
308 // Change the middle of the large data file:
309 bodyPattern = []byte{0x66}
310 // modify the large data file in memory
311 large = rsynctest.ConstructLargeDataFile(headPattern, bodyPattern, endPattern)
312 memfs["large.bin"].Data = large
313
314 incrementalStats := srv.RunClient(t, args, "./", []string{dest})
315 t.Logf("incrementalStats: %+v", incrementalStats)
316 if got, want := incrementalStats.Written, int64(2*1024*1024); got >= want {
317 t.Fatalf("rsync unexpectedly transferred more data than needed: got %d, want < %d", got, want)
318 }
319
320 // Restore write permission so that t.TempDir() cleanup succeeds
321 if err := os.Chmod(dest, 0755); err != nil {
322 t.Fatal(err)
323 }
324 }