test(ariadne): cover cluster profile builders

This commit is contained in:
codex 2026-04-21 03:42:16 -03:00
parent 24c3d842c1
commit 415da50fa1
2 changed files with 37 additions and 13 deletions

View File

@ -4,11 +4,11 @@ from typing import Any
from .cluster_state_contract import * from .cluster_state_contract import *
def _node_profiles( ProfileRows = list[dict[str, Any]]
node_context: list[dict[str, Any]], NodeWorkloadMap = dict[str, dict[str, int]]
node_pods: list[dict[str, Any]],
node_workloads: dict[str, dict[str, int]],
) -> list[dict[str, Any]]: 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)} pod_map = {entry.get("node"): entry for entry in node_pods if isinstance(entry, dict)}
workload_map = node_workloads or {} workload_map = node_workloads or {}
profiles: list[dict[str, Any]] = [] 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]] = [] output: list[dict[str, Any]] = []
for entry in entries[:_PROFILE_LIMIT]: for entry in entries[:_PROFILE_LIMIT]:
nodes = entry.get("nodes") if isinstance(entry.get("nodes"), dict) else {} nodes = entry.get("nodes")
nodes_top = ( nodes_top = (
sorted(nodes.items(), key=lambda item: (-item[1], item[0]))[:3] sorted(nodes.items(), key=lambda item: (-item[1], item[0]))[:3]
if isinstance(nodes, dict) if isinstance(nodes, dict)
@ -94,13 +94,7 @@ def _workload_profiles(workloads: list[dict[str, Any]]) -> list[dict[str, Any]]:
return output return output
def _build_profiles( def _build_profiles(node_context: ProfileRows, namespace_context: ProfileRows, node_pods: ProfileRows, workloads: ProfileRows, node_workloads: NodeWorkloadMap) -> dict[str, Any]:
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]:
return { return {
"nodes": _node_profiles(node_context, node_pods, node_workloads), "nodes": _node_profiles(node_context, node_pods, node_workloads),
"namespaces": _namespace_profiles(namespace_context), "namespaces": _namespace_profiles(namespace_context),

View File

@ -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) ranked = attention._build_attention_ranked(metrics, node_context, pod_issues, workloads_health)
assert ranked[0]["score"] > 0 assert ranked[0]["score"] > 0
assert attention._node_attention_score(node_context[0])[0] > 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"] == []