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 / test (push) Has been cancelled
Some checks failed
ci / test (push) Has been cancelled
This commit is contained in:
@@ -1,32 +1,55 @@
|
|||||||
import httpx
|
from datetime import datetime, timezone
|
||||||
import pytest
|
|
||||||
|
|
||||||
pytestmark = pytest.mark.anyio
|
from app.checker import UnsafeTarget
|
||||||
|
from app.models import CheckStatus
|
||||||
|
from tests.conftest import create_monitor
|
||||||
|
|
||||||
|
|
||||||
async def test_crud_status_and_operations(client: httpx.AsyncClient) -> None:
|
class StubChecker:
|
||||||
created = await client.post("/monitors", json={"name": "site", "url": "https://example.com/a"})
|
def __init__(self, result: CheckStatus | Exception):
|
||||||
assert created.status_code == 201
|
self.result = result
|
||||||
monitor = created.json()
|
|
||||||
monitor_id = monitor["id"]
|
|
||||||
assert monitor["status"]["state"] == "unknown"
|
|
||||||
assert (await client.get("/monitors")).json()[0]["id"] == monitor_id
|
|
||||||
assert (await client.get(f"/monitors/{monitor_id}")).status_code == 200
|
|
||||||
|
|
||||||
updated = await client.put(
|
async def check(self, url: str) -> CheckStatus:
|
||||||
f"/monitors/{monitor_id}",
|
if isinstance(self.result, Exception):
|
||||||
json={"name": "new", "url": "https://example.org/"},
|
raise self.result
|
||||||
)
|
return self.result
|
||||||
assert updated.status_code == 200
|
|
||||||
assert updated.json()["status"]["state"] == "unknown"
|
|
||||||
assert (await client.get(f"/monitors/{monitor_id}/status")).status_code == 200
|
|
||||||
assert (await client.delete(f"/monitors/{monitor_id}")).status_code == 204
|
|
||||||
assert (await client.get(f"/monitors/{monitor_id}")).status_code == 404
|
|
||||||
|
|
||||||
assert (await client.get("/health/live")).json() == {"status": "ok"}
|
|
||||||
assert (await client.get("/health/ready")).json() == {"status": "ready"}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_validation_and_missing(client: httpx.AsyncClient) -> None:
|
async def test_crud_and_operational_routes(client):
|
||||||
assert (await client.post("/monitors", json={"name": "", "url": "file:///x"})).status_code == 422
|
assert (await client.get("/healthz")).json() == {"status": "ok"}
|
||||||
assert (await client.get("/monitors/00000000-0000-0000-0000-000000000000")).status_code == 404
|
assert (await client.get("/readyz")).json() == {"status": "ready"}
|
||||||
|
created = await create_monitor(client)
|
||||||
|
monitor_id = created["id"]
|
||||||
|
assert created["status"]["state"] == "unknown"
|
||||||
|
assert len((await client.get("/v1/monitors")).json()) == 1
|
||||||
|
assert (await client.get(f"/v1/monitors/{monitor_id}")).json()["name"] == "website"
|
||||||
|
updated = await client.patch(f"/v1/monitors/{monitor_id}", json={"name": "api"})
|
||||||
|
assert updated.status_code == 200 and updated.json()["name"] == "api"
|
||||||
|
assert (await client.patch(f"/v1/monitors/{monitor_id}", json={})).status_code == 422
|
||||||
|
assert (await client.delete(f"/v1/monitors/{monitor_id}")).status_code == 204
|
||||||
|
missing = await client.get(f"/v1/monitors/{monitor_id}")
|
||||||
|
assert missing.status_code == 404
|
||||||
|
assert missing.json()["detail"]["code"] == "monitor_not_found"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_check_updates_and_status_route(app, client):
|
||||||
|
created = await create_monitor(client)
|
||||||
|
result = CheckStatus(state="up", checked_at=datetime.now(timezone.utc),
|
||||||
|
status_code=204, latency_ms=12.5)
|
||||||
|
app.state.checker = StubChecker(result)
|
||||||
|
checked = await client.post(f"/v1/monitors/{created['id']}/checks")
|
||||||
|
assert checked.status_code == 200
|
||||||
|
assert checked.json()["state"] == "up"
|
||||||
|
assert checked.json()["status_applied"] is True
|
||||||
|
current = await client.get(f"/v1/monitors/{created['id']}/status")
|
||||||
|
assert current.json()["status_code"] == 204
|
||||||
|
|
||||||
|
|
||||||
|
async def test_unsafe_check_maps_error_and_updates_status(app, client):
|
||||||
|
created = await create_monitor(client)
|
||||||
|
app.state.checker = StubChecker(UnsafeTarget("target resolves to a non-public address"))
|
||||||
|
response = await client.post(f"/v1/monitors/{created['id']}/checks")
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.json()["detail"]["code"] == "unsafe_target"
|
||||||
|
current = await client.get(f"/v1/monitors/{created['id']}/status")
|
||||||
|
assert current.json()["state"] == "error"
|
||||||
|
|||||||
Reference in New Issue
Block a user