From 0b33ca0f5296b75044ed34fd9441af9d6ad887e3 Mon Sep 17 00:00:00 2001 From: Spencer Nelson Date: Mon, 9 Aug 2021 14:19:46 -0700 Subject: [PATCH] Add health check endpoint to the server --- alertdb/server.py | 4 ++++ tests/test_integration.py | 5 +++++ tests/test_server.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/test_server.py diff --git a/alertdb/server.py b/alertdb/server.py index f55d565..24e7470 100644 --- a/alertdb/server.py +++ b/alertdb/server.py @@ -44,6 +44,10 @@ def create_server(backend: AlertDatabaseBackend) -> FastAPI: app = FastAPI() + @app.get("/v1/health") + def healthcheck(): + return Response(content=b"OK") + @app.get("/v1/schemas/{schema_id}") def get_schema(schema_id: str): try: diff --git a/tests/test_integration.py b/tests/test_integration.py index c2beff4..7037c8b 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -106,6 +106,11 @@ def test_get_missing_schema(self): response = self._get_schema("bogus") self.assertEqual(response.status_code, 404) + def test_healthcheck(self): + """Test that the healthcheck endpoint returns 200.""" + response = self.client.get("/v1/health") + self.assertEqual(response.status_code, 200) + def _get_alert(self, alert_id: str) -> requests.Response: return self.client.get(f"/v1/alerts/{alert_id}") diff --git a/tests/test_server.py b/tests/test_server.py new file mode 100644 index 0000000..3d31054 --- /dev/null +++ b/tests/test_server.py @@ -0,0 +1,19 @@ +import pytest +from fastapi.testclient import TestClient + +from alertdb.server import create_server +from alertdb.storage import FileBackend + + +@pytest.fixture +def file_backend(tmp_path): + """Pytest fixture for a file-based backend""" + yield FileBackend(str(tmp_path)) + + +def test_server_healthcheck(file_backend): + """Test that the server responds on the healthcheck endpoint.""" + server = create_server(file_backend) + client = TestClient(server) + response = client.get("/v1/health") + assert response.status_code == 200