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
diff --git a/tempest/cmd/cleanup_service.py b/tempest/cmd/cleanup_service.py
index b202940..db4407d 100644
--- a/tempest/cmd/cleanup_service.py
+++ b/tempest/cmd/cleanup_service.py
@@ -115,21 +115,32 @@
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 @@
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
diff --git a/tempest/tests/cmd/test_cleanup_services.py b/tempest/tests/cmd/test_cleanup_services.py
index 2557145..7f8db9f 100644
--- a/tempest/tests/cmd/test_cleanup_services.py
+++ b/tempest/tests/cmd/test_cleanup_services.py
@@ -610,21 +610,14 @@
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):