import os from pathlib import Path import pytest from ramscreen.meminfo import App, Memory, level, parse_meminfo, read_memory, top_apps MEMINFO = """\ MemTotal: 16000000 kB MemFree: 400000 kB MemAvailable: 1600000 kB Buffers: 100000 kB SwapTotal: 2000000 kB SwapFree: 50000 kB """ def test_parse_meminfo(): mem = parse_meminfo(MEMINFO) assert mem == Memory( total=16000000 * 1024, available=1600000 * 1024, swap_total=2000000 * 1024, swap_free=50000 * 1024, ) assert mem.used == (16000000 - 1600000) * 1024 def test_read_memory_liest_datei(tmp_path: Path): path = tmp_path / "meminfo" path.write_text(MEMINFO) assert read_memory(path).total == 16000000 * 1024 def test_read_memory_echtes_system(): mem = read_memory() assert mem.total > 0 assert 0 <= mem.available <= mem.total @pytest.mark.parametrize( ("available", "expected"), [(500, "ok"), (200, "ok"), (199, "knapp"), (100, "knapp"), (99, "kritisch"), (0, "kritisch")], ) def test_level_grenzen(available, expected): mem = Memory(total=1000, available=available, swap_total=0, swap_free=0) assert level(mem, 20, 10) == expected def make_proc(root: Path, pid, *, exe=None, comm="x", pss_kb=None, rss_kb=None, smaps_text=None): d = root / str(pid) d.mkdir() if exe is not None: os.symlink(exe, d / "exe") if comm is not None: (d / "comm").write_text(comm + "\n") if smaps_text is not None: (d / "smaps_rollup").write_text(smaps_text) elif pss_kb is not None: (d / "smaps_rollup").write_text( f"00400000-7ffc0000 ---p 00000000 00:00 0 [rollup]\n" f"Rss: {pss_kb + 10} kB\nPss: {pss_kb} kB\nPss_Anon: 1 kB\n" ) status = f"Name:\t{comm}\n" if rss_kb is not None: status += f"VmRSS:\t{rss_kb} kB\n" (d / "status").write_text(status) def test_top_apps_fasst_prozesse_eines_programms_zusammen(tmp_path: Path): make_proc(tmp_path, 1, exe="/usr/lib/firefox/firefox", comm="firefox", pss_kb=1000) make_proc(tmp_path, 2, exe="/usr/lib/firefox/firefox", comm="Isolated Web Co", pss_kb=500) make_proc(tmp_path, 3, exe="/usr/lib/virtualbox/VirtualBoxVM", comm="VirtualBoxVM", pss_kb=4000) assert top_apps(5, tmp_path) == [App("VirtualBoxVM", 4000 * 1024), App("firefox", 1500 * 1024)] def test_top_apps_begrenzt_anzahl(tmp_path: Path): for pid, kb in enumerate([100, 400, 300, 200], start=1): make_proc(tmp_path, pid, exe=f"/usr/bin/prog{pid}", pss_kb=kb) assert [a.name for a in top_apps(2, tmp_path)] == ["prog2", "prog3"] def test_top_apps_name_aus_comm_wenn_exe_fehlt(tmp_path: Path): make_proc(tmp_path, 7, exe=None, comm="fremdprozess", pss_kb=300) assert top_apps(5, tmp_path) == [App("fremdprozess", 300 * 1024)] def test_top_apps_rss_wenn_smaps_fehlt(tmp_path: Path): make_proc(tmp_path, 8, exe="/usr/bin/teamviewer", rss_kb=700) assert top_apps(5, tmp_path) == [App("teamviewer", 700 * 1024)] def test_top_apps_deleted_suffix_wird_entfernt(tmp_path: Path): make_proc(tmp_path, 1, exe="/usr/lib/firefox/firefox (deleted)", pss_kb=600) make_proc(tmp_path, 2, exe="/usr/lib/firefox/firefox", pss_kb=400) assert top_apps(5, tmp_path) == [App("firefox", 1000 * 1024)] def test_top_apps_versionsnummer_als_dateiname_nutzt_comm(tmp_path: Path): # z. B. Claude-CLI: ~/.local/share/claude/versions/2.1.276 make_proc(tmp_path, 1, exe="/home/u/.local/share/claude/versions/2.1.276", comm="claude", pss_kb=300) make_proc(tmp_path, 2, exe="/opt/claude/claude", comm="claude", pss_kb=200) assert top_apps(5, tmp_path) == [App("claude", 500 * 1024)] def test_top_apps_ueberspringt_kernel_threads_und_verschwundene(tmp_path: Path): # Kernel-Thread: kein exe, leeres smaps_rollup, kein VmRSS make_proc(tmp_path, 2, exe=None, comm="kworker/0:1", smaps_text="") # verschwundener Prozess: Verzeichnis da, aber weder exe noch comm make_proc(tmp_path, 3, exe=None, comm=None) # nicht-numerische Einträge (tmp_path / "self").mkdir() (tmp_path / "meminfo").write_text(MEMINFO) make_proc(tmp_path, 4, exe="/usr/bin/obsidian", pss_kb=200) assert top_apps(5, tmp_path) == [App("obsidian", 200 * 1024)] def test_top_apps_echtes_system(): apps = top_apps(5) assert 1 <= len(apps) <= 5 assert all(a.size > 0 for a in apps) assert apps == sorted(apps, key=lambda a: a.size, reverse=True)