feat: enhance service discovery and proxy management

This commit is contained in:
gabdevele
2026-05-05 13:57:46 +02:00 Unverified
parent 93ba8e9ac2
commit a523c2faa3
53 changed files with 3206 additions and 706 deletions
@@ -73,3 +73,97 @@ def test_running_document_without_pid_is_treated_as_stopped():
return await manager.status(proxy)
assert asyncio.run(run()) == "stopped"
def test_matches_proxy_process_uses_expected_script_and_confdir():
manager = MitmdumpManager()
proxy = make_proxy()
manager.read_cmdline = lambda _pid: [
"/home/test/.venv/bin/python",
"/home/test/.venv/bin/mitmdump",
"--mode",
"reverse:http://127.0.0.1:9000@8080",
"--listen-host",
"0.0.0.0",
"--set",
"confdir=/tmp/theplug/conf/8080",
"-s",
"/tmp/theplug/addons/8080.py",
]
assert manager.matches_proxy_process(proxy, 1234) is True
def test_reconcile_reattaches_matching_process():
manager = MitmdumpManager()
proxy = make_proxy(
status="running",
pid=4321,
service=ServiceEndpointDocument(port=9000, upstream_port=54003),
)
manager.is_process_alive = lambda _pid: True
manager.matches_proxy_process = lambda _proxy, _pid: True
async def run():
return await manager.reconcile(proxy)
values = asyncio.run(run())
assert values is not None
assert values["status"] == "running"
assert values["pid"] == 4321
assert manager.attached_pids[8080] == 4321
def test_reconcile_marks_error_for_mismatched_live_process():
manager = MitmdumpManager()
proxy = make_proxy(
status="running",
pid=4321,
service=ServiceEndpointDocument(port=9000, upstream_port=54003),
)
manager.is_process_alive = lambda _pid: True
manager.matches_proxy_process = lambda _proxy, _pid: False
async def run():
return await manager.reconcile(proxy)
values = asyncio.run(run())
assert values is not None
assert values["status"] == "error"
assert values["pid"] is None
assert "does not match" in str(values["last_error"])
def test_reconcile_restores_upstream_when_process_is_gone():
manager = MitmdumpManager()
proxy = make_proxy(
status="running",
pid=4321,
service=ServiceEndpointDocument(
port=9000,
upstream_port=54003,
name="echo",
folder="/tmp/playground",
),
)
manager.is_process_alive = lambda _pid: False
async def fake_restore(_proxy, _upstream_port):
return True
manager.restore_upstream = fake_restore
async def run():
return await manager.reconcile(proxy)
values = asyncio.run(run())
assert values == {
"status": "stopped",
"pid": None,
"service.upstream_port": None,
"last_error": None,
}
+76
View File
@@ -0,0 +1,76 @@
import asyncio
from models.proxy import ProxyDocument
from models.service import ServiceEndpointDocument
from services.mitmproxy import recovery as recovery_module
def make_proxy(**updates):
data = {
"proxy_port": 8080,
"name": "svc",
"code": "",
"service": ServiceEndpointDocument(port=9000),
}
data.update(updates)
return ProxyDocument(**data)
def test_reconcile_runtime_persists_manager_updates(monkeypatch):
proxy = make_proxy(status="running", pid=1234)
captured: list[tuple[int, dict[str, object]]] = []
async def fake_list_proxies():
return [proxy]
class FakeManager:
async def reconcile(self, document: ProxyDocument):
assert document.proxy_port == 8080
return {"status": "running", "pid": 1234, "last_error": None}
async def fake_update_proxy_runtime(proxy_port: int, values: dict[str, object]):
captured.append((proxy_port, values))
return proxy
monkeypatch.setattr(recovery_module, "list_proxies", fake_list_proxies)
monkeypatch.setattr(recovery_module, "manager", FakeManager())
monkeypatch.setattr(
recovery_module,
"update_proxy_runtime",
fake_update_proxy_runtime,
)
changes = asyncio.run(recovery_module.reconcile_runtime())
assert changes == [(8080, {"status": "running", "pid": 1234, "last_error": None})]
assert captured == [(8080, {"status": "running", "pid": 1234, "last_error": None})]
def test_reconcile_runtime_skips_unchanged_proxies(monkeypatch):
proxy = make_proxy()
updated = False
async def fake_list_proxies():
return [proxy]
class FakeManager:
async def reconcile(self, _document: ProxyDocument):
return None
async def fake_update_proxy_runtime(_proxy_port: int, _values: dict[str, object]):
nonlocal updated
updated = True
return proxy
monkeypatch.setattr(recovery_module, "list_proxies", fake_list_proxies)
monkeypatch.setattr(recovery_module, "manager", FakeManager())
monkeypatch.setattr(
recovery_module,
"update_proxy_runtime",
fake_update_proxy_runtime,
)
changes = asyncio.run(recovery_module.reconcile_runtime())
assert changes == []
assert updated is False