|
| 1 | +"""A stateful in-memory stand-in for the Monad API. |
| 2 | +
|
| 3 | +The conformance harness boots a router under test pointed at this mock (via |
| 4 | +``MONAD_API_BASE``) so the routers run their real logic without touching real |
| 5 | +Monad. It is stateful enough for the lifecycle scenario: creating a pipeline |
| 6 | +stores it, listing/detail return it, PATCH updates it, DELETE removes it. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import json |
| 12 | +import re |
| 13 | +import threading |
| 14 | +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 15 | + |
| 16 | + |
| 17 | +class _State: |
| 18 | + def __init__(self) -> None: |
| 19 | + self.lock = threading.Lock() |
| 20 | + self.pipelines: dict[str, dict] = {} |
| 21 | + self._counter = 0 |
| 22 | + |
| 23 | + def new_id(self, prefix: str) -> str: |
| 24 | + with self.lock: |
| 25 | + self._counter += 1 |
| 26 | + return f"{prefix}_{self._counter}" |
| 27 | + |
| 28 | + |
| 29 | +def _make_handler(state: _State): |
| 30 | + class Handler(BaseHTTPRequestHandler): |
| 31 | + def log_message(self, *_args) -> None: # silence per-request logging |
| 32 | + pass |
| 33 | + |
| 34 | + def _send(self, code: int, obj=None) -> None: |
| 35 | + body = b"" if obj is None else json.dumps(obj).encode() |
| 36 | + self.send_response(code) |
| 37 | + if obj is not None: |
| 38 | + self.send_header("Content-Type", "application/json") |
| 39 | + self.send_header("Content-Length", str(len(body))) |
| 40 | + self.end_headers() |
| 41 | + if body: |
| 42 | + self.wfile.write(body) |
| 43 | + |
| 44 | + def _read(self) -> dict: |
| 45 | + n = int(self.headers.get("Content-Length") or 0) |
| 46 | + if n == 0: |
| 47 | + return {} |
| 48 | + try: |
| 49 | + return json.loads(self.rfile.read(n) or b"{}") |
| 50 | + except (ValueError, TypeError): |
| 51 | + return {} |
| 52 | + |
| 53 | + def do_GET(self): # noqa: N802 |
| 54 | + self._route("GET") |
| 55 | + |
| 56 | + def do_POST(self): # noqa: N802 |
| 57 | + self._route("POST") |
| 58 | + |
| 59 | + def do_PATCH(self): # noqa: N802 |
| 60 | + self._route("PATCH") |
| 61 | + |
| 62 | + def do_DELETE(self): # noqa: N802 |
| 63 | + self._route("DELETE") |
| 64 | + |
| 65 | + def _route(self, method: str) -> None: |
| 66 | + path = self.path.split("?", 1)[0] |
| 67 | + body = self._read() if method in ("POST", "PATCH") else {} |
| 68 | + |
| 69 | + if method == "POST" and path == "/v3/sessions": |
| 70 | + return self._send(200, {"session_token": "tok_conf", "expires_at": "2026-12-31T00:00:00Z"}) |
| 71 | + |
| 72 | + if method == "GET" and re.fullmatch(r"/v1/(inputs|outputs)", path): |
| 73 | + return self._send(200, [ |
| 74 | + {"type_id": "aws-cloudtrail", "name": "AWS CloudTrail"}, |
| 75 | + {"type_id": "okta-systemlog", "name": "Okta System Log"}, |
| 76 | + ]) |
| 77 | + |
| 78 | + m = re.fullmatch(r"/v1/([^/]+)/(inputs|outputs)", path) |
| 79 | + if method == "GET" and m: |
| 80 | + kind = m.group(2) |
| 81 | + return self._send(200, {kind: [{"id": "cfg_1", "type": "aws-cloudtrail", "name": "Configured"}]}) |
| 82 | + |
| 83 | + if method == "POST" and re.fullmatch(r"/v2/([^/]+)/outputs", path): |
| 84 | + return self._send(200, {"id": state.new_id("out")}) |
| 85 | + |
| 86 | + # pipelines collection (create / list) — check before status/detail |
| 87 | + if re.fullmatch(r"/v2/([^/]+)/pipelines/?", path): |
| 88 | + if method == "POST": |
| 89 | + pid = state.new_id("pipe") |
| 90 | + with state.lock: |
| 91 | + state.pipelines[pid] = {**body, "id": pid} |
| 92 | + return self._send(200, {"id": pid}) |
| 93 | + if method == "GET": |
| 94 | + with state.lock: |
| 95 | + items = [{"id": pid, "enabled": bool(p.get("enabled"))} for pid, p in state.pipelines.items()] |
| 96 | + return self._send(200, items) |
| 97 | + |
| 98 | + if method == "GET" and re.fullmatch(r"/v2/([^/]+)/pipelines/([^/]+)/status", path): |
| 99 | + return self._send(200, {"status": "Running"}) |
| 100 | + |
| 101 | + m = re.fullmatch(r"/v2/([^/]+)/pipelines/([^/]+)", path) |
| 102 | + if m: |
| 103 | + pid = m.group(2) |
| 104 | + if method == "GET": |
| 105 | + with state.lock: |
| 106 | + p = state.pipelines.get(pid) |
| 107 | + if p is None: # lenient default so unknown ids still conform |
| 108 | + p = {"name": "default", "description": "", "enabled": True, "nodes": [], "edges": []} |
| 109 | + return self._send(200, {"config": p}) |
| 110 | + if method == "PATCH": |
| 111 | + with state.lock: |
| 112 | + state.pipelines[pid] = {**body, "id": pid} |
| 113 | + return self._send(200, {}) |
| 114 | + if method == "DELETE": |
| 115 | + with state.lock: |
| 116 | + state.pipelines.pop(pid, None) |
| 117 | + return self._send(204) |
| 118 | + |
| 119 | + if method == "DELETE" and re.fullmatch(r"/v1/([^/]+)/(inputs|outputs)/([^/]+)", path): |
| 120 | + return self._send(204) |
| 121 | + |
| 122 | + return self._send(404, {"error": f"mock unhandled {method} {path}"}) |
| 123 | + |
| 124 | + return Handler |
| 125 | + |
| 126 | + |
| 127 | +def start(port: int): |
| 128 | + """Start the mock on 127.0.0.1:port in a background thread. Returns (server, state).""" |
| 129 | + state = _State() |
| 130 | + server = ThreadingHTTPServer(("127.0.0.1", port), _make_handler(state)) |
| 131 | + threading.Thread(target=server.serve_forever, daemon=True).start() |
| 132 | + return server, state |
0 commit comments