Files
nanobot/tests/test_docker_config.py
T
LuckVd ddd22a26f8 Bootstrap a working Docker Compose config on first boot so the WebUI works out of the box
A fresh 'docker compose up' started with no config (the entrypoint only
bootstrapped for Render), so every published port was unreachable: the
gateway and WebSocket channel defaulted to a container-local 127.0.0.1
bind, and nanobot-api exited into a restart loop because serve refuses a
non-loopback host without an api_key.

Bootstrap config.json for container runs (NANOBOT_DOCKER_BOOTSTRAP, set
by docker-compose.yml) from a committed docker-config.json with binds
that survive the container boundary, baking the WebUI token and API key
at first boot (env value if provided, generated otherwise, printed to
the log). Move nanobot-api under the 'api' profile since it cannot
serve before a provider is configured. Install curl in the image and
document the new first-boot flow.

Fixes #6165
2026-10-19 13:02:26 +08:00

73 lines
2.7 KiB
Python

"""The committed Docker deploy template must always bake into a startable config.
`entrypoint.sh` initializes `~/.nanobot/config.json` from `docker-config.json`
on first boot (see docker-compose.yml). A template that drifts out of sync with
the config schema would crash-loop every container on a fresh `docker compose
up`, so pin the contract here: the baked config validates, and its wildcard
binds carry the secrets their security validators require.
"""
import json
from pathlib import Path
from nanobot.channels.websocket.runtime import WebSocketConfig
from nanobot.config.schema import Config
repo_root = Path(__file__).resolve().parents[1]
WEB_TOKEN_PLACEHOLDER = "${NANOBOT_WEB_TOKEN}"
API_KEY_PLACEHOLDER = "${NANOBOT_API_KEY}"
def _template() -> dict:
return json.loads((repo_root / "docker-config.json").read_text())
def _baked() -> dict:
"""The template as the entrypoint writes it, with both secrets substituted."""
raw = (repo_root / "docker-config.json").read_text()
raw = raw.replace(WEB_TOKEN_PLACEHOLDER, "test-web-token")
raw = raw.replace(API_KEY_PLACEHOLDER, "test-api-key")
return json.loads(raw)
def test_docker_template_bakes_to_a_valid_container_config():
config = Config.model_validate(_baked())
# Binds must survive the container boundary: a loopback bind inside the
# container is unreachable through the ports docker-compose.yml publishes.
assert config.gateway.host == "0.0.0.0"
assert config.api.host == "0.0.0.0"
assert config.api.api_key == "test-api-key"
def test_docker_template_websocket_satisfies_wildcard_host_auth():
ws = WebSocketConfig.model_validate(_baked()["channels"]["websocket"])
assert ws.enabled is True
assert ws.host == "0.0.0.0"
assert ws.port == 8765
assert ws.token_issue_secret
assert ws.websocket_requires_token is True
def test_docker_template_secrets_are_wired_for_entrypoint_substitution():
# entrypoint.sh substitutes exactly these placeholders when writing
# config.json (env value if provided, generated otherwise). A renamed
# placeholder would survive substitution and reach the runtime as a
# literal string, breaking every client that needs the real secret.
template = _template()
assert template["channels"]["websocket"]["tokenIssueSecret"] == WEB_TOKEN_PLACEHOLDER
assert template["api"]["api_key"] == API_KEY_PLACEHOLDER
assert "${" not in json.dumps(_baked())
def test_docker_template_uses_the_same_shape_as_the_render_template():
docker_template = _template()
render_template = json.loads((repo_root / "render-config.json").read_text())
assert set(docker_template["channels"]["websocket"]) == set(
render_template["channels"]["websocket"]
)