diff --git a/tests/unit/services/test_cluster_state_vm_domains.py b/tests/unit/services/test_cluster_state_vm_domains.py index d897c6f..34cbd56 100644 --- a/tests/unit/services/test_cluster_state_vm_domains.py +++ b/tests/unit/services/test_cluster_state_vm_domains.py @@ -279,3 +279,64 @@ def test_vm_usage_error_paths(monkeypatch) -> None: assert vm_usage._percentile([], 0.5) is None assert vm_usage._node_load_summary([]) == {} assert vm_usage._namespace_capacity_summary([]) == {} + + +def test_vm_usage_filters_invalid_metric_inputs(monkeypatch) -> None: + monkeypatch.setattr( + vm_usage, + "_vm_vector", + lambda _expr: [ + {"metric": "bad", "value": 1.0}, + {"metric": {"namespace": ""}, "value": 2.0}, + {"metric": {"namespace": "bad"}, "value": "not-a-number"}, + {"metric": {"namespace": "apps"}, "value": "3.5"}, + ], + ) + + assert vm_usage._usage_stats(["bad", {"value": None}, {"value": "5.5"}]) == { + "min": 5.5, + "max": 5.5, + "avg": 5.5, + } + assert vm_usage._vm_namespace_totals("expr") == {"apps": 3.5} + + profile = vm_usage._node_usage_profile( + { + "cpu": [ + {"node": "", "value": 1.0}, + {"node": "titan-2", "value": "hot"}, + {"node": "titan-1", "value": 4.0}, + ], + "ram": [{"node": "titan-1", "value": 2.0}], + }, + [{"name": "titan-1", "pressure": [], "taints": "bad", "unschedulable": True}], + ["bad"], + ) + assert profile == [ + { + "node": "titan-1", + "cpu": 4.0, + "ram": 2.0, + "disk": None, + "net": None, + "io": None, + "cpu_norm": 1.0, + "ram_norm": 1.0, + "pressure_flags": {}, + "pressure_count": 0, + "taints": [], + "unschedulable": True, + "pods_total": None, + "load_index": 1.0, + } + ] + + summary = vm_usage._namespace_capacity_summary( + [ + "bad", + {"namespace": "apps", "cpu_usage": 3.0, "cpu_requests": 2.0}, + {"namespace": "media", "mem_usage": 4.0, "mem_requests": 2.0, "mem_usage_ratio": 2.0}, + ] + ) + assert summary["cpu_overcommitted"] == 0 + assert summary["mem_overcommitted_names"] == ["media"]