| ... | ... | @@ -10,6 +10,7 @@ import subprocess |
| 10 | 10 | import tempfile |
| 11 | 11 | import threading |
| 12 | 12 | import time |
| 13 | from unittest.mock import patch |
| 13 | 14 | |
| 14 | 15 | import router |
| 15 | 16 | |
| ... | ... | @@ -51,13 +52,35 @@ def main(): |
| 51 | 52 | with tempfile.TemporaryDirectory(prefix="snowglobe-router-stream-") as temporary, ThreadingHTTPServer(("127.0.0.1", 0), Echo) as backend: |
| 52 | 53 | root = Path(temporary) |
| 53 | 54 | threading.Thread(target=backend.serve_forever, daemon=True).start() |
| 54 | | for keep in (False, True): |
| 55 | proof = root / "proof" |
| 56 | proof.write_text("a" * 64) |
| 57 | domain = "example.test" |
| 58 | environment = { |
| 59 | **os.environ, "SNOWGLOBE_DOMAIN": domain, "SNOWGLOBE_DASHBOARD_PORT": str(backend.server_port), |
| 60 | "SNOWGLOBE_PROXY_TOKEN_FILE": str(proof), "SNOWGLOBE_STATE": str(root), |
| 61 | "XDG_DATA_HOME": str(root / "data"), "XDG_CONFIG_HOME": str(root / "config"), |
| 62 | } |
| 63 | environment.pop("SNOWGLOBE_INTERNAL_PORT", None) |
| 64 | |
| 65 | def nomad(path, _): |
| 66 | if path == "/v1/services": |
| 67 | return [{"Namespace": "default", "Services": [{"ServiceName": name} for name in ("spacebar", "plain")]}] |
| 68 | if path.startswith("/v1/service/"): |
| 69 | service = path.rsplit("/", 1)[1] |
| 70 | tags = (["caddy-host=phoenix-selfhosted." + domain, "caddy-plain-host=clozy.test"] |
| 71 | if service == "spacebar" else ["caddy-host=plain." + domain]) |
| 72 | return [{"ServiceName": service, "JobID": service, "AllocID": service, |
| 73 | "Address": "127.0.0.1", "Port": backend.server_port, "Tags": tags}] |
| 74 | return {"http": {"Status": "success"}} |
| 75 | |
| 76 | with patch.dict(os.environ, environment, clear=True), patch.object(router, "nomad", nomad), patch.object(router, "ROUTE_DIR", str(root)): |
| 77 | rendered = router.render("fixture") |
| 78 | for host, keep in (("plain." + domain, False), ("snowglobe." + domain, True), ("phoenix-selfhosted." + domain, True), ("clozy.test", True)): |
| 55 | 79 | port, admin = free_port(), free_port() |
| 56 | 80 | config = root / "Caddyfile" |
| 57 | | content = "\n".join(["{", f" admin 127.0.0.1:{admin}", " auto_https off", "}", f"http://127.0.0.1:{port} {{", |
| 58 | | *router.proxy(f"127.0.0.1:{backend.server_port}", " ", keep_streams=keep), "}"]) |
| 81 | site = rendered.split(host + " {\n", 1)[1].split("\n}\n", 1)[0].replace(" tls internal\n", "") |
| 82 | content = "\n".join(["{", f" admin 127.0.0.1:{admin}", " auto_https off", "}", f"http://127.0.0.1:{port} {{", site, "}"]) |
| 59 | 83 | config.write_text(content) |
| 60 | | environment = {**os.environ, "XDG_DATA_HOME": str(root / "data"), "XDG_CONFIG_HOME": str(root / "config")} |
| 61 | 84 | with (root / "caddy.log").open("w") as log: |
| 62 | 85 | process = subprocess.Popen([args.caddy, "run", "--config", str(config)], stdout=log, stderr=log, env=environment) |
| 63 | 86 | try: |
| ... | ... | @@ -76,18 +99,18 @@ def main(): |
| 76 | 99 | pass |
| 77 | 100 | client.sendall(b"\x81\x84\0\0\0\0ping") |
| 78 | 101 | assert incoming.read(6) == b"\x81\x04ping" |
| 79 | | config.write_text(content.replace(" reverse_proxy", " header X-Reload verified\n reverse_proxy")) |
| 80 | | subprocess.run([args.caddy, "reload", "--config", str(config), "--address", f"127.0.0.1:{admin}"], check=True, capture_output=True, env=environment) |
| 81 | | client.sendall(b"\x81\x84\0\0\0\0pong") |
| 82 | | header = incoming.read(2) |
| 83 | | assert header == (b"\x81\x04" if keep else b"\x88\x02"), header |
| 84 | | if keep: |
| 85 | | assert incoming.read(4) == b"pong" |
| 102 | for revision in range(2 if keep else 1): |
| 103 | config.write_text(content.replace(" reverse_proxy", f" header X-Reload verified-{revision}\n reverse_proxy")) |
| 104 | subprocess.run([args.caddy, "reload", "--config", str(config), "--address", f"127.0.0.1:{admin}"], check=True, capture_output=True, env=environment) |
| 105 | client.sendall(b"\x81\x84\0\0\0\0pong") |
| 106 | header = incoming.read(2) |
| 107 | assert header == (b"\x81\x04" if keep else b"\x88\x02"), (host, header) |
| 108 | assert incoming.read(4 if keep else 2) == (b"pong" if keep else b"\x03\xe9") |
| 86 | 109 | finally: |
| 87 | 110 | process.terminate() |
| 88 | 111 | process.wait(timeout=5) |
| 89 | 112 | backend.shutdown() |
| 90 | | print("Caddy reload closes default WebSockets and preserves dashboard streams.") |
| 113 | print("Caddy reload closes default WebSockets and preserves dashboard and Snowcord streams on both host aliases.") |
| 91 | 114 | |
| 92 | 115 | |
| 93 | 116 | if __name__ == "__main__": |