railiance-cluster/tests/test_cluster_resources.py
codex b36ac28b49
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 2s
Publish pending unscheduled pods in cluster observations.
RAIL-BS-WP-0014: residual millicores count scheduled pods only.
state_hub_preflight_observation matches the STATE-WP-0091 fixture shape.

Assistant: grok
Assistant-Session: 01a09dc1-b21e-77e1-919e-fcad2f82b267
2026-09-14 10:34:27 +02:00

61 lines
4.1 KiB
Python

from __future__ import annotations
import importlib.util
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SPEC = importlib.util.spec_from_file_location("cluster_resources", ROOT / "tools" / "observe_cluster_resources.py")
assert SPEC and SPEC.loader
module = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(module)
class ClusterResourceTests(unittest.TestCase):
def test_quantity_conversion(self) -> None:
self.assertEqual(250, module.cpu_millicores("250m"))
self.assertEqual(2000, module.cpu_millicores("2"))
self.assertEqual(1024**3, module.bytes_value("1Gi"))
def test_aggregation_and_missing_metrics(self) -> None:
snapshots = {
"nodes": {"items": [{"metadata": {"name": "node-1", "labels": {"kubernetes.io/hostname": "node-1"}}, "spec": {}, "status": {"allocatable": {"cpu": "2", "memory": "4Gi", "ephemeral-storage": "20Gi", "pods": "110"}, "conditions": [{"type": "Ready", "status": "True"}]}}]},
"pods": {"items": [{"metadata": {"name": "api-abc123", "namespace": "apps", "labels": {"app.kubernetes.io/name": "api"}}, "spec": {"containers": [{"resources": {"requests": {"cpu": "250m", "memory": "128Mi"}, "limits": {"cpu": "1", "memory": "512Mi"}}}]}}]},
"pvcs": {"items": [{"metadata": {"name": "data", "namespace": "apps"}, "spec": {"storageClassName": "local-path", "resources": {"requests": {"storage": "1Gi"}}}, "status": {"phase": "Bound"}}]},
"pvs": {"items": [{"spec": {"capacity": {"storage": "2Gi"}}}]},
"storageclasses": {"items": [{"metadata": {"name": "local-path", "annotations": {"storageclass.kubernetes.io/is-default-class": "true"}}, "provisioner": "local-path", "reclaimPolicy": "Delete", "volumeBindingMode": "WaitForFirstConsumer"}]},
"node_top": None,
"pod_top": None,
}
result = module.build_observation(snapshots, "2026-08-22T00:00:00Z")
self.assertEqual(250, result["allocation"]["denominators"]["cpu_requested_millicores"])
self.assertEqual(1750, result["allocation"]["residual_capacity"]["cpu_millicores"])
self.assertIn("pod metrics unavailable", result["measurement_gaps"])
self.assertFalse(result["failure_domain"]["threephoenix_target_is_live"])
def test_pending_unscheduled_pods_are_named_and_excluded_from_residual(self) -> None:
snapshots = {
"nodes": {"items": [{"metadata": {"name": "node-1", "labels": {"kubernetes.io/hostname": "node-1"}}, "spec": {}, "status": {"allocatable": {"cpu": "2", "memory": "4Gi", "ephemeral-storage": "20Gi", "pods": "110"}, "conditions": [{"type": "Ready", "status": "True"}]}}]},
"pods": {"items": [
{"metadata": {"name": "api-abc123", "namespace": "apps", "labels": {"app.kubernetes.io/name": "api"}}, "spec": {"nodeName": "node-1", "containers": [{"resources": {"requests": {"cpu": "250m", "memory": "128Mi"}}}]}, "status": {"phase": "Running"}},
{"metadata": {"name": "pending-xyz", "namespace": "other"}, "spec": {"containers": [{"resources": {"requests": {"cpu": "50m", "memory": "64Mi"}}}]}, "status": {"phase": "Pending"}},
]},
"pvcs": {"items": []},
"pvs": {"items": []},
"storageclasses": {"items": []},
"node_top": None,
"pod_top": None,
}
result = module.build_observation(snapshots, "2026-09-14T00:00:00Z")
pending = result["allocation"]["pending_unscheduled"]
self.assertEqual(1, len(pending["pods"]))
self.assertEqual(50, pending["cpu_millicores"])
self.assertEqual("other/pending-xyz", result["state_hub_preflight_observation"]["pending_unrelated"][0]["name"])
self.assertEqual(250, result["allocation"]["denominators"]["cpu_requested_millicores"])
self.assertEqual(1750, result["allocation"]["residual_capacity"]["cpu_millicores"])
self.assertTrue(result["allocation"]["not_a_scheduling_guarantee"])
self.assertEqual(250, result["state_hub_preflight_observation"]["nodes"][0]["allocated_cpu_m"])
if __name__ == "__main__":
unittest.main()