test: Add custom signal handlers to integration test wrapper script

meson will send SIGTERM if the test gets stuck and hits the timeout,
in which case we still want to do log saving and analysis, so let's
add some signal handlers which allow us to do that.

This won't be very useful until https://github.com/mesonbuild/meson/pull/14513
lands, since we only get half a second from meson to handle SIGTERM
before it sends SIGKILL, but let's land this already so we immediately
start taking advantage of the meson fix once it lands.
This commit is contained in:
Daan De Meyer
2025-04-24 10:07:06 +02:00
committed by Yu Watanabe
parent 497b7a30a9
commit 45f63c3db7

View File

@@ -12,11 +12,14 @@ import os
import re
import shlex
import shutil
import signal
import subprocess
import sys
import tempfile
import textwrap
from pathlib import Path
from types import FrameType
from typing import Optional
EMERGENCY_EXIT_DROPIN = """\
[Unit]
@@ -359,7 +362,23 @@ def statfs(path: Path) -> str:
).stdout.strip()
INTERRUPTED = False
def onsignal(signal: int, frame: Optional[FrameType]) -> None:
global INTERRUPTED
if INTERRUPTED:
return
INTERRUPTED = True
raise KeyboardInterrupt()
def main() -> None:
signal.signal(signal.SIGINT, onsignal)
signal.signal(signal.SIGTERM, onsignal)
signal.signal(signal.SIGHUP, onsignal)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--mkosi', default=None)
parser.add_argument('--meson-source-dir', required=True, type=Path)
@@ -592,19 +611,22 @@ def main() -> None:
'vm' if args.vm or os.getuid() != 0 or os.getenv('TEST_PREFER_QEMU', '0') == '1' else 'boot',
] # fmt: skip
result = subprocess.run(cmd)
# On Debian/Ubuntu we get a lot of random QEMU crashes. Retry once, and then skip if it fails again.
if args.vm and result.returncode == 247 and args.exit_code != 247:
if journal_file:
journal_file.unlink(missing_ok=True)
try:
result = subprocess.run(cmd)
# On Debian/Ubuntu we get a lot of random QEMU crashes. Retry once, and then skip if it fails again.
if args.vm and result.returncode == 247 and args.exit_code != 247:
print(
f'Test {args.name} failed due to QEMU crash (error 247), ignoring',
file=sys.stderr,
)
exit(77)
if journal_file:
journal_file.unlink(missing_ok=True)
result = subprocess.run(cmd)
if args.vm and result.returncode == 247 and args.exit_code != 247:
print(
f'Test {args.name} failed due to QEMU crash (error 247), ignoring',
file=sys.stderr,
)
exit(77)
except KeyboardInterrupt:
result = subprocess.CompletedProcess(args=cmd, returncode=-signal.SIGINT)
coredumps = process_coredumps(args, journal_file)