Skip to content

Commit aa8d165

Browse files
authored
Merge branch 'main' into ci/1967-step5-shellcheck-trim
2 parents 4ae58a4 + 185b317 commit aa8d165

6 files changed

Lines changed: 166 additions & 8 deletions

File tree

‎.github/workflows/experimental.yml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ jobs:
391391
# JUnit file of its own beside junit.xml.
392392
# - `timeout` sends SIGINT at 110 minutes, under the 120-minute cap, so
393393
# pytest still writes the junit XML, and exits 124; SIGKILL 3 minutes on.
394+
# #2020: GNU's `timeout`, which Ubuntu 26.04 installs as `gnutimeout`.
395+
# Its `timeout` is uutils' (0.10.0), whose `--kill-after` sends SIGKILL
396+
# with the SIGINT, and to its child alone, so a run there exits 137
397+
# with no teardown and leaves xdist's workers running.
394398
# - `prlimit --fsize`: no file over 2 GiB.
395399
# - A transient scope, 12G and no swap, at a fifth of the runner's CPU
396400
# weight, where `sudo -n` can start one; otherwise uncapped. A scope's
@@ -470,7 +474,9 @@ jobs:
470474
if [ -n "$SHARDS" ]; then
471475
set -- /usr/bin/python3 util/pytest_shards.py "$SHARDS" -- "$@"
472476
fi
473-
set -- prlimit --fsize=2147483648 timeout --signal=INT --kill-after=3m 110m "$@"
477+
# #2020: GNU's `timeout` on any image, `gnutimeout` where it exists.
478+
gnu_timeout=$(command -v gnutimeout || command -v timeout)
479+
set -- prlimit --fsize=2147483648 "$gnu_timeout" --signal=INT --kill-after=3m 110m "$@"
474480
scoped() {
475481
unit=$1
476482
shift

‎docs/source/contributing/workflows.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,9 @@ warnings:
946946
timings`` takes them all. 3.14t's leg runs pytest once, as before.
947947
* ``timeout --signal=INT --kill-after=3m 110m`` ends pytest under the step's 120-minute
948948
cap, so it still writes the junit XML, and exits 124; ``prlimit --fsize=2147483648``
949-
caps any file at 2 GiB.
949+
caps any file at 2 GiB. The ``timeout`` is GNU's, ``gnutimeout`` where it exists
950+
(#2020): on Ubuntu 26.04 ``timeout`` is uutils', whose ``--kill-after`` sends SIGKILL
951+
with the SIGINT, and to its child alone.
950952
* Where the scope starts ``true`` under a throwaway unit, ``pcapkit-probe-<pid>.scope``,
951953
with every flag the tests get (``sudo -n --preserve-env``, ``MemoryMax=12G``,
952954
``MemorySwapMax=0``, ``CPUWeight=20``, ``--uid``, ``--gid``), the tests run in the
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# -*- coding: utf-8 -*-
2+
"""#2020: every workflow ``timeout`` that needs GNU's semantics runs GNU's.
3+
4+
Ubuntu 26.04, which ``ubuntu-latest`` now serves, ships uutils coreutils 0.10.0
5+
as ``timeout`` and GNU coreutils 9.7 as ``gnutimeout``. uutils' ``--kill-after``
6+
sends SIGKILL with the first signal, and to its child alone: in ``ubuntu:26.04``,
7+
``timeout --signal=INT --kill-after=3s 1 sleep 30`` exits 137 after 1 s where
8+
``gnutimeout`` exits 124, and a grandchild that ignores SIGINT outlives uutils'
9+
kill. ``Run unit tests`` in experimental.yml relies on that grace for pytest's
10+
teardown, so it resolves ``gnutimeout`` where it exists and ``timeout`` otherwise.
11+
12+
A ``timeout`` with no option, ``timeout 60 apt-get update`` as the apt steps run
13+
it, behaves alike under both in the same image: exit 124, and its child's own
14+
children end with it. So the rule here is that a ``timeout`` given any option
15+
runs through that resolution, in every workflow.
16+
17+
"""
18+
19+
from __future__ import annotations
20+
21+
import os
22+
import pathlib
23+
import re
24+
import shutil
25+
import sys
26+
import tempfile
27+
import unittest
28+
from typing import TYPE_CHECKING
29+
30+
from tests import _workflows
31+
from tests.project.test_resource_monitor_1740 import DEFAULT, TESTS, sh, step
32+
33+
if TYPE_CHECKING:
34+
from typing import Iterator
35+
36+
#: How the tests step resolves GNU's ``timeout``, and how it runs it.
37+
RESOLVE = 'gnu_timeout=$(command -v gnutimeout || command -v timeout)\n'
38+
RUN = '"$gnu_timeout" --signal=INT --kill-after=3m 110m "$@"\n'
39+
#: ``timeout`` run as a command and given an option, which only GNU's honours as GNU does.
40+
OPTIONED = re.compile(r'(?<![\w$"/.-])timeout\s+-')
41+
#: A shell comment, a whole line of one or one after a space.
42+
COMMENT = re.compile(r'(?m)(^|\s)#.*$')
43+
44+
45+
def scripts() -> 'Iterator[tuple[str, str, str]]':
46+
"""Every ``run:`` script of every workflow, as (file, step, script with comments stripped)."""
47+
for path in sorted(_workflows.WORKFLOWS.glob('*.yml')):
48+
for job_id, job in (_workflows.load(path.name).get('jobs') or {}).items():
49+
for entry in job.get('steps') or ():
50+
if 'run' in entry:
51+
name = f"{job_id}: {entry.get('name') or entry.get('id') or entry['run'][:40]}"
52+
yield path.name, name, COMMENT.sub(r'\1', entry['run'])
53+
54+
55+
class TestNoWorkflowRunsAnOptionedTimeoutDirectly(unittest.TestCase):
56+
57+
def test_the_pattern(self) -> None:
58+
for text in ('timeout --signal=INT 1 x', 'sudo timeout -k 3 1 x', 'set -- prlimit timeout -s INT 1 x'):
59+
with self.subTest(text=text):
60+
self.assertRegex(text, OPTIONED)
61+
for text in ('"$gnu_timeout" --signal=INT 1 x', 'sudo timeout 60 apt-get update', 'command -v timeout)',
62+
'/usr/bin/gnutimeout -k 1 1 x', 'warn "tests timed out" "timeout sent SIGINT (exit 124)"'):
63+
with self.subTest(text=text):
64+
self.assertNotRegex(text, OPTIONED)
65+
66+
def test_every_workflow(self) -> None:
67+
seen = 0
68+
for name, entry, script in scripts():
69+
seen += 1
70+
with self.subTest(workflow=name, step=entry):
71+
self.assertIsNone(OPTIONED.search(script), script)
72+
if '$gnu_timeout' in script:
73+
self.assertIn(RESOLVE, script)
74+
self.assertLess(script.index(RESOLVE), script.index('"$gnu_timeout"'))
75+
self.assertTrue(seen)
76+
77+
def test_a_direct_timeout_fails_it(self) -> None:
78+
script = step(TESTS)['run']
79+
self.assertIn(RUN, script)
80+
self.assertRegex(script.replace(RUN, 'timeout' + RUN[len('"$gnu_timeout"'):]), OPTIONED)
81+
82+
def test_the_tests_step_resolves_before_it_runs(self) -> None:
83+
script = step(TESTS)['run']
84+
self.assertEqual(script.count(RESOLVE), 1)
85+
self.assertEqual(script.count('"$gnu_timeout"'), 1)
86+
self.assertLess(script.index(RESOLVE), script.index(RUN))
87+
88+
89+
@unittest.skipUnless(sys.platform.startswith('linux') and all(map(shutil.which, ('bash', 'timeout', 'prlimit'))),
90+
'the tests step runs coreutils timeout and util-linux prlimit, as on the Ubuntu runners')
91+
class TestTheTestsStepRunsGNUs(unittest.TestCase):
92+
"""The resolution alone, then the whole step, with ``sudo``, ``python`` and ``gnutimeout`` stubbed."""
93+
94+
def setUp(self) -> None:
95+
tmp = tempfile.TemporaryDirectory(prefix='pcapkit-2020-') # pylint: disable=consider-using-with
96+
self.addCleanup(tmp.cleanup)
97+
self.tmp = pathlib.Path(tmp.name)
98+
self.bindir = self.tmp / 'bin'
99+
self.bindir.mkdir()
100+
self.calls = self.tmp / 'calls'
101+
102+
def stub(self, name: 'str', body: 'str', bindir: 'pathlib.Path | None' = None) -> 'str':
103+
path = (bindir or self.bindir) / name
104+
path.write_text('#!/bin/sh\n' + body, encoding='utf-8')
105+
path.chmod(0o755)
106+
return str(path)
107+
108+
def resolve(self, path: 'str') -> 'str':
109+
# bash by its own path, since ``path`` holds the stubs alone.
110+
result = sh(RESOLVE + 'echo "$gnu_timeout"\n', {'PATH': path}, (str(shutil.which('bash')), '-e'))
111+
self.assertEqual(result.returncode, 0, result.stderr)
112+
return result.stdout.strip()
113+
114+
def test_gnutimeout_first_then_timeout(self) -> None:
115+
only = self.tmp / 'only'
116+
only.mkdir()
117+
plain = self.stub('timeout', 'exit 0\n', only)
118+
self.assertEqual(self.resolve(str(only)), plain)
119+
gnu = self.stub('gnutimeout', 'exit 0\n', only)
120+
self.assertEqual(self.resolve(str(only)), gnu)
121+
122+
def test_the_step_runs_gnutimeout_where_it_exists(self) -> None:
123+
temp = self.tmp / 'temp'
124+
temp.mkdir()
125+
(temp / 'resource-monitor.log').write_text('', encoding='utf-8')
126+
vmstat = self.tmp / 'vmstat'
127+
vmstat.write_text('oom_kill 0\n', encoding='utf-8')
128+
real = shutil.which('gnutimeout') or shutil.which('timeout')
129+
self.stub('gnutimeout', f'echo "gnutimeout $*" >> {self.calls}\nexec {real} "$@"\n')
130+
self.stub('sudo', 'exit 1\n')
131+
self.stub('python', 'exit 0\n')
132+
env = dict(os.environ, RUNNER_TEMP=str(temp), GITHUB_WORKSPACE=str(self.tmp), WORKERS='3',
133+
GRAAL_PYTHON_VM_ARGS='', SHARDS='', LATEST_TIER='',
134+
PATH=str(self.bindir) + os.pathsep + os.environ.get('PATH', ''))
135+
self.assertEqual(shutil.which('sudo', path=env['PATH']), str(self.bindir / 'sudo'))
136+
result = sh(step(TESTS)['run'].replace('/proc/vmstat', str(vmstat)), env, DEFAULT)
137+
self.assertEqual(result.returncode, 0, result.stderr)
138+
calls = self.calls.read_text(encoding='utf-8').splitlines()
139+
self.assertEqual(len(calls), 1, calls)
140+
self.assertTrue(calls[0].startswith('gnutimeout --signal=INT --kill-after=3m 110m python -m pytest '), calls)
141+
142+
143+
if __name__ == '__main__':
144+
unittest.main()

‎tests/project/test_graalpy_shards_1963.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#: The three lines that build the command the step's caps run, in order.
4141
PYTEST = 'set -- python -m pytest "$@"\n'
4242
SHARDED = 'set -- /usr/bin/python3 util/pytest_shards.py "$SHARDS" -- "$@"\n'
43-
CAPPED = 'set -- prlimit --fsize=2147483648 timeout --signal=INT --kill-after=3m 110m "$@"\n'
43+
CAPPED = 'set -- prlimit --fsize=2147483648 "$gnu_timeout" --signal=INT --kill-after=3m 110m "$@"\n'
4444

4545

4646
def rest_ignores(shards: 'str') -> 'list[str]':

‎tests/project/test_resource_monitor_1740.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def sh(script: 'str', env: 'dict[str, str]', shell: 'tuple[str, ...]',
133133
os.unlink(file.name)
134134

135135

136+
def gnu_timeout(env: 'dict[str, str]') -> 'str | None':
137+
"""The ``timeout`` the tests step resolves on ``env``'s ``PATH``: ``gnutimeout`` first (#2020)."""
138+
return shutil.which('gnutimeout', path=env['PATH']) or shutil.which('timeout', path=env['PATH'])
139+
140+
136141
BASH = ('bash', '--noprofile', '--norc', '-eo', 'pipefail')
137142
DEFAULT = ('bash', '-e')
138143

@@ -286,15 +291,15 @@ def test_the_timeout_ends_the_tests_before_the_step_cap(self) -> None:
286291
script = step(TESTS)['run']
287292
# #1963: the command the timeout runs, pytest or GraalPy's shards of it, is set just before.
288293
self.assertIn('set -- python -m pytest "$@"\n', script)
289-
match = re.search(r'timeout --signal=INT --kill-after=(\d+)m (\d+)m "\$@"\n', script)
294+
match = re.search(r'"\$gnu_timeout" --signal=INT --kill-after=(\d+)m (\d+)m "\$@"\n', script)
290295
self.assertIsNotNone(match, script)
291296
assert match is not None
292297
kill_after, duration = map(int, match.groups())
293298
self.assertEqual((duration, kill_after), (110, 3))
294299
self.assertLess(duration + kill_after, step(TESTS)['timeout-minutes'])
295300

296301
def test_no_file_grows_past_2_gib(self) -> None:
297-
self.assertIn(f'set -- prlimit --fsize={FSIZE} timeout ', step(TESTS)['run'])
302+
self.assertIn(f'set -- prlimit --fsize={FSIZE} "$gnu_timeout" ', step(TESTS)['run'])
298303

299304
def test_the_scope_is_probed_and_falls_back_with_a_warning(self) -> None:
300305
script = step(TESTS)['run']
@@ -398,8 +403,9 @@ def test_the_scope_runs_the_tests_under_the_timeout_and_fsize_cap(self) -> None:
398403
self.assertTrue(probe.startswith(SUDO_ARGV.format(unit=unit.group(1), **ids)), probe)
399404
self.assertTrue(probe.endswith(f' GRAAL_PYTHON_VM_ARGS={HEAP} true'), probe)
400405
self.assertTrue(run.startswith(SUDO_ARGV.format(unit='pcapkit-tests.scope', **ids)), run)
401-
self.assertIn(f' prlimit --fsize={FSIZE} timeout --signal=INT --kill-after=3m 110m python -m pytest -q -n 3 ',
402-
run)
406+
# #2020: GNU's timeout, `gnutimeout` where the image has one, by its path.
407+
self.assertIn(f' prlimit --fsize={FSIZE} {gnu_timeout(self.env)} --signal=INT --kill-after=3m 110m '
408+
'python -m pytest -q -n 3 ', run)
403409
self.assertIn('args: -m pytest -q -n 3 --dist load', result.stdout)
404410
self.assertIn(f'heap {HEAP}\n', result.stdout)
405411
self.assertIn(f' GRAAL_PYTHON_VM_ARGS={HEAP} prlimit ', run)

‎tests/project/test_superseded_runs_1938.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def test_a_current_run_keeps_the_tests_status(self) -> None:
758758
def test_the_watcher_runs_the_scope_and_names_it(self) -> None:
759759
"""Every cap stays on pytest, and the watcher runs outside the scope, which it names to ``--scope``."""
760760
script = step(TESTS)['run']
761-
capped = 'set -- prlimit --fsize=2147483648 timeout --signal=INT --kill-after=3m 110m "$@"\n'
761+
capped = 'set -- prlimit --fsize=2147483648 "$gnu_timeout" --signal=INT --kill-after=3m 110m "$@"\n'
762762
scoped = 'set -- scoped pcapkit-tests.scope "$@"\n'
763763
named = 'watched=pcapkit-tests.scope\n'
764764
watched = ('set -- "$WATCHER_PYTHON" util/superseded.py watch --interval 60 --grace 30 --scope "$watched" '

0 commit comments

Comments
 (0)