From 09ce64e3e4e3b074015d071db9e40e4d66f58c0c Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 28 Oct 2026 19:33:26 -0700 Subject: [PATCH] test(e2e): test the runner of the Windows guest without a guest The fix-commit check of the known-failure markers, the scope in the result file, the step wrapper, the state key and the state that is not JSON. --- e2e/platforms/qemu_windows/run.py | 4 +- .../qemu_windows/tests/test_runner.py | 139 ++++++++++++++++++ 2 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 e2e/platforms/qemu_windows/tests/test_runner.py diff --git a/e2e/platforms/qemu_windows/run.py b/e2e/platforms/qemu_windows/run.py index 2acc4f50..d2585e13 100644 --- a/e2e/platforms/qemu_windows/run.py +++ b/e2e/platforms/qemu_windows/run.py @@ -110,13 +110,13 @@ def _with(fn, **kwargs): return step -def _contains(fix: str) -> bool: +def _contains(fix: str, repo: Path | None = None) -> bool: """Whether the commit under test has the commit `fix` in its history, so that a marker for the bug it fixed no longer applies. Not known: no.""" commit = os.environ.get("KOMOREBI_E2E_COMMIT") if not commit: return False - here = Path(__file__).resolve().parent + here = repo or Path(__file__).resolve().parent git = subprocess.run( ["git", "-C", str(here), "merge-base", "--is-ancestor", fix, commit], capture_output=True, diff --git a/e2e/platforms/qemu_windows/tests/test_runner.py b/e2e/platforms/qemu_windows/tests/test_runner.py new file mode 100644 index 00000000..47bb9ac3 --- /dev/null +++ b/e2e/platforms/qemu_windows/tests/test_runner.py @@ -0,0 +1,139 @@ +"""The runner of the Windows guest, without a guest.""" + +import json +import subprocess +from pathlib import Path +from types import SimpleNamespace + +import pytest + +from platforms.qemu_windows import run, state +from platforms.qemu_windows.guest import Guest +from platforms.qemu_windows.windows_platform import WindowsPlatform + + +def _git(repo: Path, *args: str) -> str: + return subprocess.run( + ["git", "-C", str(repo), "-c", "user.name=t", "-c", "user.email=t@t", *args], + capture_output=True, text=True, check=True, + ).stdout.strip() + + +@pytest.fixture +def history(tmp_path: Path) -> dict[str, str]: + """A repository with three commits: before the fix, the fix, after it.""" + _git(tmp_path, "init", "-q") + commits = {} + for name in ("before", "fix", "after"): + (tmp_path / "f").write_text(name) + _git(tmp_path, "add", "f") + _git(tmp_path, "commit", "-q", "-m", name) + commits[name] = _git(tmp_path, "rev-parse", "HEAD") + commits["repo"] = str(tmp_path) + return commits + + +def test_a_commit_with_the_fix_contains_it( + history: dict[str, str], monkeypatch: pytest.MonkeyPatch +) -> None: + repo = Path(history["repo"]) + monkeypatch.setenv("KOMOREBI_E2E_COMMIT", history["after"]) + assert run._contains(history["fix"], repo) + monkeypatch.setenv("KOMOREBI_E2E_COMMIT", history["fix"]) + assert run._contains(history["fix"], repo) + + +def test_a_commit_before_the_fix_does_not( + history: dict[str, str], monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("KOMOREBI_E2E_COMMIT", history["before"]) + assert not run._contains(history["fix"], Path(history["repo"])) + + +def test_an_unknown_commit_keeps_the_marker( + history: dict[str, str], monkeypatch: pytest.MonkeyPatch +) -> None: + repo = Path(history["repo"]) + monkeypatch.delenv("KOMOREBI_E2E_COMMIT", raising=False) + assert not run._contains(history["fix"], repo) + monkeypatch.setenv("KOMOREBI_E2E_COMMIT", "0" * 40) + assert not run._contains(history["fix"], repo) + + +def test_recorded_checks_keep_the_scope(capsys: pytest.CaptureFixture[str]) -> None: + checks = run.RecordedChecks() + checks.check("plain", True) + with checks.scoped("windows"): + checks.check("scoped", False) + checks.skip("skipped", "no reason") + checks.known_failure("known", False, "a bug") + + assert checks.results == [ + {"check": "plain", "passed": True}, + {"check": "scoped", "passed": False, "scope": "windows"}, + {"check": "skipped", "skipped": True, "reason": "no reason", "scope": "windows"}, + {"check": "known", "known_failure": True, "reason": "a bug", "scope": "windows"}, + ] + assert "[windows] scoped" in capsys.readouterr().out + + +def test_a_step_wrapper_keeps_the_name_and_passes_keywords() -> None: + def scenario(platform: str, checks: str, extra: int = 0) -> tuple[str, str, int]: + return platform, checks, extra + + step = run._with(scenario, extra=3) + + assert step("p", "c") == ("p", "c", 3) + assert step.__name__ == "scenario" + assert step.__module__ == scenario.__module__ + + +def test_the_state_key_follows_the_machine_and_not_the_run() -> None: + first = state._machine() + + assert first == state._machine() + assert all("/tmp" not in arg for arg in first) + changed = Guest(Path("golden"), Path("run"), ahci=True, vcpus=8) + changed.agent_port = 0 + other = changed.qemu_args(Path("ovmf"), Path("run"), Path("run/overlay.qcow2")) + assert other != first + + +def _platform(printed: str) -> WindowsPlatform: + agent = SimpleNamespace( + exec=lambda *args: {"status": 0, "stdout": printed, "stderr": ""} + ) + return WindowsPlatform(SimpleNamespace(), agent, Path(".")) + + +def test_exe_ids_have_the_extension() -> None: + assert _platform("").exe_id("e2e-exe-sent") == "e2e-exe-sent.exe" + + +def test_a_state_that_is_not_json_is_a_runtime_error_with_what_was_printed() -> None: + platform = _platform("warning: busy\n{") + + with pytest.raises(RuntimeError, match="did not print JSON.*warning: busy"): + platform.state() + + +def test_a_wait_goes_on_past_a_runtime_error_and_raises_the_last_one() -> None: + platform = _platform("not json") + calls = [] + + def sometimes() -> bool: + calls.append(1) + if len(calls) < 3: + raise RuntimeError("busy") + return True + + assert platform.wait_for(sometimes, timeout=1) + with pytest.raises(RuntimeError, match="did not print JSON"): + platform.wait_for(lambda: platform.state().managed() == 0, timeout=0.2) + + +def test_the_result_file_names_a_known_json_shape(tmp_path: Path) -> None: + checks = run.RecordedChecks() + checks.check("a", True) + + json.dumps(checks.results)