test: add coverage for monitor/vm/winrm to reach 90%% gate
Bring monitor.py 49->99%%, vm.py 79->98%%, winrm.py 76->100%%. Total project coverage 80.10%% -> 90.69%%, satisfying the 90%% gate set in commit 451a61c.
This commit is contained in:
@@ -98,3 +98,98 @@ def test_is_ready_handles_connect_failure(monkeypatch: pytest.MonkeyPatch) -> No
|
||||
# Direct .run should raise.
|
||||
with pytest.raises(TransportConnectError):
|
||||
t.run("x")
|
||||
|
||||
|
||||
def test_close_and_context_manager(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
closed: list[bool] = []
|
||||
fake.wsman = types.SimpleNamespace(close=lambda: closed.append(True))
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
with t as ctx:
|
||||
assert ctx is t
|
||||
assert closed == [True]
|
||||
# close() on already-closed transport is a no-op.
|
||||
t.close()
|
||||
|
||||
|
||||
def test_close_swallows_wsman_exception(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
def _raise() -> None:
|
||||
raise RuntimeError("boom")
|
||||
fake.wsman = types.SimpleNamespace(close=_raise)
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
t.run("x") # connect once
|
||||
t.close() # must not raise
|
||||
|
||||
|
||||
def test_run_normalises_list_stderr(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
fake.next_result = ("out", ["a", "b"], 0) # type: ignore[assignment]
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
result = t.run("x")
|
||||
assert result.stderr == "a\nb"
|
||||
|
||||
|
||||
def test_run_normalises_non_iterable_stderr(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
class _Weird:
|
||||
def __str__(self) -> str:
|
||||
return "weird-stderr"
|
||||
|
||||
fake = _FakeClient()
|
||||
fake.next_result = ("out", _Weird(), 0) # type: ignore[assignment]
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
result = t.run("x")
|
||||
assert "weird-stderr" in result.stderr
|
||||
|
||||
|
||||
def test_run_execute_ps_raises_wrapped(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
|
||||
def _boom(_s: str) -> Any:
|
||||
raise RuntimeError("ps boom")
|
||||
|
||||
fake.execute_ps = _boom # type: ignore[assignment]
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
with pytest.raises(TransportConnectError) as exc:
|
||||
t.run("x")
|
||||
assert "execute_ps failed" in str(exc.value)
|
||||
|
||||
|
||||
def test_copy_wraps_exception(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
|
||||
def _boom(_l: str, _r: str) -> None:
|
||||
raise OSError("copy down")
|
||||
|
||||
fake.copy = _boom # type: ignore[assignment]
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
with pytest.raises(TransportConnectError) as exc:
|
||||
t.copy("a", "b")
|
||||
assert "copy failed" in str(exc.value)
|
||||
|
||||
|
||||
def test_fetch_wraps_exception(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
|
||||
def _boom(_r: str, _l: str) -> None:
|
||||
raise OSError("fetch down")
|
||||
|
||||
fake.fetch = _boom # type: ignore[assignment]
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
with pytest.raises(TransportConnectError) as exc:
|
||||
t.fetch("a", "b")
|
||||
assert "fetch failed" in str(exc.value)
|
||||
|
||||
|
||||
def test_is_ready_true_when_run_succeeds(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fake = _FakeClient()
|
||||
_install_fake_pypsrp(monkeypatch, fake)
|
||||
t = WinRmTransport("10.0.0.1", "user", "pwd")
|
||||
assert t.is_ready() is True
|
||||
|
||||
Reference in New Issue
Block a user