feat(observatory): bridge local M49 worker container
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import socket
|
||||
import socketserver
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
import k1link.observatory.m49_worker_container_main as container_main
|
||||
|
||||
|
||||
class _EchoHandler(socketserver.BaseRequestHandler):
|
||||
def handle(self) -> None:
|
||||
payload = self.request.recv(1024)
|
||||
self.request.sendall(payload)
|
||||
|
||||
|
||||
def test_fixed_container_proxy_bridges_loopback_without_gateway_override() -> None:
|
||||
upstream = socketserver.ThreadingTCPServer(("127.0.0.1", 0), _EchoHandler)
|
||||
upstream_thread = threading.Thread(target=upstream.serve_forever, daemon=True)
|
||||
upstream_thread.start()
|
||||
try:
|
||||
upstream_port = upstream.server_address[1]
|
||||
assert isinstance(upstream_port, int)
|
||||
with container_main.FixedM49ContainerLoopbackProxy(
|
||||
listen_port=0,
|
||||
upstream_host="127.0.0.1",
|
||||
upstream_port=upstream_port,
|
||||
) as proxy, socket.create_connection(
|
||||
("127.0.0.1", proxy.listen_port)
|
||||
) as client:
|
||||
client.sendall(b"fixed-m49-proxy")
|
||||
client.shutdown(socket.SHUT_WR)
|
||||
assert client.recv(1024) == b"fixed-m49-proxy"
|
||||
finally:
|
||||
upstream.shutdown()
|
||||
upstream.server_close()
|
||||
upstream_thread.join(timeout=5.0)
|
||||
|
||||
|
||||
def test_container_entrypoint_owns_proxy_around_worker(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
lifecycle: list[str] = []
|
||||
|
||||
class _Proxy:
|
||||
def __enter__(self) -> _Proxy:
|
||||
lifecycle.append("proxy-started")
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args: object) -> None:
|
||||
lifecycle.append("proxy-stopped")
|
||||
|
||||
def worker(arguments: object) -> int:
|
||||
lifecycle.append(f"worker:{arguments!r}")
|
||||
return 17
|
||||
|
||||
monkeypatch.setattr(container_main, "FixedM49ContainerLoopbackProxy", _Proxy)
|
||||
monkeypatch.setattr(container_main.m49_worker_service, "main", worker)
|
||||
|
||||
assert container_main.main(("--once",)) == 17
|
||||
assert lifecycle == ["proxy-started", "worker:('--once',)", "proxy-stopped"]
|
||||
|
||||
|
||||
def test_container_proxy_refuses_non_loopback_listener() -> None:
|
||||
with pytest.raises(ValueError, match="IPv4 loopback"):
|
||||
container_main.FixedM49ContainerLoopbackProxy(listen_host="0.0.0.0")
|
||||
|
||||
|
||||
def test_production_proxy_endpoints_are_fixed() -> None:
|
||||
assert container_main.M49_CONTAINER_PROXY_LISTEN_HOST == "127.0.0.1"
|
||||
assert container_main.M49_CONTAINER_PROXY_LISTEN_PORT == 18080
|
||||
assert container_main.M49_CONTAINER_PROXY_UPSTREAM_HOST == "host.docker.internal"
|
||||
assert container_main.M49_CONTAINER_PROXY_UPSTREAM_PORT == 18080
|
||||
Reference in New Issue
Block a user