Add unit testing of connection OK

Add a unit test case for connection OK, and fix issues found while
adding it.
This commit is contained in:
Devananda van der Veen 2015-03-29 08:35:06 -07:00
parent 24cb8c2458
commit 3f39ea0a33
3 changed files with 26 additions and 8 deletions

View File

@ -127,11 +127,12 @@ import sys
import urllib2
from urlparse import urlparse
from redfish import exception
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
class RedfishConnection(object):
"""Implements basic connection handling for Redfish APIs."""
@ -196,7 +197,7 @@ class RedfishConnection(object):
elif url.scheme == 'http':
conn = httplib.HTTPConnection(host=url.netloc, strict=True)
else:
raise RedfishException(message='Unknown connection schema')
raise exception.RedfishException(message='Unknown connection schema')
# NOTE: Do not assume every HTTP operation will return a JSON body.
# For example, ExtendedError structures are only required for HTTP 400
@ -220,7 +221,9 @@ class RedfishConnection(object):
response = dict()
try:
LOG.debug("BODY: %s." % body.decode('utf-8'))
response = json.loads(body.decode('utf-8'))
LOG.debug("Loaded json: %s" % response)
except ValueError: # if it doesn't decode as json
# NOTE: resources may return gzipped content
# try to decode as gzip (we should check the headers for Content-Encoding=gzip)
@ -229,10 +232,7 @@ class RedfishConnection(object):
uncompressed_string = gzipper.read().decode('UTF-8')
response = json.loads(uncompressed_string)
except:
pass
# return empty
pass
raise exception.RedfishException(message='Failed to parse response as a JSON document, received "%s".' % body)
return resp.status, headers, response

View File

@ -15,4 +15,13 @@
class RedfishException(Exception):
"""Base class for redfish exceptions"""
pass
def __init__(self, message=None, **kwargs):
self.kwargs = kwargs
if not message:
try:
message = self.message % kwargs
except Excetion as e:
LOG.exception('Error in string format operation')
message = self.message
super(RedfishException, self).__init__(message)

View File

@ -42,7 +42,7 @@ def get_response():
class _response(object):
status = 200
def read(self):
return "{'foo': 'bar'}"
return '{"foo": "bar"}'
def getheaders(self):
return [('Fake-Header', 'fake value')]
return _response()
@ -91,3 +91,12 @@ class TestRedfishConnection(base.TestCase):
# ssl_mock.return_value = mock.Mock()
# con = connection.RedfishConnection(*get_fake_params)
# ssl_mock.assert_called_once_with(ssl.PROTOCOL_TLSv1)
def test_get_ok(self):
con = connection.RedfishConnection(*get_fake_params())
res = con.rest_get('/v1/test', '')
self.assertEqual(200, res[0])
# Headers ae lower cased when returned
self.assertIn('fake-header', res[1].keys())
print(res)
self.assertIn('foo', res[2].keys())