vmware: check for response body in error conditions

The response may not have a body in certain error conditions, like
a 500 on a socket error as seen in the VMware NSX CI. This fixes
an AttributeError when trying to access the response body in those
cases.

Change-Id: I086bb5bdb59813d326d45b5f7545da2ebb12aa95
Closes-Bug: #1517275
This commit is contained in:
Matt Riedemann
2015-11-17 17:54:00 -08:00
parent 341527c44f
commit b709dd62c1
3 changed files with 22 additions and 3 deletions

View File

@@ -531,7 +531,7 @@ class Store(glance_store.Store):
'\nThe response body:\n%(body)s') %
{'image': image_id,
'status': res.status,
'body': res.body})
'body': getattr(res, 'body', None)})
LOG.error(msg)
raise exceptions.BackendException(msg)

View File

@@ -70,10 +70,12 @@ class FakeHTTPConnection(object):
def __init__(self, status=200, *args, **kwargs):
self.status = status
self.no_response_body = kwargs.get('no_response_body', False)
pass
def getresponse(self):
return utils.FakeHTTPResponse(status=self.status)
return utils.FakeHTTPResponse(status=self.status,
no_response_body=self.no_response_body)
def request(self, *_args, **_kwargs):
pass
@@ -447,6 +449,22 @@ class TestStore(base.StoreBaseTest,
self.store.add,
expected_image_id, image, expected_size)
@mock.patch.object(vm_store.Store, 'select_datastore')
@mock.patch.object(api, 'VMwareAPISession')
def test_unexpected_status_no_response_body(self, mock_api_session,
mock_select_datastore):
expected_image_id = str(uuid.uuid4())
expected_size = FIVE_KB
expected_contents = b"*" * expected_size
image = six.BytesIO(expected_contents)
self.session = mock.Mock()
with self._mock_http_connection() as HttpConn:
HttpConn.return_value = FakeHTTPConnection(status=500,
no_response_body=True)
self.assertRaises(exceptions.BackendException,
self.store.add,
expected_image_id, image, expected_size)
@mock.patch.object(api, 'VMwareAPISession')
def test_reset_session(self, mock_api_session):
self.store.reset_session()

View File

@@ -46,7 +46,8 @@ class FakeHTTPResponse(object):
self.read = self.data.read
self.status = status
self.headers = headers or {'content-length': len(data)}
self.body = None
if not kwargs.get('no_response_body', False):
self.body = None
def getheader(self, name, default=None):
return self.headers.get(name.lower(), default)