From 8cee48b1ddf480d182bbc33ec684dcfd195b038c Mon Sep 17 00:00:00 2001
From: Andrew Laski <andrew.laski@rackspace.com>
Date: Wed, 12 Sep 2012 09:40:04 -0400
Subject: [PATCH] Make ConnectionRefused error more informative.
When the server refuses the connection the error message displayed now
lists the endpoint that refused the connection.
Fixes: bug 1043067
Change-Id: I62797106732bbb6eec8c99e491fd38850ad58ff8
---
glanceclient/common/http.py | 3 +-
tests/test_http.py | 55 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 1 deletions(-)
create mode 100644 tests/test_http.py
diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py
index d81ac9a..4349e86 100644
--- a/glanceclient/common/http.py
+++ b/glanceclient/common/http.py
@@ -140,7 +140,8 @@ class HTTPClient(object):
message = "Error finding address for %(url)s: %(e)s" % locals()
raise exc.InvalidEndpoint(message=message)
except (socket.error, socket.timeout) as e:
- message = "Error communicating with %(url)s: %(e)s" % locals()
+ endpoint = self.endpoint
+ message = "Error communicating with %(endpoint)s %(e)s" % locals()
raise exc.CommunicationError(message=message)
body_iter = ResponseBodyIterator(resp)
diff --git a/tests/test_http.py b/tests/test_http.py
new file mode 100644
index 0000000..c727c6a
--- /dev/null
+++ b/tests/test_http.py
@@ -0,0 +1,55 @@
+# Copyright 2012 OpenStack LLC.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import httplib
+import socket
+import unittest
+
+import mox
+
+from glanceclient import exc
+from glanceclient.common import http
+
+
+class TestClient(unittest.TestCase):
+ def test_connection_refused(self):
+ """
+ Should receive a CommunicationError if connection refused.
+ And the error should list the host and port that refused the
+ connection
+ """
+ endpoint = 'http://example.com:9292'
+ client = http.HTTPClient(endpoint, token=u'abc123')
+ m = mox.Mox()
+ m.StubOutWithMock(httplib.HTTPConnection, 'request')
+ httplib.HTTPConnection.request(
+ mox.IgnoreArg(),
+ mox.IgnoreArg(),
+ headers=mox.IgnoreArg(),
+ ).AndRaise(socket.error())
+ m.ReplayAll()
+ try:
+ client.json_request('GET', '/v1/images/detail?limit=20')
+ #NOTE(alaski) We expect exc.CommunicationError to be raised
+ # so we should never reach this point. try/except is used here
+ # rather than assertRaises() so that we can check the body of
+ # the exception.
+ self.fail('An exception should have bypassed this line.')
+ except exc.CommunicationError, comm_err:
+ fail_msg = ("Exception message '%s' should contain '%s'" %
+ (comm_err.message, endpoint))
+ self.assertTrue(endpoint in comm_err.message, fail_msg)
+ finally:
+ m.UnsetStubs()