mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-12-01 12:44:10 +01:00
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.
140 lines
4.6 KiB
Python
140 lines
4.6 KiB
Python
"""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)
|