diff --git a/nix/tests/vm/default.nix b/nix/tests/vm/default.nix index 1c3a3554..1bfa6685 100644 --- a/nix/tests/vm/default.nix +++ b/nix/tests/vm/default.nix @@ -15,6 +15,14 @@ let inherit (hostPkgs) lib; + # `komorebi-recorder`, when the run is given one: its repository is private, so it is not an + # input of the flake. `KOMOREBI_RECORDER=$(nix build --print-out-paths ...)` and `--impure`. + recorder = + let + path = builtins.getEnv "KOMOREBI_RECORDER"; + in + if path == "" then null else builtins.storePath path; + # the library the suites import, built for the python of the test driver komorebiVm = ps: @@ -86,10 +94,12 @@ let nodes.machine = { # a suite's `node`, if it has one, adds to the node of its kind imports = [ (./nodes + "/${suite.kind}.nix") ] ++ lib.optional (suite ? node) suite.node; + environment.systemPackages = lib.optional (recorder != null) recorder; }; testScript = '' SCALE = ${toString scale} + RECORDER = ${if recorder == null then "False" else "True"} '' + builtins.readFile (dir + "/${suite.script or "test.py"}"); }; diff --git a/nix/tests/vm/lib/komorebi_vm/recorder.py b/nix/tests/vm/lib/komorebi_vm/recorder.py new file mode 100644 index 00000000..14e6daed --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/recorder.py @@ -0,0 +1,54 @@ +"""`komorebi-recorder` alongside a suite: what the daemon and the desktop did, as an artifact. + +The recorder is optional. Its repository is private, so it is not an input of the flake: a run that +wants it builds it elsewhere and passes the store path as `KOMOREBI_RECORDER`, evaluating with +`--impure` (see docs/dev/vm-tests.md). A run without it records nothing and says so. + +The recording has no video: a headless KWin has no screen to capture, and the log, the daemon's +events and the probes of the windows are what a failed check is read against. +""" + +import shlex + +from komorebi_vm.kde import Kde + +PROGRAM = "komorebi-recorder" + + +class Recorder: + """A recording of the daemon of `kde`, taken in the guest as the test user.""" + + def __init__(self, kde: Kde, enabled: bool) -> None: + self.kde = kde + self.enabled = enabled + self.out = "" + self.pid = "" + + def start(self, name: str) -> None: + """Starts recording the session into a directory named `name`, and waits until the + recorder has made it.""" + if not self.enabled: + print(" (no recorder in this run: set KOMOREBI_RECORDER to record)") + return + self.out = f"/tmp/recording-{name}" + log = f"{self.out}.log" + command = f"{PROGRAM} record --no-video --probe-windows --out {shlex.quote(self.out)}" + self.pid = self.kde.sh( + f"setsid nohup {command} > {shlex.quote(log)} 2>&1 < /dev/null & echo $!" + ).strip() + self.kde.wait_for(lambda: self.kde.run(f"test -d {shlex.quote(self.out)}")[0] == 0) + + def stop(self) -> None: + """Stops the recording, prints what the recorder makes of it, and keeps the session as + `recording-NAME.tar.gz` with the artifacts of the run.""" + if not self.enabled or not self.pid: + return + self.kde.run(f"kill -INT {self.pid}") + self.kde.wait_for(lambda: self.kde.run(f"kill -0 {self.pid}")[0] != 0, timeout=60) + out = shlex.quote(self.out) + print(self.kde.run(f"{PROGRAM} brief {out} --compact")[1]) + print(self.kde.run(f"{PROGRAM} findings {out}")[1]) + archive = f"{self.out}.tar.gz" + self.kde.run(f"tar czf {shlex.quote(archive)} -C /tmp {shlex.quote(self.out[5:])}") + self.kde.machine.copy_from_machine(archive) + self.pid = "" diff --git a/nix/tests/vm/lib/tests/test_recorder.py b/nix/tests/vm/lib/tests/test_recorder.py new file mode 100644 index 00000000..bedda3cf --- /dev/null +++ b/nix/tests/vm/lib/tests/test_recorder.py @@ -0,0 +1,26 @@ +from pathlib import Path + +from komorebi_vm.kde import Kde +from komorebi_vm.recorder import Recorder +from tests.conftest import FakeMachine + + +def test_a_run_without_the_recorder_does_nothing(machine: FakeMachine) -> None: + recorder = Recorder(Kde(machine, "main"), enabled=False) + recorder.start("x") + recorder.stop() + assert machine.commands == [] + + +def test_the_recording_is_started_stopped_and_kept(machine: FakeMachine) -> None: + machine.reply("echo $!", output="4242\n") + machine.reply("kill -0", status=1) + recorder = Recorder(Kde(machine, "main"), enabled=True) + recorder.start("core") + recorder.stop() + joined = "\n".join(machine.commands) + assert "komorebi-recorder record --no-video" in joined + assert "kill -INT 4242" in joined + assert "komorebi-recorder brief /tmp/recording-core --compact" in joined + assert machine.copied == ["/tmp/recording-core.tar.gz"] + assert isinstance(machine.out_dir, Path) diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index e6ead4a5..3af1ada4 100644 --- a/nix/tests/vm/suites/core-e2e/test.py +++ b/nix/tests/vm/suites/core-e2e/test.py @@ -13,6 +13,7 @@ import json from komorebi_vm import Checks, Kde, Rect, daemon +from komorebi_vm.recorder import Recorder from komorebi_vm.core.platform import Client from komorebi_vm.core.scenarios import ( closing, @@ -137,6 +138,8 @@ def report(titles: list[str]) -> None: print("== 1. start") kde.start_komorebi() +recorder = Recorder(kde, RECORDER) +recorder.start("core-e2e") checks.check( "the daemon loaded its script into KWin", kde.wait_for(lambda: "loaded the KWin bridge script" in daemon_log(), timeout=30), @@ -541,5 +544,6 @@ checks.check( ) checks.check("the window stays", kde.wid("afterwards") != "") +recorder.stop() kde.stop() checks.finish() diff --git a/nix/tests/vm/suites/core-lock/test.py b/nix/tests/vm/suites/core-lock/test.py index aab23cfe..1c72d111 100644 --- a/nix/tests/vm/suites/core-lock/test.py +++ b/nix/tests/vm/suites/core-lock/test.py @@ -3,6 +3,7 @@ # desktop, not the daemon's log. The same scenario runs against a Windows guest (`LockWorkStation`). # `lock-sleep` keeps what is only KDE's: the daemon's log of the lock screen, and the signals. from komorebi_vm import Checks, Kde +from komorebi_vm.recorder import Recorder from komorebi_vm.core.scenarios import locking from komorebi_vm.kde_platform import KdePlatform @@ -21,6 +22,8 @@ checks.check( ) # the daemon tiles again when it learns of the panel's room, so the windows open after that kde.wait_for_panel_room() +recorder = Recorder(kde, RECORDER) +recorder.start("core-lock") clients = [platform.spawn(title) for title in TITLES] checks.check( "the daemon manages the three windows", @@ -30,4 +33,5 @@ checks.check( print("== the screen is locked and unlocked") locking.run(platform, checks, clients) +recorder.stop() checks.finish()