summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatyana Leontovich <tleontov@yahoo-inc.com>2013-03-06 15:01:44 +0200
committerTatyana Leontovich <tleontov@yahoo-inc.com>2013-04-04 15:06:57 +0300
commit0995045f2a6c6179b9d3daf7e8c994e30e4e2d8c (patch)
tree29b3554ee7b8de27ff74735372b48982cd54c92e
parent741c15f9633f642aa2e52de10b8eb65d6a8c9ecb (diff)
downloadpython-glanceclient-0995045f2a6c6179b9d3daf7e8c994e30e4e2d8c.tar.gz
Improve unit tests for python-glanceclient.glanceclient.common.http
Add several tests for glanceclient.common.http module Fixes: bug #1149445 Change-Id: I6a47c64e11cefea276163777dcd559316fc8e0ad
-rw-r--r--tests/test_http.py58
-rw-r--r--tests/test_ssl.py47
2 files changed, 103 insertions, 2 deletions
diff --git a/tests/test_http.py b/tests/test_http.py
index 3d87444..0043b4a 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -16,11 +16,13 @@
import httplib
import socket
import StringIO
-import testtools
+import urlparse
import mox
+import testtools
from glanceclient import exc
+import glanceclient
from glanceclient.common import http
from tests import utils
@@ -82,6 +84,60 @@ class TestClient(testtools.TestCase):
headers=headers)
self.assertEqual(resp, fake)
+ def test_connection_refused_raw_request(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')
+ httplib.HTTPConnection.request(mox.IgnoreArg(), mox.IgnoreArg(),
+ headers=mox.IgnoreArg()
+ ).AndRaise(socket.error())
+ self.mock.ReplayAll()
+ try:
+ client.raw_request('GET', '/v1/images/detail?limit=20')
+
+ 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)
+
+ def test_parse_endpoint(self):
+ endpoint = 'http://example.com:9292'
+ test_client = http.HTTPClient(endpoint, token=u'adc123')
+ actual = test_client.parse_endpoint(endpoint)
+ expected = urlparse.ParseResult(scheme='http',
+ netloc='example.com:9292', path='',
+ params='', query='', fragment='')
+ self.assertEqual(expected, actual)
+
+ def test_get_connection_class(self):
+ endpoint = 'http://example.com:9292'
+ test_client = http.HTTPClient(endpoint, token=u'adc123')
+ actual = (test_client.get_connection_class('https'))
+ self.assertEqual(actual, http.VerifiedHTTPSConnection)
+
+ def test_get_connections_kwargs_http(self):
+ endpoint = 'http://example.com:9292'
+ test_client = http.HTTPClient(endpoint, token=u'adc123')
+ actual = test_client.get_connection_kwargs('http', insecure=True)
+ self.assertEqual({'timeout': 600.0}, actual)
+
+ def test_get_connections_kwargs_https(self):
+ endpoint = 'http://example.com:9292'
+ test_client = http.HTTPClient(endpoint, token=u'adc123')
+ actual = test_client.get_connection_kwargs('https', insecure=True)
+ expected = {'cacert': None,
+ 'cert_file': None,
+ 'insecure': True,
+ 'key_file': None,
+ 'ssl_compression': True,
+ 'timeout': 600.0}
+ self.assertEqual(expected, actual)
+
class TestHostResolutionError(testtools.TestCase):
diff --git a/tests/test_ssl.py b/tests/test_ssl.py
index 8ee179f..79c6389 100644
--- a/tests/test_ssl.py
+++ b/tests/test_ssl.py
@@ -14,9 +14,9 @@
# under the License.
import os
-import testtools
from OpenSSL import crypto
+import testtools
from glanceclient import exc
from glanceclient.common import http
@@ -184,3 +184,48 @@ class TestVerifiedHTTPSConnection(testtools.TestCase):
self.assertRaises(exc.SSLCertificateError,
conn.verify_callback, None, cert, 0, 0, True)
+
+ def test_ssl_broken_key_file(self):
+ """
+ Test verify exception is raised.
+ """
+ cert_file = os.path.join(TEST_VAR_DIR, 'certificate.crt')
+ cacert = os.path.join(TEST_VAR_DIR, 'ca.crt')
+ key_file = 'fake.key'
+ self.assertRaises(
+ exc.SSLConfigurationError,
+ http.VerifiedHTTPSConnection, '127.0.0.1',
+ 0, key_file=key_file,
+ cert_file=cert_file, cacert=cacert)
+
+ def test_ssl_init_ok_with_insecure_true(self):
+ """
+ Test VerifiedHTTPSConnection class init
+ """
+ key_file = os.path.join(TEST_VAR_DIR, 'privatekey.key')
+ cert_file = os.path.join(TEST_VAR_DIR, 'certificate.crt')
+ cacert = os.path.join(TEST_VAR_DIR, 'ca.crt')
+ try:
+ conn = http.VerifiedHTTPSConnection(
+ '127.0.0.1', 0,
+ key_file=key_file,
+ cert_file=cert_file,
+ cacert=cacert, insecure=True)
+ except exc.SSLConfigurationError:
+ self.fail('Failed to init VerifiedHTTPSConnection.')
+
+ def test_ssl_init_ok_with_ssl_compression_false(self):
+ """
+ Test VerifiedHTTPSConnection class init
+ """
+ key_file = os.path.join(TEST_VAR_DIR, 'privatekey.key')
+ cert_file = os.path.join(TEST_VAR_DIR, 'certificate.crt')
+ cacert = os.path.join(TEST_VAR_DIR, 'ca.crt')
+ try:
+ conn = http.VerifiedHTTPSConnection(
+ '127.0.0.1', 0,
+ key_file=key_file,
+ cert_file=cert_file,
+ cacert=cacert, ssl_compression=False)
+ except exc.SSLConfigurationError:
+ self.fail('Failed to init VerifiedHTTPSConnection.')