|
| 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() |
0 commit comments