-
-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from onefinestay/rabbit_error_reporting
check amqp uri for better error messages
- Loading branch information
Showing
6 changed files
with
107 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import absolute_import | ||
|
||
import amqp | ||
from kombu import Connection | ||
from kombu.transport.pyamqp import Transport | ||
import six | ||
|
||
|
||
class ConnectionTester(amqp.Connection): | ||
"""Kombu doesn't have any good facilities for diagnosing rabbit | ||
connection errors, e.g. bad credentials, or unknown vhost. This hack | ||
attempts some heuristic diagnosis""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
try: | ||
super(ConnectionTester, self).__init__(*args, **kwargs) | ||
except IOError as exc: | ||
if not hasattr(self, '_wait_tune_ok'): | ||
raise | ||
elif self._wait_tune_ok: | ||
six.raise_from(IOError( | ||
'Error connecting to broker, probably caused by invalid' | ||
' credentials' | ||
), exc) | ||
else: | ||
six.raise_from(IOError( | ||
'Error connecting to broker, probably caused by using an' | ||
' invalid or unauthorized vhost' | ||
), exc) | ||
|
||
|
||
class TestTransport(Transport): | ||
Connection = ConnectionTester | ||
|
||
|
||
def verify_amqp_uri(amqp_uri): | ||
connection = Connection(amqp_uri) | ||
if connection.transport_cls != 'amqp': | ||
# Can't use these heuristics. Fall back to the existing error behaviour | ||
return | ||
|
||
transport = TestTransport(connection.transport.client) | ||
with transport.establish_connection(): | ||
pass |
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
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,51 @@ | ||
import socket | ||
|
||
import pytest | ||
from urllib3.util import parse_url, Url | ||
|
||
from nameko.amqp import verify_amqp_uri | ||
|
||
|
||
@pytest.fixture | ||
def uris(rabbit_config): | ||
amqp_uri = rabbit_config['AMQP_URI'] | ||
scheme, auth, host, port, path, _, _ = parse_url(amqp_uri) | ||
bad_port = Url(scheme, auth, host, port + 1, path).url | ||
bad_user = Url(scheme, 'invalid:invalid', host, port, path).url | ||
bad_vhost = Url(scheme, auth, host, port, '/unknown').url | ||
return { | ||
'good': amqp_uri, | ||
'bad_port': bad_port, | ||
'bad_user': bad_user, | ||
'bad_vhost': bad_vhost, | ||
} | ||
|
||
|
||
def test_good(uris): | ||
amqp_uri = uris['good'] | ||
verify_amqp_uri(amqp_uri) | ||
|
||
|
||
def test_bad_user(uris): | ||
amqp_uri = uris['bad_user'] | ||
with pytest.raises(IOError) as exc_info: | ||
verify_amqp_uri(amqp_uri) | ||
message = str(exc_info.value) | ||
assert 'Error connecting to broker' in message | ||
assert 'invalid credentials' in message | ||
|
||
|
||
def test_bad_vhost(uris): | ||
amqp_uri = uris['bad_vhost'] | ||
with pytest.raises(IOError) as exc_info: | ||
verify_amqp_uri(amqp_uri) | ||
message = str(exc_info.value) | ||
assert 'Error connecting to broker' in message | ||
assert 'invalid or unauthorized vhost' in message | ||
|
||
|
||
def test_other_error(uris): | ||
# other errors bubble | ||
amqp_uri = uris['bad_port'] | ||
with pytest.raises(socket.error): | ||
verify_amqp_uri(amqp_uri) |
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