Files
Pulse/scripts/tests/test_proxmox_memory_load.py
pulse-triage[bot] 72dc4f1f0d Require recent guest polls in the Proxmox memory watch
A complete startup roster and another dashboard message can outlive PVE polling. Bind fresh guest sightings to the end of RSS sampling and retain the observed counts without changing the RSS gate or PBS group coverage.

Change-source: pulse-maintainer
2026-11-22 03:10:57 +00:00

190 lines
9.3 KiB
Python

#!/usr/bin/env python3
"""Exercise PVE guest and PBS snapshot coverage in the memory watch."""
from collections import Counter
from datetime import datetime, timezone
import json
from pathlib import Path
import sys
import threading
import unittest
from unittest.mock import Mock
from urllib.request import urlopen
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "memory"))
import proxmox_load as load
class ProxmoxMemoryLoadTests(unittest.TestCase):
def test_cluster_inventory_matches_estate_and_node_lists(self):
counters = Counter()
nodes = ["pve1", "pve2", "pve3"]
handler, guest_refs = load.make_pve_handler(nodes, 6, 5, counters, threading.Lock())
with load.ThreadedHTTPServer(("127.0.0.1", 0), handler) as server:
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
try:
def get(path):
with urlopen(f"http://127.0.0.1:{server.server_port}/api2/json{path}", timeout=5) as response:
self.assertEqual(response.status, 200)
return json.load(response)["data"]
inventory = get("/cluster/resources?type=vm")
self.assertEqual(Counter(row["type"] for row in inventory), {"qemu": 20, "lxc": 15})
self.assertEqual(len({row["vmid"] for row in inventory}), 35)
self.assertEqual(
{(row["node"], "vm" if row["type"] == "qemu" else "ct", row["vmid"], row["name"])
for row in inventory},
set(guest_refs),
)
for row in inventory:
self.assertEqual(row["id"], f"{row['type']}/{row['vmid']}")
self.assertEqual(row["status"], "running")
self.assertGreater(row["maxcpu"], 0)
self.assertGreater(row["mem"], 0)
self.assertGreater(row["diskread"], 0)
for node in nodes:
for kind in ("qemu", "lxc"):
self.assertEqual(
{row["vmid"] for row in get(f"/nodes/{node}/{kind}")},
{r["vmid"] for r in inventory if r["node"] == node and r["type"] == kind},
)
self.assertEqual(counters["pve_requests"], 7)
finally:
server.shutdown()
worker.join(timeout=5)
def test_pbs_status_empty_and_duplicate_reads_cannot_cover_snapshot_groups(self):
guests = [("pve", "vm", 100, "vm-100"), ("pve", "ct", 101, "ct-101")]
counters = Counter()
lock = threading.Lock()
handler_type, total, primary = load.make_pbs_handler("pbs1", guests, 3, counters, lock)
handler = object.__new__(handler_type)
handler.send_json = Mock()
_, _, secondary = load.make_pbs_handler("pbs2", guests, 3, counters, lock)
self.assertEqual(total, 9)
def request(path):
handler.path = f"/api2/json/admin/datastore/{path}"
handler.do_GET()
return handler.send_json.call_args.args[0]
def gaps():
return load.missing_snapshot_coverage({
name: {store: row.counts() for store, row in coverage.items()}
for name, coverage in {"pbs1": primary, "pbs2": secondary}.items()
})
request("store-archive/status")
request("store-archive/groups")
self.assertEqual(request("store-archive/snapshots?backup-type=ct&backup-id=101"), [])
self.assertEqual(counters["pbs1_requests"], 3)
self.assertIn("pbs1/store-archive 0/1", gaps())
for _ in range(2):
self.assertEqual(len(request("store-fast/snapshots?backup-type=vm&backup-id=100")), 3)
self.assertIn("pbs1/store-fast 1/2", gaps())
handler.send_json.side_effect = BrokenPipeError
with self.assertRaises(BrokenPipeError):
request("store-fast/snapshots?backup-type=ct&backup-id=101")
self.assertIn("pbs1/store-fast 1/2", gaps())
handler.send_json.side_effect = None
request("store-fast/snapshots?backup-type=ct&backup-id=101")
request("store-archive/snapshots?backup-type=vm&backup-id=100")
self.assertEqual(gaps(), ["pbs2/store-archive 0/1", "pbs2/store-fast 0/2"])
def setUp(self):
self.expected = {("qemu", "vm-100"), ("lxc", "ct-101")}
self.stats = load.DashboardStats(expected_guests=self.expected)
self.initial = False
def frame(self, message_type, **data):
payload = json.dumps({"type": message_type, "data": data}).encode()
self.initial = load.record_ws_message(1, payload, self.stats, self.initial)
def test_state_traffic_without_guest_rows_does_not_establish_coverage(self):
self.frame("initialState", resources=[])
self.frame("rawData", lastUpdate=200)
self.assertEqual(self.stats.ws_state_updates, 1)
self.assertEqual(self.stats.missing_guests(), self.expected)
def test_guest_coverage_follows_new_rows_partial_patches_and_removals(self):
self.frame("initialState", resources=[{"id": "a", "technology": "qemu", "name": "vm-100"}])
self.assertEqual(self.stats.missing_guests(), {("lxc", "ct-101")})
self.frame("rawData", resourceDelta={"upserts": [
{"id": "a", "cpu": {"current": 0.3}},
{"id": "b", "technology": "lxc", "name": "ct-101"},
]})
self.assertFalse(self.stats.missing_guests())
self.frame("rawData", resourceDelta={"upserts": [{"id": "b", "technology": "docker"}], "removed": ["a"]})
self.assertEqual(self.stats.missing_guests(), self.expected)
def test_withheld_or_replaced_state_cannot_reuse_previous_guests(self):
rows = [
{"id": "a", "technology": "qemu", "name": "vm-100"},
{"id": "b", "technology": "lxc", "name": "ct-101"},
]
self.frame("initialState", resources=rows)
self.frame("rawData", resources=[])
self.assertEqual(self.stats.missing_guests(), self.expected)
self.frame("rawData", resources=rows)
self.assertFalse(self.stats.missing_guests())
self.frame("stateTooLarge", supersedes="rawData")
self.frame("rawData", resourceDelta={"upserts": rows})
self.assertFalse(self.initial)
self.assertEqual(self.stats.missing_guests(), self.expected)
self.frame("initialState", resources=rows[:1])
self.assertEqual(self.stats.missing_guests(), {("lxc", "ct-101")})
def test_a_retained_roster_or_unrelated_update_does_not_prove_late_collection(self):
old = "2026-11-21T00:00:30Z"
end = datetime(2026, 11, 21, 0, 5, tzinfo=timezone.utc).timestamp()
self.frame("initialState", resources=[
{"id": "a", "technology": "qemu", "name": "vm-100", "lastSeen": old},
{"id": "b", "technology": "lxc", "name": "ct-101", "lastSeen": old},
])
self.frame("rawData", resourceDelta={"upserts": [
{"id": "a", "lastSeen": "2026-11-21T00:04:00.999999999Z"},
{"id": "b", "updatedAt": "2026-11-21T00:04:55Z"},
]})
self.assertFalse(self.stats.missing_guests())
self.assertEqual(self.stats.missing_recent_guests(end - 300, end), {("lxc", "ct-101")})
self.frame("rawData", resourceDelta={"upserts": [
{"id": "b", "lastSeen": "2026-11-21T01:04:58+01:00"},
]})
self.assertFalse(self.stats.missing_recent_guests(end - 300, end))
def test_missing_or_invalid_times_and_previous_run_do_not_prove_a_poll(self):
end = datetime(2026, 11, 21, 0, 5, tzinfo=timezone.utc).timestamp()
for timestamp in ("2026-11-21T00:04:00Z", "2026-11-21T00:05:01Z",
"2026-11-21T00:04:59", "not a date", None):
with self.subTest(timestamp=timestamp):
self.frame("initialState", resources=[
{"id": "a", "technology": "qemu", "name": "vm-100", "lastSeen": timestamp},
{"id": "b", "technology": "lxc", "name": "ct-101"},
])
self.assertFalse(self.stats.missing_guests())
self.assertEqual(self.stats.missing_recent_guests(end - 30, end), self.expected)
def test_recent_sighting_stays_bound_to_the_guest_and_current_inventory(self):
end = datetime(2026, 11, 21, 0, 5, tzinfo=timezone.utc).timestamp()
self.frame("initialState", resources=[
{"id": "a", "technology": "qemu", "name": "vm-100", "lastSeen": "2026-11-21T00:04:51Z"},
{"id": "b", "technology": "lxc", "name": "ct-101", "lastSeen": "2026-11-21T00:04:52Z"},
])
self.frame("rawData", resourceDelta={"upserts": [{"id": "a", "cpu": {"current": 0.3}}]})
self.assertFalse(self.stats.missing_recent_guests(end - 300, end))
self.frame("rawData", resourceDelta={"upserts": [
{"id": "c", "technology": "qemu", "name": "vm-100"},
], "removed": ["a"]})
self.assertEqual(self.stats.missing_recent_guests(end - 300, end), {("qemu", "vm-100")})
self.frame("rawData", resourceDelta={"upserts": [
{"id": "b", "technology": "qemu", "name": "vm-100"},
]})
self.assertEqual(self.stats.missing_recent_guests(end - 300, end), self.expected)
if __name__ == "__main__":
unittest.main()