|
| 1 | +"""Tests for the configurable Gateway listener port (gateway.listenerPort). |
| 2 | +
|
| 3 | +Covers 11_infra.yaml.j2, which renders the per-stack Gateway resource for |
| 4 | +each supported gateway class: |
| 5 | +
|
| 6 | +* gke / agentgateway / istio (and any custom istio class) - emit a default |
| 7 | + HTTP listener on ``gateway.listenerPort`` when it is set, and omit the |
| 8 | + ``listeners`` block entirely when it is not. |
| 9 | +* data-science-gateway-class - pins the listener to port 443 (HTTPS+TLS), |
| 10 | + so setting ``gateway.listenerPort`` is unsupported and must fail loudly. |
| 11 | +
|
| 12 | +The template is rendered through the real RenderPlans Jinja environment so |
| 13 | +the ``raise`` global and custom filters are exercised exactly as in prod. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from pathlib import Path |
| 19 | +from unittest.mock import MagicMock |
| 20 | + |
| 21 | +import pytest |
| 22 | +import yaml |
| 23 | + |
| 24 | +from llmdbenchmark.parser.render_plans import RenderPlans |
| 25 | + |
| 26 | +TEMPLATE_PATH = ( |
| 27 | + Path(__file__).resolve().parents[1] |
| 28 | + / "config" |
| 29 | + / "templates" |
| 30 | + / "jinja" |
| 31 | + / "11_infra.yaml.j2" |
| 32 | +) |
| 33 | + |
| 34 | +# Gateway classes that support a configurable listener port. "istio" and an |
| 35 | +# arbitrary custom class both fall through to the default (Istio) branch. |
| 36 | +LISTENER_PORT_CLASSES = ["gke", "agentgateway", "istio", "custom-istio-class"] |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope="module") |
| 40 | +def template() -> str: |
| 41 | + return TEMPLATE_PATH.read_text(encoding="utf-8") |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def renderer(): |
| 46 | + """A RenderPlans wired only with what _render_template needs.""" |
| 47 | + r = RenderPlans.__new__(RenderPlans) |
| 48 | + r.logger = MagicMock() |
| 49 | + r._jinja_env = None |
| 50 | + return r |
| 51 | + |
| 52 | + |
| 53 | +def _values(gw_class: str, listener_port: int | None = None) -> dict: |
| 54 | + """Minimal values dict mirroring defaults.yaml for the infra template.""" |
| 55 | + gateway = { |
| 56 | + "className": gw_class, |
| 57 | + "logLevel": "error", |
| 58 | + "service": {"type": "NodePort"}, |
| 59 | + "resources": { |
| 60 | + "limits": {"cpu": "16", "memory": "16Gi"}, |
| 61 | + "requests": {"cpu": "4", "memory": "4Gi"}, |
| 62 | + }, |
| 63 | + } |
| 64 | + if listener_port is not None: |
| 65 | + gateway["listenerPort"] = listener_port |
| 66 | + return { |
| 67 | + "standalone": {"enabled": False}, |
| 68 | + "kustomize": {"enabled": False}, |
| 69 | + "gateway": gateway, |
| 70 | + "model_id_label": "model-1", |
| 71 | + "namespace": {"name": "llmdbench"}, |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +class TestListenerPortRendered: |
| 76 | + """Classes that honour gateway.listenerPort.""" |
| 77 | + |
| 78 | + @pytest.mark.parametrize("gw_class", LISTENER_PORT_CLASSES) |
| 79 | + def test_port_set_emits_http_listener(self, renderer, template, gw_class): |
| 80 | + out = renderer._render_template(template, _values(gw_class, 8080)) |
| 81 | + doc = yaml.safe_load(out) |
| 82 | + listeners = doc["gateway"]["listeners"] |
| 83 | + assert listeners == [ |
| 84 | + { |
| 85 | + "name": "default", |
| 86 | + "port": 8080, |
| 87 | + "protocol": "HTTP", |
| 88 | + "allowedRoutes": {"namespaces": {"from": "All"}}, |
| 89 | + } |
| 90 | + ] |
| 91 | + |
| 92 | + @pytest.mark.parametrize("gw_class", LISTENER_PORT_CLASSES) |
| 93 | + def test_port_unset_omits_listeners(self, renderer, template, gw_class): |
| 94 | + out = renderer._render_template(template, _values(gw_class, None)) |
| 95 | + doc = yaml.safe_load(out) |
| 96 | + assert "listeners" not in doc["gateway"] |
| 97 | + |
| 98 | + @pytest.mark.parametrize("gw_class", LISTENER_PORT_CLASSES) |
| 99 | + def test_port_zero_is_treated_as_unset(self, renderer, template, gw_class): |
| 100 | + """A falsy port (0) must not emit a listener block.""" |
| 101 | + out = renderer._render_template(template, _values(gw_class, 0)) |
| 102 | + doc = yaml.safe_load(out) |
| 103 | + assert "listeners" not in doc["gateway"] |
| 104 | + |
| 105 | + |
| 106 | +class TestDataScienceGatewayClass: |
| 107 | + """data-science-gateway-class pins the listener to 443 - port is rejected.""" |
| 108 | + |
| 109 | + def test_default_keeps_fixed_443_listener(self, renderer, template): |
| 110 | + out = renderer._render_template( |
| 111 | + template, _values("data-science-gateway-class", None) |
| 112 | + ) |
| 113 | + doc = yaml.safe_load(out) |
| 114 | + listener = doc["gateway"]["listeners"][0] |
| 115 | + assert listener["port"] == 443 |
| 116 | + assert listener["protocol"] == "HTTPS" |
| 117 | + assert listener["tls"]["mode"] == "Terminate" |
| 118 | + |
| 119 | + def test_listener_port_raises(self, renderer, template): |
| 120 | + with pytest.raises(ValueError, match="data-science-gateway-class"): |
| 121 | + renderer._render_template( |
| 122 | + template, _values("data-science-gateway-class", 8080) |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +class TestRaiseHelper: |
| 127 | + """The `raise` global that backs the data-science guard.""" |
| 128 | + |
| 129 | + def test_raise_global_registered(self, renderer): |
| 130 | + env = renderer._get_jinja_env() |
| 131 | + assert "raise" in env.globals |
| 132 | + |
| 133 | + def test_raise_helper_raises_value_error(self): |
| 134 | + with pytest.raises(ValueError, match="boom"): |
| 135 | + RenderPlans._raise_helper("boom") |
0 commit comments