# The test-script helpers the three session checks share: how to run something # as the session account, how to call the CLI, how to ask the compositor, and # what to dump when an assertion fails. # # Helpers only. There is not one assertion in this file, deliberately: a shared # prelude that asserted things would make each check's own script an incomplete # account of what it proves, and the whole point of these scripts is that they # can be read top to bottom when one of them goes red. { hhstream, coreutils, sessionUser, compositorRuntime ? "/run/hypr-stream", controlSocket ? "/run/hhstream/control.sock", }: '' import json import time USER = "${sessionUser}" CONTROL = "${controlSocket}" CONTROL_DIR = "/run/hhstream" COMPOSITOR_RUNTIME = "${compositorRuntime}" HHSTREAM = "${hhstream}/bin/hhstream" TRUE = "${coreutils}/bin/true" FALSE = "${coreutils}/bin/false" # The module pins the compositor's XDG_RUNTIME_DIR; the instance signature # under it is the only part not known ahead of time. Root may connect to the # IPC socket even though it belongs to `hhstream`. Same shape as # vm-hyprland.nix, which asks this for monitors. HYPRCTL = ( "set -eu; " f"sig=$(ls -1 {COMPOSITOR_RUNTIME}/hypr | head -n1); " f"XDG_RUNTIME_DIR={COMPOSITOR_RUNTIME}" " HYPRLAND_INSTANCE_SIGNATURE=$sig hyprctl -j" ) #: A transient `systemd-run` unit inherits systemd's own minimal PATH, not #: the guest's. That is an artefact of how these checks invoke things, not a #: property of the system under test: a person's login has the system profile #: on PATH, and `hhstream shot --output` looks `grim` up there. Measured, by #: a check that failed with the CLI's own "grim is not on PATH, and --output #: needs it" -- which is the right message and the wrong environment. SYSTEM_PATH = "/run/current-system/sw/bin" def as_user(command, env=""): """`command` (absolute path plus argv, never a shell) run as USER. A transient unit rather than `su`, because it gives a clean argv with no login shell in the middle, and `--pipe --wait` propagates the command's own exit status -- which each check proves before anything depends on it. """ return ( "systemd-run --quiet --collect --pipe --wait" f" --uid={USER} --setenv=HOME=/home/{USER}" f" --setenv=PATH={SYSTEM_PATH} {env} -- {command}" ) def background(unit, command, env="", user=None): """A transient unit that keeps running after the call returns. For the programs a check must have running *before* the thing they witness happens: the event subscriber, and a window that is not a session. No `--collect`: the unit has to stay loaded after it exits, because its exit status is part of what is asserted, and a collected unit is unloaded the moment it stops. `user=None` means root, which is how the driver already reaches the compositor's IPC socket (see HYPRCTL); pass USER for something that should be subject to the same permissions an operator has. """ who = "" if user is None else f" --uid={user} --setenv=HOME=/home/{user}" return ( f"systemd-run --quiet --unit={unit}{who}" f" --setenv=PATH={SYSTEM_PATH} {env} -- {command}" ) def compositor_env(): """`--setenv` flags that point a Wayland client at this session. The socket name is discovered, never assumed: the compositor binds the first free `wayland-N`, and a crashed one leaves `wayland-0` behind. """ display = machine.succeed( f"cd {COMPOSITOR_RUNTIME} && ls -1 wayland-* | grep -v '[.]lock$' | head -n1" ).strip() assert display, f"no wayland socket in {COMPOSITOR_RUNTIME}" return ( f"--setenv=XDG_RUNTIME_DIR={COMPOSITOR_RUNTIME}" f" --setenv=WAYLAND_DISPLAY={display}" ) def cli(args): """`hhstream session …` as USER, against the control socket.""" return as_user(f"{HHSTREAM} session --socket {CONTROL} {args}") def cli_json(args, timeout=120): """Run a `--json` verb and parse what it printed. Fails loudly if it did not.""" out = machine.succeed(cli(args), timeout=timeout) try: return json.loads(out) except json.JSONDecodeError: raise AssertionError(f"not JSON from `session {args}`: {out!r}") def compositor(resource): """`hyprctl -j `, parsed. The compositor is the authority here.""" return json.loads(machine.succeed(f"{HYPRCTL} {resource}", timeout=120)) def monitor_named(name): for entry in compositor("monitors"): if entry.get("name") == name: return entry return None def wait_for(predicate, timeout, what, interval=1.0): """Poll a *Python* predicate to a deadline, and return its value. `machine.wait_until_succeeds` can only wait on a shell command, which forces anything with structure into jq. This is for the waits whose condition is a parsed value -- "the client has reported a third button event" -- where a shell pipeline would be both unreadable and quietly wrong when `grep -c` prints 0 and exits 1. It waits for an *event that must happen*, never for an assertion to come good: every caller below asserts after it returns, not inside it. """ deadline = time.monotonic() + timeout while True: value = predicate() if value: return value if time.monotonic() > deadline: raise AssertionError(f"timed out after {timeout}s waiting for {what}") time.sleep(interval) def dump(label, session_id=None): print(f"########## diagnostics: {label} ##########") status, out = machine.execute("hhs-hyprland-diagnostics", timeout=300) print(out) print(f"----- hhs-hyprland-diagnostics exit {status} -----") commands = [ "systemctl --no-pager --full status hhstream-sessions.service", "journalctl --no-pager --boot --lines=200 --unit hhstream-sessions.service", "systemctl --no-pager --full status hhstream-pointer.service", "journalctl --no-pager --boot --lines=200 --unit hhstream-pointer.service", f"ls -la {CONTROL_DIR} /var/lib/hhstream/sessions", "ps -eo pid,ppid,pgid,sid,user,stat,comm,args --sort=pid", cli("list --all"), cli("container"), cli("pointer state"), ] if session_id: commands += [ cli(f"status {session_id}"), # Every session's captured output, not just the named one: when # a pointer event goes to the wrong window, the wrong window's # log is the evidence and the named session's is empty. cli("list"), # The application's own captured output: the only place a failed # launch, or a pointer event that never arrived, explains itself. cli(f"logs {session_id} --lines 200"), ] for command in commands: status, out = machine.execute(f"{command} 2>&1", timeout=180) print(f"----- {command} (exit {status}) -----") print(out) for resource in ("monitors", "workspaces", "clients", "cursorpos"): status, out = machine.execute(f"{HYPRCTL} {resource} 2>&1", timeout=180) print(f"----- hyprctl {resource} (exit {status}) -----") print(out) print("########## end diagnostics ##########") def bring_up(label="the session stack"): """Compositor, then broker, then the socket each exists to serve. `wait_for_unit` alone is not enough for the broker and that is measured, not assumed: the unit is `Type=simple` with no readiness protocol, so systemd calls it active the instant it has forked. A daemon that exits two milliseconds later and is restarted every second passes it. The honest test of "it came up" is the socket plus a restart count of zero. """ try: machine.wait_for_unit("hyprland-headless.service", timeout=300) machine.wait_for_file(f"{COMPOSITOR_RUNTIME}/hypr", timeout=120) machine.wait_for_unit("hhstream-sessions.service", timeout=180) machine.wait_for_file(CONTROL, timeout=120) machine.succeed("systemctl is-active hhstream-sessions.service") restarts = machine.succeed( "systemctl show -p NRestarts --value hhstream-sessions.service" ).strip() assert restarts == "0", ( f"the broker has restarted {restarts} time(s) already: it is " "crash-looping, not running" ) except Exception: dump(f"{label} did not come up") raise ''