Debian Codegraph @

testsuite/alt-dest_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/alt-dest.test.
3 #
4 # Exercise rsync's --compare-dest, --copy-dest and --link-dest
5 # alternative-destination options, both locally and across the lsh.sh
6 # remote-shell stand-in. Also covers the tmpfile path in copy_file() by
7 # pointing --copy-dest at a directory holding a same-name candidate.
8
9 import os
10 import shutil
11 import time
12
13 from rsyncfns import (
14 CHKDIR, FROMDIR, RSYNC, RSYNC_PEER, SCRATCHDIR, SRCDIR, TMPDIR, TODIR,
15 checkit, hands_setup, rmtree, run_rsync, test_fail, rsync_path_arg, rsh_cmd,
16 )
17
18
19 alt1dir = TMPDIR / 'alt1'
20 alt2dir = TMPDIR / 'alt2'
21 alt3dir = TMPDIR / 'alt3'
22
23 SSH = rsh_cmd()
24
25 hands_setup()
26
27 # Seed alt1 and alt2 with disjoint single-file subtrees of fromdir.
28 run_rsync('-av', '--include=text', '--include=*/', '--exclude=*',
29 f'{FROMDIR}/', f'{alt1dir}/')
30 run_rsync('-av', '--include=etc-ltr-list', '--include=*/', '--exclude=*',
31 f'{FROMDIR}/', f'{alt2dir}/')
32
33 # Create a side dir with one identically-named candidate so copy_file()'s
34 # tmpfile path gets exercised.
35 (FROMDIR / 'likely').write_text("This is a test file\n")
36 alt3dir.mkdir()
37 (alt3dir / 'likely').write_text("This is a test file\n")
38
39 time.sleep(1)
40 os.utime(FROMDIR / 'dir' / 'text')
41 os.utime(FROMDIR / 'likely')
42
43 # chkdir: what a vanilla copy would produce, minus /text and etc-ltr-list.
44 run_rsync('-av', '--exclude=/text', '--exclude=etc-ltr-list',
45 f'{FROMDIR}/', f'{CHKDIR}/')
46
47 # Stacked --compare-dest: dest grows just the deltas alt1+alt2 don't have.
48 checkit(['-avv', '--no-whole-file',
49 f'--compare-dest={alt1dir}', f'--compare-dest={alt2dir}',
50 f'{FROMDIR}/', f'{TODIR}/'], CHKDIR, TODIR)
51
52 rmtree(TODIR)
53 # Stacked --copy-dest: dest gets full copy because content can be hardlinked
54 # from the alt dirs where available.
55 checkit(['-avv', '--no-whole-file',
56 f'--copy-dest={alt1dir}', f'--copy-dest={alt2dir}',
57 f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR)
58
59 # Test that copy_file() works correctly with tmpfiles. Combine each of
60 # {direct, --inplace} with each of {local, remote-source, remote-dest}.
61 for maybe_inplace in ([], ['--inplace']):
62 rmtree(TODIR)
63 checkit(['-av', *maybe_inplace, f'--copy-dest={alt3dir}',
64 f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR)
65 # --copy-dest must COPY the unchanged candidate, not hard-link it: the
66 # result is a distinct inode from the alt-dir source (this is the property
67 # that distinguishes --copy-dest from --link-dest, which checkit's tree
68 # comparison alone does not capture).
69 if os.stat(TODIR / 'likely').st_ino == os.stat(alt3dir / 'likely').st_ino:
70 test_fail(f"--copy-dest{' --inplace' if maybe_inplace else ''} "
71 "hard-linked 'likely' instead of copying it")
72
73 for srchost in ('', 'localhost:'):
74 desthost = 'localhost:' if not srchost else ''
75 rmtree(TODIR)
76 checkit(['-ave', SSH, f'--rsync-path={rsync_path_arg()}', *maybe_inplace,
77 f'--copy-dest={alt3dir}',
78 f'{srchost}{FROMDIR}/', f'{desthost}{TODIR}/'],
79 FROMDIR, TODIR)