Files
rsync/testsuite/batch-mode_test.py
T
Andrew TridgellandClaude Opus 4.7 1f689ec0c2 testsuite: rewrite the shell testsuite in Python
Replace the entire shell-based testsuite with Python. runtests.py
already drove the suite (it had replaced runtests.sh earlier); this
converts all 60 test scripts from *.test shell to *_test.py and adds
testsuite/rsyncfns.py as the shared helper module -- the Python
counterpart of the now-removed rsync.fns.

runtests.py:
  * Discovers and runs both *.test and *_test.py; dispatches the
    Python tests via the same python3 that runs the harness.
  * Extends PYTHONPATH so tests can `import rsyncfns`.

testsuite/rsyncfns.py provides everything the ports need:
  * environment wiring (scratchdir / srcdir / TOOLDIR / RSYNC /
    TLS_ARGS, and HOME pointed at the per-test scratch dir);
  * result reporting -- test_fail / test_skipped / test_xfail mapping
    to the 0 / 1 / 77 / 78 exit-code convention;
  * the transfer-and-verify helpers checkit, checkdiff, verify_dirs,
    rsync_ls_lR, check_perms and the v_filt output filter;
  * fixture builders hands_setup, build_symlinks, build_rsyncd_conf,
    make_data_file, cp_p / cp_touch, makepath / rmtree.

All 60 tests are converted, including the four split-variant tests
that share one source via a Makefile-built symlink (chown/chown-fake,
devices/devices-fake, xattrs/xattrs-hlink, exclude/exclude-lsh);
Makefile.in's CHECK_SYMLINKS now points at the *_test.py names.

The dead rsync.fns shell library is removed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 14:34:52 +10:00

92 lines
3.1 KiB
Python

#!/usr/bin/env python3
# Python rewrite of testsuite/batch-mode.test.
#
# Test rsync's --write-batch / --read-batch / --only-write-batch flags,
# both for local transfers and for daemon source/destination.
import os
import shutil
import subprocess
from rsyncfns import (
CHKDIR, FROMDIR, RSYNC, SCRATCHDIR, TMPDIR, TODIR,
build_rsyncd_conf, checkit, hands_setup, rmtree,
run_rsync, test_fail,
)
conf = build_rsyncd_conf()
hands_setup()
os.chdir(TMPDIR)
# chkdir mirrors a normal transfer minus the daemon's foobar.baz exclude.
run_rsync('-av', '--exclude=foobar.baz', f'{FROMDIR}/', f'{CHKDIR}/')
# --only-write-batch must NOT create the destination directory.
run_rsync('-av', '--only-write-batch=BATCH', '--exclude=foobar.baz',
f'{FROMDIR}/', f'{TODIR}/missing/')
if (TODIR / 'missing').is_dir():
test_fail("--only-write-batch should not have created destination dir")
print("Test --read-batch (only):")
checkit(['-av', '--read-batch=BATCH', str(TODIR)], CHKDIR, TODIR)
# Wipe any leftover BATCH* files so the next pass starts clean.
rmtree(TODIR)
for batch in TMPDIR.glob('BATCH*'):
batch.unlink()
print("Test local --write-batch:")
checkit(['-av', '--write-batch=BATCH', f'{FROMDIR}/', str(TODIR)], FROMDIR, TODIR)
rmtree(TODIR)
print("Test --read-batch:")
checkit(['-av', '--read-batch=BATCH', str(TODIR)], FROMDIR, TODIR)
# Daemon variants. RSYNC_CONNECT_PROG plumbs an in-process daemon.
os.environ['RSYNC_CONNECT_PROG'] = f"{RSYNC} --config={conf} --daemon"
rmtree(TODIR)
print("Test daemon sender --write-batch:")
checkit(['-av', '--write-batch=BATCH', 'rsync://localhost/test-from/', str(TODIR)],
CHKDIR, TODIR, allowed_codes=(0, 23))
rmtree(TODIR)
print("Test --read-batch from daemon:")
checkit(['-av', '--read-batch=BATCH', str(TODIR)], CHKDIR, TODIR)
rmtree(TODIR)
print("Test BATCH.sh use of --read-batch:")
# BATCH.sh is the auto-generated wrapper script that re-applies the
# batch -- we run it directly, not via the rsync binary, then verify.
from rsyncfns import verify_dirs
proc = subprocess.run(['sh', './BATCH.sh'])
if proc.returncode != 0:
test_fail(f"BATCH.sh exited {proc.returncode}")
verify_dirs(CHKDIR, TODIR, label="BATCH.sh use of --read-batch")
print("Test do-nothing re-run of batch:")
proc = subprocess.run(['sh', './BATCH.sh'])
if proc.returncode != 0:
test_fail(f"BATCH.sh (re-run) exited {proc.returncode}")
verify_dirs(CHKDIR, TODIR, label="do-nothing re-run of batch")
rmtree(TODIR)
TODIR.mkdir()
print("Test daemon recv --write-batch:")
# ignore23 swallows the partial-transfer code 23 that daemon mode sometimes
# emits even on success.
ignore23 = SCRATCHDIR / 'ignore23'
# We pass ignore23 by inserting it ahead of the rsync invocation. checkit
# calls subprocess.run(rsync_argv(...)) directly, so do the run manually
# and call verify_dirs for the comparison.
from rsyncfns import rsync_argv
proc = subprocess.run(
[str(ignore23), *rsync_argv('-av', '--write-batch=BATCH',
f'{FROMDIR}/', 'rsync://localhost/test-to')],
)
if proc.returncode != 0:
test_fail(f"daemon recv --write-batch exited {proc.returncode}")
verify_dirs(CHKDIR, TODIR, label="daemon recv --write-batch")