| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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' |
| 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 |
|
| 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: |
| 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,): |
| 81 |
test_fail(f"regression: transfer aborted with RERR_PROTOCOL (code 2)\n{out}") |
| 82 |
|
| 83 |
if proc.returncode not in (0, 23, 24): |
| 84 |
test_fail(f"unexpected rsync exit {proc.returncode}\n{out}") |