Debian Codegraph @

testsuite/daemon-exec_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Daemon coverage: pre-xfer exec and post-xfer exec.
3
4 A module's pre-xfer/post-xfer exec hooks must run with the documented
5 environment (RSYNC_MODULE_NAME, RSYNC_EXIT_STATUS, ...), and a non-zero
6 pre-xfer exec must abort the transfer.
7 """
8
9 import shlex
10 import subprocess
11 import time
12
13 from rsyncfns import (
14 FROMDIR, SCRATCHDIR,
15 make_tree, makepath, rmtree, rsync_argv, start_test_daemon, test_fail,
16 write_daemon_conf,
17 )
18
19
20 def wait_for(path, want, secs=5):
21 """Poll for a marker file to contain `want`. post-xfer exec runs on the
22 daemon side after the client disconnects (a real race under --use-tcp),
23 so we must wait for it rather than check immediately."""
24 deadline = time.monotonic() + secs
25 while time.monotonic() < deadline:
26 if path.is_file() and path.read_text().strip() == want:
27 return True
28 time.sleep(0.05)
29 return False
30
31 DAEMON_PORT = 12889
32
33 src = FROMDIR
34 rmtree(src)
35 make_tree(src, depth=3)
36
37 markers = SCRATCHDIR / 'markers'
38 rmtree(markers)
39 makepath(markers)
40 hookdir = SCRATCHDIR / 'hookdest'
41 faildir = SCRATCHDIR / 'faildest'
42 makepath(hookdir, faildir)
43
44
45 def script(name, body):
46 p = SCRATCHDIR / name
47 p.write_text('#!/bin/sh\n' + body)
48 p.chmod(0o755)
49 return p
50
51
52 pre = script('pre.sh', f'echo "$RSYNC_MODULE_NAME" > {shlex.quote(str(markers / "pre.out"))}\nexit 0\n')
53 post = script('post.sh', f'echo "$RSYNC_EXIT_STATUS" > {shlex.quote(str(markers / "post.out"))}\n'
54 'exit 0\n')
55 prefail = script('prefail.sh', 'exit 1\n')
56
57 conf = write_daemon_conf([
58 ('hook', {'path': hookdir, 'read only': 'no',
59 'pre-xfer exec': shlex.quote(str(pre)),
60 'post-xfer exec': shlex.quote(str(post))}),
61 ('failhook', {'path': faildir, 'read only': 'no',
62 'pre-xfer exec': shlex.quote(str(prefail))}),
63 ])
64 url = start_test_daemon(conf, DAEMON_PORT)
65
66 # --- pre/post hooks run with the documented environment ---------------------
67 proc = subprocess.run(rsync_argv('-a', f'{src}/', f'{url}hook/'),
68 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
69 text=True)
70 if proc.returncode not in (0, 23):
71 test_fail(f"transfer through exec-hook module failed: {proc.stderr}")
72 if not wait_for(markers / 'pre.out', 'hook'):
73 test_fail("pre-xfer exec did not run with RSYNC_MODULE_NAME=hook")
74 if not wait_for(markers / 'post.out', '0'):
75 test_fail("post-xfer exec did not run with RSYNC_EXIT_STATUS=0")
76
77 # --- a failing pre-xfer exec aborts the transfer ----------------------------
78 proc = subprocess.run(rsync_argv('-a', f'{src}/', f'{url}failhook/'),
79 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
80 text=True)
81 if proc.returncode == 0:
82 test_fail("a failing pre-xfer exec did not abort the transfer")
83 if list(faildir.iterdir()):
84 test_fail("transfer wrote files despite a failing pre-xfer exec")
85
86 print("daemon-exec: pre-xfer/post-xfer exec env + abort-on-failure verified")