test(host_agent): update existing collector tests for new agent API
collect_cpu now returns (aggregate, [per_core]) and build_sample takes a rate-state dict, so the pre-existing tests that asserted the old float return / no-state signature needed updating. Mocks the new collectors (temps/psi/net/diskio) for determinism. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,14 +9,19 @@ def test_build_sample_shape():
|
|||||||
fake_load = {"1m": 0.1, "5m": 0.2, "15m": 0.3}
|
fake_load = {"1m": 0.1, "5m": 0.2, "15m": 0.3}
|
||||||
fake_storage = [{"mount": "/", "total_bytes": 1000, "used_bytes": 400}]
|
fake_storage = [{"mount": "/", "total_bytes": 1000, "used_bytes": 400}]
|
||||||
|
|
||||||
with patch.object(a, "collect_cpu", return_value=12.5), \
|
with patch.object(a, "collect_cpu", return_value=(12.5, [12.0, 13.0])), \
|
||||||
patch.object(a, "collect_memory", return_value=fake_mem), \
|
patch.object(a, "collect_memory", return_value=fake_mem), \
|
||||||
patch.object(a, "collect_load", return_value=fake_load), \
|
patch.object(a, "collect_load", return_value=fake_load), \
|
||||||
patch.object(a, "collect_uptime", return_value=1234), \
|
patch.object(a, "collect_uptime", return_value=1234), \
|
||||||
patch.object(a, "collect_storage", return_value=fake_storage):
|
patch.object(a, "collect_storage", return_value=fake_storage), \
|
||||||
sample = a.build_sample(mounts=["/"])
|
patch.object(a, "collect_temps", return_value=[]), \
|
||||||
|
patch.object(a, "collect_psi", return_value={}), \
|
||||||
|
patch.object(a, "collect_net_raw", return_value={}), \
|
||||||
|
patch.object(a, "collect_diskio_raw", return_value={}):
|
||||||
|
sample = a.build_sample(["/"], {})
|
||||||
|
|
||||||
assert sample["cpu_pct"] == 12.5
|
assert sample["cpu_pct"] == 12.5
|
||||||
|
assert sample["cpu_cores"] == [12.0, 13.0]
|
||||||
assert sample["mem"]["used_bytes"] == 40
|
assert sample["mem"]["used_bytes"] == 40
|
||||||
assert sample["load"]["1m"] == 0.1
|
assert sample["load"]["1m"] == 0.1
|
||||||
assert sample["uptime_secs"] == 1234
|
assert sample["uptime_secs"] == 1234
|
||||||
@@ -36,8 +41,12 @@ def test_build_sample_tolerates_collector_failure():
|
|||||||
"available_bytes": 1, "swap_used_bytes": 0}), \
|
"available_bytes": 1, "swap_used_bytes": 0}), \
|
||||||
patch.object(a, "collect_load", return_value={"1m": 0.0, "5m": 0.0, "15m": 0.0}), \
|
patch.object(a, "collect_load", return_value={"1m": 0.0, "5m": 0.0, "15m": 0.0}), \
|
||||||
patch.object(a, "collect_uptime", return_value=0), \
|
patch.object(a, "collect_uptime", return_value=0), \
|
||||||
patch.object(a, "collect_storage", return_value=[]):
|
patch.object(a, "collect_storage", return_value=[]), \
|
||||||
sample = a.build_sample(mounts=["/"])
|
patch.object(a, "collect_temps", return_value=[]), \
|
||||||
|
patch.object(a, "collect_psi", return_value={}), \
|
||||||
|
patch.object(a, "collect_net_raw", return_value={}), \
|
||||||
|
patch.object(a, "collect_diskio_raw", return_value={}):
|
||||||
|
sample = a.build_sample(["/"], {})
|
||||||
assert sample["cpu_pct"] is None
|
assert sample["cpu_pct"] is None
|
||||||
assert sample["mem"]["total_bytes"] == 1
|
assert sample["mem"]["total_bytes"] == 1
|
||||||
|
|
||||||
|
|||||||
@@ -56,10 +56,11 @@ def test_collect_cpu_reads_twice():
|
|||||||
|
|
||||||
with patch.object(a, "_read_file", _read), \
|
with patch.object(a, "_read_file", _read), \
|
||||||
patch.object(a.time, "sleep", lambda _s: None):
|
patch.object(a.time, "sleep", lambda _s: None):
|
||||||
pct = a.collect_cpu(sample_window=0.0)
|
pct, cores = a.collect_cpu(sample_window=0.0)
|
||||||
# totals: 1000 → 2000 (delta 1000). idle+iowait: 850 → 1700 (delta 850).
|
# totals: 1000 → 2000 (delta 1000). idle+iowait: 850 → 1700 (delta 850).
|
||||||
# busy = 150. pct = 15.0.
|
# busy = 150. pct = 15.0. No cpuN lines in the fixture → no per-core values.
|
||||||
assert pct == pytest.approx(15.0, abs=0.1)
|
assert pct == pytest.approx(15.0, abs=0.1)
|
||||||
|
assert cores == []
|
||||||
|
|
||||||
|
|
||||||
def test_collect_storage():
|
def test_collect_storage():
|
||||||
|
|||||||
Reference in New Issue
Block a user