MAP_COMMAND_TO_FAKE_RESPONSE['/lungroup/associate?ID=12&ASSOCIATEOBJTYPE=11'
'&ASSOCIATEOBJID=12/DELETE'] = (
FAKE_COMMON_SUCCESS_RESPONSE)
+
# mock snapshot info map
MAP_COMMAND_TO_FAKE_RESPONSE['/snapshot'] = (
FAKE_CREATE_SNAPSHOT_INFO_RESPONSE)
MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator?ISFREE=true&'
'range=[0-8191]/GET'] = (
FAKE_FC_INFO_RESPONSE)
+
MAP_COMMAND_TO_FAKE_RESPONSE['/fc_initiator/10000090fa0d6754/GET'] = (
FAKE_FC_INFO_RESPONSE)
lun_info = self.driver.create_volume(test_volume)
self.assertEqual('1', lun_info['provider_location'])
+ def test_get_id_from_result(self):
+ self.driver.restclient.login()
+ result = {}
+ name = 'test_name'
+ key = 'NAME'
+ re = self.driver.restclient._get_id_from_result(result, name, key)
+ self.assertIsNone(re)
+
+ result = {'data': {}}
+ re = self.driver.restclient._get_id_from_result(result, name, key)
+ self.assertIsNone(re)
+
+ result = {'data': [{'COUNT': 1, 'ID': '1'},
+ {'COUNT': 2, 'ID': '2'}]}
+
+ re = self.driver.restclient._get_id_from_result(result, name, key)
+ self.assertIsNone(re)
+
+ result = {'data': [{'NAME': 'test_name1', 'ID': '1'},
+ {'NAME': 'test_name2', 'ID': '2'}]}
+ re = self.driver.restclient._get_id_from_result(result, name, key)
+ self.assertIsNone(re)
+
+ result = {'data': [{'NAME': 'test_name', 'ID': '1'},
+ {'NAME': 'test_name2', 'ID': '2'}]}
+ re = self.driver.restclient._get_id_from_result(result, name, key)
+ self.assertEqual('1', re)
+
def create_fake_conf_file(self):
"""Create a fake Config file
def _get_id_from_result(self, result, name, key):
if 'data' in result:
for item in result['data']:
- if name == item[key]:
+ if name == item.get(key):
return item['ID']
def get_volume_by_name(self, name):
result = self.call(url, None, "GET")
self._assert_rest_result(result, _('Check portgroup associate error.'))
- if 'data' in result:
- for item in result['data']:
- if view_id == item['ID']:
- return True
+ if self._get_id_from_result(result, view_id, 'ID'):
+ return True
return False
def do_mapping(self, lun_id, hostgroup_id, host_id, tgtportgroup_id=None):
result = self.call(url, None, "GET")
self._assert_rest_result(result, _('Check lungroup associate error.'))
- if 'data' in result:
- for item in result['data']:
- if view_id == item['ID']:
- return True
+ if self._get_id_from_result(result, view_id, 'ID'):
+ return True
return False
def hostgroup_associated(self, view_id, hostgroup_id):
result = self.call(url, None, "GET")
self._assert_rest_result(result, _('Check hostgroup associate error.'))
- if 'data' in result:
- for item in result['data']:
- if view_id == item['ID']:
- return True
+ if self._get_id_from_result(result, view_id, 'ID'):
+ return True
return False
def find_host_lun_id(self, host_id, lun_id):
result = self.call(url, None, "GET")
self._assert_rest_result(result, _('Check hostgroup associate error.'))
- if 'data' in result:
- for item in result['data']:
- if host_id == item['ID']:
- return True
+ if self._get_id_from_result(result, host_id, 'ID'):
+ return True
return False
self._assert_rest_result(result,
_('Check initiator added to array error.'))
- if 'data' in result:
- for item in result['data']:
- if item['ID'] == ininame:
- return True
+ if self._get_id_from_result(result, ininame, 'ID'):
+ return True
return False
def is_initiator_associated_to_host(self, ininame):
result = self.call(url, None, "GET")
self._assert_rest_result(result, _('Get partition by name error.'))
- if 'data' in result:
- for item in result['data']:
- if name == item['NAME']:
- return item['ID']
+ return self._get_id_from_result(result, name, 'NAME')
def get_partition_info_by_id(self, partition_id):
result = self.call(url, None, "GET")
self._assert_rest_result(result, _('Get cache by name error.'))
- if 'data' in result:
- for item in result['data']:
- if name == item['NAME']:
- return item['ID']
+ return self._get_id_from_result(result, name, 'NAME')
def get_cache_info_by_id(self, cacheid):
url = "/SMARTCACHEPARTITION/" + cacheid