get_services moves into WindowsServicesMixin and reads, beside state and start mode, Win32_Service's ExitCode (1066 hands over to ServiceSpecificExitCode, 1077 is not started since boot), StartName and whether a TriggerInfo key starts the service, as napalm-device-types' ServiceDict result, exit_code, trigger_start and account.
126 lines
4.8 KiB
Python
126 lines
4.8 KiB
Python
"""The host's services and how each one's last run ended (NetOrk/netork#2897).
|
|
|
|
``Win32_Service`` says whether a service runs and how it starts, and also what its
|
|
last run ended with: ``ExitCode`` (``1066`` hands over to
|
|
``ServiceSpecificExitCode``, ``1077`` is "not started since boot"). Together with
|
|
whether a trigger starts it (a ``TriggerInfo`` key under the service) that tells a
|
|
service that died from one stopped on purpose. The reading is the shape of
|
|
tests/fixtures/synthetic/services.json.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from napalm_windows import WindowsDriver
|
|
from napalm_windows import services as mod
|
|
|
|
FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "services.json"
|
|
|
|
|
|
class FakeTransport:
|
|
def __init__(self, answer: str) -> None:
|
|
self.answer = answer
|
|
self.sent: list[str] = []
|
|
self.is_open = True
|
|
|
|
def run(self, script: str) -> str:
|
|
self.sent.append(script)
|
|
return self.answer if script == mod._PS_SERVICES else ""
|
|
|
|
|
|
def _services(answer: str | None = None) -> dict[str, dict]:
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeTransport(FIXTURE.read_text() if answer is None else answer)
|
|
return {s["name"]: s for s in d.get_services()}
|
|
|
|
|
|
class TestRunningAndEnabled:
|
|
def test_maps_state_and_start_mode(self):
|
|
services = _services()
|
|
assert {n: (s["running"], s["enabled"], s["pid"]) for n, s in services.items()} == {
|
|
"WinRM": (True, True, 1234),
|
|
"Spooler": (False, False, 0),
|
|
"MSSQL$SQLEXPRESS": (True, True, 4321),
|
|
"RemoteRegistry": (False, False, 0),
|
|
"wuauserv": (False, False, 0),
|
|
"AppReadiness": (False, True, 0),
|
|
"BackupAgent": (False, True, 0),
|
|
"VendorSync": (False, True, 0),
|
|
"BthServ": (False, True, 0),
|
|
}
|
|
|
|
def test_a_single_service_is_still_a_list(self):
|
|
one = json.loads(FIXTURE.read_text())[0]
|
|
assert list(_services(json.dumps(one))) == ["WinRM"]
|
|
|
|
def test_nothing_read_is_no_services(self):
|
|
assert _services("") == {}
|
|
|
|
|
|
class TestHowTheLastRunEnded:
|
|
def test_a_running_service_succeeded(self):
|
|
assert _services()["WinRM"] == {
|
|
"name": "WinRM",
|
|
"running": True,
|
|
"enabled": True,
|
|
"pid": 1234,
|
|
"result": "success",
|
|
"exit_code": None,
|
|
"trigger_start": False,
|
|
"account": "NT AUTHORITY\\NetworkService",
|
|
}
|
|
|
|
def test_one_stopped_on_purpose_succeeded(self):
|
|
assert (_services()["Spooler"]["result"], _services()["Spooler"]["exit_code"]) == (
|
|
"success",
|
|
None,
|
|
)
|
|
|
|
def test_one_not_started_since_boot_has_no_result(self):
|
|
"""1077: a delayed or a demand-start service that has not run yet."""
|
|
service = _services()["AppReadiness"]
|
|
assert (service["result"], service["exit_code"]) == (None, None)
|
|
|
|
def test_one_that_died_ended_with_its_exit_code(self):
|
|
"""1067: the process terminated unexpectedly."""
|
|
service = _services()["BackupAgent"]
|
|
assert (service["result"], service["exit_code"]) == ("exit-code", 1067)
|
|
|
|
def test_a_service_specific_error_is_the_services_own_code(self):
|
|
service = _services()["VendorSync"]
|
|
assert (service["result"], service["exit_code"]) == ("exit-code", 2147942405)
|
|
|
|
def test_a_trigger_start_service_says_so(self):
|
|
assert _services()["BthServ"]["trigger_start"] is True
|
|
assert _services()["BackupAgent"]["trigger_start"] is False
|
|
|
|
def test_the_account_it_runs_as(self):
|
|
assert _services()["BackupAgent"]["account"] == "CORP\\svc-backup"
|
|
assert _services()["BthServ"]["account"] is None
|
|
|
|
def test_a_reading_without_the_new_fields_says_nothing_of_them(self):
|
|
"""An older script's answer: running and enabled only, nothing invented."""
|
|
old = [{"name": "WinRM", "state": "Stopped", "start_mode": "Auto", "pid": 0}]
|
|
service = _services(json.dumps(old))["WinRM"]
|
|
assert (service["result"], service["exit_code"], service["trigger_start"]) == (
|
|
None,
|
|
None,
|
|
False,
|
|
)
|
|
|
|
|
|
class TestTheScript:
|
|
def test_exit_codes_leave_the_host_as_wide_integers(self):
|
|
"""``ServiceSpecificExitCode`` is a UInt32: ``[int]`` would throw on an HRESULT."""
|
|
assert "[int64]$_.ExitCode" in mod._PS_SERVICES
|
|
assert "[int64]$_.ServiceSpecificExitCode" in mod._PS_SERVICES
|
|
|
|
def test_trigger_start_is_read_once_from_the_registry(self):
|
|
assert r"Services\*\TriggerInfo" in mod._PS_SERVICES
|
|
assert "-ErrorAction SilentlyContinue" in mod._PS_SERVICES
|
|
|
|
def test_a_list_goes_through_input_object(self):
|
|
assert "-InputObject @(" in mod._PS_SERVICES
|