From 957a8db0bb8b9577635308d55ae0100e9adfe04a Mon Sep 17 00:00:00 2001 From: yogeshprasad Date: Thu, 7 May 2015 16:47:12 +0530 Subject: [PATCH] Fix the KeyError in CloudByte iSCSI cinder driver Driver returns KeyError during list TSM if there is no TSM found in CloudByte Storage. This patch will make driver to return proper message. Change-Id: I77f137afc87b63c0b491e010a1b8ccf63e9e89c2 Closes-Bug: 1452642 --- cinder/tests/unit/test_cloudbyte.py | 28 ++++++++++++++++++++ cinder/volume/drivers/cloudbyte/cloudbyte.py | 13 ++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cinder/tests/unit/test_cloudbyte.py b/cinder/tests/unit/test_cloudbyte.py index 051ec2c21..a1ba649ef 100644 --- a/cinder/tests/unit/test_cloudbyte.py +++ b/cinder/tests/unit/test_cloudbyte.py @@ -674,6 +674,13 @@ class CloudByteISCSIDriverTestCase(testtools.TestCase): return MAP_COMMAND_TO_FAKE_RESPONSE[cmd] + def _none_response_to_list_tsm(self, cmd, params, version='1.0'): + """This is a side effect function.""" + if cmd == 'listTsm': + return {"listTsmResponse": {}} + + return MAP_COMMAND_TO_FAKE_RESPONSE[cmd] + def _side_effect_api_req_to_list_filesystem( self, cmd, params, version='1.0'): """This is a side effect function.""" @@ -1036,6 +1043,27 @@ class CloudByteISCSIDriverTestCase(testtools.TestCase): "list iscsi initiators."): self.driver.create_volume(volume) + # Test - VIII + + volume['id'] = fake_volume_id + volume['size'] = 22 + + # reconfigure the dependencies + # reset the mocks + mock_api_req.reset_mock() + + # configure or re-configure the mocks + mock_api_req.side_effect = ( + self._none_response_to_list_tsm) + + # now run the test + with testtools.ExpectedException( + exception.VolumeBackendAPIException, + "Bad or unexpected response from the storage volume " + "backend API: TSM \[openstack\] was not found in CloudByte " + "storage for account \[CustomerA\]."): + self.driver.create_volume(volume) + @mock.patch.object(cloudbyte.CloudByteISCSIDriver, '_api_request_for_cloudbyte') @mock.patch.object(cloudbyte.CloudByteISCSIDriver, diff --git a/cinder/volume/drivers/cloudbyte/cloudbyte.py b/cinder/volume/drivers/cloudbyte/cloudbyte.py index cff37f458..436fefc65 100644 --- a/cinder/volume/drivers/cloudbyte/cloudbyte.py +++ b/cinder/volume/drivers/cloudbyte/cloudbyte.py @@ -236,9 +236,16 @@ class CloudByteISCSIDriver(san.SanISCSIDriver): data = self._api_request_for_cloudbyte(async_cmd, params) return data - def _get_tsm_details(self, data, tsm_name): + def _get_tsm_details(self, data, tsm_name, account_name): # Filter required tsm's details - tsms = data['listTsmResponse']['listTsm'] + tsms = data['listTsmResponse'].get('listTsm') + + if tsms is None: + msg = (_("TSM [%(tsm)s] was not found in CloudByte storage " + "for account [%(account)s].") % + {'tsm': tsm_name, 'account': account_name}) + raise exception.VolumeBackendAPIException(data=msg) + tsmdetails = {} for tsm in tsms: if tsm['name'] == tsm_name: @@ -582,7 +589,7 @@ class CloudByteISCSIDriver(san.SanISCSIDriver): 'tsm': tsm_name}) tsm_data = self._request_tsm_details(account_id) - tsm_details = self._get_tsm_details(tsm_data, tsm_name) + tsm_details = self._get_tsm_details(tsm_data, tsm_name, account_name) # Send request to create a qos group before creating a volume LOG.debug("Creating qos group for CloudByte volume [%s].", -- 2.45.2