From 3e9cc4fc5f8e82021b3bfd3106d3e50d2caaf91a Mon Sep 17 00:00:00 2001 From: Wilson Liu Date: Sat, 10 Oct 2015 11:37:04 +0800 Subject: [PATCH] Huawei driver handle volume exists error Currently when we create a volume on an array where that volume already exists, there will be an exception. But in fact it should just go on and doesn't need an exception. Closes-Bug: #1504718 Change-Id: I13a52e81c1c271ea6728cb4a23717609d95bae69 --- cinder/tests/unit/test_huawei_drivers.py | 31 +++++++++++++++++---- cinder/volume/drivers/huawei/constants.py | 1 + cinder/volume/drivers/huawei/rest_client.py | 4 +++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cinder/tests/unit/test_huawei_drivers.py b/cinder/tests/unit/test_huawei_drivers.py index 4a53a3913..e79426891 100644 --- a/cinder/tests/unit/test_huawei_drivers.py +++ b/cinder/tests/unit/test_huawei_drivers.py @@ -230,7 +230,7 @@ FAKE_LUN_INFO_RESPONSE = """ } """ -FAKE_LUN_DELETE_SUCCESS_RESPONSE = """ +FAKE_LUN_GET_SUCCESS_RESPONSE = """ { "error": { "code": 0 @@ -265,7 +265,7 @@ FAKE_QUERY_ALL_LUN_RESPONSE = """ "code": 0 }, "data": [{ - "ID": "1", + "ID": "11", "NAME": "IexzQZJWSXuX2e9I7c8GNQ" }] } @@ -970,10 +970,10 @@ MAP_COMMAND_TO_FAKE_RESPONSE['/lun'] = ( FAKE_LUN_INFO_RESPONSE) MAP_COMMAND_TO_FAKE_RESPONSE['/lun/11/GET'] = ( - FAKE_LUN_DELETE_SUCCESS_RESPONSE) + FAKE_LUN_GET_SUCCESS_RESPONSE) MAP_COMMAND_TO_FAKE_RESPONSE['/lun/1/GET'] = ( - FAKE_LUN_DELETE_SUCCESS_RESPONSE) + FAKE_LUN_GET_SUCCESS_RESPONSE) MAP_COMMAND_TO_FAKE_RESPONSE['/lun/11/DELETE'] = ( FAKE_COMMON_SUCCESS_RESPONSE) @@ -1072,7 +1072,7 @@ MAP_COMMAND_TO_FAKE_RESPONSE['/snapshot?range=[0-32767]/GET'] = ( # mock QoS info map MAP_COMMAND_TO_FAKE_RESPONSE['/ioclass/11/GET'] = ( - FAKE_LUN_DELETE_SUCCESS_RESPONSE) + FAKE_LUN_GET_SUCCESS_RESPONSE) MAP_COMMAND_TO_FAKE_RESPONSE['/ioclass/11/DELETE'] = ( FAKE_COMMON_SUCCESS_RESPONSE) @@ -1722,6 +1722,27 @@ class Huawei18000ISCSIDriverTestCase(test.TestCase): (qos_id, lun_list) = self.driver.restclient.find_available_qos(qos) self.assertEqual(("11", u'["0", "1", "2"]'), (qos_id, lun_list)) + @mock.patch.object(rest_client.RestClient, 'get_volume_by_name', + return_value='11') + @mock.patch.object(rest_client.RestClient, 'get_lun_info', + return_value={'ID': '11'}) + def test_create_volume_exist(self, mock_lun_info, mock_volume_info): + self.driver.restclient.login() + lun_param = {'NAME': 'IexzQZJWSXuX2e9I7c8GNQ'} + + fack_error_volume_exist = {"error": {"code": 1077948993}} + with mock.patch.object(rest_client.RestClient, 'call', + return_value=fack_error_volume_exist): + lun_info = self.driver.restclient.create_volume(lun_param) + self.assertEqual('11', lun_info['ID']) + + fack_error_volume_exist = {"error": {"code": 123456789}} + with mock.patch.object(rest_client.RestClient, 'call', + return_value=fack_error_volume_exist): + self.assertRaises(exception.VolumeBackendAPIException, + self.driver.restclient.create_volume, + lun_param) + def create_fake_conf_file(self): """Create a fake Config file. diff --git a/cinder/volume/drivers/huawei/constants.py b/cinder/volume/drivers/huawei/constants.py index 4e3eb5cd7..47f59a566 100644 --- a/cinder/volume/drivers/huawei/constants.py +++ b/cinder/volume/drivers/huawei/constants.py @@ -40,6 +40,7 @@ MIGRATION_COMPLETE = '76' ERROR_CONNECT_TO_SERVER = -403 ERROR_UNAUTHORIZED_TO_SERVER = -401 SOCKET_TIMEOUT = 52 +ERROR_VOLUME_ALREADY_EXIST = 1077948993 LOGIN_SOCKET_TIMEOUT = 4 THICK_LUNTYPE = 0 diff --git a/cinder/volume/drivers/huawei/rest_client.py b/cinder/volume/drivers/huawei/rest_client.py index 6a94a7799..c956e1c07 100644 --- a/cinder/volume/drivers/huawei/rest_client.py +++ b/cinder/volume/drivers/huawei/rest_client.py @@ -168,6 +168,10 @@ class RestClient(object): url = "/lun" data = json.dumps(lun_param) result = self.call(url, data) + if result['error']['code'] == constants.ERROR_VOLUME_ALREADY_EXIST: + lun_id = self.get_volume_by_name(lun_param["NAME"]) + if lun_id: + return self.get_lun_info(lun_id) msg = _('Create volume error.') self._assert_rest_result(result, msg) -- 2.45.2