from __future__ import annotations
import re
from pathlib import Path
import pytest
_REPO_ROOT = Path(__file__).resolve().parents[3]
_HELM = _REPO_ROOT / "helm"
_VALUES_FILES = [
"values.yaml",
"values-production.yaml",
"values-development.yaml",
"values-local-storage.yaml",
"test-values.yaml",
]
def _read(relative: str) -> str:
path = _REPO_ROOT / relative
if not path.exists():
pytest.skip(f"{relative} is not present in this checkout")
return path.read_text()
def test_helm_scrape_annotation_is_not_hardcoded_to_the_grpc_port():
for name in _VALUES_FILES:
content = _read(f"helm/{name}")
for line in content.splitlines():
stripped = line.strip()
if stripped.startswith("#"):
continue
assert "prometheus.io/port" not in stripped, (
f"helm/{name} hand-writes prometheus.io/port. Set "
f"metrics.port instead; deployment.yaml generates the "
f"annotation from it."
)
@pytest.mark.parametrize("name", _VALUES_FILES)
def test_metrics_port_differs_from_the_grpc_port(name):
values = _read(f"helm/{name}")
service_port = re.search(r"^service:\n(?:.*\n)*?\s+port:\s*(\d+)", values, re.M)
metrics_port = re.search(r"^metrics:\n(?:.*\n)*?\s+port:\s*(\d+)", values, re.M)
if not metrics_port:
pytest.skip(f"helm/{name} does not set metrics.port; it inherits values.yaml")
assert service_port, f"helm/{name} sets metrics.port but no service.port: {values}"
assert service_port.group(1) != metrics_port.group(1), (
f"helm/{name} points metrics.port at the gRPC port ({service_port.group(1)}). "
f"tonic serves HTTP/2 only, so every scrape of it fails and the target "
f"reports up == 0."
)
def test_deployment_publishes_the_metrics_port_and_env():
deployment = _read("helm/templates/deployment.yaml")
assert "name: metrics" in deployment
assert "- name: MODEL_EXPRESS_SERVER_METRICS_PORT" in deployment
assert 'prometheus.io/port" (.Values.metrics.port' in deployment
def test_metrics_port_is_the_single_source_of_truth():
deployment = _read("helm/templates/deployment.yaml")
service = _read("helm/templates/service.yaml")
for consumer, text, needle in [
("scrape annotation", deployment, 'prometheus.io/port" (.Values.metrics.port'),
("containerPort", deployment, "containerPort: {{ .Values.metrics.port"),
("env var", deployment, "{{ .Values.metrics.port | default 9401 | quote }}"),
("service port", service, "port: {{ .Values.metrics.port"),
]:
assert needle in text, f"the {consumer} no longer derives from .Values.metrics.port"
assert "fail " in deployment and "MODEL_EXPRESS_SERVER_METRICS_PORT" in deployment, (
"the chart must reject MODEL_EXPRESS_SERVER_METRICS_PORT set via .Values.env "
"or .Values.extraEnv, because it moves only where the server listens"
)
emission = deployment.index("- name: MODEL_EXPRESS_SERVER_METRICS_PORT")
guard = deployment.rindex("{{- fail ", 0, emission)
between = deployment[guard:emission]
assert "{{- if" not in between.split("{{- end }}")[-1], (
"the env emission is inside a conditional again; it must be unconditional "
"so it can never disagree with the annotation and containerPort"
)