]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Add custom signal handlers to integration test wrapper script
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 24 Apr 2025 08:07:06 +0000 (10:07 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 24 Apr 2025 21:11:43 +0000 (06:11 +0900)
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.

test/integration-tests/integration-test-wrapper.py

index 9c7f64a20b6f90f6a226e332f39ad424712770c5..77552cbfad6956dd3f67e7f772e01975be2d4c1a 100755 (executable)
@@ -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)