Files
zulip/zerver/tests/test_health.py
Alex Vandiver 5ee4b642ad views: Add a /health healthcheck endpoint.
This endpoint verifies that the services that Zulip needs to function
are running, and Django can talk to them.  It is designed to be used
as a readiness probe[^1] for Zulip, either by Kubernetes, or some other
reverse-proxy load-balancer in front of Zulip.  Because of this, it
limits access to only localhost and the IP addresses of configured
reverse proxies.

Tests are limited because we cannot stop running services (which would
impact other concurrent tests) and there would be extremely limited
utility to mocking the very specific methods we're calling to raising
the exceptions that we're looking for.

[^1]: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
2023-09-20 09:53:59 -07:00

25 lines
959 B
Python

from unittest import mock
from zerver.lib.exceptions import ServerNotReadyError
from zerver.lib.test_classes import ZulipTestCase
class HealthTest(ZulipTestCase):
def test_healthy(self) -> None:
# We do not actually use rabbitmq in tests, so this fails
# unless it's mocked out.
with mock.patch("zerver.views.health.check_rabbitmq"):
result = self.client_get("/health")
self.assert_json_success(result)
def test_database_failure(self) -> None:
with mock.patch(
"zerver.views.health.check_database",
side_effect=ServerNotReadyError("Cannot query postgresql"),
), self.assertLogs(level="ERROR") as logs:
result = self.client_get("/health")
self.assert_json_error(result, "Cannot query postgresql", status_code=500)
self.assertIn(
"zerver.lib.exceptions.ServerNotReadyError: Cannot query postgresql", logs.output[0]
)