Debian Codegraph @

testsuite/open-noatime_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/open-noatime.test.
3 #
4 # Test that rsync --open-noatime keeps the source atime intact across the
5 # transfer. --open-noatime did not work properly on files with size > 0
6 # at one point, so the test uses a non-empty source file.
7
8 import datetime
9 import os
10 import platform
11 import shlex
12 import subprocess
13
14 import rsyncfns
15 from rsyncfns import FROMDIR, TMPDIR, TODIR, TOOLDIR, run_rsync, test_fail, test_skipped
16
17
18 vv = run_rsync('-VV', check=True, capture_output=True)
19 if '"atimes": true' not in vv.stdout:
20 test_skipped("Rsync is configured without atimes support")
21
22 # O_NOATIME is Linux-specific.
23 if platform.system() != 'Linux':
24 test_skipped("O_NOATIME is only supported on Linux")
25
26 FROMDIR.mkdir(parents=True, exist_ok=True)
27 foo = FROMDIR / 'foo'
28 foo.write_text("content\n")
29
30 # Pin the source atime to a known historical value (mtime preserved).
31 atime = datetime.datetime(2001, 2, 3, 17, 17, 42).timestamp()
32 mtime = foo.stat().st_mtime
33 os.utime(foo, (atime, mtime))
34
35 rsyncfns.TLS_ARGS = '--atimes'
36
37 # Capture the atime of the source via tls BEFORE the rsync run.
38 def _tls_listing(path: str) -> str:
39 cmd = [str(TOOLDIR / 'tls')] + shlex.split(rsyncfns.TLS_ARGS) + [str(path)]
40 return subprocess.check_output(cmd, text=True)
41
42
43 before = _tls_listing(foo)
44 (TMPDIR / 'atime-from-before').write_text(before)
45
46 # Don't use checkit() here -- the file-content diff it does would update
47 # atimes on the source and defeat the test.
48 run_rsync('--open-noatime', '--archive', '--recursive', '--times',
49 '--atimes', '-vvv', f'{FROMDIR}/', f'{TODIR}/')
50
51 after = _tls_listing(foo)
52 (TMPDIR / 'atime-from-after').write_text(after)
53
54 if before != after:
55 diff = subprocess.run(
56 ['diff', '-u',
57 str(TMPDIR / 'atime-from-before'),
58 str(TMPDIR / 'atime-from-after')],
59 capture_output=True, text=True,
60 )
61 print(diff.stdout)
62 test_fail("source atime changed across rsync --open-noatime run")