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.
Some checks failed
ci / container (push) Has been cancelled
ci / quality (push) Has been cancelled

This commit is contained in:
2026-08-09 16:07:12 +00:00
parent c27984722d
commit 46038e31ab

View File

@@ -1,58 +1,41 @@
import httpx from fastapi.testclient import TestClient
import pytest
from app.main import app
from tests.conftest import install_checker
pytestmark = pytest.mark.anyio
async def test_crud_and_status_lifecycle(api_client): def create(client: TestClient, name: str = "site", url: str = "https://example.com/health") -> dict:
created = await api_client.post("/api/v1/monitors", json={"name": "Example", "url": "https://example.com/path?secret=yes"}) response = client.post("/monitors", json={"name": name, "target_url": url})
assert created.status_code == 201 assert response.status_code == 201
monitor_id = created.json()["id"] return response.json()
assert created.json()["status"] == "unknown"
assert (await api_client.get("/api/v1/monitors")).json()[0]["id"] == monitor_id
updated = await api_client.put(f"/api/v1/monitors/{monitor_id}", json={"name": "New", "url": "https://example.org"})
def test_crud_and_status(client: TestClient) -> None:
monitor = create(client)
monitor_id = monitor["id"]
assert monitor["current_status"]["state"] == "unknown"
assert client.get("/monitors").json()[0]["id"] == monitor_id
assert client.get(f"/monitors/{monitor_id}").status_code == 200
updated = client.put(
f"/monitors/{monitor_id}",
json={"name": "renamed", "target_url": "https://example.org/ready"},
)
assert updated.status_code == 200 assert updated.status_code == 200
assert updated.json()["name"] == "New" assert updated.json()["name"] == "renamed"
assert (await api_client.get(f"/api/v1/monitors/{monitor_id}/status")).json()["status"] == "unknown"
assert (await api_client.delete(f"/api/v1/monitors/{monitor_id}")).status_code == 204
assert (await api_client.get(f"/api/v1/monitors/{monitor_id}")).status_code == 404
checked = client.post(f"/monitors/{monitor_id}/check")
async def test_validation_and_probes(api_client):
assert (await api_client.post("/api/v1/monitors", json={"name": "", "url": "file:///tmp/x"})).status_code == 422
assert (await api_client.get("/healthz")).json() == {"status": "ok"}
assert (await api_client.get("/readyz")).json() == {"status": "ready"}
async def test_check_updates_status(api_client):
install_checker(lambda request: httpx.Response(204, request=request))
item = (await api_client.post("/api/v1/monitors", json={"name": "ok", "url": "https://example.com"})).json()
checked = await api_client.post(f"/api/v1/monitors/{item['id']}/check")
assert checked.status_code == 200 assert checked.status_code == 200
assert checked.json()["status"] == "up" assert checked.json()["state"] == "up"
assert checked.json()["http_status"] == 204 assert client.get(f"/monitors/{monitor_id}/status").json()["http_status"] == 204
assert checked.json()["latency_ms"] >= 0
assert (await api_client.get(f"/api/v1/monitors/{item['id']}/status")).json()["status"] == "up" assert client.delete(f"/monitors/{monitor_id}").status_code == 204
assert client.get(f"/monitors/{monitor_id}").status_code == 404
async def test_http_failure_is_down(api_client): def test_validation_and_not_found(client: TestClient) -> None:
install_checker(lambda request: httpx.Response(503, request=request)) assert client.post("/monitors", json={"name": "", "target_url": "file:///tmp/x"}).status_code == 422
item = (await api_client.post("/api/v1/monitors", json={"name": "bad", "url": "https://example.com"})).json() assert client.get("/monitors/not-a-uuid").status_code == 422
checked = await api_client.post(f"/api/v1/monitors/{item['id']}/check") assert client.get("/monitors/00000000-0000-0000-0000-000000000000").status_code == 404
assert checked.json()["status"] == "down"
assert checked.json()["http_status"] == 503
async def test_timeout_is_sanitized_error(api_client): def test_health(client: TestClient) -> None:
def timeout(request): assert client.get("/health/live").json() == {"status": "ok"}
raise httpx.ReadTimeout("contains-a-secret", request=request) assert client.get("/health/ready").json() == {"status": "ready"}
install_checker(timeout)
item = (await api_client.post("/api/v1/monitors", json={"name": "slow", "url": "https://example.com"})).json()
checked = await api_client.post(f"/api/v1/monitors/{item['id']}/check")
assert checked.json()["status"] == "error"
assert checked.json()["error"] == "timeout"
assert "contains-a-secret" not in checked.text