mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 09:56:17 -05:00
Consolidating controller tests. Fixing controller test cleanup which slowed down tests significantly.
This commit is contained in:
parent
b0be30c3d6
commit
2806673e25
11 changed files with 366 additions and 970 deletions
|
|
@ -12,84 +12,40 @@ class TestCollectAllStatus:
|
|||
"""Tests for collect_all_status function."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collects_all_stats(self):
|
||||
"""Should collect all status types."""
|
||||
async def test_collects_all_stats_and_iso_timestamp(self):
|
||||
"""Should collect every status type and include a parseable ISO timestamp."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
status = await collect_all_status()
|
||||
|
||||
assert "cpu_stats" in status
|
||||
assert "memory_stats" in status
|
||||
assert "load_stats" in status
|
||||
assert "drives_stats" in status
|
||||
assert "nic_bandwidth_stats" in status
|
||||
assert "power_stats" in status
|
||||
assert "last_status" in status
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_last_status_is_iso_timestamp(self):
|
||||
"""Should include a valid ISO timestamp."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
status = await collect_all_status()
|
||||
|
||||
# Should be parseable as ISO datetime
|
||||
for key in (
|
||||
"cpu_stats", "memory_stats", "load_stats", "drives_stats",
|
||||
"nic_bandwidth_stats", "power_stats", "last_status",
|
||||
):
|
||||
assert key in status
|
||||
datetime.fromisoformat(status["last_status"])
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handles_cpu_failure(self):
|
||||
"""Should handle failures in CPU stat collector."""
|
||||
@pytest.mark.parametrize("collector,result_key,failure_value", [
|
||||
("get_cpu_status", "cpu_stats", None),
|
||||
("get_memory_status", "memory_stats", None),
|
||||
("get_drive_status", "drives_stats", []),
|
||||
("get_network_status", "nic_bandwidth_stats", {}),
|
||||
])
|
||||
async def test_individual_collector_failure(self, collector, result_key, failure_value):
|
||||
"""A single collector raising should leave the failed key at its fallback value
|
||||
and let the other collectors still populate the result."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
with mock.patch(
|
||||
"controller.lib.status_worker.get_cpu_status",
|
||||
side_effect=Exception("CPU error")
|
||||
f"controller.lib.status_worker.{collector}",
|
||||
side_effect=Exception(f"{collector} error"),
|
||||
):
|
||||
status = await collect_all_status()
|
||||
|
||||
# CPU should be None but others should work
|
||||
assert status["cpu_stats"] is None
|
||||
assert status["memory_stats"] is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handles_memory_failure(self):
|
||||
"""Should handle failures in memory stat collector."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
with mock.patch(
|
||||
"controller.lib.status_worker.get_memory_status",
|
||||
side_effect=Exception("Memory error")
|
||||
):
|
||||
status = await collect_all_status()
|
||||
|
||||
assert status["memory_stats"] is None
|
||||
assert status["cpu_stats"] is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_drives_stats_is_list_on_failure(self):
|
||||
"""Should return empty list for drives on failure."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
with mock.patch(
|
||||
"controller.lib.status_worker.get_drive_status",
|
||||
side_effect=Exception("Drive error")
|
||||
):
|
||||
status = await collect_all_status()
|
||||
|
||||
assert status["drives_stats"] == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_nic_stats_is_dict_on_failure(self):
|
||||
"""Should return empty dict for network on failure."""
|
||||
from controller.lib.status_worker import collect_all_status
|
||||
|
||||
with mock.patch(
|
||||
"controller.lib.status_worker.get_network_status",
|
||||
side_effect=Exception("Network error")
|
||||
):
|
||||
status = await collect_all_status()
|
||||
|
||||
assert status["nic_bandwidth_stats"] == {}
|
||||
assert status[result_key] == failure_value
|
||||
# At least one other collector should still have produced real data.
|
||||
other_keys = [k for k in ("cpu_stats", "memory_stats") if k != result_key]
|
||||
assert any(status[k] is not None for k in other_keys)
|
||||
|
||||
|
||||
class TestGetAdaptiveSleep:
|
||||
|
|
@ -155,94 +111,32 @@ class TestGetAdaptiveSleep:
|
|||
|
||||
|
||||
class TestAggregatedStatsEndpoint:
|
||||
"""Tests for the new /api/stats aggregated endpoint."""
|
||||
"""Tests for the /api/stats aggregated endpoint."""
|
||||
|
||||
def test_aggregated_stats_returns_200(self, test_client):
|
||||
"""Aggregated stats endpoint should return 200 OK."""
|
||||
def test_aggregated_stats_payload(self, test_client):
|
||||
"""Endpoint should return 200 with every status key and the expected sub-shapes."""
|
||||
response = test_client.get("/api/stats")
|
||||
assert response.status_code == 200
|
||||
|
||||
def test_aggregated_stats_returns_all_fields(self, test_client):
|
||||
"""Aggregated stats should include all status types."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
assert "cpu_stats" in data
|
||||
assert "memory_stats" in data
|
||||
assert "load_stats" in data
|
||||
assert "drives_stats" in data
|
||||
assert "nic_bandwidth_stats" in data
|
||||
assert "power_stats" in data
|
||||
assert "last_status" in data
|
||||
|
||||
def test_aggregated_stats_has_valid_timestamp(self, test_client):
|
||||
"""Should include a timestamp of when data was collected."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
assert "last_status" in data
|
||||
# Should be ISO format timestamp
|
||||
for key in (
|
||||
"cpu_stats", "memory_stats", "load_stats", "drives_stats",
|
||||
"nic_bandwidth_stats", "power_stats", "last_status",
|
||||
):
|
||||
assert key in data
|
||||
# last_status is an ISO timestamp.
|
||||
datetime.fromisoformat(data["last_status"])
|
||||
|
||||
def test_aggregated_stats_cpu_has_expected_fields(self, test_client):
|
||||
"""CPU stats should have expected fields."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
cpu = data["cpu_stats"]
|
||||
assert cpu is not None
|
||||
assert "percent" in cpu
|
||||
assert "cores" in cpu
|
||||
|
||||
def test_aggregated_stats_memory_has_expected_fields(self, test_client):
|
||||
"""Memory stats should have expected fields."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
memory = data["memory_stats"]
|
||||
assert memory is not None
|
||||
assert "total" in memory
|
||||
assert "used" in memory
|
||||
assert "free" in memory
|
||||
|
||||
def test_aggregated_stats_load_has_expected_fields(self, test_client):
|
||||
"""Load stats should have expected fields."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
load = data["load_stats"]
|
||||
assert load is not None
|
||||
assert "minute_1" in load
|
||||
assert "minute_5" in load
|
||||
assert "minute_15" in load
|
||||
|
||||
def test_aggregated_stats_drives_is_list(self, test_client):
|
||||
"""Drives stats should be a list."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
# Sub-field shape spot-checks.
|
||||
for field in ("percent", "cores"):
|
||||
assert field in data["cpu_stats"]
|
||||
for field in ("total", "used", "free"):
|
||||
assert field in data["memory_stats"]
|
||||
for field in ("minute_1", "minute_5", "minute_15"):
|
||||
assert field in data["load_stats"]
|
||||
assert isinstance(data["drives_stats"], list)
|
||||
|
||||
def test_aggregated_stats_network_is_dict(self, test_client):
|
||||
"""Network stats should be a dict."""
|
||||
response = test_client.get("/api/stats")
|
||||
data = response.json()
|
||||
|
||||
assert isinstance(data["nic_bandwidth_stats"], dict)
|
||||
|
||||
def test_aggregated_stats_trailing_slash(self, test_client):
|
||||
"""Aggregated stats should work with trailing slash."""
|
||||
response = test_client.get("/api/stats/")
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
class TestOpenAPIIncludesAggregatedStats:
|
||||
"""Test that OpenAPI includes the new aggregated endpoint."""
|
||||
|
||||
def test_openapi_has_aggregated_stats_path(self, test_client):
|
||||
"""OpenAPI schema should include /api/stats path."""
|
||||
response = test_client.get("/openapi.json")
|
||||
data = response.json()
|
||||
# Check for either with or without trailing slash
|
||||
has_path = "/api/stats" in data["paths"] or "/api/stats/" in data["paths"]
|
||||
assert has_path, f"Expected /api/stats in paths, got: {list(data['paths'].keys())}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue