From 415da50fa1bcd39dc087adcdb8ddd26023302f96 Mon Sep 17 00:00:00 2001 From: codex Date: Tue, 21 Apr 2026 03:42:16 -0300 Subject: [PATCH] test(ariadne): cover cluster profile builders --- ariadne/services/cluster_state_profiles.py | 20 +++++-------- .../test_cluster_state_context_domains.py | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/ariadne/services/cluster_state_profiles.py b/ariadne/services/cluster_state_profiles.py index bd13ef9..e28a6a8 100644 --- a/ariadne/services/cluster_state_profiles.py +++ b/ariadne/services/cluster_state_profiles.py @@ -4,11 +4,11 @@ from typing import Any from .cluster_state_contract import * -def _node_profiles( - node_context: list[dict[str, Any]], - node_pods: list[dict[str, Any]], - node_workloads: dict[str, dict[str, int]], -) -> list[dict[str, Any]]: +ProfileRows = list[dict[str, Any]] +NodeWorkloadMap = dict[str, dict[str, int]] + + +def _node_profiles(node_context: ProfileRows, node_pods: ProfileRows, node_workloads: NodeWorkloadMap) -> ProfileRows: pod_map = {entry.get("node"): entry for entry in node_pods if isinstance(entry, dict)} workload_map = node_workloads or {} profiles: list[dict[str, Any]] = [] @@ -74,7 +74,7 @@ def _workload_profiles(workloads: list[dict[str, Any]]) -> list[dict[str, Any]]: ) output: list[dict[str, Any]] = [] for entry in entries[:_PROFILE_LIMIT]: - nodes = entry.get("nodes") if isinstance(entry.get("nodes"), dict) else {} + nodes = entry.get("nodes") nodes_top = ( sorted(nodes.items(), key=lambda item: (-item[1], item[0]))[:3] if isinstance(nodes, dict) @@ -94,13 +94,7 @@ def _workload_profiles(workloads: list[dict[str, Any]]) -> list[dict[str, Any]]: return output -def _build_profiles( - node_context: list[dict[str, Any]], - namespace_context: list[dict[str, Any]], - node_pods: list[dict[str, Any]], - workloads: list[dict[str, Any]], - node_workloads: dict[str, dict[str, int]], -) -> dict[str, Any]: +def _build_profiles(node_context: ProfileRows, namespace_context: ProfileRows, node_pods: ProfileRows, workloads: ProfileRows, node_workloads: NodeWorkloadMap) -> dict[str, Any]: return { "nodes": _node_profiles(node_context, node_pods, node_workloads), "namespaces": _namespace_profiles(namespace_context), diff --git a/tests/unit/services/test_cluster_state_context_domains.py b/tests/unit/services/test_cluster_state_context_domains.py index 327d139..b2b01eb 100644 --- a/tests/unit/services/test_cluster_state_context_domains.py +++ b/tests/unit/services/test_cluster_state_context_domains.py @@ -206,3 +206,33 @@ def test_health_anomaly_signal_profile_and_attention_domains() -> None: ranked = attention._build_attention_ranked(metrics, node_context, pod_issues, workloads_health) assert ranked[0]["score"] > 0 assert attention._node_attention_score(node_context[0])[0] > 0 + + +def test_profile_builders_filter_bad_nodes_and_workload_nodes() -> None: + node_profiles = profiles._node_profiles( + [ + None, + {"node": ""}, + {"node": 123}, + {"node": "titan-2", "load_index": 1.0}, + {"node": "titan-1", "load_index": 2.0}, + ], + [{"node": "titan-1", "pods_total": 3, "pods_running": 2}], + {"titan-1": {"apps/api": 2, "apps/worker": 1}}, + ) + + assert [profile["node"] for profile in node_profiles] == ["titan-1", "titan-2"] + assert node_profiles[0]["pods_total"] == 3 + assert node_profiles[0]["workloads_top"] == [("apps/api", 2), ("apps/worker", 1)] + assert node_profiles[1]["namespaces_top"] == [] + + workload_profiles = profiles._workload_profiles( + [ + None, + {"namespace": "apps", "workload": "api", "pods_total": 2, "nodes": {"titan-2": 1, "titan-1": 2}}, + {"namespace": "apps", "workload": "bad", "pods_total": 1, "nodes": "not-a-map"}, + ] + ) + + assert workload_profiles[0]["nodes_top"] == [("titan-1", 2), ("titan-2", 1)] + assert workload_profiles[1]["nodes_top"] == []