|
| 1 | +import unittest |
| 2 | +from monplugin import Status |
| 3 | +from typing import List |
| 4 | + |
| 5 | + |
| 6 | +def check_cluster_health(failed: int, members: int, thresholds: List[str]) -> Status: |
| 7 | + def parse_threshold(t_str: str) -> tuple: |
| 8 | + """Parse threshold string and return (max_members, warn_str, crit_str).""" |
| 9 | + parts = t_str.split(":") |
| 10 | + |
| 11 | + if len(parts) == 3: |
| 12 | + try: |
| 13 | + max_m = int(parts[0]) |
| 14 | + except ValueError: |
| 15 | + raise ValueError(f"Invalid max_members value: '{parts[0]}'") |
| 16 | + return (max_m, parts[1], parts[2]) |
| 17 | + elif len(parts) == 2: |
| 18 | + return (float("inf"), parts[0], parts[1]) |
| 19 | + else: |
| 20 | + raise ValueError(f"Malformed threshold string: '{t_str}'") |
| 21 | + |
| 22 | + def resolve_value(s: str) -> float: |
| 23 | + """Resolve a threshold value (can be a number or percentage).""" |
| 24 | + s = s.strip() |
| 25 | + if s.endswith("%"): |
| 26 | + return (float(s[:-1]) / 100.0) * members |
| 27 | + return float(s) |
| 28 | + |
| 29 | + # Parse and validate all thresholds |
| 30 | + parsed = {} |
| 31 | + for t_str in thresholds: |
| 32 | + max_m, warn_str, crit_str = parse_threshold(t_str) |
| 33 | + |
| 34 | + if max_m in parsed: |
| 35 | + label = ( |
| 36 | + "Fallback (Infinity)" |
| 37 | + if max_m == float("inf") |
| 38 | + else f"max_members {max_m}" |
| 39 | + ) |
| 40 | + raise ValueError(f"Duplicate threshold configuration found for {label}") |
| 41 | + |
| 42 | + parsed[max_m] = (warn_str, crit_str) |
| 43 | + |
| 44 | + if float("inf") not in parsed: |
| 45 | + raise ValueError( |
| 46 | + "No fallback threshold (Infinity) provided in thresholds list." |
| 47 | + ) |
| 48 | + |
| 49 | + # Select the tightest threshold applicable to `members` |
| 50 | + warn_str, crit_str = parsed[min(m for m in parsed if m >= members)] |
| 51 | + |
| 52 | + # Compare failed count against resolved limits |
| 53 | + if failed >= resolve_value(crit_str): |
| 54 | + return Status.CRITICAL |
| 55 | + if failed >= resolve_value(warn_str): |
| 56 | + return Status.WARNING |
| 57 | + return Status.OK |
| 58 | + |
| 59 | + |
| 60 | +class TestThreshold(unittest.TestCase): |
| 61 | + def test_it(self): |
| 62 | + thresholds = ["3:1:1", "4:1:2", "5:1:2", "10:1:30%", "3:30%"] |
| 63 | + self.assertEqual(check_cluster_health(0, 1, thresholds), Status.OK) |
| 64 | + self.assertEqual(check_cluster_health(0, 2, thresholds), Status.OK) |
| 65 | + self.assertEqual(check_cluster_health(0, 3, thresholds), Status.OK) |
| 66 | + self.assertEqual(check_cluster_health(0, 4, thresholds), Status.OK) |
| 67 | + self.assertEqual(check_cluster_health(0, 5, thresholds), Status.OK) |
| 68 | + self.assertEqual(check_cluster_health(0, 6, thresholds), Status.OK) |
| 69 | + self.assertEqual(check_cluster_health(0, 9, thresholds), Status.OK) |
| 70 | + self.assertEqual(check_cluster_health(0, 10, thresholds), Status.OK) |
| 71 | + self.assertEqual(check_cluster_health(0, 11, thresholds), Status.OK) |
| 72 | + self.assertEqual(check_cluster_health(1, 11, thresholds), Status.OK) |
| 73 | + self.assertEqual(check_cluster_health(0, 100, thresholds), Status.OK) |
| 74 | + self.assertEqual(check_cluster_health(1, 100, thresholds), Status.OK) |
| 75 | + self.assertEqual(check_cluster_health(2, 100, thresholds), Status.OK) |
| 76 | + |
| 77 | + self.assertEqual(check_cluster_health(1, 4, thresholds), Status.WARNING) |
| 78 | + self.assertEqual(check_cluster_health(1, 5, thresholds), Status.WARNING) |
| 79 | + self.assertEqual(check_cluster_health(1, 6, thresholds), Status.WARNING) |
| 80 | + self.assertEqual(check_cluster_health(1, 9, thresholds), Status.WARNING) |
| 81 | + self.assertEqual(check_cluster_health(1, 10, thresholds), Status.WARNING) |
| 82 | + self.assertEqual(check_cluster_health(2, 10, thresholds), Status.WARNING) |
| 83 | + self.assertEqual(check_cluster_health(3, 100, thresholds), Status.WARNING) |
| 84 | + self.assertEqual(check_cluster_health(28, 100, thresholds), Status.WARNING) |
| 85 | + self.assertEqual(check_cluster_health(29, 100, thresholds), Status.WARNING) |
| 86 | + |
| 87 | + self.assertEqual(check_cluster_health(1, 1, thresholds), Status.CRITICAL) |
| 88 | + self.assertEqual(check_cluster_health(2, 1, thresholds), Status.CRITICAL) |
| 89 | + self.assertEqual(check_cluster_health(1, 2, thresholds), Status.CRITICAL) |
| 90 | + self.assertEqual(check_cluster_health(9, 3, thresholds), Status.CRITICAL) |
| 91 | + self.assertEqual(check_cluster_health(2, 4, thresholds), Status.CRITICAL) |
| 92 | + self.assertEqual(check_cluster_health(3, 4, thresholds), Status.CRITICAL) |
| 93 | + self.assertEqual(check_cluster_health(3, 4, thresholds), Status.CRITICAL) |
| 94 | + self.assertEqual(check_cluster_health(2, 5, thresholds), Status.CRITICAL) |
| 95 | + self.assertEqual(check_cluster_health(3, 5, thresholds), Status.CRITICAL) |
| 96 | + self.assertEqual(check_cluster_health(3, 10, thresholds), Status.CRITICAL) |
| 97 | + self.assertEqual(check_cluster_health(30, 100, thresholds), Status.CRITICAL) |
| 98 | + self.assertEqual(check_cluster_health(39, 100, thresholds), Status.CRITICAL) |
| 99 | + self.assertEqual(check_cluster_health(1000, 100, thresholds), Status.CRITICAL) |
| 100 | + |
| 101 | + self.assertEqual(check_cluster_health(0, 10, ["1:100%"]), Status.OK) |
| 102 | + self.assertEqual(check_cluster_health(1, 10, ["1:100%"]), Status.WARNING) |
| 103 | + self.assertEqual(check_cluster_health(1, 1, ["1:100%"]), Status.CRITICAL) |
| 104 | + |
| 105 | + def test_duplicate_error(self): |
| 106 | + with self.assertRaises(ValueError): |
| 107 | + check_cluster_health(0, 5, ["10:1:2", "10:2:3", "5:6"]) |
| 108 | + with self.assertRaises(ValueError): |
| 109 | + check_cluster_health(0, 5, ["10:1:2", "2:3", "5:6"]) |
| 110 | + |
| 111 | + def test_too_large_warn(self): |
| 112 | + self.assertEqual(check_cluster_health(2, 10, ["20:10%"]), Status.CRITICAL) |
| 113 | + |
| 114 | + def test_missing_fallback_error(self): |
| 115 | + with self.assertRaises(ValueError): |
| 116 | + check_cluster_health(0, 5, ["10:1:2"]) |
| 117 | + with self.assertRaises(ValueError): |
| 118 | + check_cluster_health(0, 5, ["10:1:2", "20:2:3"]) |
0 commit comments