Debian Codegraph @

testsuite/daemon_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/daemon.test.
3 #
4 # Basic daemon-mode operations against an in-process rsyncd: list
5 # modules, list a hidden module, list a single-glob match, and the
6 # atimes-format variant. We avoid actually starting a listening server
7 # by using RSYNC_CONNECT_PROG to spawn the daemon as a child of rsync.
8
9 # Rerun under the fleet harness's non-root pass (testsuite/fleettest.py): a
10 # non-root rsyncd emits different uid/gid config, so exercise that path too.
11 fleet_nonroot = True
12
13 import os
14 import subprocess
15
16 from rsyncfns import (
17 CHKFILE, FROMDIR, OUTFILE, RSYNC, RSYNC_PEER, SCRATCHDIR, SRCDIR, TODIR,
18 build_rsyncd_conf, get_rootuid, get_testuid, makepath,
19 rsync_argv, run_rsync, start_test_daemon, test_fail, rsh_cmd,
20 )
21
22
23 DAEMON_PORT = 12877
24
25
26 SSH = rsh_cmd(None, '--no-cd')
27
28
29 def listed_paths(text: str) -> set:
30 """The set of path names in an `rsync -r` listing. Each listing line is
31 "<mode> <size> <date> <time> <path>"; pull out the trailing path. Comparing
32 the whole set (not just checking individual paths are present) catches a
33 listing that leaks EXTRA paths, not only one that omits expected ones."""
34 paths = set()
35 for line in text.splitlines():
36 parts = line.split()
37 # A listing line starts with a 10-char mode string and ends with the
38 # path; -U adds extra date columns, so take the last token (the test's
39 # paths contain no spaces).
40 if len(parts) >= 5 and len(parts[0]) == 10 and parts[0][0] in '-dlbcps':
41 paths.add(parts[-1])
42 return paths
43
44
45 conf = build_rsyncd_conf()
46
47 makepath(FROMDIR / 'foo', FROMDIR / 'bar' / 'baz', TODIR)
48 (FROMDIR / 'foo' / 'one').write_text("one\n")
49 (FROMDIR / 'bar' / 'two').write_text("two\n")
50 (FROMDIR / 'bar' / 'baz' / 'three').write_text("three\n")
51
52 os.chdir(SCRATCHDIR)
53 if not (SCRATCHDIR / 'rsyncd.conf').exists():
54 os.symlink('test-rsyncd.conf', SCRATCHDIR / 'rsyncd.conf')
55
56 confopt = []
57 if get_testuid() == get_rootuid():
58 # Root needs an explicit --config; otherwise rsync uses /etc/rsyncd.conf.
59 print(f"Forcing --config={conf}")
60 confopt = [f'--config={conf}']
61
62 expected_modules = (
63 "test-from \tr/o\n"
64 "test-to \tr/w\n"
65 "test-scratch \t\n"
66 )
67
68
69 def run_and_check(args, label, capture_stderr=False):
70 proc = subprocess.run(
71 rsync_argv(*args),
72 capture_output=True, text=True,
73 )
74 out = proc.stdout
75 if capture_stderr:
76 out += proc.stderr
77 print(f"--- {label} output:")
78 print(out)
79 if proc.returncode != 0 and not capture_stderr:
80 test_fail(f"{label}: rsync exited {proc.returncode}\n{proc.stderr}")
81 return out
82
83
84 # Module list via the lsh.sh stand-in.
85 rsync_path = f"{RSYNC_PEER}{(' ' + ' '.join(confopt)) if confopt else ''}"
86 out = run_and_check(
87 ['-ve', SSH, f'--rsync-path={rsync_path}', 'localhost::'],
88 "module list via lsh.sh",
89 )
90 if expected_modules not in out:
91 test_fail("module list via lsh.sh did not contain the expected modules")
92 # test-hidden is `list = no`; it must NOT appear in the module listing.
93 if 'test-hidden' in out:
94 print(out)
95 test_fail("module list via lsh.sh leaked the `list = no` test-hidden module")
96 print('====')
97
98 # Same module list via the test daemon (pipe transport by default; real
99 # loopback rsyncd under --use-tcp).
100 daemon_url = start_test_daemon(conf, DAEMON_PORT).rstrip('/')
101
102 out = run_and_check(['-v', f'{daemon_url}/'], "module list via daemon")
103 if expected_modules not in out:
104 test_fail("module list via daemon did not contain the expected modules")
105 # test-hidden is `list = no`; it must NOT appear in the module listing.
106 if 'test-hidden' in out:
107 print(out)
108 test_fail("module list via daemon leaked the `list = no` test-hidden module")
109 print('====')
110
111 # test-hidden: a recursive listing of the whole module. Compare the exact set
112 # of listed paths so an unexpected/leaked extra path is caught, not only a
113 # missing one.
114 out = run_and_check(['-r', f'{daemon_url}/test-hidden'], "test-hidden listing")
115 got = listed_paths(out)
116 want = {'.', 'bar', 'bar/two', 'bar/baz', 'bar/baz/three', 'foo', 'foo/one'}
117 if got != want:
118 print(out)
119 test_fail(f"test-hidden listing paths {sorted(got)} != expected {sorted(want)}")
120
121 # test-from/f* glob: only the foo subtree, nothing from bar.
122 out = run_and_check(['-r', f'{daemon_url}/test-from/f*'], "test-from glob")
123 got = listed_paths(out)
124 want = {'foo', 'foo/one'}
125 if got != want:
126 print(out)
127 test_fail(f"test-from glob paths {sorted(got)} != expected {sorted(want)}")
128
129 # atimes-format variant -- only if rsync was built with atimes support.
130 vv = run_rsync('-VV', check=True, capture_output=True)
131 if '"atimes": true' in vv.stdout:
132 out = run_and_check(['-rU', f'{daemon_url}/test-from/f*'], "test-from glob with -U")
133 got = listed_paths(out)
134 if got != want:
135 print(out)
136 test_fail(f"-U glob paths {sorted(got)} != expected {sorted(want)}")