Improve unit tests for python-glanceclient.glanceclient.common.http

Add several tests for glanceclient.common.http module

Fixes: bug #1149445
Change-Id: I6a47c64e11cefea276163777dcd559316fc8e0ad
This commit is contained in:
Tatyana Leontovich 2013-03-06 15:01:44 +02:00
parent 741c15f963
commit 0995045f2a
2 changed files with 103 additions and 2 deletions

View File

@ -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):

View File

@ -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.')