test: Shut down tests on crash

If an assert in systemd fails it can't shut down normally.

By default it freezes. For interactive runs we want the crash shell
to enable further debugging, but during test runs we want it to exit
without having to wait for the test timeout.

By deactivating the crash shell, enabling reboot, and configuring qemu
so that it shuts down instead of rebooting we can shut down instead.

Because by default UEFI will enroll keys and then reboot
we also have to set --qemu-firmware-variables=custom
so it doesn't need to auto-enroll.

Because mkosi has to handle not receiving an EXIT_STATUS notification
it falls back to the exit code of qemu, which in the case of reboot
would be 0, we also override the success exit status to 123
and check that we got that as an exit code from mkosi.
This commit is contained in:
Richard Maw
2024-04-23 14:13:22 +01:00
committed by Daan De Meyer
parent 796cf1b483
commit 2fd849016b

View File

@@ -64,6 +64,7 @@ def main():
"""
[Unit]
SuccessAction=exit
SuccessActionExitStatus=123
FailureAction=exit
"""
)
@@ -87,7 +88,9 @@ def main():
f"systemd.extra-unit.emergency-exit.service={shlex.quote(EMERGENCY_EXIT_SERVICE)}",
'--credential',
f"systemd.unit-dropin.emergency.target={shlex.quote(EMERGENCY_EXIT_DROPIN)}",
'--kernel-command-line-extra=systemd.mask=serial-getty@.service',
'--kernel-command-line-extra=systemd.mask=serial-getty@.service systemd.show_status=no systemd.crash_shell=0 systemd.crash_reboot',
# Custom firmware variables allow bypassing the EFI auto-enrollment reboot so we only reboot on crash
'--qemu-firmware-variables=custom',
]
if not sys.stderr.isatty()
else []
@@ -103,12 +106,13 @@ def main():
]),
*args.mkosi_args,
'qemu',
*(['-no-reboot'] if not sys.stderr.isatty() else [])
]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
if e.returncode != 77 and journal_file:
result = subprocess.run(cmd)
# Return code 123 is the expected success code
if result.returncode != (0 if sys.stderr.isatty() else 123):
if result.returncode != 77 and journal_file:
cmd = [
'journalctl',
'--no-hostname',
@@ -119,7 +123,7 @@ def main():
]
print("Test failed, relevant logs can be viewed with: \n\n"
f"{shlex.join(str(a) for a in cmd)}\n", file=sys.stderr)
exit(e.returncode)
exit(result.returncode or 1)
# Do not keep journal files for tests that don't fail.
if journal_file: