Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_broker_access_refused(self, mocked_connection):
mocked_conn = mock.MagicMock()
mocked_connection.return_value.__enter__.return_value = mocked_conn
mocked_conn.connect.side_effect = AccessRefused('Access Refused')
results = RabbitMQCheck.run()
assert results['RABBITMQ'].is_healthy is False
assert results['RABBITMQ'].severity == Result.ERROR
mocked_connection.assert_called_once_with('broker_url')
def test_broker_connection_upon_none_url(self, mocked_connection):
mocked_conn = mock.MagicMock()
mocked_connection.return_value.__enter__.return_value = mocked_conn
mocked_conn.connect.side_effect = AccessRefused('Connection Refused')
results = RabbitMQCheck.run()
assert results['RABBITMQ'].is_healthy is False
assert results['RABBITMQ'].severity == Result.ERROR
mocked_connection.assert_called_once_with(None)
def check() -> Result:
"""Open and close the broker channel."""
if conf.get(CELERY_BROKER_BACKEND) == 'redis':
return Result()
try:
# Context to release connection
with Connection(conf.get(CELERY_BROKER_URL)) as conn:
conn.connect()
except ConnectionRefusedError:
return Result(message='Service unable to connect, "Connection was refused".',
severity=Result.ERROR)
except AccessRefused:
return Result(message='Service unable to connect, "Authentication error".',
severity=Result.ERROR)
except IOError:
return Result(message='Service has an "IOError".', severity=Result.ERROR)
except Exception as e:
return Result(message='Service has an "{}" error.'.format(e), severity=Result.ERROR)
else:
return Result()
"""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 established. RabbitMQ is healthy.")