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,
}