Fix bug in healthcheck
This commit is contained in:
parent
e9d219676f
commit
cba35edb19
2 changed files with 70 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ def healthcheck() -> None:
|
|||
r: requests.Response = requests.get(url="http://localhost:5000", timeout=5)
|
||||
if r.ok:
|
||||
sys.exit(0)
|
||||
sys.exit(1)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Healthcheck failed: {e}", file=sys.stderr) # noqa: T201
|
||||
sys.exit(1)
|
||||
|
|
|
|||
69
tests/test_healthcheck.py
Normal file
69
tests/test_healthcheck.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue