Debian Codegraph @

testsuite/skiplist-spec_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # runtests.py's RSYNC_EXPECT_SKIPPED parser. The per-platform expected-skip
3 # lists live in testsuite/skiplist/*.txt and are referenced from the workflows
4 # as @FILE, so a bug here silently weakens the skip oracle on every CI job (an
5 # unreadable or truncated list must never read as "expect no skips"). Also
6 # checks that the committed lists themselves parse and are referenced.
7
8 import importlib.util
9 import os
10 from pathlib import Path
11
12 from rsyncfns import SCRATCHDIR, SRCDIR, test_fail
13
14 # srcdir arrives relative under `make installcheck` (--srcdir=../src). Resolve
15 # it once: paths built from it are handed back to a srcdir-relative API below,
16 # which would otherwise re-prefix them.
17 SRC = Path(SRCDIR).resolve()
18
19 spec = importlib.util.spec_from_file_location('runtests', SRC / 'runtests.py')
20 runtests = importlib.util.module_from_spec(spec)
21 spec.loader.exec_module(runtests)
22
23 SUITE = str(SRC / 'testsuite')
24 ERROR = f'exit:{runtests.Exit.ERROR}'
25
26
27 def expand(text_spec, srcdir=None, suitedir=None):
28 """expand_skip_spec, but reporting a SystemExit as a string."""
29 try:
30 return runtests.expand_skip_spec(text_spec, srcdir or str(SRC),
31 suitedir or SUITE)
32 except SystemExit as e:
33 return f'exit:{e.code}'
34
35
36 def write(name, body):
37 p = SCRATCHDIR / name
38 if isinstance(body, bytes):
39 p.write_bytes(body)
40 else:
41 p.write_text(body)
42 return '@' + str(p)
43
44
45 # A stand-in suite whose entries make each malformed name *look* real, so that
46 # removing the guard for it cannot be masked by the "no such test" check.
47 FAKE = SCRATCHDIR / 'fakesuite'
48 FAKE.mkdir(exist_ok=True)
49 (FAKE / 'acls sparse_test.py').write_text('') # if the one-name-per-line
50 (FAKE / 'foo,bar_test.py').write_text('') # ... comma and
51 (FAKE / 'adir_test.py').mkdir(exist_ok=True) # ... regular-file guards went
52
53 # --- a bad spec must be a hard error, never a silently smaller expectation --
54 #
55 # The sorted/duplicate cases use real test names in descending order, so that
56 # dropping the sort check cannot be masked by the stale-name check.
57
58 for what, arg, suite in (
59 ('missing file', '@testsuite/skiplist/does-not-exist.txt', None),
60 ('unknown test name', 'no-such-test-here', None),
61 ('unsorted list', write('unsorted.txt', 'sparse\nacls\n'), None),
62 ('duplicate entry', write('dup.txt', 'acls\nacls\n'), None),
63 ('stale name in a list', write('stale.txt', 'gone-away-test\n'), None),
64 ('empty file', write('empty.txt', ''), None),
65 ('comments only', write('comments.txt', '# nothing here\n\n'), None),
66 ('not valid text', write('binary.txt', b'acls\n\xff\xfe not utf-8\n'), None),
67 ('empty entry', 'acls,,sparse', None),
68 ('trailing comma', 'acls,', None),
69 ('leading comma', ',acls', None),
70 ('path in a name', '../testsuite/acls', None),
71 ('two names on a line', write('twonames.txt', 'acls sparse\n'), str(FAKE)),
72 ('comma in a name', write('comma.txt', 'foo,bar\n'), str(FAKE)),
73 ('a directory, not a test', 'adir', str(FAKE)),
74 ):
75 got = expand(arg, suitedir=suite)
76 if got != ERROR:
77 test_fail(f'{what}: expected a hard error, got {got!r}')
78
79 # --- good specs ------------------------------------------------------------
80
81 one = write('one.txt', '# comment\n\nacls # trailing comment\nsparse\n')
82 two = write('two.txt', 'devices\nsparse\n')
83
84 for what, arg, want in (
85 ('@FILE expansion', one, 'acls,sparse'),
86 ('composing two lists', f'{one},{two}', 'acls,devices,sparse'),
87 ('a bare name with @FILE', f'{one},crtimes', 'acls,crtimes,sparse'),
88 ('an empty spec is "no skips"', '', ''),
89 ):
90 got = expand(arg)
91 if got != want:
92 test_fail(f'{what}: expected {want!r}, got {got!r}')
93
94 # A relative @FILE resolves against srcdir, not the cwd: `make check` in an
95 # out-of-tree build directory would otherwise not find the lists. srcdir is
96 # itself relative under `make installcheck` (--srcdir=../src), so check a
97 # non-trivial one too -- in-tree, os.path.relpath() would just be ".".
98 got = expand('@testsuite/skiplist/common.txt', str(SRC))
99 if got.startswith('exit:') or not got:
100 test_fail(f'relative @FILE did not resolve against srcdir: {got!r}')
101
102 cwd = os.getcwd()
103 try:
104 os.chdir(SRC.parent)
105 got = expand('@testsuite/skiplist/common.txt', SRC.name)
106 finally:
107 os.chdir(cwd)
108 if got.startswith('exit:') or not got:
109 test_fail(f'@FILE did not resolve against a relative srcdir: {got!r}')
110
111 # --- the committed lists ---------------------------------------------------
112
113 # backport.txt is not an expected-skip list: it is an EXCLUDE list that a
114 # backport branch ships to say which of this suite's tests it cannot run, and
115 # fleettest reads it from the BUILT tree rather than from any workflow. It only
116 # appears here when a backport tree has been overlaid with this suite, so it is
117 # exempt from the checks below that assume a workflow points at the file.
118 BACKPORT_LIST = 'backport.txt'
119
120 lists = sorted(p for p in (SRC / 'testsuite' / 'skiplist').glob('*.txt')
121 if p.name != BACKPORT_LIST)
122 if len(lists) < 5:
123 test_fail(f'expected the per-platform skip lists, found {lists}')
124 for path in lists:
125 got = expand('@' + str(path))
126 if got.startswith('exit:') or not got:
127 test_fail(f'{path.name} is empty or does not parse: {got!r} '
128 '(run runtests.py to see the diagnostic)')
129
130 # Every workflow reference must expand, and every committed list must be
131 # referenced by some workflow -- a list nothing points at silently stops
132 # being enforced.
133 wf = sorted((SRC / '.github' / 'workflows').glob('*.yml'))
134 refs, referenced = 0, set()
135 for path in wf:
136 for line in path.read_text().splitlines():
137 if 'RSYNC_EXPECT_SKIPPED=' not in line:
138 continue
139 arg = line.split('RSYNC_EXPECT_SKIPPED=', 1)[1].split()[0]
140 got = expand(arg)
141 if got.startswith('exit:') or not got:
142 test_fail(f'{path.name}: unusable RSYNC_EXPECT_SKIPPED spec {arg!r}')
143 refs += 1
144 for tok in arg.split(','):
145 if tok.startswith('@'):
146 referenced.add(Path(tok[1:]).name)
147 if wf:
148 if refs == 0:
149 test_fail('no workflow references RSYNC_EXPECT_SKIPPED any more')
150 orphans = sorted({p.name for p in lists} - referenced)
151 if orphans:
152 test_fail(f'skip lists no workflow references: {", ".join(orphans)}')
153 # '-name' removals. fleettest emits these for a host that can genuinely run a
154 # test its platform list expects to skip; the name being dropped lives inside an
155 # @FILE, so no composer can subtract it before runtests expands the spec.
156 common = SRC / 'testsuite' / 'skiplist' / 'common.txt'
157 if common.is_file():
158 full = expand('@' + str(common))
159 victim = full.split(',')[0]
160 got = expand(f'@{common},-{victim}')
161 if got == full or victim in got.split(','):
162 test_fail(f'-{victim} did not remove it from the expanded list')
163 if len(got.split(',')) != len(full.split(',')) - 1:
164 test_fail(f'-{victim} removed more than the one name')
165 # Order must not matter: removals apply after every addition.
166 if expand(f'-{victim},@{common}') != got:
167 test_fail('a removal before the list it removes from behaved differently')
168 # A removal that removes nothing is stale, and shrinking the expected set
169 # quietly is the whole failure mode this parser exists to prevent.
170 for stale in (f'@{common},-no-such-test-here', f'@{common},-{victim},-{victim}'):
171 if expand(stale) != ERROR:
172 test_fail(f'a stale removal was accepted: {stale!r}')
173
174 print(f'ok: {len(lists)} skip lists, {refs} workflow references')