From 281eebea6fd0efc309f3079f5d9d82c16c2c6eda Mon Sep 17 00:00:00 2001 From: Christopher Bartz Date: Wed, 29 May 2024 14:12:24 +0200 Subject: [PATCH] Add health check endpoint (#9) * Add health check endpoint * remove self-hosted runners due to capacity issues --- .github/workflows/integration_test.yaml | 3 +-- .github/workflows/test.yaml | 3 +-- src-docs/app.py.md | 18 ++++++++++++++++++ tests/unit/test_app.py | 11 +++++++++++ webhook_router/app.py | 13 ++++++++++++- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/.github/workflows/integration_test.yaml b/.github/workflows/integration_test.yaml index 55d8f3c..7eaef69 100644 --- a/.github/workflows/integration_test.yaml +++ b/.github/workflows/integration_test.yaml @@ -11,7 +11,6 @@ jobs: juju-channel: 3.1/stable channel: 1.28-strict/stable trivy-image-config: "trivy.yaml" - self-hosted-runner: true - self-hosted-runner-label: "edge" + self-hosted-runner: false rockcraft-channel: latest/edge charmcraft-channel: latest/edge diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index bd1426c..99e540d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,5 +8,4 @@ jobs: uses: canonical/operator-workflows/.github/workflows/test.yaml@main secrets: inherit with: - self-hosted-runner: true - self-hosted-runner-label: "edge" + self-hosted-runner: false diff --git a/src-docs/app.py.md b/src-docs/app.py.md index 055260f..8fc83ea 100644 --- a/src-docs/app.py.md +++ b/src-docs/app.py.md @@ -23,6 +23,24 @@ Receive a GitHub webhook and append the payload to a file. +**Returns:** + A tuple containing an empty string and 200 status code on success or a failure message and 403 status code. + + +--- + + + +## function `health_check` + +```python +health_check() → tuple[str, int] +``` + +Health check endpoint. + + + **Returns:** A tuple containing an empty string and 200 status code. diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 9b0cdb3..dc6c8c5 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -110,3 +110,14 @@ def test_webhook_validation( assert response.status_code == expected_status assert response.text == expected_reason + + +def test_health_check(client: FlaskClient): + """ + arrange: A test client. + act: Request the health check endpoint. + assert: 200 status code is returned. + """ + response = client.get("/health") + assert response.status_code == 200 + assert response.data == b"" diff --git a/webhook_router/app.py b/webhook_router/app.py index 26f30b7..708eb82 100644 --- a/webhook_router/app.py +++ b/webhook_router/app.py @@ -20,7 +20,8 @@ def handle_github_webhook() -> tuple[str, int]: """Receive a GitHub webhook and append the payload to a file. Returns: - A tuple containing an empty string and 200 status code. + A tuple containing an empty string and 200 status code on success or + a failure message and 403 status code. """ if secret := app.config.get("WEBHOOK_SECRET"): if not (signature := request.headers.get(WEBHOOK_SIGNATURE_HEADER)): @@ -40,6 +41,16 @@ def handle_github_webhook() -> tuple[str, int]: return "", 200 +@app.route("/health", methods=["GET"]) +def health_check() -> tuple[str, int]: + """Health check endpoint. + + Returns: + A tuple containing an empty string and 200 status code. + """ + return "", 200 + + # Exclude from coverage since unit tests should not run as __main__ if __name__ == "__main__": # pragma: no cover # Start development server