Fix cleanup of keypairs for --prefix option

When running tempest cleanup --prefix, there is an issue with
cleaning up keypairs, because of incorrect listing of them. This
issue is solved by setting the right dictionary key.

Change-Id: I81da575b0f40d2f04440a322b3b694f332c5d58e
This commit is contained in:
Katarina Strenkova 2024-07-25 10:41:31 +00:00
parent a4a48b2f29
commit 0795166adf
2 changed files with 25 additions and 26 deletions

View File

@ -115,21 +115,32 @@ class BaseService(object):
return [item for item in item_list
if item['tenant_id'] == self.tenant_id]
def _filter_by_prefix(self, item_list):
items = [item for item in item_list
if item['name'].startswith(self.prefix)]
def _filter_by_prefix(self, item_list, top_key=None):
items = []
for item in item_list:
name = item[top_key]['name'] if top_key else item['name']
if name.startswith(self.prefix):
items.append(item)
return items
def _filter_by_resource_list(self, item_list, attr):
if attr not in self.resource_list_json:
return []
items = [item for item in item_list if item['id']
in self.resource_list_json[attr].keys()]
items = []
for item in item_list:
item_id = (item['keypair']['name'] if attr == 'keypairs'
else item['id'])
if item_id in self.resource_list_json[attr].keys():
items.append(item)
return items
def _filter_out_ids_from_saved(self, item_list, attr):
items = [item for item in item_list if item['id']
not in self.saved_state_json[attr].keys()]
items = []
for item in item_list:
item_id = (item['keypair']['name'] if attr == 'keypairs'
else item['id'])
if item_id not in self.saved_state_json[attr].keys():
items.append(item)
return items
def list(self):
@ -294,16 +305,11 @@ class KeyPairService(BaseService):
keypairs = client.list_keypairs()['keypairs']
if self.prefix:
keypairs = self._filter_by_prefix(keypairs)
keypairs = self._filter_by_prefix(keypairs, 'keypair')
elif self.is_resource_list:
keypairs = [keypair for keypair in keypairs
if keypair['keypair']['name']
in self.resource_list_json['keypairs'].keys()]
keypairs = self._filter_by_resource_list(keypairs, 'keypairs')
elif not self.is_save_state:
# recreate list removing saved keypairs
keypairs = [keypair for keypair in keypairs
if keypair['keypair']['name']
not in self.saved_state_json['keypairs'].keys()]
keypairs = self._filter_out_ids_from_saved(keypairs, 'keypairs')
LOG.debug("List count, %s Keypairs", len(keypairs))
return keypairs

View File

@ -610,21 +610,14 @@ class TestKeyPairService(BaseCmdServiceTests):
self._test_prefix_opt_precedence(delete_mock)
def test_resource_list_opt_precedence(self):
delete_mock = [(self.filter_prefix, [], None),
delete_mock = [(self.filter_saved_state, [], None),
(self.filter_resource_list, [], None),
(self.filter_prefix, [], None),
(self.get_method, self.response, 200),
(self.validate_response, 'validate', None),
(self.delete_method, 'error', None),
(self.log_method, 'exception', None)]
serv = self._create_cmd_service(
self.service_class, is_resource_list=True)
_, fixtures = self.run_function_with_mocks(
serv.delete,
delete_mock
)
# Check that prefix was not used for filtering
fixtures[0].mock.assert_not_called()
self._test_resource_list_opt_precedence(delete_mock)
class TestVolumeService(BaseCmdServiceTests):