feat(power): restart and shut the host down with Restart-/Stop-Computer (NetOrk/netork#2841)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 13s
driver / test (3.13) (pull_request) Successful in 43s
driver / types (pull_request) Successful in 56s
driver / lowest (pull_request) Successful in 25s
driver / test (3.12) (pull_request) Successful in 55s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 2m2s
driver / publish (pull_request) Skipped

reboot_host() and shutdown_host() run Restart-Computer and Stop-Computer
without -Force over the PSRP connection: Windows asks running programs
to close and refuses while other users are logged on. A refusal raises
RuntimeError with the host's answer; a connection the host drops before
answering counts as accepted, which PsrpTransport.run_last() reports.
This commit is contained in:
Christian Manivong committed 2026-11-29 11:32:33 +01:00
1 parent 442541d30a
commit d7511efd43
6 files changed
+178 -2

No files matched your search

+67 -1
View File
@@ -28,9 +28,15 @@ def _fixture(name: str) -> str:
class FakeTransport:
"""Answers the driver's scripts from fixtures and records what it was sent."""
def __init__(self, answers: dict[str, str] | None = None, error: str | None = None):
def __init__(
self,
answers: dict[str, str] | None = None,
error: str | None = None,
drops: bool = False,
):
self.answers = answers or {}
self.error = error
self.drops = drops
self.sent: list[str] = []
self.is_open = True
@@ -40,6 +46,14 @@ class FakeTransport:
raise PowerShellError(self.error)
return self.answers.get(script, "")
def run_last(self, script: str) -> bool:
self.sent.append(script)
if self.error:
raise PowerShellError(self.error)
if self.drops:
self.is_open = False
return not self.drops
def close(self) -> None:
self.is_open = False
@@ -490,3 +504,55 @@ class TestManageService:
"success": False,
"output": "Cannot find any service with service name 'nope'.",
}
class TestHostPower:
"""``reboot_host`` and ``shutdown_host``: the clean restart and shutdown of the host
itself, never a forced one (NetOrk/netork#2841)."""
@pytest.mark.parametrize(
("method", "cmdlet"),
[("reboot_host", "Restart-Computer"), ("shutdown_host", "Stop-Computer")],
)
def test_sends_the_cmdlet_without_force(self, method, cmdlet):
d = _driver()
getattr(d, method)()
(script,) = d._transport.sent
assert script.startswith(cmdlet)
assert "-ErrorAction Stop" in script
assert "-Force" not in script
@pytest.mark.parametrize(
("method", "what"), [("reboot_host", "restart"), ("shutdown_host", "shut down")]
)
def test_a_refusal_raises_with_the_hosts_answer(self, method, what):
answer = (
"Failed to restart the computer WIN01 with the following error message: The "
"system shutdown cannot be initiated because there are other users logged on "
"to the computer."
)
d = WindowsDriver("win01", "admin", "secret")
d._transport = FakeTransport(error=answer)
with pytest.raises(RuntimeError, match=f"refused to {what}: Failed to restart"):
getattr(d, method)()
@pytest.mark.parametrize("method", ["reboot_host", "shutdown_host"])
def test_a_connection_dropped_by_the_host_counts_as_accepted(self, method):
d = WindowsDriver("win01", "admin", "secret")
d._transport = FakeTransport(drops=True)
getattr(d, method)()
assert len(d._transport.sent) == 1
@pytest.mark.parametrize("method", ["reboot_host", "shutdown_host"])
def test_before_open_raises_connection_closed(self, method):
with pytest.raises(ConnectionClosedException):
getattr(WindowsDriver("win01", "admin", "secret"), method)()
@pytest.mark.parametrize("method", ["reboot_host", "shutdown_host"])
def test_the_capability_is_there_for_introspection(self, method):
assert callable(getattr(WindowsDriver("win01", "admin", "secret"), method, None))