Check if a exception has a code on it before read the code

In 'nova.volume.cinder.API.initialize_connection' method, all exceptions
threw by 'terminate_connection' will be caught and logged. When log the
exceptions, the code try to record the 'code' attribute of the exception
since the code is expecting a CinderClientException or NovaException[1].
But if the some else exception(like TypeError) which doesn't have a code
on it was threw up, we will got a AttributeError.

This commit add logic to check if the exception has a code on it before
try to read it.

[1]https://github.com/openstack/nova/blob/13.0.0.0rc3/nova/volume/cinder.py#L437

Change-Id: I42fd2f2b77c41a60dfaf0cc882a344596d50daf5
Closes-Bug: #1564551
(cherry picked from commit 2135a3f55f)
This commit is contained in:
Wenzhi Yu 2016-04-02 23:34:22 +08:00
parent 95c9d6a2fd
commit 5e12cd8756
2 changed files with 21 additions and 1 deletions

View File

@ -293,6 +293,25 @@ class CinderApiTestCase(test.NoDBTestCase):
mock_cinderclient.return_value.volumes. \
initialize_connection.assert_called_once_with(volume_id, connector)
@mock.patch('nova.volume.cinder.LOG')
@mock.patch('nova.volume.cinder.cinderclient')
def test_initialize_connection_exception_no_code(
self, mock_cinderclient, mock_log):
mock_cinderclient.return_value.volumes. \
initialize_connection.side_effect = (
cinder_exception.ClientException(500, "500"))
mock_cinderclient.return_value.volumes. \
terminate_connection.side_effect = (
test.TestingException)
connector = {'host': 'fakehost1'}
self.assertRaises(cinder_exception.ClientException,
self.api.initialize_connection,
self.ctx,
'id1',
connector)
self.assertIsNone(mock_log.error.call_args_list[1][0][1]['code'])
@mock.patch('nova.volume.cinder.cinderclient')
def test_initialize_connection_rollback(self, mock_cinderclient):
mock_cinderclient.return_value.volumes.\

View File

@ -366,7 +366,8 @@ class API(object):
{'vol': volume_id,
'host': connector.get('host'),
'msg': six.text_type(exc),
'code': exc.code})
'code': (
exc.code if hasattr(exc, 'code') else None)})
@translate_volume_exception
def terminate_connection(self, context, volume_id, connector):