From: Daan De Meyer Date: Fri, 8 Dec 2023 15:53:51 +0000 (+0100) Subject: tests: Make more robust on distros with recent systemd X-Git-Tag: v20~93^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bfae58fb84e1e484d66e38738d0e62731ac38d8a;p=thirdparty%2Fmkosi.git tests: Make more robust on distros with recent systemd 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. --- diff --git a/mkosi.extra/usr/lib/systemd/system/mkosi-check-and-shutdown.service b/mkosi.extra/usr/lib/systemd/system/mkosi-check-and-shutdown.service index 31304d92d..33058f286 100644 --- a/mkosi.extra/usr/lib/systemd/system/mkosi-check-and-shutdown.service +++ b/mkosi.extra/usr/lib/systemd/system/mkosi-check-and-shutdown.service @@ -5,6 +5,7 @@ After=multi-user.target network-online.target Requires=multi-user.target SuccessAction=exit FailureAction=exit +SuccessActionExitStatus=123 [Service] Type=oneshot diff --git a/mkosi/distributions/__init__.py b/mkosi/distributions/__init__.py index a24aa1e44..75108f6c7 100644 --- a/mkosi/distributions/__init__.py +++ b/mkosi/distributions/__init__.py @@ -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 ( diff --git a/tests/__init__.py b/tests/__init__.py index 8e4c815b1..74de5c4bc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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)