Debian Codegraph @

testsuite/batch-mode_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/batch-mode.test.
3 #
4 # Test rsync's --write-batch / --read-batch / --only-write-batch flags,
5 # both for local transfers and for daemon source/destination.
6
7 import os
8 import shutil
9 import subprocess
10
11 from rsyncfns import (
12 CHKDIR, FROMDIR, SCRATCHDIR, TMPDIR, TODIR,
13 build_rsyncd_conf, checkit, hands_setup, rmtree,
14 run_rsync, start_test_daemon, test_fail,
15 )
16
17
18 DAEMON_PORT = 12874
19
20 conf = build_rsyncd_conf()
21 hands_setup()
22
23 os.chdir(TMPDIR)
24
25 # chkdir mirrors a normal transfer minus the daemon's foobar.baz exclude.
26 run_rsync('-av', '--exclude=foobar.baz', f'{FROMDIR}/', f'{CHKDIR}/')
27
28 # --only-write-batch must NOT create the destination directory.
29 run_rsync('-av', '--only-write-batch=BATCH', '--exclude=foobar.baz',
30 f'{FROMDIR}/', f'{TODIR}/missing/')
31 if (TODIR / 'missing').is_dir():
32 test_fail("--only-write-batch should not have created destination dir")
33
34 print("Test --read-batch (only):")
35 checkit(['-av', '--read-batch=BATCH', str(TODIR)], CHKDIR, TODIR)
36
37 # Wipe any leftover BATCH* files so the next pass starts clean.
38 rmtree(TODIR)
39 for batch in TMPDIR.glob('BATCH*'):
40 batch.unlink()
41
42 print("Test local --write-batch:")
43 checkit(['-av', '--write-batch=BATCH', f'{FROMDIR}/', str(TODIR)], FROMDIR, TODIR)
44
45 rmtree(TODIR)
46 print("Test --read-batch:")
47 checkit(['-av', '--read-batch=BATCH', str(TODIR)], FROMDIR, TODIR)
48
49 # Daemon variants: pipe transport by default, real loopback rsyncd under
50 # --use-tcp.
51 url = start_test_daemon(conf, DAEMON_PORT)
52
53 rmtree(TODIR)
54 print("Test daemon sender --write-batch:")
55 checkit(['-av', '--write-batch=BATCH',
56 f'{url}test-from/', str(TODIR)],
57 CHKDIR, TODIR, allowed_codes=(0, 23))
58
59 rmtree(TODIR)
60 print("Test --read-batch from daemon:")
61 checkit(['-av', '--read-batch=BATCH', str(TODIR)], CHKDIR, TODIR)
62
63 rmtree(TODIR)
64 print("Test BATCH.sh use of --read-batch:")
65 # BATCH.sh is the auto-generated wrapper script that re-applies the
66 # batch -- we run it directly, not via the rsync binary, then verify.
67 from rsyncfns import verify_dirs
68 proc = subprocess.run(['sh', './BATCH.sh'])
69 if proc.returncode != 0:
70 test_fail(f"BATCH.sh exited {proc.returncode}")
71 verify_dirs(CHKDIR, TODIR, label="BATCH.sh use of --read-batch")
72
73 print("Test do-nothing re-run of batch:")
74 proc = subprocess.run(['sh', './BATCH.sh'])
75 if proc.returncode != 0:
76 test_fail(f"BATCH.sh (re-run) exited {proc.returncode}")
77 verify_dirs(CHKDIR, TODIR, label="do-nothing re-run of batch")
78
79 rmtree(TODIR)
80 TODIR.mkdir()
81 print("Test daemon recv --write-batch:")
82 # ignore23 swallows the partial-transfer code 23 that daemon mode sometimes
83 # emits even on success.
84 ignore23 = SCRATCHDIR / 'ignore23'
85 # We pass ignore23 by inserting it ahead of the rsync invocation. checkit
86 # calls subprocess.run(rsync_argv(...)) directly, so do the run manually
87 # and call verify_dirs for the comparison.
88 from rsyncfns import rsync_argv
89 proc = subprocess.run(
90 [str(ignore23), *rsync_argv('-av', '--write-batch=BATCH',
91 f'{FROMDIR}/', f'{url}test-to')],
92 )
93 if proc.returncode != 0:
94 test_fail(f"daemon recv --write-batch exited {proc.returncode}")
95 verify_dirs(CHKDIR, TODIR, label="daemon recv --write-batch")