77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import asyncio
|
|
|
|
from app.models.proxy import ProxyDocument
|
|
from app.models.service import ServiceEndpointDocument
|
|
from app.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
|