71 lines
2 KiB
Python
71 lines
2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
import pyro_mcp.demo as demo_module
|
|
|
|
|
|
def test_run_demo_happy_path(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
calls: list[tuple[str, dict[str, Any]]] = []
|
|
|
|
class StubManager:
|
|
def __init__(self) -> None:
|
|
pass
|
|
|
|
def create_vm(
|
|
self,
|
|
*,
|
|
profile: str,
|
|
vcpu_count: int,
|
|
mem_mib: int,
|
|
ttl_seconds: int,
|
|
) -> dict[str, str]:
|
|
calls.append(
|
|
(
|
|
"create_vm",
|
|
{
|
|
"profile": profile,
|
|
"vcpu_count": vcpu_count,
|
|
"mem_mib": mem_mib,
|
|
"ttl_seconds": ttl_seconds,
|
|
},
|
|
)
|
|
)
|
|
return {"vm_id": "vm-1"}
|
|
|
|
def start_vm(self, vm_id: str) -> dict[str, str]:
|
|
calls.append(("start_vm", {"vm_id": vm_id}))
|
|
return {"vm_id": vm_id}
|
|
|
|
def exec_vm(self, vm_id: str, *, command: str, timeout_seconds: int) -> dict[str, Any]:
|
|
calls.append(
|
|
(
|
|
"exec_vm",
|
|
{"vm_id": vm_id, "command": command, "timeout_seconds": timeout_seconds},
|
|
)
|
|
)
|
|
return {"vm_id": vm_id, "stdout": "git version 2.x", "exit_code": 0}
|
|
|
|
monkeypatch.setattr(demo_module, "VmManager", StubManager)
|
|
result = demo_module.run_demo()
|
|
|
|
assert result["exit_code"] == 0
|
|
assert calls[0][0] == "create_vm"
|
|
assert calls[1] == ("start_vm", {"vm_id": "vm-1"})
|
|
assert calls[2][0] == "exec_vm"
|
|
|
|
|
|
def test_main_prints_json(
|
|
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
monkeypatch.setattr(
|
|
demo_module,
|
|
"run_demo",
|
|
lambda: {"stdout": "git version 2.x", "exit_code": 0},
|
|
)
|
|
demo_module.main()
|
|
rendered = json.loads(capsys.readouterr().out)
|
|
assert rendered["exit_code"] == 0
|