Debian Codegraph @

testsuite/backup-acl-xattr-cache_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage: cache_tmp_acl/cache_tmp_xattr (and cache_rsync_acl/uncache_duo_acls)
3 in backup.c make_backup_dir_tree().
4
5 When --backup --backup-dir=<dir> backs up a file at a nested path, rsync
6 recreates the intermediate directories under the backup dir and copies
7 each one's ACL/xattrs from the corresponding live directory. That copy
8 goes through cache_tmp_acl()/cache_tmp_xattr() (backup.c:~127/134), which
9 no other test reaches: backup_test.py doesn't pass -A/-X, and the
10 make_backup() per-file path normally short-circuits via link_or_rename()
11 on a same-fs backup dir, never reaching its own cache_tmp_* block.
12
13 Skips when the scratch fs lacks ACL or xattr support.
14 """
15
16 import os
17
18 from rsyncfns import (
19 SCRATCHDIR,
20 acls_supported, xattrs_supported, acl_set, xattr_set,
21 makepath, rmtree, rsync_argv, test_fail, test_skipped,
22 )
23 import subprocess
24
25 if not acls_supported():
26 test_skipped("ACLs not supported on this filesystem")
27 if not xattrs_supported():
28 test_skipped("xattrs not supported on this filesystem")
29
30 src = SCRATCHDIR / 'bak-src'
31 dest = SCRATCHDIR / 'bak-dest'
32 bak = SCRATCHDIR / 'bak-dir'
33 rmtree(src); rmtree(dest); rmtree(bak)
34 makepath(src / 'd1' / 'd2', dest / 'd1' / 'd2', bak)
35
36 # Stage: dest has an OLDER d1/d2/file than src, so the next push backs it up.
37 (src / 'd1' / 'd2' / 'file').write_bytes(b'NEW')
38 (dest / 'd1' / 'd2' / 'file').write_bytes(b'OLD')
39 os.utime(dest / 'd1' / 'd2' / 'file', (1_600_000_000, 1_600_000_000))
40
41 # Decorate the LIVE dest dirs with an ACL and an xattr -- these are what
42 # make_backup_dir_tree() reads via get_acl()/get_xattr() and passes through
43 # cache_tmp_acl()/cache_tmp_xattr() when it mkdirs bak/d1 and bak/d2.
44 for d in (dest / 'd1', dest / 'd1' / 'd2'):
45 if not acl_set('u:0:rwx', d):
46 test_skipped(f"setfacl failed on {d}")
47 xattr_set('user.bak-cache', 'v', d)
48
49 r = subprocess.run(
50 rsync_argv('-aAX', '--backup', f'--backup-dir={bak}', f'{src}/', f'{dest}/'),
51 capture_output=True, text=True,
52 )
53 if r.returncode != 0:
54 test_fail(f"-aAX --backup --backup-dir push -> rc={r.returncode}\n{r.stderr}")
55
56 # Backed-up file present, and the intermediate backup dirs were created.
57 bf = bak / 'd1' / 'd2' / 'file'
58 if not bf.is_file() or bf.read_bytes() != b'OLD':
59 test_fail(f"backup file missing or wrong content: {bf}")
60 for d in (bak / 'd1', bak / 'd1' / 'd2'):
61 if not d.is_dir():
62 test_fail(f"backup intermediate dir not created: {d}")
63
64 # Coverage goal: cache_tmp_acl/cache_tmp_xattr were *entered* (which the
65 # structural checks above plus -A -X on a nested backup path guarantee --
66 # verified via gcov: backup.c:127/134 each fire 2x for d1 and d2). Whether
67 # set_file_attrs() then propagates the cached ACL/xattr onto the freshly-
68 # mkdired backup dir is a separate behavioural question we deliberately do
69 # NOT assert here, since it depends on set_file_attrs's compare-against-dest
70 # path and the answer is the same with or without this test.
71
72 # And dest/d1/d2/file is the new content.
73 if (dest / 'd1' / 'd2' / 'file').read_bytes() != b'NEW':
74 test_fail("dest/d1/d2/file not updated to NEW")
75
76 print("backup-acl-xattr-cache: make_backup_dir_tree cache_tmp_acl/xattr -> "
77 "bak/d1, bak/d2 created with propagated ACL+xattr")