prole/tests/installer/test_k3s_registry_hostport_resolution.py

75 lines
2.0 KiB
Python

from __future__ import annotations
from pathlib import Path
import pytest
import knoe.ui.screens.docker as docker_mod
from knoe.ui.screens.docker import DockerScreenMixin
class _DummyDockerScreen(DockerScreenMixin):
def __init__(self, knoe_cfg_data: dict):
self.knoe_cfg_data = knoe_cfg_data
@pytest.mark.parametrize(
"server_url",
[
"https://0.0.0.0:6443",
"https://localhost:6443",
"https://127.0.0.1:6443",
],
)
def test_k3s_registry_hostport_avoids_unusable_server_hosts_and_falls_back_to_kubeconfig(
monkeypatch, tmp_path: Path, server_url: str
):
# Regression: k3s server URLs sometimes end up as bind addresses like 0.0.0.0.
# Those are not usable registry client endpoints; we should fall back to kubeconfig.
kubeconfig = tmp_path / "kubeconfig.yaml"
kubeconfig.write_text(
"""
apiVersion: v1
clusters:
- cluster:
server: https://myrddin.knoe.org:6443
name: default
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
""".lstrip(),
encoding="utf-8",
)
monkeypatch.delenv("K3S_REGISTRY_HOST", raising=False)
monkeypatch.delenv("K3S_REGISTRY_PORT", raising=False)
monkeypatch.setattr(docker_mod, "_find_kubeconfig_file", lambda *_a, **_k: str(kubeconfig))
screen = _DummyDockerScreen(
{
"Global": {"DEPLOYMENT_MODE": "k3s", "PROLE_K3S_SERVER": server_url},
"Docker Build": {},
}
)
assert screen._k3s_registry_hostport() == "myrddin.knoe.org:5000"
def test_k3s_registry_hostport_prefers_explicit_env_host_port(monkeypatch):
monkeypatch.setenv("K3S_REGISTRY_HOST", "registry.example.com")
monkeypatch.setenv("K3S_REGISTRY_PORT", "5443")
screen = _DummyDockerScreen(
{
"Global": {"DEPLOYMENT_MODE": "k3s", "PROLE_K3S_SERVER": "https://0.0.0.0:6443"},
"Docker Build": {},
}
)
assert screen._k3s_registry_hostport() == "registry.example.com:5443"