pyro-mcp/tests/test_runtime_network_check.py
Thales Maciel cc5f566bcc Speed up workspace tests and parallelize make test
make test was dominated by teardown-heavy workspace integration tests, not by coverage overhead. Service shutdown was treating zombie processes as live, which forced repeated timeout waits, and one shell test was leaving killpg monkeypatched during cleanup, which made shell close paths burn the full wait budget.\n\nTreat Linux zombie pids as stopped in the workspace manager so service teardown completes promptly. Restore the real killpg implementation before shell test cleanup so the shell close path no longer pays the artificial timeout. Also isolate sys.argv in the runtime-network-check main() test so parallel pytest flags do not leak into argparse-based tests.\n\nAdd pytest-xdist to the dev environment and run make test with pytest -n auto by default so available cores are used automatically during local iteration.\n\nValidation:\n- uv lock\n- targeted hot-spot pytest rerun after the fix dropped the worst tests from roughly 10-21s each to sub-second timings\n- UV_CACHE_DIR=.uv-cache make check\n- UV_CACHE_DIR=.uv-cache make dist-check
2026-03-13 13:04:59 -03:00

66 lines
2.1 KiB
Python

from __future__ import annotations
import sys
import pytest
import pyro_mcp.runtime_network_check as runtime_network_check
def test_network_check_uses_network_enabled_manager(monkeypatch: pytest.MonkeyPatch) -> None:
observed: dict[str, object] = {}
class StubPyro:
def __init__(self, **kwargs: object) -> None:
observed.update(kwargs)
def run_in_vm(self, **kwargs: object) -> dict[str, object]:
observed["run_kwargs"] = kwargs
return {
"vm_id": "vm123",
"execution_mode": "guest_vsock",
"exit_code": 0,
"stdout": "true\n",
"stderr": "",
"cleanup": {"deleted": True, "vm_id": "vm123", "reason": "post_exec_cleanup"},
}
monkeypatch.setattr(runtime_network_check, "Pyro", StubPyro)
result = runtime_network_check.run_network_check()
assert observed["run_kwargs"] == {
"environment": "debian:12",
"command": runtime_network_check.NETWORK_CHECK_COMMAND,
"vcpu_count": 1,
"mem_mib": 1024,
"timeout_seconds": 120,
"ttl_seconds": 600,
"network": True,
}
assert result.execution_mode == "guest_vsock"
assert result.network_enabled is True
assert result.exit_code == 0
def test_network_check_main_fails_on_unsuccessful_command(
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(sys, "argv", ["runtime-network-check"])
monkeypatch.setattr(
runtime_network_check,
"run_network_check",
lambda **kwargs: runtime_network_check.NetworkCheckResult(
vm_id="vm123",
execution_mode="guest_vsock",
network_enabled=True,
exit_code=1,
stdout="",
stderr="curl failed",
cleanup={"deleted": True},
),
)
with pytest.raises(SystemExit, match="1"):
runtime_network_check.main()
output = capsys.readouterr().out
assert "[network] result=failure" in output
assert "[network] stderr=curl failed" in output