decomposer: generate deliverable files for Define the service contract and project architecture for the FastAPI endpoint monitoring service.; Implement the typed monitor CRUD API and concurrency-safe in-memory state according to the service design.; Implement secure on-demand endpoint checks with status updates, latency measurement, robust error handling, and redacted structured logs.; Add operational API endpoints and environment-driven runtime configuration to the monitoring service.; Create automated tests for the monitoring service.; Package the service with Docker and developer documentation.; Validate the complete project.

This commit is contained in:
2026-08-09 15:38:29 +00:00
parent ba485e182b
commit 4da2460dfe

View File

@@ -1,55 +1,35 @@
from unittest.mock import AsyncMock import httpx
import pytest
from fastapi.testclient import TestClient
from app.models import CurrentStatus, State, utcnow
def create(client: TestClient, url: str = "https://example.com/health") -> dict: @pytest.mark.anyio
response = client.post("/v1/monitors", json={"name": "web", "url": url}) async def test_crud_check_status_and_operations(client: httpx.AsyncClient) -> None:
assert response.status_code == 201 created = await client.post("/monitors", json={"name": "Example", "url": "https://example.com/a?token=secret"})
return response.json() assert created.status_code == 201
monitor_id = created.json()["id"]
assert created.json()["status"]["state"] == "unknown"
assert (await client.get("/monitors")).json()[0]["name"] == "Example"
changed = await client.patch(f"/monitors/{monitor_id}", json={"name": "Changed"})
def test_crud_and_status(client: TestClient) -> None:
item = create(client)
assert item["current_status"]["state"] == "unknown"
assert client.get("/v1/monitors").json()[0]["id"] == item["id"]
changed = client.patch(f"/v1/monitors/{item['id']}", json={"name": "api"})
assert changed.status_code == 200 assert changed.status_code == 200
assert changed.json()["name"] == "api" assert changed.json()["name"] == "Changed"
assert client.get(f"/v1/monitors/{item['id']}/status").json()["state"] == "unknown"
assert client.delete(f"/v1/monitors/{item['id']}").status_code == 204
missing = client.get(f"/v1/monitors/{item['id']}")
assert missing.status_code == 404
assert missing.json()["error"]["code"] == "monitor_not_found"
checked = await client.post(f"/monitors/{monitor_id}/check")
def test_validation_and_operations(client: TestClient) -> None:
assert client.post("/v1/monitors", json={"name": "x", "url": "file:///tmp/x"}).status_code == 422
assert client.post("/v1/monitors", json={"name": "x", "url": "https://u:p@example.com"}).status_code == 422
assert client.get("/healthz").json() == {"status": "ok"}
ready = client.get("/readyz").json()
assert ready == {"status": "ready", "storage": "process-local-memory"}
def test_check_updates_current_status(client: TestClient) -> None:
item = create(client)
result = CurrentStatus(state=State.UP, checked_at=utcnow(), latency_ms=2,
http_status=204, final_url="https://example.com/health")
client.app.state.checker.check = AsyncMock(return_value=result)
checked = client.post(f"/v1/monitors/{item['id']}/check")
assert checked.status_code == 200 assert checked.status_code == 200
assert checked.json()["state"] == "up" assert checked.json()["status"]["state"] == "up"
stored = client.get(f"/v1/monitors/{item['id']}/status").json() assert "token" not in checked.json()["url"]
assert stored["http_status"] == 204 assert (await client.get(f"/monitors/{monitor_id}/status")).json()["http_status"] == 204
assert (await client.get("/healthz")).json() == {"status": "ok"}
assert (await client.get("/readyz")).status_code == 200
assert (await client.delete(f"/monitors/{monitor_id}")).status_code == 204
missing = await client.get(f"/monitors/{monitor_id}")
assert missing.status_code == 404
assert missing.json()["error"]["code"] == "http_404"
def test_outbound_error_is_recorded_not_raised(client: TestClient) -> None: @pytest.mark.anyio
item = create(client) async def test_validation_error_has_contract_shape(client: httpx.AsyncClient) -> None:
result = CurrentStatus(state=State.ERROR, checked_at=utcnow(), latency_ms=1, response = await client.post("/monitors", json={"name": "", "url": "file:///etc/passwd"})
error="timeout") assert response.status_code == 422
client.app.state.checker.check = AsyncMock(return_value=result) assert response.json()["error"]["code"] == "validation_error"
response = client.post(f"/v1/monitors/{item['id']}/check")
assert response.status_code == 200
assert response.json()["error"] == "timeout"