phoebus_os/installer/tests/vm/gui-driver.py
Phoebus b8152054b6
Some checks are pending
ci / test (push) Waiting to run
fix(installer): harden boot recovery and desktop reliability
2026-10-01 07:03:42 -07:00

1610 lines
72 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""Headless driver for the Phoebus graphical live/installer ISO.
The live medium boots into Mango + DankMaterialShell and opens the branded
welcome window. This driver boots it with a graphical device and a serial
console, checks the launcher/branding payload, drives the graphical installer
over the live session, and captures QEMU framebuffer screenshots through QMP.
Modes:
gui : boot, log in on serial, verify the welcome/branding payload, open
the installer from the welcome action, prove duplicate prevention,
walk the wizard steps and capture screenshots.
opencode: drive the OpenCode AI step through the real graphical UI and
validate a model response and proposal.
gui-install: complete a full installation through the visible wizard (AI
connection, disk, account, direct "Install now", install) on a
disposable disk.
install : boot, log in on serial, fetch a non-interactive config and 0600
secrets over QEMU user networking, run the deterministic install
with the static binary, scan the log for a secret leak, power off.
boot : boot the installed disk alone (no ISO), unlock LUKS, log in and run
verification commands.
desktop : boot with a graphical seat and assert the installed Mango session.
Safety: every writable drive must be a qcow2 file inside the disposable
directory named by `PHOEBUS_VM_DISKS` (default
`$PHOEBUS_VM_STATE/disks`). The driver refuses block devices, `/dev/*`,
symlinks that escape the directory, raw images and images with an outside
backing chain. Each run gets its own private state directory, a fresh writable
OVMF variable file and a unique QMP monitor socket. Results are structured and
the process exits nonzero on any failed assertion, timeout, unexpected EOF or
missing result.
"""
from __future__ import annotations
import json
import os
import re
import shlex
import shutil
import subprocess
import sys
import time
from pathlib import Path
import pexpect
from PIL import Image, ImageChops
import vmsafety
from vmsafety import BLOCKED, FAIL, PASS, Results, HarnessError
QEMU = os.environ.get("QEMU_BIN", "qemu-system-x86_64")
OVMF_CODE = os.environ["OVMF_CODE"]
OVMF_VARS_TEMPLATE = os.environ["OVMF_VARS"]
ISO = os.environ.get("ISO", "")
DISKS = os.environ.get("DISKS") or os.environ.get("DISK", "")
SERVE_URL = os.environ.get("SERVE_URL", "http://10.0.2.2:8000")
LIVE_USER = os.environ.get("LIVE_USER", "phoebus")
LIVE_PW = os.environ.get("LIVE_PASSWORD", "phoebus")
USER = os.environ.get("USERNAME", "phoebus")
USER_PW = os.environ.get("USER_PASSWORD", "phoebus-user-test")
LUKS_PW = os.environ.get("LUKS_PASSPHRASE", "phoebus-luks-test")
USER_NAME = os.environ.get("USER_USERNAME", USER)
HOST_NAME = os.environ.get("HOST_NAME", "phoebus-vm")
MEM = os.environ.get("VM_MEM", "8192")
SMP = os.environ.get("VM_SMP", "8")
BOOT_TIMEOUT = int(os.environ.get("BOOT_TIMEOUT", "900"))
INSTALL_TIMEOUT = int(os.environ.get("INSTALL_TIMEOUT", "10800"))
BACKEND_SETTLE = int(os.environ.get("BACKEND_SETTLE", "30"))
# OpenCode GUI-driving knobs (test-only). The wizard focuses the first AI
# provider chip when the AI step opens, so three Tabs reach the OpenCode chip.
# After selecting it, the field order is server, password (with a keyboard
# reveal control), provider, model, load, Test, Connect.
OC_PORT = int(os.environ.get("OC_PORT", "4096"))
OC_TARGET_PORT = int(os.environ.get("OC_TARGET_PORT", "41999"))
OC_TAB_TO_CHIP = int(os.environ.get("OC_TAB_TO_CHIP", "3"))
OC_PROVIDER = os.environ.get("OC_PROVIDER", "deepseek")
OC_MODEL = os.environ.get("OC_MODEL", "deepseek-v4-pro")
OC_USE_PASSWORD = os.environ.get("OC_USE_PASSWORD", "0") in ("1", "yes", "true")
def _template_dir() -> Path:
return Path(
os.environ.get("PHOEBUS_VM_STATE", "/var/tmp/phoebus-vm")
)
def _disposable_dir() -> Path:
return Path(os.environ.get("PHOEBUS_VM_DISKS", str(_template_dir() / "disks")))
class Driver:
def __init__(self, mode: str):
self.mode = mode
self.paths = vmsafety.make_run_paths(
f"gui-{mode}", base=_template_dir(), ovmf_vars_template=OVMF_VARS_TEMPLATE
)
self.results = Results(
f"gui-{mode}", run_id=self.paths.run_id,
secrets=[LUKS_PW, USER_PW, LIVE_PW, os.environ.get("OC_PASSWORD"),
os.environ.get("OPENAI_API_KEY"), os.environ.get("DEEPSEEK_API_KEY")],
)
self.manifest: list[dict] = []
self.seq = 0
self.child: pexpect.spawn | None = None
self.log = None
self.spawned = False
self.disks: list[Path] = []
self.boot_disk = False
self._prepare()
# -- setup -------------------------------------------------------------
def _prepare(self) -> None:
if not Path(QEMU).exists() and shutil.which(QEMU) is None:
raise HarnessError(f"QEMU binary not found: {QEMU}")
if DISKS:
try:
self.disks = vmsafety.validate_writable_drives(
DISKS.split(","), _disposable_dir()
)
except vmsafety.UnsafeDiskError as exc:
raise HarnessError(str(exc)) from exc
if ISO:
iso = Path(ISO)
if not iso.is_file():
raise HarnessError(f"ISO is not a regular file: {iso}")
if not Path(OVMF_CODE).is_file():
raise HarnessError(f"OVMF_CODE is not a file: {OVMF_CODE}")
if not Path(OVMF_VARS_TEMPLATE).is_file():
raise HarnessError(f"OVMF_VARS template is not a file: {OVMF_VARS_TEMPLATE}")
# -- qemu --------------------------------------------------------------
def qemu_args(self, attach_iso: bool, boot_disk: bool) -> list[str]:
args = [
"-machine", "q35,accel=kvm",
"-cpu", "host", "-smp", SMP, "-m", MEM,
"-drive", f"if=pflash,format=raw,readonly=on,file={OVMF_CODE}",
"-drive", f"if=pflash,format=raw,file={self.paths.ovmf_vars}",
]
for disk in self.disks:
args += ["-drive", f"file={disk},if=virtio,format=qcow2"]
args += [
# A real GPU device so the compositor gets a DRM node and a seat.
"-device", "virtio-gpu-pci",
"-netdev", "user,id=n0", "-device", "virtio-net-pci,netdev=n0,romfile=",
"-device", "virtio-rng-pci",
]
if attach_iso and ISO:
args += ["-drive", f"file={ISO},media=cdrom,readonly=on"]
args += [
"-boot", "order=c" if boot_disk else "order=d",
"-vga", "none", "-display", "none",
"-serial", "stdio",
"-qmp", f"unix:{self.paths.monitor_sock},server,nowait",
"-no-reboot",
]
return args
def spawn(self, attach_iso: bool = True, boot_disk: bool = False) -> None:
if attach_iso and not os.environ.get("SKIP_ENSURE_ISO"):
script = Path(__file__).resolve().parents[3] / "scripts" / "ensure-live-iso.sh"
if script.exists():
subprocess.run(["bash", str(script)], check=True)
if self.paths.monitor_sock.exists():
self.paths.monitor_sock.unlink()
args = self.qemu_args(attach_iso, boot_disk)
print(f"[run {self.paths.run_id}] {QEMU} " + " ".join(args), flush=True)
self.child = pexpect.spawn(
QEMU, args, timeout=BOOT_TIMEOUT, encoding="utf-8",
codec_errors="replace", maxread=65536,
)
self.child.delaybeforesend = 0.4
self.log = open(self.paths.serial_log, "w")
# A prompt race can echo a credential back from the guest. Buffer
# complete terminal lines so redaction also covers split reads.
self.child.logfile_read = vmsafety.RedactingTextLog(
self.log,
[LUKS_PW, USER_PW, LIVE_PW, os.environ.get("OC_PASSWORD"),
os.environ.get("OPENAI_API_KEY"), os.environ.get("DEEPSEEK_API_KEY")],
)
self.spawned = True
self.results.pass_(
"qemu-spawned", f"{len(self.disks)} writable disk(s); iso={bool(ISO)}"
)
# -- assertions and screenshots ---------------------------------------
def run_id(self) -> str:
return self.paths.run_id
def shot(
self,
slug: str,
ui_state: str,
action: str,
expected: str,
observed: str | None = None,
) -> Path | None:
self.seq += 1
name = vmsafety.screenshot_name(self.seq, slug)
ppm = self.paths.shot_dir / (name + ".ppm")
png = self.paths.shot_dir / (name + ".png")
if not self.paths.monitor_sock.exists():
self.results.skip(f"screenshot-{name}", "no monitor socket (not spawned)")
return None
ok = False
try:
with vmsafety.QmpClient(self.paths.monitor_sock) as client:
client.screendump(ppm)
vmsafety.convert_ppm(ppm, png)
ok = ppm.exists() and ppm.stat().st_size > 0
except Exception as exc: # noqa: BLE001 - report, never crash the run
self.results.fail(f"screenshot-{name}", str(exc))
self.manifest.append({
"run_id": self.paths.run_id,
"scenario": self.mode,
"sequence": self.seq,
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"ui_state": ui_state,
"action": action,
"expected": expected,
"observed": observed if observed is not None else ("captured" if ok else "capture failed"),
"ppm": str(ppm) if ppm.exists() else None,
"png": str(png) if png.exists() else None,
})
if ok:
self.results.pass_(f"screenshot-{name}", str(png if png.exists() else ppm))
return ppm if ok else None
@staticmethod
def frame_change_ratio(before: Path | None, after: Path | None) -> float:
"""Fraction of visibly changed pixels between two guest frames."""
if before is None or after is None:
return 0.0
with Image.open(before) as previous, Image.open(after) as current:
if previous.size != current.size:
return 1.0
diff = ImageChops.difference(previous.convert("RGB"), current.convert("RGB"))
# Ignore tiny cursor and antialiasing changes. DMS overlays cover
# a substantial fraction of the screen; a clock tick does not.
changed = sum(1 for value in diff.convert("L").getdata() if value > 24)
return changed / (previous.width * previous.height)
def write_manifest(self) -> None:
path = self.paths.directory / "screenshot-manifest.json"
path.write_text(json.dumps(self.manifest, indent=2) + "\n")
# -- pexpect helpers ---------------------------------------------------
def expect(self, patterns, timeout: int, label: str):
assert self.child is not None
try:
index = self.child.expect(patterns, timeout=timeout)
except pexpect.TIMEOUT as exc:
self.results.fail(label, f"timeout after {timeout}s")
raise HarnessError(f"timeout: {label}") from exc
except pexpect.EOF as exc:
self.results.fail(label, "unexpected EOF from the VM")
raise HarnessError(f"unexpected EOF: {label}") from exc
self.last_match = self.child.match
return index
def last_group(self, index: int) -> str:
match = getattr(self, "last_match", None)
if match is None:
return ""
try:
return match.group(index)
except (IndexError, AttributeError):
return ""
def last_text(self) -> str:
match = getattr(self, "last_match", None)
return match.group(0) if match is not None else ""
def shell(self, command: str, end_marker: str, timeout: int = 180, label: str = "") -> str:
assert self.child is not None
self.child.sendline(command)
self.expect(end_marker, timeout=timeout, label=label or f"shell:{end_marker}")
return self.child.before or ""
def poll_shell(
self, command: str, pattern: str, timeout: int, label: str, record: bool = True
) -> str | None:
"""Send a command and retry until `pattern` matches or the deadline.
Bounded readiness polling instead of blind sleeps. `record=False` lets a
caller retry without recording an intermediate failure.
"""
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
assert self.child is not None
self.child.sendline(command)
try:
self.child.expect(pattern, timeout=15)
return self.child.match.group(0)
except (pexpect.TIMEOUT, pexpect.EOF):
time.sleep(2)
if record:
self.results.fail(label, f"readiness not reached within {timeout}s")
return None
def qmp(self, action: str, *args, **kwargs) -> None:
assert self.child is not None
try:
with vmsafety.QmpClient(self.paths.monitor_sock) as client:
getattr(client, action)(*args, **kwargs)
except Exception as exc: # noqa: BLE001
self.results.fail(f"qmp-{action}", str(exc))
raise
def sendkey(self, *keys: str) -> None:
for key in keys:
self.qmp("sendkey", key)
time.sleep(0.2)
# -- guest login -------------------------------------------------------
def login_live(self) -> None:
assert self.child is not None
idx = self.expect([r"login:", r"[$#] $"], timeout=BOOT_TIMEOUT, label="live-login-prompt")
if idx == 0:
self.child.sendline(LIVE_USER)
self.expect(r"[Pp]assword:", timeout=90, label="live-password-prompt")
self.child.sendline(LIVE_PW)
self.expect(r"[$#] $", timeout=180, label="live-shell")
self.results.pass_("live-login")
def set_wayland(self) -> None:
out = self.shell(
'export XDG_RUNTIME_DIR=/run/user/1000; '
'export WAYLAND_DISPLAY="$(basename "$(ls /run/user/1000/wayland-* 2>/dev/null | head -1)")"; '
'echo WAYLAND=$WAYLAND_DISPLAY',
r"WAYLAND=wayland-\d+",
timeout=60,
label="wayland-session",
)
self.results.pass_("wayland-session", out.strip()[-40:])
# -- modes -------------------------------------------------------------
def run(self) -> int:
try:
if self.mode == "gui":
self.do_gui()
elif self.mode == "opencode":
self.do_opencode()
elif self.mode == "gui-install":
self.do_gui_install()
elif self.mode == "install":
self.do_install()
elif self.mode == "boot":
self.do_boot()
elif self.mode == "desktop":
self.do_boot(desktop=True)
else:
print(__doc__)
return 2
except HarnessError as exc:
self.results.fail("harness", str(exc))
except Exception as exc: # noqa: BLE001
self.results.fail("harness-exception", repr(exc))
finally:
if self.spawned and self.child is not None and self.child.isalive():
try:
self.shot("final-state", "final", "end of run", "last observed state")
except Exception: # noqa: BLE001
pass
self.teardown()
self.write_manifest()
results_path = self.results.write(self.paths.results_json)
print(self.results.summary(), flush=True)
print(f"results: {results_path}", flush=True)
print(f"serial: {self.paths.serial_log}", flush=True)
print(f"shots: {self.paths.shot_dir}", flush=True)
return self.results.exit_code()
def teardown(self) -> None:
if self.child is not None:
redactor = self.child.logfile_read
try:
self.child.close(force=True)
except Exception: # noqa: BLE001
pass
if isinstance(redactor, vmsafety.RedactingTextLog):
redactor.finish()
if self.log is not None:
try:
self.log.close()
except Exception: # noqa: BLE001
pass
# Cleanup is limited to this run's own socket.
try:
if self.paths.monitor_sock.exists():
self.paths.monitor_sock.unlink()
except OSError:
pass
# -- GUI journey -------------------------------------------------------
def do_gui(self) -> None:
self.spawn(attach_iso=True, boot_disk=False)
self.login_live()
welcome = self.poll_shell(
"echo WELCOME=$(pgrep -fc 'quickshell.*phoebus-welcome' 2>/dev/null || echo 0)",
r"WELCOME=([1-9][0-9]*)",
timeout=int(os.environ.get("WELCOME_TIMEOUT", "180")),
label="welcome-window",
)
self.results.assert_true(
"welcome-autostarted", welcome is not None, "welcome window did not appear"
)
self.shot("01-welcome", "welcome window", "observe at startup",
"branded welcome window with a dominant Install action")
# Markers are built with command substitution so the shell's echo of the
# typed command cannot contain them; a match therefore proves output.
checks = (
"echo ===GUI-CHECKS-$(printf START)===; "
"test -e /etc/phoebus/live && echo LIVE-$(printf MARKER); "
"test -s /usr/share/phoebus/branding/wallpaper.svg && echo BRAND-$(printf WALLPAPER); "
"test -s /usr/share/icons/hicolor/scalable/apps/phoebus.svg && echo BRAND-$(printf ICON); "
"test -s /etc/xdg/quickshell/phoebus-install/shell.qml && echo INSTALL-$(printf QML); "
"test -s /usr/share/applications/phoebus-install.desktop && echo DESKTOP-$(printf ENTRY); "
"test -f /home/phoebus/.config/DankMaterialShell/.firstlaunch && echo ONBOARDING-$(printf SUPPRESSED); "
"echo ===GUI-CHECKS-$(printf END)==="
)
payload = self.shell(checks, "===GUI-CHECKS-END===", timeout=180, label="payload-checks")
for marker in (
"LIVE-MARKER", "BRAND-WALLPAPER", "BRAND-ICON", "INSTALL-QML", "DESKTOP-ENTRY",
):
self.results.assert_true(
f"payload-{marker.lower()}", marker in payload, f"{marker} not in command output"
)
if "ONBOARDING-SUPPRESSED" not in payload:
self.results.skip("onboarding-suppressed", "first-run marker not present")
self.set_wayland()
# Disk protection is a deterministic backend property. The bridge JSON
# is long and the serial console wraps it, so a small guest-side summary
# is emitted instead of parsing wrapped JSON. Every marker is built with
# command substitution so the echoed command cannot be mistaken for it.
self.shell(
"printf '%s\\n%s\\n' '{\"id\":1,\"op\":\"disks\"}' '{\"id\":2,\"op\":\"quit\"}' "
"> /tmp/req.json; echo REQ-$(printf DONE)",
"REQ-DONE",
timeout=30,
label="disk-probe-request",
)
summary = self.shell(
"sudo -n /usr/local/bin/phoebus-installer --gui-bridge < /tmp/req.json "
"2>/dev/null | head -1 > /tmp/disks.json; "
"echo DISKS-$(printf COUNT)=$(grep -o '{[^{}]*}' /tmp/disks.json | wc -l); "
"echo LIVE-$(printf COUNT)=$(grep -o '{[^{}]*\"live_medium\":true[^{}]*}' "
"/tmp/disks.json | wc -l); "
"echo LIVE-$(printf PROTECTED)=$(grep -o '{[^{}]*\"live_medium\":true[^{}]*}' "
"/tmp/disks.json | grep -c '\"protected\":true'); "
"echo PROBE-$(printf DONE)",
"PROBE-DONE",
timeout=120,
label="disk-probe",
)
self._assert_live_medium_protected(summary)
# Activate the focused welcome action with Return (the real launcher).
# Retry a few times; a transient focus race must not be recorded as a
# passing launcher test, and the fallback launch is recorded as a FAIL.
self.shot("01b-focus", "welcome focused", "before launcher key",
"primary Install action visibly focused")
installed = None
for _attempt in range(3):
self.sendkey("ret")
installed = self.poll_shell(
"echo INST=$(pgrep -fc 'quickshell.*phoebus-install' 2>/dev/null || echo 0)",
r"INST=([1-9][0-9]*)",
timeout=30,
label="launcher-opens-installer",
record=False,
)
if installed is not None:
break
if installed is None:
# Fallback launch for diagnostics only: the launcher test stays failed.
self.results.fail(
"launcher-opens-installer",
"Return did not open the installer; using a diagnostic fallback launch",
)
self.shell(
"setsid /usr/local/bin/phoebus-install >/tmp/install-gui.log 2>&1 & echo LAUNCHED",
"LAUNCHED",
timeout=30,
label="fallback-launch",
)
time.sleep(7)
else:
self.results.pass_("launcher-opens-installer", "welcome action opened the installer")
# The frontend log must not contain bridge errors or a crash.
logdump = self.shell(
"echo QMLLOG-$(printf START); "
"cat /run/user/1000/quickshell/by-id/*/log.qslog 2>/dev/null | "
"grep -aiE 'phoebus-bridge|error|failed|handshake' | tail -30; "
"echo QMLLOG-$(printf END)",
"QMLLOG-END",
timeout=60,
label="frontend-log",
)
hard = [ln for ln in logdump.splitlines()
if "phoebus-bridge:" in ln and "error" in ln.lower()]
self.results.assert_true("frontend-no-bridge-errors", not hard, "; ".join(hard[:3]))
self.shot("02-installer-start", "installer wizard", "opened from the welcome",
"language and region step with visible focus")
time.sleep(BACKEND_SETTLE)
# Close the welcome so the tiled installer holds the keyboard.
self.shell(
"pkill -f 'quickshell.*phoebus-wel[c]ome' || true; sleep 2; echo CLOSED",
"CLOSED",
timeout=30,
label="close-welcome",
)
# Duplicate prevention: a second launch must not create a second session.
self.shell(
"pgrep -f 'quickshell.*phoebus-install' | wc -l > /tmp/n1; "
"/usr/local/bin/phoebus-install >/tmp/install-gui2.log 2>&1; "
"pgrep -f 'quickshell.*phoebus-install' | wc -l > /tmp/n2; "
"echo DUP n1=$(cat /tmp/n1) n2=$(cat /tmp/n2)",
r"DUP n1=(\d+) n2=(\d+)",
timeout=60,
label="duplicate-launch",
)
n1, n2 = int(self.last_group(1)), int(self.last_group(2))
# Exactly one session before and after: two pre-existing sessions must
# not pass as "no duplicate".
self.results.assert_true(
"exactly-one-installer-session", n1 == 1 and n2 == 1,
f"n1={n1} n2={n2}",
)
# The installer opens directly on step 1 (language and keyboard): the
# duplicate step-0 welcome was removed. Capture it and walk with
# Ctrl+Right from the frontend's own state, not a keystroke count.
at_language = self._goto_step(1)
self.results.assert_true(
"language-step-reached",
at_language is not None and at_language["step"] == 1,
f"the wizard did not open on the language step: {at_language}",
)
self.shot("03-language", "language and keyboard", "installer opened",
"the language and keyboard step is shown")
# Regression: the suggestion list supports arrow-key navigation and
# Enter selection, with the value stored being the identifier.
self._gui_type("de_DE")
self.shot("03a-locale-filter", "locale search", "typed de_DE",
"the locale list is filtered from keyboard input")
time.sleep(1.0)
self.sendkey("down")
time.sleep(0.5)
self.sendkey("ret")
time.sleep(1.0)
picked = self._frontend_state()
self.results.assert_true(
"keyboard-suggestion-select",
picked is not None and picked["step"] == 1
and picked["locale"].startswith("de_DE"),
f"Down/Enter did not select the highlighted suggestion: {picked}",
)
self.shot("03b-locale-selected", "locale selected", "Down then Enter",
"the highlighted locale is selected")
for slug, label, target in [
("04-network", "network and prerequisites", 2),
("05-ai-model", "AI model connection", 3),
]:
reached = self._goto_step(target)
self.results.assert_true(
f"{slug}-reached", reached is not None and reached["step"] == target,
f"did not reach {label}: {reached}",
)
self.shot(slug, label, "Ctrl+Right", "next wizard step")
if not self._installer_alive():
self.results.fail("installer-survived-navigation", f"crashed at {slug}")
return
# Advancing without a connected model must be refused: the process must
# still be alive and the backend must not have started an install.
self.sendkey("ctrl-right")
time.sleep(2)
self.shot("06-ai-blocked", "AI model blocked", "Ctrl+Right without a model",
"advance refused; no install started")
blocked = self._bridge_has_no_install_start()
self.results.assert_true(
"ai-step-blocks-install", blocked,
"the AI step allowed advancement without a connected model",
)
# Behavioral evidence from the frontend's own state lines: the wizard is
# still on the AI step (index 3) and has not reported a connection, and
# it shows an inline error. A surviving process or a screenshot would
# not prove any of this.
state = self._frontend_state()
if state is None:
self.results.fail("ai-page-remains", "no frontend state line found")
else:
self.results.assert_true(
"ai-page-remains", state["step"] == 3 and not state["aiConnected"],
f"last state: {state}",
)
self.results.assert_true(
"ai-error-shown", bool(state["aiError"]),
f"the AI step showed no inline error: {state}",
)
self.results.pass_("gui-walk-completed")
def _frontend_state(self) -> dict | None:
"""Read the frontend's last secret-free state line.
The pattern is built from a variable so the shell echo of the command
cannot be mistaken for a real state line.
"""
assert self.child is not None
self.child.sendline(
'P=phoebus-; grep -ah "${P}state " '
"/run/user/1000/quickshell/by-id/*/log.qslog 2>/dev/null | tail -1; "
"echo STATE-$(printf END)"
)
try:
self.child.expect(r"STATE-END[\s\S]*?[$#] ", timeout=30)
except (pexpect.TIMEOUT, pexpect.EOF):
return None
raw_text = self.child.before or ""
blocks = [b for b in raw_text.split("phoebus-state") if b.strip()]
if not blocks:
return None
last_block = blocks[-1]
step = ai = tested = review = None
applied = 0
disk = ""
locale = ""
keymap = ""
phase = ""
confirmed = False
focus = ""
err = ""
match = re.search(r"step=(\d+)", last_block)
if match:
step = int(match.group(1))
match = re.search(r"phase=(\w+)", last_block)
if match:
phase = match.group(1)
match = re.search(r"aiConnected=(true|false)", last_block)
if match:
ai = match.group(1) == "true"
match = re.search(r"aiTested=(true|false)", last_block)
if match:
tested = match.group(1) == "true"
match = re.search(r"aiReview=(true|false)", last_block)
if match:
review = match.group(1) == "true"
match = re.search(r"aiApplied=(\d+)", last_block)
if match:
applied = int(match.group(1))
match = re.search(r"disk=(\S*)", last_block)
if match:
disk = match.group(1)
match = re.search(r"locale=(\S*)", last_block)
if match:
locale = match.group(1)
match = re.search(r"keymap=(\S*)", last_block)
if match:
keymap = match.group(1)
match = re.search(r"confirm=(true|false)", last_block)
if match:
confirmed = match.group(1) == "true"
match = re.search(r"focus=(\S*)", last_block)
if match:
focus = match.group(1)
match = re.search(r"aiError=(.*?) aiTest=", last_block)
if match:
err = match.group(1).strip()
if step is None:
return None
return {
"step": step,
"phase": phase,
"aiConnected": bool(ai),
"aiTested": bool(tested),
"aiApplied": applied,
"aiReview": bool(review),
"disk": disk,
"locale": locale,
"keymap": keymap,
"confirm": confirmed,
"focus": focus,
"aiError": err,
}
def wait_state(self, predicate, timeout: int):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
state = self._frontend_state()
if state is not None and predicate(state):
return state
time.sleep(2)
return None
def _goto_step(self, target: int, *, timeout: int = 90) -> dict | None:
"""Advance to a wizard step using the frontend's own state.
The installer now starts directly on step 1 (the duplicate step-0
welcome was removed), so counting keystrokes is brittle. Poll the
secret-free state line and advance with Ctrl+Right until the step is
reached: a state-based wait, not a fixed number of keypresses.
"""
state = self._frontend_state()
if state is not None and state["step"] >= target:
return state
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
self.sendkey("ctrl-right")
time.sleep(1.5)
state = self._frontend_state()
if state is not None and state["step"] >= target:
return state
return state
def _install_progress(self) -> str:
"""A short, secret-free description of the current install state."""
state = self._frontend_state()
if state is None:
return "no frontend state line available"
return (
f"step={state['step']} phase={state['phase']} "
f"aiConnected={state['aiConnected']}"
)
def do_opencode(self) -> None:
"""Drive the OpenCode AI step through the real graphical UI.
The model is reached from the guest through a loopback relay (the same
shape as an SSH tunnel), so the connection satisfies the transport
policy while staying host-local or host+LAN only.
"""
self.spawn(attach_iso=True, boot_disk=False)
self.login_live()
welcome = self.poll_shell(
"echo WELCOME=$(pgrep -fc 'quickshell.*phoebus-welcome' 2>/dev/null || echo 0)",
r"WELCOME=([1-9][0-9]*)", timeout=int(os.environ.get("WELCOME_TIMEOUT", "180")),
label="welcome-window",
)
self.results.assert_true("welcome-autostarted", welcome is not None, "welcome did not appear")
self.set_wayland()
# Start the guest-local relay to the host OpenCode server.
self.shell(
f"curl -fsSL {SERVE_URL}/oc-forward.py -o /tmp/oc-forward.py && "
f"chmod 755 /tmp/oc-forward.py && "
f"setsid python3 /tmp/oc-forward.py {OC_PORT} 10.0.2.2 {OC_TARGET_PORT} "
f">/tmp/oc-forward.log 2>&1 < /dev/null & echo FWD-$(printf START)",
"FWD-START", timeout=60, label="forwarder-start",
)
ready = self.poll_shell(
f"curl -s -m 5 -o /dev/null -w 'HEALTH=%{{http_code}}' "
f"http://127.0.0.1:{OC_PORT}/global/health",
r"HEALTH=(200|401)", timeout=60, label="forwarder-ready",
)
self.results.assert_true(
"opencode-reachable-from-guest", ready is not None,
"the guest relay could not reach the OpenCode server",
)
self.sendkey("ret")
opened = self.poll_shell(
"echo INST=$(pgrep -fc 'quickshell.*phoebus-install' 2>/dev/null || echo 0)",
r"INST=([1-9][0-9]*)", timeout=int(os.environ.get("INSTALLER_OPEN_TIMEOUT", "90")),
label="launcher-opens-installer", record=False,
)
if opened is None:
self.results.fail("launcher-opens-installer", "Return did not open the installer")
return
self.results.pass_("launcher-opens-installer", "welcome action opened the installer")
time.sleep(BACKEND_SETTLE)
self.shell(
"pkill -f 'quickshell.*phoebus-wel[c]ome' || true; sleep 2; echo CLOSED",
"CLOSED", timeout=30, label="close-welcome",
)
# Navigate to the AI step from step 1 (language -> network -> AI).
state = self._goto_step(3)
self.results.assert_true(
"ai-step-reached", state is not None and state["step"] == 3,
f"frontend state: {state}",
)
self.shot("oc-01-ai", "AI step", "Ctrl+Right to step 3", "the AI model step is shown")
# Select the "OpenCode agent" chip with the keyboard. The AI step opens
# with the first provider chip focused.
for _ in range(OC_TAB_TO_CHIP):
self.sendkey("tab")
time.sleep(0.3)
self.sendkey("ret")
time.sleep(1)
self.shot("oc-02-selected", "OpenCode selected", "Tab to the chip and Return",
"server, password, provider and model fields appear")
# Enter the connection through the UI. Focus is on the chip after
# selection; Tab order is server, password (input + reveal), provider,
# model, load, Test. Ctrl+A selects the existing default before typing.
def type_field(tabs: int, value: str, secret_file: str | None = None) -> None:
# `wtype` (Wayland) and QEMU `sendkey` (hardware) are two input
# paths; settle between them or the next key can truncate the text.
for _ in range(tabs):
self.sendkey("tab")
time.sleep(0.4)
self.sendkey("ctrl-a")
time.sleep(0.5)
if secret_file:
# wtype's `-` reads stdin; the credential never enters argv
# or the serial command echo.
self.shell(
f'wtype - < {secret_file} >/dev/null 2>&1; echo TYPED',
"TYPED", timeout=30, label="type-secret",
)
else:
self.shell(
f"wtype {shlex.quote(value)} >/dev/null 2>&1; echo TYPED",
"TYPED", timeout=30, label="type-field",
)
time.sleep(1.4)
# Clear the selection highlight so a screenshot shows the value.
self.sendkey("end")
time.sleep(0.3)
type_field(1, f"http://127.0.0.1:{OC_PORT}")
if OC_USE_PASSWORD:
self.shell(
f"curl -fsSL {SERVE_URL}/oc.pw -o /tmp/oc.pw && chmod 600 /tmp/oc.pw "
"&& echo PW-$(printf LOADED)",
"PW-LOADED", timeout=30, label="fetch-password",
)
type_field(1, "", secret_file="/tmp/oc.pw")
self.sendkey("tab") # the keyboard reveal control
else:
self.sendkey("tab") # the empty password field
time.sleep(0.25)
self.sendkey("tab") # the keyboard reveal control
time.sleep(0.25)
type_field(1, OC_PROVIDER)
type_field(1, OC_MODEL)
self.shot("oc-02b-fields", "OpenCode fields", "typed server/provider/model",
"the fields hold the entered values")
self._tab_until_focus("Test")
self.sendkey("ret")
time.sleep(3)
self.shot("oc-03-test", "Test connection", "Return on Test connection",
"a real model response is obtained and validated")
tested = self.wait_state(lambda s: s["aiTested"], timeout=180)
self.results.assert_true(
"opencode-test-connection", tested is not None,
"Test connection did not obtain a valid model response",
)
# The next focus is the "Connect and validate" button.
self._tab_until_focus("Connect and validate")
self.sendkey("ret")
self.shot("oc-04-connect", "Connect and validate", "Return on Connect and validate",
"a validated proposal is applied; Next is enabled")
connected = self.wait_state(lambda s: s["aiConnected"], timeout=240)
self.results.assert_true(
"opencode-validated-proposal",
connected is not None,
f"Connect and validate did not validate a proposal: {connected}",
)
if connected is not None:
# A validated proposal may legitimately contain zero changes.
self.results.pass_(
"opencode-proposal-applied",
f"applied={connected['aiApplied']} change(s)",
)
# Regression: editing the model must invalidate the green "validated"
# indicator, because the backend would require revalidation.
self._shifttab_until_focus("model")
self._gui_type(OC_MODEL + "-changed")
edited = self.wait_state(lambda s: not s["aiConnected"], timeout=30)
self.results.assert_true(
"ai-invalidated-on-model-edit", edited is not None,
"editing the model left the AI connection green",
)
self.results.pass_("opencode-gui-completed")
# -- full graphical installation --------------------------------------
_PLAIN_KEYS = {
" ": "spc", "-": "minus", "=": "equal", "[": "bracketleft",
"]": "bracketright", "\\": "backslash", ";": "semicolon",
"'": "apostrophe", ",": "comma", ".": "dot", "/": "slash", "`": "grave",
}
_SHIFT_KEYS = {
"_": "minus", "+": "equal", ":": "semicolon", '"': "apostrophe",
"<": "comma", ">": "dot", "?": "slash", "{": "bracketleft",
"}": "bracketright", "|": "backslash", "~": "grave",
}
def _tabs(self, count: int) -> None:
for _ in range(count):
self.sendkey("tab")
time.sleep(0.4)
def _tab_until_focus(self, target_substr: str, max_tabs: int = 15) -> bool:
target_lower = target_substr.lower().replace(" ", "_")
target_words = target_lower.split("_")
for _ in range(max_tabs):
state = self._frontend_state()
if state is not None:
current_focus = state.get("focus", "").lower()
words = current_focus.split("_")
if target_lower == current_focus or all(w in words for w in target_words):
return True
self.sendkey("tab")
time.sleep(0.4)
state = self._frontend_state()
if state is not None:
current_focus = state.get("focus", "").lower()
words = current_focus.split("_")
return target_lower == current_focus or all(w in words for w in target_words)
return False
def _shifttab_until_focus(self, target_substr: str, max_tabs: int = 15) -> bool:
target_lower = target_substr.lower().replace(" ", "_")
target_words = target_lower.split("_")
for _ in range(max_tabs):
state = self._frontend_state()
if state is not None:
current_focus = state.get("focus", "").lower()
words = current_focus.split("_")
if target_lower == current_focus or all(w in words for w in target_words):
return True
self.sendkey("shift-tab")
time.sleep(0.4)
state = self._frontend_state()
if state is not None:
current_focus = state.get("focus", "").lower()
words = current_focus.split("_")
return target_lower == current_focus or all(w in words for w in target_words)
return False
def _send_chars(self, text: str) -> None:
"""Type text as hardware key events.
The QEMU/HMP `wtype` path used by `opencode` mode can leave the
compositor focus ambiguous. The full-install journey types every field
with real key events so focus is always exactly the focused control.
"""
for ch in text:
if ch.isalpha():
self.sendkey(ch if ch.islower() else "shift-" + ch.lower())
elif ch.isdigit():
self.sendkey(ch)
elif ch in self._PLAIN_KEYS:
self.sendkey(self._PLAIN_KEYS[ch])
elif ch in self._SHIFT_KEYS:
self.sendkey("shift-" + self._SHIFT_KEYS[ch])
else:
raise HarnessError(f"no QEMU key for character {ch!r}")
time.sleep(0.1)
def _gui_type(self, value: str) -> None:
self.sendkey("ctrl-a")
time.sleep(0.3)
self._send_chars(value)
time.sleep(1.0)
def _accept_ai_review(self, applied: int) -> bool:
"""Accept a proposed-change review if one was applied.
The Accept button follows Connect (and possibly the conditional Retry
button), so try a few Tab/Return pairs and stop as soon as the review
is accepted.
"""
if applied <= 0:
return True
state = self._frontend_state()
if state is not None and state["aiReview"]:
return True
if self._tab_until_focus("Accept"):
self.sendkey("ret")
time.sleep(1.5)
accepted = self.wait_state(lambda s: s["aiReview"], timeout=30)
if accepted is not None and accepted["aiReview"]:
return True
for _ in range(4):
self.sendkey("tab")
time.sleep(0.4)
self.sendkey("ret")
time.sleep(1.5)
state = self._frontend_state()
if state is not None and state["aiReview"]:
return True
return False
def do_gui_install(self) -> None:
"""Complete installation through the visible wizard.
`appliance-guest-gui.sh` installs non-interactively, which does not
prove the graphical journey. This mode presses the real controls: it
connects OpenCode through the UI, accepts the reviewed AI changes,
selects a disk, sets the account, triggers the direct "Install now"
action, and checks the frontend reports completion. The disk must be a
disposable qcow2.
"""
if not self.disks:
self.results.blocked("gui-install-disk", "no writable disk configured")
return
self.spawn(attach_iso=True, boot_disk=False)
self.login_live()
welcome = self.poll_shell(
"echo WELCOME=$(pgrep -fc 'quickshell.*phoebus-welcome' 2>/dev/null || echo 0)",
r"WELCOME=([1-9][0-9]*)",
timeout=int(os.environ.get("WELCOME_TIMEOUT", "180")),
label="welcome-window",
)
self.results.assert_true("welcome-autostarted", welcome is not None, "welcome did not appear")
self.set_wayland()
# Preseed the wizard defaults so the run is bounded (safe kernel, no
# desktop) while the wizard, validation, AI gate and the direct
# "Install now" authorization remain the real graphical path.
# The live privilege policy no longer grants bare `install`, so stage
# the config and authenticate the one privileged write with the
# live-only credential, fetched to a 0600 file (never on the command
# line and never in the serial transcript).
self.shell(
f"curl -fsSL {SERVE_URL}/test.conf -o /tmp/gui-install.conf && "
f"curl -fsSL {SERVE_URL}/live.pw -o /tmp/live.pw && chmod 600 /tmp/live.pw && "
"sudo -S -p '' install -D -m 0644 /tmp/gui-install.conf /etc/phoebus/install.conf "
"< /tmp/live.pw && echo PRESEED-$(printf DONE)",
"PRESEED-DONE", timeout=60, label="preseed-config",
)
# Update the live guest with the current static binaries and UI
self.shell(
f"curl -fsSL {SERVE_URL}/update.sh -o /tmp/update.sh && "
"sudo -S -p '' bash /tmp/update.sh < /tmp/live.pw && "
"echo GUEST_PAYLOAD-$(printf UPDATED)",
"GUEST_PAYLOAD-UPDATED", timeout=120, label="update-guest-payload",
)
# Start the loopback relay to the host OpenCode server.
self.shell(
f"curl -fsSL {SERVE_URL}/oc-forward.py -o /tmp/oc-forward.py && "
f"chmod 755 /tmp/oc-forward.py && "
f"setsid python3 /tmp/oc-forward.py {OC_PORT} 10.0.2.2 {OC_TARGET_PORT} "
f">/tmp/oc-forward.log 2>&1 < /dev/null & echo FWD-$(printf START)",
"FWD-START", timeout=60, label="forwarder-start",
)
ready = self.poll_shell(
f"curl -s -m 5 -o /dev/null -w 'HEALTH=%{{http_code}}' "
f"http://127.0.0.1:{OC_PORT}/global/health",
r"HEALTH=(200|401)", timeout=60, label="forwarder-ready",
)
self.results.assert_true(
"opencode-reachable-from-guest", ready is not None,
"the guest relay could not reach the OpenCode server",
)
# Launch the installer from the welcome action.
opened = None
for _attempt in range(5):
self.sendkey("ret")
opened = self.poll_shell(
"echo INST=$(pgrep -fc 'quickshell.*phoebus-install' 2>/dev/null || echo 0)",
r"INST=([1-9][0-9]*)", timeout=15,
label="launcher-opens-installer",
record=False,
)
if opened is not None:
break
time.sleep(1)
if opened is None:
self.shell(
"setsid /usr/local/bin/phoebus-install >/tmp/install-gui.log 2>&1 & echo LAUNCHED",
"LAUNCHED",
timeout=30,
label="fallback-install-launch",
)
time.sleep(5)
opened = self.poll_shell(
"echo INST=$(pgrep -fc 'quickshell.*phoebus-install' 2>/dev/null || echo 0)",
r"INST=([1-9][0-9]*)", timeout=int(os.environ.get("INSTALLER_OPEN_TIMEOUT", "90")),
label="launcher-opens-installer",
)
self.results.assert_true("launcher-opens-installer", opened is not None,
"welcome action did not open the installer")
time.sleep(BACKEND_SETTLE)
self.shell(
"pkill -f 'quickshell.*phoebus-wel[c]ome' || true; sleep 2; echo CLOSED",
"CLOSED", timeout=30, label="close-welcome",
)
# The installer opens on step 1; advance to the AI step.
state = self._goto_step(3)
self.results.assert_true("gui-install-ai-step", state is not None and state["step"] == 3,
f"AI step not reached: {state}")
self.shot("gi-01-ai", "AI step", "Ctrl+Right to step 3", "AI connection step")
# Select OpenCode and connect through the UI.
for _ in range(OC_TAB_TO_CHIP):
self.sendkey("tab")
time.sleep(0.3)
self.sendkey("ret")
time.sleep(1)
self._tabs(1) # chip -> server
self._gui_type(f"http://127.0.0.1:{OC_PORT}")
self._tab_until_focus("Test")
self.sendkey("ret")
tested = self.wait_state(lambda s: s["aiTested"], timeout=300)
self.results.assert_true("gui-install-ai-tested", tested is not None,
"Test connection did not succeed")
self._tab_until_focus("Connect and validate")
self.sendkey("ret")
connected = self.wait_state(lambda s: s["aiConnected"], timeout=240)
self.results.assert_true("gui-install-ai-connected", connected is not None,
f"Connect and validate failed: {connected}")
applied = connected["aiApplied"] if connected else 0
self.results.assert_true(
"gui-install-ai-reviewed", self._accept_ai_review(applied),
"the AI review could not be accepted",
)
self.shot("gi-02-ai-reviewed", "AI reviewed", "Accept changes", "review accepted")
# Destination disk: pick the first selectable disk.
self._goto_step(4)
self.sendkey("tab")
time.sleep(0.5)
self.sendkey("ret")
time.sleep(1)
state = self._frontend_state()
self.results.assert_true(
"gui-install-disk-selected", state is not None and state["disk"] != "",
f"no destination disk was selected: {state}",
)
self.shot("gi-03-disk", "disk selected", "Tab and Return on the first disk",
"a destination disk is selected")
# Regression: re-checking network readiness from the network step must
# not reset the wizard or clear the chosen disk, and unrelated
# navigation must preserve the validated AI connection.
selected = state["disk"]
self.sendkey("ctrl-left") # disk -> AI
time.sleep(1.5)
self.sendkey("ctrl-left") # AI -> network
time.sleep(1.5)
before = self._frontend_state()
self.results.assert_true(
"back-nav-preserves-ai", before is not None and before["aiConnected"],
f"back-navigation lost the AI validation: {before}",
)
self.sendkey("ret") # Re-check (focused when the network step opens)
time.sleep(4)
after = self._frontend_state()
self.results.assert_true(
"readiness-refresh-preserves-wizard",
after is not None and after["step"] == 2 and after["disk"] == selected,
f"re-check reset the wizard or cleared the disk: before={before} after={after}",
)
# Walk forward again; the disk selection must still be present.
self.sendkey("ctrl-right") # network -> AI
time.sleep(2)
self.sendkey("ctrl-right") # AI -> disk
time.sleep(3)
state = self._frontend_state()
self.results.assert_true(
"disk-survives-backtrack",
state is not None and state["step"] == 4 and state["disk"] == selected,
f"the disk selection did not survive backtracking: {state}",
)
# Account and encryption. The current product has a single Username
# field, a Computer name, Password and Confirm password; the
# "Use same password to encrypt disk" toggle is on by default, so the
# separate LUKS passphrase fields are hidden and must not be driven.
state = self._goto_step(5)
self.results.assert_true("gui-install-account-step", state is not None and state["step"] == 5,
f"account step not reached: {state}")
# Focus opens on Username.
self._tab_until_focus("Username")
self._gui_type(USER_NAME)
self._tab_until_focus("Computer name")
self._gui_type(HOST_NAME)
self._tab_until_focus("Password")
self._gui_type(USER_PW)
self._tab_until_focus("Confirm password")
self._gui_type(USER_PW)
self.shot("gi-04-account", "account and encryption", "typed credentials",
"username, computer name and matching password entered")
# Review. The product now uses a direct "Install now" action: typing
# ERASE and the confirmation checkbox were intentionally removed, and
# Ctrl+Right on the review step runs exactly that action. The backend
# still enforces approval binding and disk revalidation.
state = self._goto_step(6)
self.results.assert_true("gui-install-review-step", state is not None and state["step"] == 6,
f"review step not reached: {state}")
self.shot("gi-05-review", "review and confirmation", "review shown",
"backend facts and the installation plan")
self.sendkey("ctrl-right") # Install now
started = time.monotonic()
self.shot("gi-06-installing", "installing", "Ctrl+Right on review",
"the installation has started")
# Approval replay: once the install is underway, submitting the review
# again must not crash the installer or regress it to a failure. The
# backend binds the authorization to the reviewed plan; the frontend
# must survive a repeated action.
underway = self.wait_state(
lambda s: s["phase"] in ("installing", "done", "failed"), timeout=180
)
if underway is not None:
self.sendkey("ctrl-right")
time.sleep(3)
replay = self._frontend_state()
self.results.assert_true(
"gui-install-replay-safe",
replay is not None and replay["phase"] != "failed" and self._installer_alive(),
f"re-submitting the review was not handled safely: {replay}",
)
done = self.wait_state(
lambda s: s["phase"] in ("done", "failed"),
timeout=INSTALL_TIMEOUT,
)
if done is None:
elapsed = int(time.monotonic() - started)
self.results.fail(
"gui-install-completes",
f"the wizard did not report completion after {elapsed}s "
f"({self._install_progress()})",
)
return
if done["phase"] != "done":
try:
dump = self.shell(
"cat /run/phoebus-installer/install.log 2>/dev/null | tail -n 120; echo LOG-$(printf DONE)",
"LOG-DONE", timeout=30, label="dump-install-log"
)
print("\n=== INSTALLER FAILURE LOG ===\n" + dump + "\n=== END LOG ===\n", flush=True)
except Exception:
pass
self.results.assert_true(
"gui-install-completes", done["phase"] == "done",
f"the installer reported failure: {done}",
)
self.shot("gi-07-complete", "completion", "installation finished",
"the completion screen is shown")
if done["phase"] == "done":
self.results.pass_("gui-install-journey")
# Power off cleanly so the installed disk is flushed for the boot mode.
self.shell("sudo -n systemctl poweroff >/dev/null 2>&1 || true; echo POW",
"POW", timeout=60, label="poweroff")
try:
self.child.expect(pexpect.EOF, timeout=300)
except (pexpect.TIMEOUT, pexpect.EOF):
pass
def _installer_alive(self) -> bool:
assert self.child is not None
self.child.sendline(
"echo ALIVE=$(pgrep -fc 'quickshell.*phoebus-install' 2>/dev/null || echo 0)"
)
try:
self.child.expect(r"ALIVE=(\d+)[\s\S]*?[$#] ", timeout=30)
except (pexpect.TIMEOUT, pexpect.EOF):
return False
return int(self.child.match.group(1)) >= 1
def _bridge_has_no_install_start(self) -> bool:
# The installer log records storage_prepare only when a backend run
# actually starts; at this point it must not have completed it. The
# marker is built with command substitution so the echoed command cannot
# be mistaken for the result.
assert self.child is not None
self.child.sendline(
"echo NOSTART=$(grep -l 'stage complete: storage_prepare' "
"/run/phoebus-installer/install.log /tmp/phoebus-installer/install.log "
"2>/dev/null | wc -l)"
)
try:
self.child.expect(r"NOSTART=(\d+)[\s\S]*?[$#] ", timeout=30)
except (pexpect.TIMEOUT, pexpect.EOF):
return False
return self.child.match.group(1).strip() == "0"
@staticmethod
def _first_int(text: str, key: str) -> int | None:
match = re.search(re.escape(key) + r"=(\d+)", text)
return int(match.group(1)) if match else None
def _assert_live_medium_protected(self, summary: str) -> None:
total = self._first_int(summary, "DISKS-COUNT")
live = self._first_int(summary, "LIVE-COUNT")
protected = self._first_int(summary, "LIVE-PROTECTED")
if total is None or live is None or protected is None:
self.results.fail(
"disks-probe",
f"could not read the disk summary (total={total} live={live} protected={protected})",
)
return
self.results.assert_true("disks-listed", total >= 1, "no disks reported")
if live >= 1:
self.results.assert_true(
"live-medium-protected",
protected == live,
f"{protected}/{live} live media were protected",
)
else:
self.results.skip("live-medium-protected", "no live medium in the probe")
# -- non-interactive install ------------------------------------------
def do_install(self) -> None:
if not self.disks:
self.results.blocked("install-disk", "no writable disk configured")
return
self.spawn(attach_iso=True, boot_disk=False)
self.login_live()
self.shell(
f"curl -fsSL {SERVE_URL}/appliance-guest-gui.sh -o /tmp/gi.sh && bash /tmp/gi.sh",
r"===INSTALL-RC=([0-9]+)===",
timeout=INSTALL_TIMEOUT,
label="install-completes",
)
rc = self.child.match.group(1) if self.child else "?"
self.results.assert_true("install-rc-zero", rc == "0", f"INSTALL-RC={rc}")
# The secret-leak marker is emitted by the guest script.
assert self.child is not None
try:
self.child.expect(r"===NO_SECRET_LEAK===", timeout=120)
self.results.pass_("no-secret-leak")
except (pexpect.TIMEOUT, pexpect.EOF):
self.results.fail("no-secret-leak", "the guest did not report NO_SECRET_LEAK")
try:
self.child.expect(["===POWEROFF===", pexpect.EOF], timeout=600)
except pexpect.TIMEOUT:
self.results.fail("poweroff", "the guest did not power off")
try:
self.child.expect(pexpect.EOF, timeout=300)
except pexpect.TIMEOUT:
self.results.fail("install-eof", "QEMU did not exit after poweroff")
# -- installed graphical seat ------------------------------------------
def verify_installed_desktop(self) -> None:
"""Authenticate through greetd on the virtual seat and exercise it.
The serial login is only the observer. All login and shortcut keys
below enter through QEMU's virtio keyboard on the graphical seat.
"""
greet = self.poll_shell(
"echo GREET=$(pgrep -xc tuigreet || echo 0)",
r"GREET=([1-9][0-9]*)",
timeout=90,
label="desktop-greeter",
record=False,
)
if greet is None:
self.results.fail("desktop-greeter", "tuigreet is not running on the installed system")
return
self.results.pass_("desktop-greeter")
# The installed greetd config owns VT7; VT1 still shows dracut's
# earlier LUKS prompt after the serial console unlocked the disk.
self.sendkey("ctrl-alt-f7")
self.shot("desktop-greeter", "installed greeter", "observe seat login",
"tuigreet asks for an authenticated user login")
self._send_chars(USER)
self.sendkey("ret")
self._send_chars(USER_PW)
self.sendkey("ret")
session = self.poll_shell(
"echo DESKTOP_MANGO=$(pgrep -xc mango || echo 0) "
"DESKTOP_DMS=$(pgrep -xc dms || echo 0)",
r"DESKTOP_MANGO=([1-9][0-9]*) DESKTOP_DMS=([1-9][0-9]*)",
timeout=180,
label="desktop-session-ready",
record=False,
)
if session is None:
self.results.fail("desktop-session-ready", "authenticated Mango and DMS did not start")
self.shot("desktop-login-failed", "failed graphical login", "after credentials",
"Mango and DMS should be visible")
return
self.results.pass_("desktop-session-ready")
if os.environ.get("DESKTOP_EXPECT_WELCOME") == "1":
welcome = self.poll_shell(
"echo WELCOME=$(pgrep -ax quickshell 2>/dev/null "
"| grep -Fc phoebus-welcome-installed)",
r"WELCOME=([1-9][0-9]*)", timeout=60,
label="desktop-first-login-welcome", record=False,
)
self.results.assert_true(
"desktop-first-login-welcome", welcome is not None,
"the Phoebus first-login welcome did not start",
)
check = self.shell(
"echo SEAT-$(printf CHECK)=$(loginctl list-sessions --no-legend | grep -c seat0); "
"echo GPU-$(printf CHECK)=$(test -e /dev/dri/card0 && echo yes || echo no); "
"echo AUDIO-$(printf CHECK)=$(wpctl status >/dev/null 2>&1 && echo yes || echo no); "
"echo NET-$(printf CHECK)=$(nmcli general status >/dev/null 2>&1 && echo yes || echo no); "
"echo CONFIG-$(printf CHECK)=$(test -r \"$HOME/.config/mango/config.conf\" "
"&& echo yes || echo no); "
"echo DMS-SEED-$(printf CHECK)=$(test -f "
"\"$HOME/.config/DankMaterialShell/.firstlaunch\" "
"&& echo yes || echo no); "
"echo DMS-CHANGELOG-$(printf CHECK)=$(test -f "
"\"$HOME/.config/DankMaterialShell/.changelog-1.6\" "
"&& echo yes || echo no); "
"echo DESKTOP-$(printf CHECKS-DONE)",
"DESKTOP-CHECKS-DONE", label="desktop-basic-checks",
)
self.results.assert_true("desktop-real-seat", "SEAT-CHECK=1" in check and
"GPU-CHECK=yes" in check, "no active graphical DRM seat")
self.results.assert_true("desktop-audio-control", "AUDIO-CHECK=yes" in check,
"PipeWire/WirePlumber control is unavailable")
self.results.assert_true("desktop-network-control", "NET-CHECK=yes" in check,
"NetworkManager control is unavailable")
self.results.assert_true("desktop-mango-config", "CONFIG-CHECK=yes" in check,
"Mango did not seed a readable per-user config")
self.results.assert_true("desktop-dms-onboarding-suppressed", "DMS-SEED-CHECK=yes" in check,
"installed user lacks the DMS onboarding marker")
self.results.assert_true("desktop-dms-changelog-suppressed",
"DMS-CHANGELOG-CHECK=yes" in check,
"installed user lacks the DMS versioned changelog marker")
self.shot("desktop-session", "installed desktop", "after authenticated login",
"Mango compositor and DMS panel are visible")
if os.environ.get("DESKTOP_EXPECT_WELCOME") == "1":
self.sendkey("esc")
seen = self.poll_shell(
"echo WELCOME-SEEN=$(test -f "
"\"$HOME/.config/phoebus/welcome-seen\" && echo yes || echo no)",
r"WELCOME-SEEN=yes", timeout=30,
label="desktop-welcome-dismissed", record=False,
)
self.results.assert_true(
"desktop-welcome-dismissed", seen is not None,
"closing the first-login welcome did not record its seen marker",
)
self.sendkey("meta_l-shift-ret")
terminal = self.poll_shell(
"echo KITTY=$(pgrep -xc kitty || echo 0)",
r"KITTY=([1-9][0-9]*)",
timeout=60,
label="desktop-terminal",
record=False,
)
self.results.assert_true("desktop-terminal", terminal is not None,
"Mod+Shift+Return did not launch kitty")
terminal_frame = self.shot("desktop-terminal", "kitty", "Mod+Shift+Return",
"terminal window is visible")
self.sendkey("meta_l-spc")
time.sleep(1)
launcher_frame = self.shot("desktop-launcher", "DMS spotlight", "Mod+Space",
"spotlight launcher is visible")
self.results.assert_true(
"desktop-launcher-visible",
self.frame_change_ratio(terminal_frame, launcher_frame) > 0.02,
"Mod+Space did not visibly open the launcher",
)
self.sendkey("esc")
time.sleep(1)
unlocked_frame = self.shot("desktop-before-lock", "desktop", "dismiss launcher",
"desktop is visible")
self.sendkey("meta_l-alt-l")
time.sleep(1)
locked_frame = self.shot("desktop-locked", "DMS lock screen", "Mod+Alt+L",
"session is locked and password field is visible")
self.results.assert_true(
"desktop-lock-visible",
self.frame_change_ratio(unlocked_frame, locked_frame) > 0.05,
"Mod+Alt+L did not visibly lock the session",
)
self._send_chars(USER_PW)
self.sendkey("ret")
alive = self.poll_shell(
"echo SESSION=$(pgrep -xc mango || echo 0):$(pgrep -xc dms || echo 0)",
r"SESSION=[1-9][0-9]*:[1-9][0-9]*",
timeout=30,
label="desktop-unlocked",
record=False,
)
self.results.assert_true("desktop-unlocked", alive is not None,
"Mango or DMS exited during lock/unlock")
after_unlock = self.shot("desktop-unlocked", "installed desktop",
"unlock with account password", "desktop is visible again")
self.results.assert_true(
"desktop-unlock-visible",
self.frame_change_ratio(locked_frame, after_unlock) > 0.05,
"the lock screen did not visibly disappear after authentication",
)
self.sendkey("meta_l-shift-e")
gone = self.poll_shell(
"echo LOGOUT=$(pgrep -xc mango || echo 0)",
r"LOGOUT=0",
timeout=45,
label="desktop-logout",
record=False,
)
self.results.assert_true("desktop-logout", gone is not None,
"Mango session did not exit after Mod+Shift+e")
self.shot("desktop-logout", "greeter", "log out",
"greetd login is visible and no session remains")
# -- detached boot -----------------------------------------------------
def do_boot(self, desktop: bool = False) -> None:
if not self.disks:
self.results.blocked("boot-disk", "no writable disk configured")
return
self.spawn(attach_iso=False, boot_disk=True)
checks = os.environ.get(
"BOOT_CHECKS",
"setopt NO_EQUALS 2>/dev/null; echo ===CHECKS-$(printf START)===; "
"echo COMM_CHECK:$(cat /proc/1/comm 2>&1); "
"echo SYSTEMD_STATUS:$(systemctl is-system-running 2>&1); "
"sys history --legacy >/tmp/phoebus-sys-history 2>&1; echo SYS_CLI_RC:$?; "
"systemctl --failed --no-pager; ip -brief addr; "
"lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS; echo ===CHECKS-$(printf END)===",
)
assert self.child is not None
idx = self.expect([r"[Pp]assphrase", r"login:"], timeout=BOOT_TIMEOUT, label="boot-prompt")
if idx == 0:
# `gui-install` uses the default single-password encryption, so the
# disk passphrase is the user password; the non-interactive
# `install` journey sets a separate one. Try both, in that order,
# and let cryptsetup's immediate re-prompt disambiguate without
# consuming the login prompt on success.
override = os.environ.get("BOOT_PASSPHRASE")
candidates = [override] if override else [USER_PW, LUKS_PW]
self.child.sendline(candidates[0])
for candidate in candidates[1:]:
try:
self.child.expect(r"[Pp]assphrase", timeout=15)
self.child.sendline(candidate)
except (pexpect.TIMEOUT, pexpect.EOF):
break # accepted; the system is booting
logged_in = False
for _ in range(5):
try:
self.child.expect(r"(?m)^phoebus login:", timeout=300)
except (pexpect.TIMEOUT, pexpect.EOF):
break
# The early getty can be replaced while default.target settles.
# Let the final prompt stabilize before sending credentials.
time.sleep(5)
self.child.sendline(USER)
choice = self.child.expect(
[r"[Pp]assword:", r"(?m)^phoebus login:", r"Login incorrect"], timeout=60
)
if choice == 0:
self.child.sendline(USER_PW)
if self.child.expect([r"[$#] |❯", r"Login incorrect", r"(?m)^phoebus login:"],
timeout=120) == 0:
logged_in = True
break
self.results.assert_true("detached-boot-login", logged_in, "could not log in")
self.shell(checks, "===CHECKS-END===",
timeout=int(os.environ.get("CHECKS_TIMEOUT", "300")), label="boot-checks")
serial = self.paths.serial_log.read_text(errors="replace")
checks_match = re.search(r"===CHECKS-START===(.*?)===CHECKS-END===", serial, re.DOTALL)
checks_text = checks_match.group(1) if checks_match else serial
systemd_status_match = re.search(r"SYSTEMD_STATUS:([a-z\-]+)", checks_text)
systemd_status = systemd_status_match.group(1) if systemd_status_match else "unknown"
self.results.assert_true(
"boot-systemd-running",
systemd_status == "running" if desktop else systemd_status in ("running", "degraded"),
f"systemd did not reach running/degraded state (observed: {systemd_status})",
)
self.results.assert_true(
"boot-sys-installed",
"SYS_CLI_RC:0" in checks_text,
"sys history --legacy did not complete",
)
self.shot("boot-desktop", "booted desktop", "boot", "graphical desktop or login screen")
if desktop:
self.verify_installed_desktop()
try:
self.child.sendline("sudo -v; echo SUDO-$(printf DONE)")
idx = self.child.expect([r"[Pp]assword(?: for [^:]+)?:", "SUDO-DONE"], timeout=30)
if idx == 0:
self.child.sendline(USER_PW)
self.child.expect("SUDO-DONE", timeout=30)
self.child.sendline("sudo -n systemctl poweroff")
self.child.expect(pexpect.EOF, timeout=120)
self.results.pass_("boot-clean-poweroff")
except (pexpect.TIMEOUT, pexpect.EOF) as exc:
self.results.fail("boot-clean-poweroff", str(exc))
def main() -> int:
mode = sys.argv[1] if len(sys.argv) > 1 else ""
if mode not in ("gui", "opencode", "gui-install", "install", "boot", "desktop"):
print(__doc__)
return 2
if mode in ("install", "boot", "desktop", "gui-install") and not DISKS:
print("error: DISK/DISKS must name a disposable qcow2 image", file=sys.stderr)
return 2
try:
return Driver(mode).run()
except HarnessError as exc:
print(f"harness error: {exc}", file=sys.stderr)
return 2
if __name__ == "__main__":
sys.exit(main())