]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
tests: Make more robust on distros with recent systemd
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 8 Dec 2023 15:53:51 +0000 (16:53 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 9 Dec 2023 13:06:03 +0000 (14:06 +0100)
Let's make use of the fact that we can communicate the exit status
from VMs on recent versions of systemd. Even when it fails to run
qemu will often exit with exit status 0 so let's make our successful
exit status 123 and check for that instead of 0.

Let's also rework how we have systemd log. Instead of using
default_standard_output, let's have journald forward all logs to the
console. While we're at, let's also add some general useful debugging
kernel command line arguments that we also use in the systemd repository.

mkosi.extra/usr/lib/systemd/system/mkosi-check-and-shutdown.service
mkosi/distributions/__init__.py
tests/__init__.py

index 31304d92d931021de2e291d3f640ae5919c6bd73..33058f2864c801a2de13dbffb56e4703b7414818 100644 (file)
@@ -5,6 +5,7 @@ After=multi-user.target network-online.target
 Requires=multi-user.target
 SuccessAction=exit
 FailureAction=exit
+SuccessActionExitStatus=123
 
 [Service]
 Type=oneshot
index a24aa1e443b969b00444a7f59540646a9b0dd8b5..75108f6c73d6cc24d1134e7b91315442b6ef1e7b 100644 (file)
@@ -91,7 +91,13 @@ class Distribution(StrEnum):
     custom       = enum.auto()
 
     def is_centos_variant(self) -> bool:
-        return self in (Distribution.centos, Distribution.alma, Distribution.rocky)
+        return self in (
+            Distribution.centos,
+            Distribution.alma,
+            Distribution.rocky,
+            Distribution.rhel,
+            Distribution.rhel_ubi,
+        )
 
     def is_dnf_distribution(self) -> bool:
         return self in (
index 8e4c815b1c931cfc3a0ae4dbcccf05f7f4d3a107..74de5c4bc395d9ccd26a2aa7cb6226e92937c923 100644 (file)
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
 import os
+import subprocess
 import sys
 import tempfile
 from collections.abc import Iterator, Sequence
@@ -54,7 +55,20 @@ class Image:
         stdin: _FILE = None,
         user: Optional[int] = None,
         group: Optional[int] = None,
+        check: bool = True,
     ) -> CompletedProcess:
+        kcl = [
+            "console=ttyS0",
+            "systemd.crash_shell",
+            "systemd.log_level=debug",
+            "udev.log_level=info",
+            "systemd.log_ratelimit_kmsg=0",
+            "systemd.journald.forward_to_console",
+            "systemd.journald.max_level_console=debug",
+            "printk.devkmsg=on",
+            "systemd.early_core_pattern=/core",
+        ]
+
         return run([
             "python3", "-m", "mkosi",
             "--distribution", str(self.distribution),
@@ -63,14 +77,12 @@ class Image:
             *options,
             "--output-dir", self.output_dir.name,
             "--cache-dir", "mkosi.cache",
-            "--kernel-command-line=console=ttyS0",
-            "--kernel-command-line=systemd.log_target=console",
-            "--kernel-command-line=systemd.default_standard_output=journal+console",
+            *(f"--kernel-command-line={i}" for i in kcl),
             "--qemu-vsock=yes",
             "--qemu-mem=4G",
             verb,
             *args,
-        ], stdin=stdin, stdout=sys.stdout, user=user, group=group)
+        ], check=check, stdin=stdin, stdout=sys.stdout, user=user, group=group)
 
     def build(self, options: Sequence[str] = (), args: Sequence[str] = ()) -> CompletedProcess:
         return self.mkosi(
@@ -83,18 +95,36 @@ class Image:
         )
 
     def boot(self, options: Sequence[str] = (), args: Sequence[str] = ()) -> CompletedProcess:
-        return self.mkosi("boot", [*options, "--debug"], args, stdin=sys.stdin if sys.stdin.isatty() else None)
+        result = self.mkosi(
+            "boot",
+            [*options, "--debug"],
+            args, stdin=sys.stdin if sys.stdin.isatty() else None,
+            check=False,
+        )
+
+        if result.returncode != 123:
+            raise subprocess.CalledProcessError(result.returncode, result.args, result.stdout, result.stderr)
+
+        return result
 
     def qemu(self, options: Sequence[str] = (), args: Sequence[str] = ()) -> CompletedProcess:
-        return self.mkosi(
+        result = self.mkosi(
             "qemu",
             [*options, "--debug"],
             args,
             stdin=sys.stdin if sys.stdin.isatty() else None,
             user=INVOKING_USER.uid,
             group=INVOKING_USER.gid,
+            check=False,
         )
 
+        rc = 0 if self.distribution == Distribution.ubuntu or self.distribution.is_centos_variant() else 123
+
+        if result.returncode != rc:
+            raise subprocess.CalledProcessError(result.returncode, result.args, result.stdout, result.stderr)
+
+        return result
+
     def summary(self, options: Sequence[str] = ()) -> CompletedProcess:
         return self.mkosi("summary", options, user=INVOKING_USER.uid, group=INVOKING_USER.gid)