Debian Codegraph @

testsuite/batch-only-remove-source-regression_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A batch-only run must not remove a source it has not installed."""
3
4 import os
5 import subprocess
6
7 from rsyncfns import SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail
8
9
10 base = SCRATCHDIR / 'batch-only-remove-source-regression'
11 source = base / 'source'
12 destination = base / 'destination'
13 rmtree(base)
14 makepath(source / 'one' / 'two' / 'three')
15 makepath(destination / 'one' / 'two' / 'three')
16
17 source_file = source / 'one' / 'two' / 'three' / 'payload'
18 destination_file = destination / 'one' / 'two' / 'three' / 'payload'
19 source_bytes = b'replacement payload\n' * 31
20 destination_bytes = b'original receiver bytes\n' * 17
21 source_file.write_bytes(source_bytes)
22 destination_file.write_bytes(destination_bytes)
23 source_link_a = source / 'one' / 'two' / 'linked-a'
24 source_link_b = source / 'one' / 'two' / 'linked-b'
25 source_link_a.write_bytes(source_bytes)
26 os.link(source_link_a, source_link_b)
27 destination_link_a = destination / 'one' / 'two' / 'linked-a'
28 destination_link_b = destination / 'one' / 'two' / 'linked-b'
29 destination_link_a.write_bytes(destination_bytes)
30 destination_link_b.write_bytes(destination_bytes)
31
32 batch = base / 'transfer.batch'
33 write = subprocess.run(
34 rsync_argv('-aH', f'--only-write-batch={batch}',
35 '--remove-source-files', f'{source}/', f'{destination}/'),
36 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
37 if write.returncode != 0:
38 test_fail(f'batch-only transfer exited {write.returncode}:\n{write.stdout}')
39 if destination_file.read_bytes() != destination_bytes:
40 test_fail('--only-write-batch changed the destination before replay')
41 if (destination_link_a.read_bytes() != destination_bytes
42 or destination_link_b.read_bytes() != destination_bytes):
43 test_fail('--only-write-batch changed hard-linked destinations before replay')
44 if not batch.is_file() or batch.stat().st_size <= 4:
45 test_fail('--only-write-batch did not create a replayable batch file')
46 if not source_file.exists():
47 test_fail('--only-write-batch --remove-source-files deleted the nested '
48 'source before the batch was applied')
49 if source_file.read_bytes() != source_bytes:
50 test_fail('batch-only transfer changed the retained source')
51 if not source_link_a.exists() or not source_link_b.exists():
52 test_fail('--only-write-batch removed a hard-linked source before replay: '
53 f'linked-a={source_link_a.exists()} '
54 f'linked-b={source_link_b.exists()}')
55 if source_link_a.stat().st_ino != source_link_b.stat().st_ino:
56 test_fail('batch-only transfer broke the retained source hard link')
57
58 replay = subprocess.run(
59 rsync_argv('-aH', f'--read-batch={batch}', f'{destination}/'),
60 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
61 if replay.returncode != 0:
62 test_fail(f'batch replay exited {replay.returncode}:\n{replay.stdout}')
63 if destination_file.read_bytes() != source_bytes:
64 test_fail('the retained batch did not install the source payload')
65 if (destination_link_a.read_bytes() != source_bytes
66 or destination_link_b.read_bytes() != source_bytes):
67 test_fail('the retained batch did not install hard-linked payloads')
68 if destination_link_a.stat().st_ino != destination_link_b.stat().st_ino:
69 test_fail('the retained batch did not reproduce the source hard link')
70 if source_file.read_bytes() != source_bytes:
71 test_fail('batch replay changed or removed the source')
72
73 print('batch-only-remove-source-regression: source retained until batch replay')