Debian Codegraph @

testsuite/partial_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage of --partial / --partial-dir at depth and across directory
3 boundaries.
4
5 --partial keeps a partially transferred file so a later run can resume it.
6 --partial-dir=DIR keeps the partial in DIR instead of the destination file:
7 a RELATIVE dir is created in (and removed from) each destination file's own
8 directory; an ABSOLUTE dir is a reserved location that holds partials by
9 basename. All of this is parent- and cross-directory path resolution -- what
10 the resolver restructure rewrites -- so exercise it on a file several levels
11 deep, with the absolute partial-dir kept OUTSIDE the destination tree.
12 """
13
14 import os
15 import signal
16 import subprocess
17 import time
18
19 from rsyncfns import (
20 FROMDIR, SCRATCHDIR, TODIR,
21 assert_same, make_data_file, makepath, rmtree, rsync_argv, run_rsync,
22 test_fail,
23 )
24
25 src = FROMDIR
26 deepdir = os.path.join('d1', 'd2', 'd3')
27 deep = os.path.join(deepdir, 'f3')
28 FULL = 12_000_000
29
30
31 def seed_big():
32 rmtree(src)
33 rmtree(TODIR)
34 makepath(src / deepdir)
35 make_data_file(src / deep, FULL)
36
37
38 def is_prefix(partial) -> bool:
39 pb = partial.read_bytes()
40 return 0 < len(pb) < FULL and (src / deep).read_bytes()[:len(pb)] == pb
41
42
43 def interrupt_transfer(extra_args, partial_path):
44 """Start a deliberately slow transfer, SIGTERM it once the receiver's
45 in-progress temp (.f3.XXXXXX) has some data, and wait for `partial_path`
46 (where this mode keeps the partial) to materialise.
47
48 The bandwidth limit is low so the multi-second transfer cannot finish
49 before we interrupt it -- important under a loaded parallel run (-j16),
50 where the polling loop can lag by seconds. We then poll for the partial,
51 since rsync moves it into place from its exit_cleanup handler."""
52 proc = subprocess.Popen(
53 rsync_argv('-a', '--no-whole-file', '--bwlimit=400', *extra_args,
54 f'{src}/', f'{TODIR}/'),
55 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
56 tdir = TODIR / deepdir
57 caught = False
58 deadline = time.monotonic() + 30
59 while time.monotonic() < deadline:
60 if proc.poll() is not None:
61 break # exited before we caught it
62 if tdir.is_dir():
63 temps = [p for p in tdir.glob('.f3.*')
64 if p.is_file() and p.stat().st_size > 0]
65 if temps:
66 caught = True
67 break
68 time.sleep(0.02)
69 proc.send_signal(signal.SIGTERM)
70 proc.wait()
71 if not caught:
72 test_fail("never caught an in-progress temp (transfer finished too "
73 "fast to interrupt)")
74 # rsync moves the partial into place from exit_cleanup; give it a moment.
75 pdeadline = time.monotonic() + 5
76 while time.monotonic() < pdeadline:
77 if partial_path.is_file() and partial_path.stat().st_size > 0:
78 return
79 time.sleep(0.05)
80
81
82 # --- 1. --partial (no dir): partial kept in the dest file itself, at depth --
83 seed_big()
84 interrupt_transfer(['--partial'], TODIR / deep)
85 if not (TODIR / deep).is_file() or not is_prefix(TODIR / deep):
86 test_fail("--partial did not leave a valid partial in the dest file")
87 run_rsync('-a', '--partial', '--no-whole-file', f'{src}/', f'{TODIR}/')
88 assert_same(TODIR / deep, src / deep, label='--partial resume')
89
90 # --- 2. relative --partial-dir at depth: deterministic clean pre-seed -------
91 rmtree(src)
92 rmtree(TODIR)
93 makepath(src / deepdir, TODIR / deepdir / '.rsync-partial')
94 make_data_file(src / deep, 1_000_000)
95 full = (src / deep).read_bytes()
96 (TODIR / deepdir / '.rsync-partial' / 'f3').write_bytes(full[:400_000])
97 run_rsync('-a', '--partial-dir=.rsync-partial', '--no-whole-file',
98 f'{src}/', f'{TODIR}/')
99 assert_same(TODIR / deep, src / deep, label='rel partial-dir preseed')
100 if (TODIR / deepdir / '.rsync-partial').exists():
101 test_fail("relative --partial-dir not removed after the partial was used")
102
103 # --- 3. relative --partial-dir at depth: interrupt then resume -------------
104 seed_big()
105 part = TODIR / deepdir / '.rsync-partial' / 'f3'
106 interrupt_transfer(['--partial-dir=.rsync-partial'], part)
107 if not part.is_file() or not is_prefix(part):
108 test_fail("relative --partial-dir did not keep a valid partial at depth")
109 run_rsync('-a', '--partial-dir=.rsync-partial', '--no-whole-file',
110 f'{src}/', f'{TODIR}/')
111 assert_same(TODIR / deep, src / deep, label='rel partial-dir resume')
112
113 # --- 4. absolute --partial-dir OUTSIDE the tree (cross-dir): interrupt write -
114 ext = SCRATCHDIR / 'partials' # sibling of from/ and to/ -- outside both
115 rmtree(ext)
116 ext.mkdir()
117 seed_big()
118 interrupt_transfer([f'--partial-dir={ext}'], ext / 'f3')
119 if not (ext / 'f3').is_file() or not is_prefix(ext / 'f3'):
120 test_fail("absolute --partial-dir did not write the partial to the "
121 "outside-tree dir")
122
123 # --- 5. absolute --partial-dir delta resume completes (regression guard) ----
124 # A delta (--no-whole-file) resume from an absolute, outside-tree partial-dir
125 # used to fail whole-file verification forever: the receiver couldn't open the
126 # absolute basis, so matched blocks were dropped from the verify checksum.
127 rmtree(src)
128 rmtree(TODIR)
129 rmtree(ext)
130 makepath(src / deepdir, ext)
131 make_data_file(src / deep, 1_000_000)
132 (ext / 'f3').write_bytes((src / deep).read_bytes()[:400_000]) # clean prefix
133 run_rsync('-a', f'--partial-dir={ext}', '--no-whole-file', f'{src}/', f'{TODIR}/')
134 assert_same(TODIR / deep, src / deep, label='abs partial-dir delta resume')
135 if (ext / 'f3').exists():
136 test_fail("absolute --partial-dir basis was not consumed after a "
137 "successful delta resume")
138
139 print("partial: --partial + relative/absolute --partial-dir verified at depth")