-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6064578
commit e49faf9
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default_app_config = 'health_check.contrib.rabbitmq.apps.HealthCheckConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.apps import AppConfig | ||
|
||
from health_check.plugins import plugin_dir | ||
|
||
|
||
class HealthCheckConfig(AppConfig): | ||
name = "health_check.contrib.rabbitmq" | ||
|
||
def ready(self): | ||
from .backends import RabbitMQHealthCheck | ||
|
||
plugin_dir.register(RabbitMQHealthCheck) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
|
||
from amqp.exceptions import AccessRefused | ||
from django.conf import settings | ||
from kombu import Connection | ||
|
||
from health_check.backends import BaseHealthCheckBackend | ||
from health_check.exceptions import ServiceUnavailable | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RabbitMQHealthCheck(BaseHealthCheckBackend): | ||
"""Health check for RabbitMQ.""" | ||
|
||
def check_status(self): | ||
"""Check RabbitMQ service by opening and closing a broker channel.""" | ||
logger.debug("Checking for a broker_url on django settings...") | ||
|
||
broker_url = getattr(settings, "BROKER_URL", None) | ||
|
||
logger.debug("Got %s as the broker_url. Connecting to rabbit...", broker_url) | ||
|
||
logger.debug("Attempting to connect to rabbit...") | ||
try: | ||
# conn is used as a context to release opened resources later | ||
with Connection(broker_url) as conn: | ||
conn.connect() # exceptions may be raised upon calling connect | ||
except ConnectionRefusedError as e: | ||
self.add_error(ServiceUnavailable("Unable to connect to RabbitMQ: Connection was refused."), e) | ||
|
||
except AccessRefused as e: | ||
self.add_error(ServiceUnavailable("Unable to connect to RabbitMQ: Authentication error."), e) | ||
|
||
except IOError as e: | ||
self.add_error(ServiceUnavailable("IOError"), e) | ||
|
||
except BaseException as e: | ||
self.add_error(ServiceUnavailable("Unknown error"), e) | ||
else: | ||
logger.debug("Connection estabilished. RabbitMQ is healthy.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import mock | ||
from amqp.exceptions import AccessRefused | ||
|
||
from health_check.contrib.rabbitmq.backends import RabbitMQHealthCheck | ||
|
||
|
||
class TestRabbitMQHealthCheck: | ||
"""Test RabbitMQ health check.""" | ||
|
||
@mock.patch("health_check.contrib.rabbitmq.backends.getattr") | ||
@mock.patch("health_check.contrib.rabbitmq.backends.Connection") | ||
def test_broker_refused_connection(self, mocked_connection, mocked_getattr): | ||
"""Test when the connection to RabbitMQ is refused.""" | ||
mocked_getattr.return_value = "broker_url" | ||
|
||
conn_exception = ConnectionRefusedError("Refused connection") | ||
|
||
# mock returns | ||
mocked_conn = mock.MagicMock() | ||
mocked_connection.return_value.__enter__.return_value = mocked_conn | ||
mocked_conn.connect.side_effect = conn_exception | ||
|
||
# instantiates the class | ||
rabbitmq_healthchecker = RabbitMQHealthCheck() | ||
|
||
# invokes the method check_status() | ||
rabbitmq_healthchecker.check_status() | ||
assert len(rabbitmq_healthchecker.errors), 1 | ||
|
||
# mock assertions | ||
mocked_connection.assert_called_once_with("broker_url") | ||
|
||
@mock.patch("health_check.contrib.rabbitmq.backends.getattr") | ||
@mock.patch("health_check.contrib.rabbitmq.backends.Connection") | ||
def test_broker_auth_error(self, mocked_connection, mocked_getattr): | ||
"""Test that the connection to RabbitMQ has an authentication error.""" | ||
mocked_getattr.return_value = "broker_url" | ||
|
||
conn_exception = AccessRefused("Refused connection") | ||
|
||
# mock returns | ||
mocked_conn = mock.MagicMock() | ||
mocked_connection.return_value.__enter__.return_value = mocked_conn | ||
mocked_conn.connect.side_effect = conn_exception | ||
|
||
# instantiates the class | ||
rabbitmq_healthchecker = RabbitMQHealthCheck() | ||
|
||
# invokes the method check_status() | ||
rabbitmq_healthchecker.check_status() | ||
assert len(rabbitmq_healthchecker.errors), 1 | ||
|
||
# mock assertions | ||
mocked_connection.assert_called_once_with("broker_url") | ||
|
||
@mock.patch("health_check.contrib.rabbitmq.backends.getattr") | ||
@mock.patch("health_check.contrib.rabbitmq.backends.Connection") | ||
def test_broker_connection_upon_none_url(self, mocked_connection, mocked_getattr): | ||
"""Thest when the connection to RabbitMQ has no ``broker_url``.""" | ||
mocked_getattr.return_value = None | ||
# if the variable BROKER_URL is not set, AccessRefused exception is raised | ||
conn_exception = AccessRefused("Refused connection") | ||
|
||
# mock returns | ||
mocked_conn = mock.MagicMock() | ||
mocked_connection.return_value.__enter__.return_value = mocked_conn | ||
mocked_conn.connect.side_effect = conn_exception | ||
|
||
# instantiates the class | ||
rabbitmq_healthchecker = RabbitMQHealthCheck() | ||
|
||
# invokes the method check_status() | ||
rabbitmq_healthchecker.check_status() | ||
assert len(rabbitmq_healthchecker.errors), 1 | ||
|
||
# mock assertions | ||
mocked_connection.assert_called_once_with(None) |