Files
local-ci-cd-system/tests/python/test_cli.py
T
Simone 80f6661ad5 feat(a2): port leaf PS scripts to ci_orchestrator CLI
Implements Phase A2 of plans/implementation-plan-A-B.md:

- commands/wait.py    -> wait-ready (replaces Wait-VMReady.ps1)

- commands/vm.py      -> vm remove + vm cleanup (replaces Remove-BuildVM.ps1, Cleanup-OrphanedBuildVMs.ps1); cleanup accepts injected VmBackend (Phase C ESXi hook preserved)

- commands/monitor.py -> monitor disk + monitor runner (replaces Watch-DiskSpace.ps1, Watch-RunnerHealth.ps1)

- commands/report.py  -> report job (replaces Get-CIJobSummary.ps1)

Each PS script reduced to a 3-line shim that delegates to the Python CLI and preserves \0.

Tests: 69 pytest cases across new test_commands_*.py modules; ruff clean, mypy --strict clean, coverage 74.5% (>=70 gate).
2026-05-14 18:34:03 +02:00

135 lines
3.8 KiB
Python

"""Smoke tests for the click CLI entry point.
These do not hit any real VM — backend, transport, credentials and
``time.sleep`` are all monkeypatched.
"""
from __future__ import annotations
from typing import Any
import pytest
from click.testing import CliRunner
import ci_orchestrator.__main__ as cli_module
import ci_orchestrator.commands.wait as wait_module
from ci_orchestrator import __version__
from ci_orchestrator.backends.protocol import VmHandle
from ci_orchestrator.credentials import Credential
class _FakeBackend:
def __init__(
self,
*,
running: bool = True,
ip: str | None = "10.0.0.42",
) -> None:
self._running = running
self._ip = ip
def is_running(self, _h: VmHandle) -> bool:
return self._running
def get_ip(self, _h: VmHandle, timeout: float = 0.0) -> str | None:
return self._ip
class _FakeTransport:
def __init__(self, *_a: Any, **_kw: Any) -> None:
pass
def __enter__(self) -> _FakeTransport:
return self
def __exit__(self, *_e: object) -> None:
pass
def is_ready(self) -> bool:
return True
class _FakeStore:
def get(self, _target: str) -> Credential:
return Credential("user", "pwd")
def _patch_common(monkeypatch: pytest.MonkeyPatch, backend: _FakeBackend) -> None:
monkeypatch.setattr(wait_module, "load_backend", lambda _cfg: backend)
monkeypatch.setattr(wait_module, "KeyringCredentialStore", lambda: _FakeStore())
monkeypatch.setattr(wait_module, "WinRmTransport", _FakeTransport)
monkeypatch.setattr(wait_module, "SshTransport", _FakeTransport)
monkeypatch.setattr(wait_module.time, "sleep", lambda _s: None)
def test_help_lists_wait_ready() -> None:
result = CliRunner().invoke(cli_module.cli, ["--help"])
assert result.exit_code == 0
assert "wait-ready" in result.output
def test_version() -> None:
result = CliRunner().invoke(cli_module.cli, ["--version"])
assert result.exit_code == 0
assert __version__ in result.output
def test_wait_ready_windows_success(monkeypatch: pytest.MonkeyPatch) -> None:
_patch_common(monkeypatch, _FakeBackend(running=True, ip="10.0.0.42"))
result = CliRunner().invoke(
cli_module.cli,
["wait-ready", "--vmx", "x.vmx", "--guest-os", "windows", "--timeout", "1"],
)
assert result.exit_code == 0, result.output
assert "WinRM ready" in result.output
def test_wait_ready_linux_success(monkeypatch: pytest.MonkeyPatch) -> None:
_patch_common(monkeypatch, _FakeBackend(running=True, ip="10.0.0.42"))
result = CliRunner().invoke(
cli_module.cli,
["wait-ready", "--vmx", "x.vmx", "--guest-os", "linux", "--timeout", "1"],
)
assert result.exit_code == 0, result.output
assert "SSH ready" in result.output
def test_wait_ready_timeout_when_not_running(monkeypatch: pytest.MonkeyPatch) -> None:
_patch_common(monkeypatch, _FakeBackend(running=False))
result = CliRunner().invoke(
cli_module.cli,
[
"wait-ready",
"--vmx",
"x.vmx",
"--guest-os",
"windows",
"--timeout",
"0.01",
"--poll-interval",
"0.001",
],
)
assert result.exit_code == 2
assert "timeout waiting for VM" in result.output
def test_wait_ready_timeout_no_ip(monkeypatch: pytest.MonkeyPatch) -> None:
_patch_common(monkeypatch, _FakeBackend(running=True, ip=None))
result = CliRunner().invoke(
cli_module.cli,
[
"wait-ready",
"--vmx",
"x.vmx",
"--guest-os",
"windows",
"--timeout",
"0.01",
"--poll-interval",
"0.001",
],
)
assert result.exit_code == 3
assert "timeout waiting for guest IP" in result.output