From ce0b952fc779cff1be12e4c12647eade1d77fb62 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 11 Jun 2024 12:52:40 -0400 Subject: [PATCH 1/4] add init healthcheck code --- 05-assistive-chatbot/chatbot_api.py | 31 ++++++++++++++----- 05-assistive-chatbot/test/__init__.py | 0 05-assistive-chatbot/test/test_chatbot_api.py | 20 ++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 05-assistive-chatbot/test/__init__.py create mode 100644 05-assistive-chatbot/test/test_chatbot_api.py diff --git a/05-assistive-chatbot/chatbot_api.py b/05-assistive-chatbot/chatbot_api.py index 5117b6e..0e0dc53 100755 --- a/05-assistive-chatbot/chatbot_api.py +++ b/05-assistive-chatbot/chatbot_api.py @@ -11,8 +11,10 @@ from functools import cached_property from typing import Dict -from fastapi import FastAPI, Request -from fastapi.responses import HTMLResponse + +from fastapi import FastAPI, Request, status +from pydantic import BaseModel + import chatbot @@ -47,17 +49,30 @@ def query(message: str | Dict): response = app_state.chat_engine().gen_response(message) return response - -@app.get("/healthcheck") -def healthcheck(request: Request): +class HealthCheck(BaseModel): + """Response model to validate and return when performing a health check.""" + + status: str + build_date: str + git_sha: str + + +@app.get( + "/healthcheck", + tags=["healthcheck"], + summary="Perform a Health Check", + response_description="Return HTTP Status Code 200 (OK)", + status_code=status.HTTP_200_OK, + response_model=HealthCheck, +) +def healthcheck(request: Request) -> HealthCheck: logger.info(request.headers) - # TODO: Add a health check - https://pypi.org/project/fastapi-healthchecks/ git_sha = os.environ.get("GIT_SHA", "") build_date = os.environ.get("BUILD_DATE", "") - + logger.info("Returning: Healthy %s %s", build_date, git_sha) - return HTMLResponse(f"Healthy {build_date} {git_sha}") + return HealthCheck(build_date=build_date, git_sha=git_sha, status="OK") if __name__ == "__main__": diff --git a/05-assistive-chatbot/test/__init__.py b/05-assistive-chatbot/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/05-assistive-chatbot/test/test_chatbot_api.py b/05-assistive-chatbot/test/test_chatbot_api.py new file mode 100644 index 0000000..a29e28a --- /dev/null +++ b/05-assistive-chatbot/test/test_chatbot_api.py @@ -0,0 +1,20 @@ +import json +import unittest +from chatbot_api import app +from fastapi.testclient import TestClient +import logging + + +# logger = logging.getLogger(f"chatbot.chatbot_api") + +client = TestClient(app) + +class TestAPI(unittest.TestCase): + def test_read_healthcheck(self): + response = client.get("/healthcheck") + response_data = json.loads(response.content) + assert response.status_code == 200 + assert response_data["status"] == "OK" + with self.assertLogs("chatbot.chatbot_api", level='INFO') as cm: + print(cm.output) + From d8dd76ceea00cff6cf824a1bdea88b874ea03999 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 11 Jun 2024 13:25:28 -0400 Subject: [PATCH 2/4] resolve logging test --- 05-assistive-chatbot/chatbot_api.py | 11 +++++++---- 05-assistive-chatbot/test/test_chatbot_api.py | 15 +++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/05-assistive-chatbot/chatbot_api.py b/05-assistive-chatbot/chatbot_api.py index 2b994d1..7d5e948 100755 --- a/05-assistive-chatbot/chatbot_api.py +++ b/05-assistive-chatbot/chatbot_api.py @@ -51,9 +51,10 @@ def query(message: str | Dict): response = app_state.chat_engine().gen_response(message) return response + class HealthCheck(BaseModel): """Response model to validate and return when performing a health check.""" - + status: str build_date: str git_sha: str @@ -70,17 +71,19 @@ class HealthCheck(BaseModel): response_model=HealthCheck, ) async def healthcheck(request: Request) -> HealthCheck: -# Make sure to use async functions for faster responses + # Make sure to use async functions for faster responses logger.info(request.headers) git_sha = os.environ.get("GIT_SHA", "") build_date = os.environ.get("BUILD_DATE", "") - + service_name = os.environ.get("SERVICE_NAME", "") hostname = f"{platform.node()} {socket.gethostname()}" logger.info("Healthy {git_sha} built at {build_date}
{service_name} {hostname}") - return HealthCheck(build_date=build_date, git_sha=git_sha, status="OK", service_name=service_name, hostname=hostname) + return HealthCheck( + build_date=build_date, git_sha=git_sha, status="OK", service_name=service_name, hostname=hostname + ) ALLOWED_ENV_VARS = [ diff --git a/05-assistive-chatbot/test/test_chatbot_api.py b/05-assistive-chatbot/test/test_chatbot_api.py index a29e28a..81d6f30 100644 --- a/05-assistive-chatbot/test/test_chatbot_api.py +++ b/05-assistive-chatbot/test/test_chatbot_api.py @@ -2,19 +2,18 @@ import unittest from chatbot_api import app from fastapi.testclient import TestClient -import logging # logger = logging.getLogger(f"chatbot.chatbot_api") client = TestClient(app) + class TestAPI(unittest.TestCase): def test_read_healthcheck(self): - response = client.get("/healthcheck") - response_data = json.loads(response.content) - assert response.status_code == 200 - assert response_data["status"] == "OK" - with self.assertLogs("chatbot.chatbot_api", level='INFO') as cm: - print(cm.output) - + with self.assertLogs("chatbot.chatbot_api", level="INFO") as cm: + response = client.get("/healthcheck") + response_data = json.loads(response.content) + assert response.status_code == 200 + assert response_data["status"] == "OK" + self.assertIn("Healthy", cm.output[1]) From 38efbff8a5aa5546dc1ce839f80ee658091eed0c Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 11 Jun 2024 14:48:22 -0400 Subject: [PATCH 3/4] revise to add fixture --- 05-assistive-chatbot/chatbot_api.py | 2 +- 05-assistive-chatbot/test/test_chatbot_api.py | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/05-assistive-chatbot/chatbot_api.py b/05-assistive-chatbot/chatbot_api.py index 7d5e948..31e7c92 100755 --- a/05-assistive-chatbot/chatbot_api.py +++ b/05-assistive-chatbot/chatbot_api.py @@ -80,7 +80,7 @@ async def healthcheck(request: Request) -> HealthCheck: service_name = os.environ.get("SERVICE_NAME", "") hostname = f"{platform.node()} {socket.gethostname()}" - logger.info("Healthy {git_sha} built at {build_date}
{service_name} {hostname}") + logger.info(f"Healthy {git_sha} built at {build_date}
{service_name} {hostname}") return HealthCheck( build_date=build_date, git_sha=git_sha, status="OK", service_name=service_name, hostname=hostname ) diff --git a/05-assistive-chatbot/test/test_chatbot_api.py b/05-assistive-chatbot/test/test_chatbot_api.py index 81d6f30..7a22aaa 100644 --- a/05-assistive-chatbot/test/test_chatbot_api.py +++ b/05-assistive-chatbot/test/test_chatbot_api.py @@ -1,19 +1,22 @@ import json -import unittest -from chatbot_api import app +import logging +import pytest + from fastapi.testclient import TestClient +from chatbot_api import app -# logger = logging.getLogger(f"chatbot.chatbot_api") -client = TestClient(app) +@pytest.fixture() +def test_client(): + return TestClient(app) -class TestAPI(unittest.TestCase): - def test_read_healthcheck(self): - with self.assertLogs("chatbot.chatbot_api", level="INFO") as cm: - response = client.get("/healthcheck") +class TestAPI: + def test_read_healthcheck(self, caplog, test_client): + with caplog.at_level(logging.INFO, logger="chatbot.chatbot_api"): + response = test_client.get("/healthcheck") response_data = json.loads(response.content) assert response.status_code == 200 assert response_data["status"] == "OK" - self.assertIn("Healthy", cm.output[1]) + assert "Healthy" in caplog.messages[1] From 75ba8ef624829a15be986163c35b923577b535e9 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 11 Jun 2024 15:01:47 -0400 Subject: [PATCH 4/4] fix warning for fixture --- 05-assistive-chatbot/test/test_chatbot_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/05-assistive-chatbot/test/test_chatbot_api.py b/05-assistive-chatbot/test/test_chatbot_api.py index 7a22aaa..363a704 100644 --- a/05-assistive-chatbot/test/test_chatbot_api.py +++ b/05-assistive-chatbot/test/test_chatbot_api.py @@ -7,8 +7,8 @@ from chatbot_api import app -@pytest.fixture() -def test_client(): +@pytest.fixture(name="test_client") +def fixture_test_client(): return TestClient(app)