|
| 1 | +"""Unit tests for the ML-preload startup-timeout floor. |
| 2 | +
|
| 3 | +The bump is implemented as a pure function (`_apply_ml_preload_startup_floor`) |
| 4 | +on `ComponentSpec`, so the tests exercise it directly — no SDK mocking, no |
| 5 | +provision flow stubbing. This matches the production code path: every |
| 6 | +`_create_component` call site for the proxy runs the spec through the same |
| 7 | +helper before handing it to the Basilica SDK. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +import unittest |
| 14 | +from typing import Mapping |
| 15 | + |
| 16 | +from deployments.basilica import lifecycle |
| 17 | + |
| 18 | + |
| 19 | +def _proxy_spec( |
| 20 | + *, env: Mapping[str, str], startup_timeout_seconds: int |
| 21 | +) -> lifecycle.ComponentSpec: |
| 22 | + return lifecycle.ComponentSpec( |
| 23 | + image="ghcr.io/techlab-innov/llmtrace-proxy:latest", |
| 24 | + port=8080, |
| 25 | + cpu="2", |
| 26 | + memory="4Gi", |
| 27 | + replicas=1, |
| 28 | + env=env, |
| 29 | + startup_timeout_seconds=startup_timeout_seconds, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +class MLPreloadStartupFloorTests(unittest.TestCase): |
| 34 | + def test_bumps_when_below_floor_and_ml_preload_on(self) -> None: |
| 35 | + spec = _proxy_spec( |
| 36 | + env={"LLMTRACE_ML_ENABLED": "1", "LLMTRACE_ML_PRELOAD": "1"}, |
| 37 | + startup_timeout_seconds=600, |
| 38 | + ) |
| 39 | + with self.assertLogs(lifecycle.LOGGER, level=logging.WARNING) as captured: |
| 40 | + bumped = lifecycle._apply_ml_preload_startup_floor( |
| 41 | + spec, tenant_id="acme" |
| 42 | + ) |
| 43 | + |
| 44 | + self.assertEqual( |
| 45 | + bumped.startup_timeout_seconds, |
| 46 | + lifecycle.ML_PRELOAD_STARTUP_FLOOR_SECONDS, |
| 47 | + ) |
| 48 | + # All other fields preserved. |
| 49 | + self.assertEqual(bumped.image, spec.image) |
| 50 | + self.assertEqual(bumped.env, spec.env) |
| 51 | + self.assertEqual(bumped.port, spec.port) |
| 52 | + # Exactly one warning, mentioning the tenant + the floor. |
| 53 | + self.assertEqual(len(captured.records), 1) |
| 54 | + message = captured.records[0].getMessage() |
| 55 | + self.assertIn("acme", message) |
| 56 | + self.assertIn("600", message) |
| 57 | + self.assertIn(str(lifecycle.ML_PRELOAD_STARTUP_FLOOR_SECONDS), message) |
| 58 | + |
| 59 | + def test_no_bump_when_caller_value_meets_floor(self) -> None: |
| 60 | + explicit = lifecycle.ML_PRELOAD_STARTUP_FLOOR_SECONDS + 300 |
| 61 | + spec = _proxy_spec( |
| 62 | + env={"LLMTRACE_ML_ENABLED": "true", "LLMTRACE_ML_PRELOAD": "yes"}, |
| 63 | + startup_timeout_seconds=explicit, |
| 64 | + ) |
| 65 | + # `assertNoLogs` is Python 3.10+. Capture and assert empty as a portable |
| 66 | + # alternative that still fails loudly if a warning leaks. |
| 67 | + with self.assertLogs(lifecycle.LOGGER, level=logging.DEBUG) as captured: |
| 68 | + lifecycle.LOGGER.debug("anchor") # ensure the context has >=1 record |
| 69 | + result = lifecycle._apply_ml_preload_startup_floor( |
| 70 | + spec, tenant_id="acme" |
| 71 | + ) |
| 72 | + |
| 73 | + self.assertIs(result, spec) |
| 74 | + self.assertEqual(result.startup_timeout_seconds, explicit) |
| 75 | + warnings = [r for r in captured.records if r.levelno >= logging.WARNING] |
| 76 | + self.assertEqual(warnings, []) |
| 77 | + |
| 78 | + def test_no_bump_when_caller_value_equals_floor(self) -> None: |
| 79 | + spec = _proxy_spec( |
| 80 | + env={"LLMTRACE_ML_ENABLED": "1", "LLMTRACE_ML_PRELOAD": "1"}, |
| 81 | + startup_timeout_seconds=lifecycle.ML_PRELOAD_STARTUP_FLOOR_SECONDS, |
| 82 | + ) |
| 83 | + with self.assertLogs(lifecycle.LOGGER, level=logging.DEBUG) as captured: |
| 84 | + lifecycle.LOGGER.debug("anchor") |
| 85 | + result = lifecycle._apply_ml_preload_startup_floor( |
| 86 | + spec, tenant_id="acme" |
| 87 | + ) |
| 88 | + |
| 89 | + self.assertIs(result, spec) |
| 90 | + warnings = [r for r in captured.records if r.levelno >= logging.WARNING] |
| 91 | + self.assertEqual(warnings, []) |
| 92 | + |
| 93 | + def test_no_bump_when_ml_preload_off(self) -> None: |
| 94 | + spec = _proxy_spec( |
| 95 | + env={"LLMTRACE_ML_ENABLED": "1", "LLMTRACE_ML_PRELOAD": "0"}, |
| 96 | + startup_timeout_seconds=300, |
| 97 | + ) |
| 98 | + with self.assertLogs(lifecycle.LOGGER, level=logging.DEBUG) as captured: |
| 99 | + lifecycle.LOGGER.debug("anchor") |
| 100 | + result = lifecycle._apply_ml_preload_startup_floor( |
| 101 | + spec, tenant_id="acme" |
| 102 | + ) |
| 103 | + |
| 104 | + # Spec untouched, no warning — even though startup_timeout is well |
| 105 | + # below the floor (caller's choice when preload is off). |
| 106 | + self.assertIs(result, spec) |
| 107 | + self.assertEqual(result.startup_timeout_seconds, 300) |
| 108 | + warnings = [r for r in captured.records if r.levelno >= logging.WARNING] |
| 109 | + self.assertEqual(warnings, []) |
| 110 | + |
| 111 | + def test_no_bump_when_ml_disabled(self) -> None: |
| 112 | + spec = _proxy_spec( |
| 113 | + env={"LLMTRACE_ML_ENABLED": "0", "LLMTRACE_ML_PRELOAD": "1"}, |
| 114 | + startup_timeout_seconds=300, |
| 115 | + ) |
| 116 | + with self.assertLogs(lifecycle.LOGGER, level=logging.DEBUG) as captured: |
| 117 | + lifecycle.LOGGER.debug("anchor") |
| 118 | + result = lifecycle._apply_ml_preload_startup_floor( |
| 119 | + spec, tenant_id="acme" |
| 120 | + ) |
| 121 | + |
| 122 | + self.assertIs(result, spec) |
| 123 | + self.assertEqual(result.startup_timeout_seconds, 300) |
| 124 | + warnings = [r for r in captured.records if r.levelno >= logging.WARNING] |
| 125 | + self.assertEqual(warnings, []) |
| 126 | + |
| 127 | + def test_no_bump_when_both_flags_absent(self) -> None: |
| 128 | + spec = _proxy_spec(env={}, startup_timeout_seconds=120) |
| 129 | + with self.assertLogs(lifecycle.LOGGER, level=logging.DEBUG) as captured: |
| 130 | + lifecycle.LOGGER.debug("anchor") |
| 131 | + result = lifecycle._apply_ml_preload_startup_floor( |
| 132 | + spec, tenant_id="acme" |
| 133 | + ) |
| 134 | + |
| 135 | + self.assertIs(result, spec) |
| 136 | + self.assertEqual(result.startup_timeout_seconds, 120) |
| 137 | + warnings = [r for r in captured.records if r.levelno >= logging.WARNING] |
| 138 | + self.assertEqual(warnings, []) |
| 139 | + |
| 140 | + def test_custom_floor_override(self) -> None: |
| 141 | + spec = _proxy_spec( |
| 142 | + env={"LLMTRACE_ML_ENABLED": "1", "LLMTRACE_ML_PRELOAD": "1"}, |
| 143 | + startup_timeout_seconds=100, |
| 144 | + ) |
| 145 | + with self.assertLogs(lifecycle.LOGGER, level=logging.WARNING): |
| 146 | + bumped = lifecycle._apply_ml_preload_startup_floor( |
| 147 | + spec, tenant_id="acme", floor=2400 |
| 148 | + ) |
| 149 | + |
| 150 | + self.assertEqual(bumped.startup_timeout_seconds, 2400) |
| 151 | + |
| 152 | + def test_truthy_variants_recognised(self) -> None: |
| 153 | + # Any value in the truthy set on BOTH flags triggers the bump. |
| 154 | + for value in ("1", "true", "TRUE", "True", "yes", "YES"): |
| 155 | + with self.subTest(value=value): |
| 156 | + spec = _proxy_spec( |
| 157 | + env={ |
| 158 | + "LLMTRACE_ML_ENABLED": value, |
| 159 | + "LLMTRACE_ML_PRELOAD": value, |
| 160 | + }, |
| 161 | + startup_timeout_seconds=600, |
| 162 | + ) |
| 163 | + bumped = lifecycle._apply_ml_preload_startup_floor( |
| 164 | + spec, tenant_id="acme" |
| 165 | + ) |
| 166 | + self.assertEqual( |
| 167 | + bumped.startup_timeout_seconds, |
| 168 | + lifecycle.ML_PRELOAD_STARTUP_FLOOR_SECONDS, |
| 169 | + ) |
| 170 | + |
| 171 | + def test_falsy_variants_do_not_trigger(self) -> None: |
| 172 | + for value in ("0", "false", "no", "", "off", "FALSE"): |
| 173 | + with self.subTest(value=value): |
| 174 | + spec = _proxy_spec( |
| 175 | + env={ |
| 176 | + "LLMTRACE_ML_ENABLED": "1", |
| 177 | + "LLMTRACE_ML_PRELOAD": value, |
| 178 | + }, |
| 179 | + startup_timeout_seconds=300, |
| 180 | + ) |
| 181 | + result = lifecycle._apply_ml_preload_startup_floor( |
| 182 | + spec, tenant_id="acme" |
| 183 | + ) |
| 184 | + self.assertIs(result, spec) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + unittest.main() |
0 commit comments