2026-05-08 17:44:22 +02:00
|
|
|
"""Regression tests for pmon process-name normalization in usage_report."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
import _usage_report_parsing as parsing
|
|
|
|
|
|
2026-05-08 17:44:22 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_pmon_command_prefers_first_executable_token() -> None:
|
|
|
|
|
"""The parser should keep executable-like token, not trailing args."""
|
|
|
|
|
tokens = ["code-insiders", "--type=", "gpu-process", "Not"]
|
|
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
assert parsing._normalize_pmon_command(tokens) == "code-insiders"
|
2026-05-08 17:44:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_pmon_command_skips_leading_option_tokens() -> None:
|
|
|
|
|
"""If the first token is an option, use the next non-option token."""
|
|
|
|
|
tokens = ["--type=", "code-insiders", "--flag"]
|
|
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
assert parsing._normalize_pmon_command(tokens) == "code-insiders"
|
2026-05-08 17:44:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_ingest_pmon_row_uses_command_field_start_not_last_token() -> None:
|
|
|
|
|
"""Rows with command args should aggregate under process name, not args."""
|
|
|
|
|
row = [
|
|
|
|
|
"20260507",
|
|
|
|
|
"12:00:00",
|
|
|
|
|
"0",
|
|
|
|
|
"123",
|
|
|
|
|
"C",
|
|
|
|
|
"10",
|
|
|
|
|
"5",
|
|
|
|
|
"0",
|
|
|
|
|
"0",
|
|
|
|
|
"0",
|
|
|
|
|
"0",
|
|
|
|
|
"code-insiders",
|
|
|
|
|
"--type=",
|
|
|
|
|
"gpu-process",
|
|
|
|
|
]
|
|
|
|
|
agg: dict[str, object] = {}
|
|
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
consumed = parsing._ingest_pmon_row(row, agg)
|
2026-05-08 17:44:22 +02:00
|
|
|
|
|
|
|
|
assert consumed == 1
|
|
|
|
|
assert "code-insiders" in agg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_ingest_pmon_row_falls_back_to_proc_comm_on_unknown(
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""When command field is empty, parser can recover with /proc/<pid>/comm."""
|
|
|
|
|
row = [
|
|
|
|
|
"20260507",
|
|
|
|
|
"12:00:00",
|
|
|
|
|
"0",
|
|
|
|
|
"999",
|
|
|
|
|
"C",
|
|
|
|
|
"30",
|
|
|
|
|
"10",
|
|
|
|
|
"0",
|
|
|
|
|
"0",
|
|
|
|
|
"0",
|
|
|
|
|
"0",
|
|
|
|
|
]
|
|
|
|
|
agg: dict[str, object] = {}
|
|
|
|
|
|
2026-06-04 18:13:47 +02:00
|
|
|
monkeypatch.setattr(parsing, "_pid_comm_name", lambda _pid: "python")
|
|
|
|
|
consumed = parsing._ingest_pmon_row(row, agg)
|
2026-05-08 17:44:22 +02:00
|
|
|
|
|
|
|
|
assert consumed == 1
|
|
|
|
|
assert "python" in agg
|