Autofix WebRTC ranges before compose up, strip CR from stack env, and fail start when instances are unhealthy. Co-authored-by: Cursor <cursoragent@cursor.com>
103 lines
4.1 KiB
Python
103 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
import importlib.util
|
|
|
|
|
|
def _load():
|
|
path = ROOT / "src" / "neko_webrtc_ports.py"
|
|
spec = importlib.util.spec_from_file_location("neko_webrtc_ports", path)
|
|
assert spec and spec.loader
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
class NekoWebrtcPortsTests(unittest.TestCase):
|
|
def test_sanitize_strips_cr(self) -> None:
|
|
mod = _load()
|
|
self.assertEqual(mod.sanitize_env_text("9200\r"), "9200")
|
|
self.assertEqual(mod.sanitize_env_text("52000-52099\r\n"), "52000-52099")
|
|
|
|
def test_fit_udp_range_shrinks_before_crashcore(self) -> None:
|
|
mod = _load()
|
|
start, end = mod.fit_udp_range(52000, 52099, {52099})
|
|
self.assertEqual((start, end), (52000, 52098))
|
|
|
|
def test_fit_udp_range_shifts_when_prefix_too_small(self) -> None:
|
|
mod = _load()
|
|
blocked = set(range(52000, 52090))
|
|
start, end = mod.fit_udp_range(52000, 52099, blocked, min_ports=16)
|
|
self.assertGreaterEqual(end - start + 1, 16)
|
|
self.assertTrue(all(p not in blocked for p in range(start, end + 1)))
|
|
|
|
def test_upsert_env_file_normalizes_crlf_and_updates(self) -> None:
|
|
mod = _load()
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "system-stack.env"
|
|
path.write_bytes(b"FOO=1\r\nNEKO_WEBRTC_UDP_RANGE=52000-52099\r\n")
|
|
mod.upsert_env_file(path, {"NEKO_WEBRTC_UDP_RANGE": "52000-52098", "NEKO_WEBRTC_EPR": "52000-52098"})
|
|
text = path.read_bytes()
|
|
self.assertNotIn(b"\r", text)
|
|
decoded = text.decode("utf-8")
|
|
self.assertIn("NEKO_WEBRTC_UDP_RANGE=52000-52098\n", decoded)
|
|
self.assertIn("NEKO_WEBRTC_EPR=52000-52098\n", decoded)
|
|
self.assertIn("FOO=1\n", decoded)
|
|
|
|
def test_autofix_updates_environ_when_52099_blocked(self) -> None:
|
|
mod = _load()
|
|
with patch.dict(
|
|
os.environ,
|
|
{
|
|
"NEKO_WEBRTC_UDP_RANGE": "52000-52099",
|
|
"NEKO_WEBRTC_UDP_RANGE_1": "52000-52099",
|
|
"NEKO_WEBRTC_EPR": "52000-52099",
|
|
"NEKO_WEBRTC_EPR_1": "52000-52099",
|
|
"NEKO_SKIP_UDP_PORT_PROBE": "1",
|
|
},
|
|
clear=False,
|
|
):
|
|
with patch.object(mod, "list_foreign_udp_holders", return_value={52099: "CrashCore"}):
|
|
changes = mod.apply_neko_udp_range_autofix(2, stack_env=None, persist=False)
|
|
self.assertEqual(os.environ["NEKO_WEBRTC_UDP_RANGE_1"], "52000-52098")
|
|
self.assertEqual(os.environ["NEKO_WEBRTC_EPR_1"], "52000-52098")
|
|
self.assertTrue(any(c["to"] == "52000-52098" for c in changes))
|
|
|
|
def test_renderer_strips_cr_from_ports(self) -> None:
|
|
path = ROOT / "tools" / "render_neko_compose.py"
|
|
spec = importlib.util.spec_from_file_location("render_neko_compose", path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with patch.dict(
|
|
os.environ,
|
|
{
|
|
"NEKO_IMAGE": "test-image",
|
|
"NEKO_PROFILE_HOST_DIR": f"{tmp}/profile",
|
|
"NEKO_HTTP_PORT": "9200\r",
|
|
"NEKO_HTTP_PORT_1": "9200\r",
|
|
"NEKO_WEBRTC_TCPMUX_1": "52300\r",
|
|
"NEKO_WEBRTC_UDPMUX_1": "52300\r",
|
|
"NEKO_WEBRTC_UDP_RANGE_1": "52000-52098\r",
|
|
"NEKO_WEBRTC_EPR_1": "52000-52098\r",
|
|
"NEKO_SKIP_UDP_PORT_PROBE": "1",
|
|
},
|
|
clear=False,
|
|
):
|
|
rendered = module.render(1)
|
|
self.assertIn('"9200:8080"', rendered)
|
|
self.assertNotIn("9200\\r", rendered)
|
|
self.assertIn('"52000-52098:52000-52098/udp"', rendered)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|