Repository navigation
Expand file tree
/
Copy pathtest_processes.py
More file actions
56 lines (41 loc) · 1.91 KB
/
Copy pathtest_processes.py
File metadata and controls
56 lines (41 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Regression tests for graceful degradation in collector/processes.py.
AGENTS.md rule 3: every read of /proc must tolerate PermissionError, record
the reason, and continue. These tests simulate the exact non-root failure
seen on Linux CI hosts (PermissionError on /proc/<pid>/exe).
"""
from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from collector.processes import enumerate_processes, read_process
class _FakePidDir:
"""Minimal stand-in exposing only the attributes read_process touches."""
def __init__(self, base: Path, name: str) -> None:
self.name = name
self._base = base
def __truediv__(self, other: str) -> Path:
return self._base / other
def _raise_permission_denied(path: os.PathLike) -> str:
raise PermissionError(13, "Permission denied", str(path))
class ReadProcessDegradationTest(unittest.TestCase):
def test_permission_error_is_recorded_not_raised(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
skips: list[dict[str, str]] = []
pid_dir = _FakePidDir(Path(tmp), "1")
original_readlink = os.readlink
os.readlink = _raise_permission_denied # type: ignore[assignment]
try:
result = read_process(pid_dir, skips)
finally:
os.readlink = original_readlink # type: ignore[assignment]
self.assertIsNone(result)
self.assertTrue(skips, "skip reason must be recorded")
self.assertEqual(skips[0]["source"], "process:1")
self.assertIn("Permission denied", skips[0]["reason"])
def test_enumerate_processes_survives_missing_proc(self) -> None:
"""On a host without /proc (e.g. macOS) the collector yields no workloads."""
processes = enumerate_processes()
self.assertIsInstance(processes, list)
if __name__ == "__main__":
unittest.main()