A `{ref, bind}` to a TCP-exposed deployment (e.g. postgres) projected
nothing, so consumers hardcoded the docker bridge IP. Project the address
it's already documented as reachable at.
64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""Tests for `_requires_env` — a `{ref, bind}` requirement projects the target's URL."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import wildpc_core.config as C
|
|
from wildpc_core.deploy import _requires_env
|
|
from wildpc_core.manifest import RemoteDeployment, SystemdDeployment
|
|
|
|
|
|
def _svc(requires: list | None = None, port: int | None = None) -> SystemdDeployment:
|
|
spec: dict = {"manager": "systemd", "run": {"launcher": "command", "argv": ["x"]}}
|
|
if requires is not None:
|
|
spec["requires"] = requires
|
|
if port is not None:
|
|
spec["expose"] = {"http": {"internal": {"port": port}}}
|
|
spec["reach"] = "internal"
|
|
return SystemdDeployment.model_validate(spec)
|
|
|
|
|
|
def _cfg(deployments: dict) -> C.WildpcConfig:
|
|
return C.WildpcConfig(
|
|
root=None,
|
|
gateway=C.GatewayConfig(port=9000, tls="acme", domain="node.example"),
|
|
repo=None,
|
|
programs={},
|
|
deployments=deployments,
|
|
)
|
|
|
|
|
|
def test_bind_projects_target_url() -> None:
|
|
consumer = _svc(requires=[{"ref": "api", "bind": "API_URL"}])
|
|
cfg = _cfg({"api": _svc(port=9100), "app": consumer})
|
|
assert _requires_env(cfg, consumer) == {"API_URL": "https://api.node.example"}
|
|
|
|
|
|
def test_bind_path_is_appended() -> None:
|
|
consumer = _svc(requires=[{"ref": "llm", "bind": "LLM_BASE", "path": "/v1"}])
|
|
cfg = _cfg({"llm": _svc(port=8100), "app": consumer})
|
|
assert _requires_env(cfg, consumer) == {"LLM_BASE": "https://llm.node.example/v1"}
|
|
|
|
|
|
def test_bind_path_on_reference_base_url() -> None:
|
|
ref = RemoteDeployment.model_validate(
|
|
{"manager": "none", "base_url": "https://ext.example/"}
|
|
)
|
|
consumer = _svc(requires=[{"ref": "ext", "bind": "EXT", "path": "/v1"}])
|
|
cfg = _cfg({"ext": ref, "app": consumer})
|
|
assert _requires_env(cfg, consumer) == {"EXT": "https://ext.example/v1"}
|
|
|
|
|
|
def test_bind_tcp_service_projects_host_port() -> None:
|
|
"""A raw-TCP target (e.g. postgres) binds as `<name>.<domain>:<port>`."""
|
|
pg = SystemdDeployment.model_validate(
|
|
{
|
|
"manager": "systemd",
|
|
"run": {"launcher": "command", "argv": ["x"]},
|
|
"expose": {"tcp": {"port": 5432}},
|
|
"reach": "internal",
|
|
}
|
|
)
|
|
consumer = _svc(requires=[{"ref": "postgres", "bind": "DB_HOST"}])
|
|
cfg = _cfg({"postgres": pg, "app": consumer})
|
|
assert _requires_env(cfg, consumer) == {"DB_HOST": "postgres.node.example:5432"}
|