"""Processes on Windows: ``Win32_Process`` joined with ``Get-Process`` (NetOrk/netork#2889). One script reads both; a fake transport answers it from tests/fixtures/synthetic/processes.json. ``Win32_Process`` gives the parent, the command line, the start and the CPU and memory counters, ``Get-Process -IncludeUserName`` the owner. The fixture holds the cases the join decides: * 0, the System Idle Process: no process, left out as ``ps`` leaves it out; * 5120, gone by the time ``Get-Process`` ran: dropped, not half-filled; * 6000, a PID ``Get-Process`` reports with another start: a new process under a PID that was reused between the two reads, dropped; * 7777, started after ``Win32_Process`` was read: not part of the reading. """ from __future__ import annotations import json from pathlib import Path from typing import Any import pytest from napalm_windows import WindowsDriver from napalm_windows import processes as mod FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "processes.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 def _reading() -> dict[str, Any]: return json.loads(FIXTURE.read_text()) def _driver(reading: dict[str, Any] | None = None) -> WindowsDriver: d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport(json.dumps(reading if reading is not None else _reading())) return d def _by_pid(reading: dict[str, Any] | None = None) -> dict[int, Any]: return {p["pid"]: p for p in _driver(reading).get_processes()} def test_the_join_keeps_the_processes_both_reads_saw(): assert sorted(_by_pid()) == [4, 812, 4312] def test_a_process_reads_like_ps(): assert _by_pid()[4312] == { "pid": 4312, "ppid": 3988, "user": "WIN01\\admin", # 120 CPU seconds in 600 s on 4 logical processors "cpu": 5.0, # 100 MiB of 8 GiB "memory": 1.2, "vsz": 2203500000000 // 1024, "rss": 102400, "tty": "", "state": "", # 11:50 UTC, an hour east of it "started": "Mon Dec 1 12:50:00 2025", "command": '"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" -NoLogo', } def test_a_process_without_a_command_line_shows_its_name(): system = _by_pid()[4] assert system["command"] == "System" assert system["started"] == "" assert system["cpu"] == 0.0 def test_a_start_one_second_apart_is_the_same_process(): # Get-Process and CIM round the start differently. reading = _reading() reading["owners"][2]["started"] += 1 assert 812 in _by_pid(reading) def test_without_elevation_the_owner_is_unknown(): # Get-Process -IncludeUserName needs an elevated session; the script falls back. reading = _reading() for owner in reading["owners"]: owner.pop("user") assert {p["user"] for p in _by_pid(reading).values()} == {""} def test_cpu_stays_within_a_hundred_percent(): reading = _reading() reading["processes"][3]["cpu"] = 10**13 assert _by_pid(reading)[4312]["cpu"] == 100.0 def test_a_single_process_survives_powershells_unwrapping(): reading = _reading() reading["processes"] = reading["processes"][2] reading["owners"] = reading["owners"][2] assert sorted(_by_pid(reading)) == [812] def test_one_script_reads_both_sources(): d = _driver() d.get_processes() (script,) = d._transport.sent # type: ignore[union-attr] assert script == mod._PS_PROCESSES assert "Win32_Process" in script assert "Get-Process -IncludeUserName" in script def test_an_empty_answer_is_no_reading(): d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport("") with pytest.raises(ValueError): d.get_processes()