Fix bug in healthcheck

This commit is contained in:
Joakim Hellsén 2026-04-09 19:14:12 +02:00
commit cba35edb19
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk
2 changed files with 70 additions and 0 deletions

View file

@ -16,6 +16,7 @@ def healthcheck() -> None:
r: requests.Response = requests.get(url="http://localhost:5000", timeout=5) r: requests.Response = requests.get(url="http://localhost:5000", timeout=5)
if r.ok: if r.ok:
sys.exit(0) sys.exit(0)
sys.exit(1)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"Healthcheck failed: {e}", file=sys.stderr) # noqa: T201 print(f"Healthcheck failed: {e}", file=sys.stderr) # noqa: T201
sys.exit(1) sys.exit(1)

69
tests/test_healthcheck.py Normal file
View file

@ -0,0 +1,69 @@
from __future__ import annotations
from unittest.mock import MagicMock
from unittest.mock import patch
import pytest
import requests
from discord_rss_bot.healthcheck import healthcheck
def test_healthcheck_success() -> None:
"""Test that healthcheck exits with 0 when the website is up."""
mock_response = MagicMock()
mock_response.ok = True
with (
patch("discord_rss_bot.healthcheck.requests.get", return_value=mock_response),
pytest.raises(SystemExit) as exc_info,
):
healthcheck()
assert exc_info.value.code == 0
def test_healthcheck_not_ok() -> None:
"""Test that healthcheck exits with 1 when the response is not ok."""
mock_response = MagicMock()
mock_response.ok = False
with (
patch("discord_rss_bot.healthcheck.requests.get", return_value=mock_response),
pytest.raises(SystemExit) as exc_info,
):
healthcheck()
assert exc_info.value.code == 1
def test_healthcheck_request_exception(capsys: pytest.CaptureFixture) -> None:
"""Test that healthcheck exits with 1 on a request exception."""
with (
patch(
"discord_rss_bot.healthcheck.requests.get",
side_effect=requests.exceptions.ConnectionError("Connection refused"),
),
pytest.raises(SystemExit) as exc_info,
):
healthcheck()
assert exc_info.value.code == 1
captured = capsys.readouterr()
assert "Healthcheck failed" in captured.err
def test_healthcheck_timeout(capsys: pytest.CaptureFixture) -> None:
"""Test that healthcheck exits with 1 on a timeout."""
with (
patch(
"discord_rss_bot.healthcheck.requests.get",
side_effect=requests.exceptions.Timeout("Request timed out"),
),
pytest.raises(SystemExit) as exc_info,
):
healthcheck()
assert exc_info.value.code == 1
captured = capsys.readouterr()
assert "Healthcheck failed" in captured.err