test(ariadne): cover cluster profile builders
This commit is contained in:
parent
24c3d842c1
commit
415da50fa1
@ -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),
|
||||
|
||||
@ -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"] == []
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user