Debian Codegraph @

testsuite/mutatefns.py

raw symbols SCIP
1 # Shared helper for "resource changes while being transferred" tests.
2 #
3 # rsync scans the file list first, then sends file data and applies metadata.
4 # A resource (content, size, perms, owner, mtime, xattr, ACL) mutated in that
5 # window must not abort the whole transfer or corrupt the destination -- rsync
6 # should transfer what it can and, at worst, warn. The growing-file regression
7 # (a hardening check that turned a benign mid-transfer append into a fatal
8 # RERR_PROTOCOL) showed this whole class was untested.
9 #
10 # The reproduction is deterministic: a big 'aaa_pacer' file sorts first and is
11 # throttled with --bwlimit, so a later 'zzz_target' entry is sent well after the
12 # flist is built (--no-inc-recursive locks all sizes up front) but before its
13 # own data/metadata is processed. A background thread performs the mutation in
14 # that window.
15
16 import os
17 import subprocess
18 import threading
19 import time
20
21 from rsyncfns import SCRATCHDIR, make_data_file, rmtree, rsync_argv, test_fail
22
23 TARGET = 'zzz_target' # sorts after aaa_pacer -> processed second
24
25
26 def run_mutating_transfer(setup, mutate, *, extra_rsync=('-a',),
27 pacer_bytes=6 * 1024 * 1024, bwlimit=1500, delay=0.6):
28 """Run a local rsync while `mutate` runs in the transfer window.
29
30 setup(src_dir) -- create the target resource(s); called before rsync.
31 mutate(src_dir) -- perform the mid-transfer change; called ~delay s in.
32 extra_rsync -- rsync flags (default -a).
33
34 Returns (proc, src, dst). Fails the test if the mutation never ran.
35 """
36 base = SCRATCHDIR / 'mutate'
37 rmtree(base)
38 src = base / 'src'
39 dst = base / 'dst'
40 src.mkdir(parents=True)
41 dst.mkdir(parents=True)
42
43 # The throttled pacer that keeps the transfer alive during the mutation.
44 make_data_file(src / 'aaa_pacer', pacer_bytes)
45 setup(src)
46
47 done = threading.Event()
48 err = {}
49
50 def worker():
51 time.sleep(delay)
52 try:
53 mutate(src)
54 except Exception as e: # noqa: BLE001 - report it
55 err['exc'] = e
56 finally:
57 done.set()
58
59 t = threading.Thread(target=worker)
60 t.start()
61 proc = subprocess.run(
62 rsync_argv(*extra_rsync, '--no-inc-recursive', f'--bwlimit={bwlimit}',
63 f'{src}/', f'{dst}/'),
64 capture_output=True, text=True)
65 t.join()
66
67 if not done.is_set():
68 test_fail("test setup: the mutation thread did not run")
69 if 'exc' in err:
70 test_fail(f"test setup: mutation raised {err['exc']!r}")
71 return proc, src, dst
72
73
74 def assert_no_protocol_abort(proc):
75 """rsync must not have torn the whole transfer down with a protocol error."""
76 out = proc.stdout + proc.stderr
77 if 'received more data than file length' in out:
78 test_fail("regression: mid-transfer change aborted the run with "
79 f"'received more data than file length'\n{out}")
80 if proc.returncode in (2,): # RERR_PROTOCOL
81 test_fail(f"regression: transfer aborted with RERR_PROTOCOL (code 2)\n{out}")
82 # 0 = OK, 23/24 = partial/vanished (acceptable for a changing source).
83 if proc.returncode not in (0, 23, 24):
84 test_fail(f"unexpected rsync exit {proc.returncode}\n{out}")