From 7a897511db36b53f715278e00c51e748eac4284e Mon Sep 17 00:00:00 2001 From: Idan Hefetz Date: Mon, 4 Dec 2017 11:55:51 +0000 Subject: [PATCH] more tempest refactoring replace the old filter by parameters method with general_utils match. remove unicode to str method. remove _get_value method. Change-Id: I6e54d7894d05ed908715f5b7a26d35b54b51beb3 --- .../tests/api/alarms/test_alarms.py | 11 +- vitrage_tempest_tests/tests/api/rca/base.py | 106 ++++++++---------- .../tests/api/rca/test_rca.py | 6 +- .../tests/api/templates/base.py | 6 +- vitrage_tempest_tests/tests/base.py | 15 --- .../tests/common/general_utils.py | 6 +- .../tests/common/neutron_utils.py | 7 +- .../tests/common/nova_utils.py | 2 +- .../tests/common/vitrage_utils.py | 4 +- .../tests/datasources/test_neutron.py | 4 +- .../tests/e2e/test_actions_base.py | 4 +- .../tests/e2e/test_basic_actions.py | 4 +- .../tests/e2e/test_overlapping_actions.py | 16 +-- 13 files changed, 80 insertions(+), 111 deletions(-) diff --git a/vitrage_tempest_tests/tests/api/alarms/test_alarms.py b/vitrage_tempest_tests/tests/api/alarms/test_alarms.py index 0429f18b2..c7544411f 100644 --- a/vitrage_tempest_tests/tests/api/alarms/test_alarms.py +++ b/vitrage_tempest_tests/tests/api/alarms/test_alarms.py @@ -16,10 +16,10 @@ import json from oslo_log import log as logging -from vitrage.common.constants import VertexProperties as VProps from vitrage.datasources.aodh import AODH_DATASOURCE from vitrage_tempest_tests.tests.api.alarms.base import BaseAlarmsTest from vitrage_tempest_tests.tests.common import aodh_utils +from vitrage_tempest_tests.tests.common import general_utils as g_utils from vitrage_tempest_tests.tests.common import nova_utils from vitrage_tempest_tests.tests.common.tempest_clients import TempestClients from vitrage_tempest_tests.tests import utils @@ -55,7 +55,7 @@ class TestAlarms(BaseAlarmsTest): 'vitrage alarm list', self.conf) self._compare_alarms_lists( api_alarms, cli_alarms, AODH_DATASOURCE, - utils.uni2str(instances[0].id)) + instances[0].id) except Exception as e: self._handle_exception(e) raise @@ -78,12 +78,11 @@ class TestAlarms(BaseAlarmsTest): cli_items = cli_alarms.splitlines() - api_by_type = self._filter_list_by_pairs_parameters( - api_alarms, [VProps.VITRAGE_TYPE], [resource_type]) + api_by_type = g_utils.all_matches( + api_alarms, vitrage_type=resource_type) cli_by_type = cli_alarms.count(' ' + resource_type + ' ') - api_by_id = self._filter_list_by_pairs_parameters( - api_alarms, ['resource_id'], [resource_id]) + api_by_id = g_utils.all_matches(api_alarms, resource_id=resource_id) cli_by_id = cli_alarms.count(resource_id) self.assertEqual(len(cli_items), len(api_alarms) + 4) diff --git a/vitrage_tempest_tests/tests/api/rca/base.py b/vitrage_tempest_tests/tests/api/rca/base.py index cabdf22bc..4fb41c67e 100644 --- a/vitrage_tempest_tests/tests/api/rca/base.py +++ b/vitrage_tempest_tests/tests/api/rca/base.py @@ -30,10 +30,10 @@ from vitrage.evaluator.actions.evaluator_event_transformer \ import VITRAGE_DATASOURCE from vitrage_tempest_tests.tests.api.alarms.base import BaseAlarmsTest from vitrage_tempest_tests.tests.common import aodh_utils +from vitrage_tempest_tests.tests.common import general_utils as g_utils from vitrage_tempest_tests.tests.common import nova_utils from vitrage_tempest_tests.tests.common.tempest_clients import TempestClients from vitrage_tempest_tests.tests.common import vitrage_utils -from vitrage_tempest_tests.tests import utils LOG = logging.getLogger(__name__) RCA_ALARM_NAME = 'rca_test_host_alarm' @@ -57,9 +57,10 @@ class BaseRcaTest(BaseAlarmsTest): list_alarms = self.vitrage_client.alarm.list(vitrage_id='all', all_tenants=True) - expected_alarm = self._filter_list_by_pairs_parameters( - list_alarms, [VProps.RESOURCE_ID, VProps.VITRAGE_TYPE], - [resource_id, AODH_DATASOURCE]) + expected_alarm = g_utils.all_matches( + list_alarms, + resource_id=resource_id, + vitrage_type=AODH_DATASOURCE) if not expected_alarm: return None return expected_alarm[0] @@ -80,14 +81,16 @@ class BaseRcaTest(BaseAlarmsTest): self.assertNotEqual(len(rca), 0, 'The rca is empty') LOG.info("The rca alarms list is : " + str(json.dumps(rca))) - resource_alarm = self._filter_list_by_pairs_parameters( - rca, [VProps.VITRAGE_TYPE, VProps.NAME], - [AODH_DATASOURCE, RCA_ALARM_NAME]) + resource_alarm = g_utils.all_matches( + rca, + vitrage_type=AODH_DATASOURCE, + name=RCA_ALARM_NAME) - deduce_alarms = self._filter_list_by_pairs_parameters( - rca, [VProps.VITRAGE_TYPE, VProps.NAME, VProps.SEVERITY], - [VITRAGE_DATASOURCE, VITRAGE_ALARM_NAME, - OperationalAlarmSeverity.WARNING]) + deduce_alarms = g_utils.all_matches( + rca, + vitrage_type=VITRAGE_DATASOURCE, + name=VITRAGE_ALARM_NAME, + severity=OperationalAlarmSeverity.WARNING) self.assertEqual(3, len(rca)) self.assertEqual(1, len(resource_alarm)) @@ -101,30 +104,26 @@ class BaseRcaTest(BaseAlarmsTest): # Find the vitrage_id of the deduced alarms using their original id. vitrage_resources = TempestClients.vitrage().resource.list( all_tenants=False) - vitrage_instance_0_id = self._filter_list_by_pairs_parameters( - vitrage_resources, [VProps.ID], - [utils.uni2str(instances[0].id)])[0] + vitrage_instance_0_id = g_utils.first_match(vitrage_resources, + id=instances[0].id) - vitrage_instance_1_id = self._filter_list_by_pairs_parameters( - vitrage_resources, [VProps.ID], - [utils.uni2str(instances[1].id)])[0] + vitrage_instance_1_id = g_utils.first_match(vitrage_resources, + id=instances[1].id) # Find the deduced alarms based on their properties - deduce_alarms_1 = self._filter_list_by_pairs_parameters( + deduce_alarms_1 = g_utils.all_matches( alarms, - [VProps.VITRAGE_TYPE, VProps.NAME, VProps.VITRAGE_RESOURCE_TYPE, - VProps.VITRAGE_RESOURCE_ID], - [VITRAGE_DATASOURCE, VITRAGE_ALARM_NAME, - NOVA_INSTANCE_DATASOURCE, - vitrage_instance_0_id[VProps.VITRAGE_ID]]) + vitrage_type=VITRAGE_DATASOURCE, + name=VITRAGE_ALARM_NAME, + vitrage_resource_type=NOVA_INSTANCE_DATASOURCE, + vitrage_resource_id=vitrage_instance_0_id[VProps.VITRAGE_ID]) - deduce_alarms_2 = self._filter_list_by_pairs_parameters( + deduce_alarms_2 = g_utils.all_matches( alarms, - [VProps.VITRAGE_TYPE, VProps.NAME, VProps.VITRAGE_RESOURCE_TYPE, - VProps.VITRAGE_RESOURCE_ID], - [VITRAGE_DATASOURCE, VITRAGE_ALARM_NAME, - NOVA_INSTANCE_DATASOURCE, - vitrage_instance_1_id[VProps.VITRAGE_ID]]) + vitrage_type=VITRAGE_DATASOURCE, + name=VITRAGE_ALARM_NAME, + vitrage_resource_type=NOVA_INSTANCE_DATASOURCE, + vitrage_resource_id=vitrage_instance_1_id[VProps.VITRAGE_ID]) self.assertEqual(3, len(alarms), "Expected 3 alarms - 1 on host and " "2 deduced") @@ -137,12 +136,10 @@ class BaseRcaTest(BaseAlarmsTest): flag = True for item in links: - source_alarm_name = self._get_value( - alarms[item['source']], VProps.NAME) - target_alarm_name = self._get_value( - alarms[item['target']], VProps.NAME) - if self._get_value(item, 'key') != EdgeLabel.CAUSES \ - or self._get_value(item, EdgeProperties.RELATIONSHIP_TYPE) != EdgeLabel.CAUSES \ + source_alarm_name = alarms[item['source']].get(VProps.NAME) + target_alarm_name = alarms[item['target']].get(VProps.NAME) + if item.get('key') != EdgeLabel.CAUSES \ + or item.get(EdgeProperties.RELATIONSHIP_TYPE) != EdgeLabel.CAUSES \ or source_alarm_name != RCA_ALARM_NAME \ or target_alarm_name != VITRAGE_ALARM_NAME: flag = False @@ -152,33 +149,26 @@ class BaseRcaTest(BaseAlarmsTest): def _validate_set_state(self, topology, instances): self.assertNotEqual(len(topology), 0, 'The topology graph is empty') - - host = self._filter_list_by_pairs_parameters( + host = g_utils.all_matches( topology, - [VProps.VITRAGE_TYPE, VProps.ID, VProps.VITRAGE_STATE, - VProps.VITRAGE_AGGREGATED_STATE], - [NOVA_HOST_DATASOURCE, - self._get_hostname(), - OperationalResourceState.ERROR, - OperationalResourceState.ERROR]) + vitrage_type=NOVA_HOST_DATASOURCE, + id=self._get_hostname(), + vitrage_state=OperationalResourceState.ERROR, + vitrage_aggregated_state=OperationalResourceState.ERROR) - vm1 = self._filter_list_by_pairs_parameters( + vm1 = g_utils.all_matches( topology, - [VProps.VITRAGE_TYPE, VProps.ID, VProps.VITRAGE_STATE, - VProps.VITRAGE_AGGREGATED_STATE], - [NOVA_INSTANCE_DATASOURCE, - utils.uni2str(instances[0].id), - OperationalResourceState.SUBOPTIMAL, - OperationalResourceState.SUBOPTIMAL]) + vitrage_type=NOVA_INSTANCE_DATASOURCE, + id=instances[0].id, + vitrage_state=OperationalResourceState.SUBOPTIMAL, + vitrage_aggregated_state=OperationalResourceState.SUBOPTIMAL) - vm2 = self._filter_list_by_pairs_parameters( + vm2 = g_utils.all_matches( topology, - [VProps.VITRAGE_TYPE, VProps.ID, VProps.VITRAGE_STATE, - VProps.VITRAGE_AGGREGATED_STATE], - [NOVA_INSTANCE_DATASOURCE, - utils.uni2str(instances[1].id), - OperationalResourceState.SUBOPTIMAL, - OperationalResourceState.SUBOPTIMAL]) + vitrage_type=NOVA_INSTANCE_DATASOURCE, + id=instances[1].id, + vitrage_state=OperationalResourceState.SUBOPTIMAL, + vitrage_aggregated_state=OperationalResourceState.SUBOPTIMAL) self.assertEqual(1, len(host)) self.assertEqual(1, len(vm1)) @@ -208,7 +198,7 @@ class BaseRcaTest(BaseAlarmsTest): def _get_hostname(self): host = vitrage_utils.get_first_host() - return self._get_value(item=host, key=VProps.ID) + return host.get(VProps.ID) @staticmethod def _clean_timestamps(alist): diff --git a/vitrage_tempest_tests/tests/api/rca/test_rca.py b/vitrage_tempest_tests/tests/api/rca/test_rca.py index 163ac9545..c4435e0bb 100644 --- a/vitrage_tempest_tests/tests/api/rca/test_rca.py +++ b/vitrage_tempest_tests/tests/api/rca/test_rca.py @@ -50,8 +50,7 @@ class TestRca(BaseRcaTest): resource_id=instances[0].id, alarm_name='instance_rca_alarm', unic=True) - vitrage_id = self._get_value( - instance_alarm, VProps.VITRAGE_ID) + vitrage_id = instance_alarm.get(VProps.VITRAGE_ID) api_rca = self.vitrage_client.rca.get(alarm_id=vitrage_id) cli_rca = utils.run_vitrage_command( 'vitrage rca show ' + vitrage_id, self.conf) @@ -82,8 +81,7 @@ class TestRca(BaseRcaTest): resource_id=self._get_hostname(), alarm_name=RCA_ALARM_NAME) api_rca = self.vitrage_client.rca.get( - alarm_id=self._get_value(host_alarm, - VProps.VITRAGE_ID), all_tenants=True) + alarm_id=host_alarm.get(VProps.VITRAGE_ID), all_tenants=True) self._validate_rca(rca=api_rca['nodes']) self._validate_relationship(links=api_rca['links'], diff --git a/vitrage_tempest_tests/tests/api/templates/base.py b/vitrage_tempest_tests/tests/api/templates/base.py index de8a6f5ec..96b34898b 100644 --- a/vitrage_tempest_tests/tests/api/templates/base.py +++ b/vitrage_tempest_tests/tests/api/templates/base.py @@ -16,6 +16,7 @@ import json from oslo_log import log as logging from vitrage_tempest_tests.tests.base import BaseVitrageTempest +from vitrage_tempest_tests.tests.common import general_utils as g_utils from vitrage_tempest_tests.tests import utils LOG = logging.getLogger(__name__) @@ -77,8 +78,9 @@ class BaseTemplateTest(BaseVitrageTempest): len(api_templates) + 4) def _validate_passed_templates_length(self, api_templates, cli_templates): - api_passes_templates = self._filter_list_by_pairs_parameters( - api_templates, ['status details'], [self.OK_MSG]) + api_passes_templates = g_utils.all_matches( + api_templates, + **{'status details': self.OK_MSG}) cli_passes_templates = cli_templates.count(' ' + self.OK_MSG + ' ') self.assertEqual(cli_passes_templates, len(api_passes_templates)) diff --git a/vitrage_tempest_tests/tests/base.py b/vitrage_tempest_tests/tests/base.py index bbd95321e..297c88948 100644 --- a/vitrage_tempest_tests/tests/base.py +++ b/vitrage_tempest_tests/tests/base.py @@ -60,21 +60,6 @@ class BaseVitrageTempest(base.BaseTestCase): cls.num_default_entities = 3 cls.num_default_edges = 2 - @staticmethod - def _filter_list_by_pairs_parameters(origin_list, - keys, values): - filtered_list = [] - for item in origin_list: - verification = 0 - for index in range(len(keys)): - if utils.uni2str(item[keys[index]]) == values[index]: - verification += 1 - else: - break - if verification == len(keys): - filtered_list.append(item) - return filtered_list - def _create_graph_from_graph_dictionary(self, api_graph): self.assertIsNotNone(api_graph) graph = NXGraph() diff --git a/vitrage_tempest_tests/tests/common/general_utils.py b/vitrage_tempest_tests/tests/common/general_utils.py index 13508448f..35d8919f3 100644 --- a/vitrage_tempest_tests/tests/common/general_utils.py +++ b/vitrage_tempest_tests/tests/common/general_utils.py @@ -14,16 +14,14 @@ import six -def get_first_match(list_of_dicts, **kwargs): +def first_match(list_of_dicts, **kwargs): subset_dict = _subset_dict(**kwargs) for d in list_of_dicts: if is_subset(subset_dict, d): return d -def get_all_matches(list_of_dicts, **kwargs): - # TODO(idan_hefetz) this method can replace the notorious - # TODO(idan_hefetz) '_filter_list_by_pairs_parameters' +def all_matches(list_of_dicts, **kwargs): subset_dict = _subset_dict(**kwargs) return [d for d in list_of_dicts if is_subset(subset_dict, d)] diff --git a/vitrage_tempest_tests/tests/common/neutron_utils.py b/vitrage_tempest_tests/tests/common/neutron_utils.py index 37981a3d8..7c36aa899 100644 --- a/vitrage_tempest_tests/tests/common/neutron_utils.py +++ b/vitrage_tempest_tests/tests/common/neutron_utils.py @@ -11,13 +11,10 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -from vitrage.common.constants import VertexProperties as VProps +from vitrage_tempest_tests.tests.common import general_utils as g_utils from vitrage_tempest_tests.tests.common.tempest_clients import TempestClients -from vitrage_tempest_tests.tests.utils import uni2str def get_public_network(): nets = TempestClients.neutron().list_networks() - return next( - (n for n in nets['networks'] if uni2str(n[VProps.NAME]) == 'public'), - None) + return g_utils.first_match(nets['networks'], name='public') diff --git a/vitrage_tempest_tests/tests/common/nova_utils.py b/vitrage_tempest_tests/tests/common/nova_utils.py index 3d0c12e12..d86b569ab 100644 --- a/vitrage_tempest_tests/tests/common/nova_utils.py +++ b/vitrage_tempest_tests/tests/common/nova_utils.py @@ -42,7 +42,7 @@ def create_instances(num_instances=1, set_public_network=False, name='vm'): def delete_all_instances(**kwargs): instances = TempestClients.nova().servers.list() - instances_to_delete = g_utils.get_all_matches(instances, **kwargs) + instances_to_delete = g_utils.all_matches(instances, **kwargs) for item in instances_to_delete: try: TempestClients.nova().servers.delete(item) diff --git a/vitrage_tempest_tests/tests/common/vitrage_utils.py b/vitrage_tempest_tests/tests/common/vitrage_utils.py index 9320e4bd2..4610799ea 100644 --- a/vitrage_tempest_tests/tests/common/vitrage_utils.py +++ b/vitrage_tempest_tests/tests/common/vitrage_utils.py @@ -39,10 +39,10 @@ def generate_fake_host_alarm(hostname, event_type, enabled=True): def get_first_host(**kwargs): hosts = TempestClients.vitrage().resource.list( NOVA_HOST_DATASOURCE, all_tenants=True) - return g_utils.get_first_match(hosts, **kwargs) + return g_utils.first_match(hosts, **kwargs) def get_first_instance(**kwargs): instances = TempestClients.vitrage().resource.list( NOVA_INSTANCE_DATASOURCE, all_tenants=True) - return g_utils.get_first_match(instances, **kwargs) + return g_utils.first_match(instances, **kwargs) diff --git a/vitrage_tempest_tests/tests/datasources/test_neutron.py b/vitrage_tempest_tests/tests/datasources/test_neutron.py index 8f8878ff5..fbc413f50 100644 --- a/vitrage_tempest_tests/tests/datasources/test_neutron.py +++ b/vitrage_tempest_tests/tests/datasources/test_neutron.py @@ -16,6 +16,7 @@ from oslo_log import log as logging from vitrage.common.constants import VertexProperties as VProps from vitrage_tempest_tests.tests.api.topology.base import BaseTopologyTest +from vitrage_tempest_tests.tests.common import general_utils as g_utils from vitrage_tempest_tests.tests.common import nova_utils from vitrage_tempest_tests.tests import utils @@ -99,6 +100,5 @@ class TestNeutron(BaseTopologyTest): def _port_to_network_edges(self, networks, ports): counter = 0 for net in networks: - counter += len(self._filter_list_by_pairs_parameters( - ports, ['network_id'], [net['id']])) + counter += len(g_utils.all_matches(ports, network_id=net['id'])) return counter diff --git a/vitrage_tempest_tests/tests/e2e/test_actions_base.py b/vitrage_tempest_tests/tests/e2e/test_actions_base.py index 706398157..415966302 100644 --- a/vitrage_tempest_tests/tests/e2e/test_actions_base.py +++ b/vitrage_tempest_tests/tests/e2e/test_actions_base.py @@ -55,7 +55,7 @@ class TestActionsBase(BaseVitrageTempest): alarms = TempestClients.vitrage().alarm.list( vitrage_id=resource_id, all_tenants=True) - deduces = g_utils.get_all_matches(alarms, **deduced_props) + deduces = g_utils.all_matches(alarms, **deduced_props) self.assertEqual( deduced_count, len(deduces), @@ -66,7 +66,7 @@ class TestActionsBase(BaseVitrageTempest): self.assertEqual(len(expected_alarms), len(rca['nodes'])) for expected_alarm in expected_alarms: self.assertIsNotNone( - g_utils.get_first_match(rca['nodes'], **expected_alarm), + g_utils.first_match(rca['nodes'], **expected_alarm), 'expected_alarm is not in the rca %s' % str(expected_alarm)) rca_inspected = rca['nodes'][rca['inspected_index']] self.assertEqual( diff --git a/vitrage_tempest_tests/tests/e2e/test_basic_actions.py b/vitrage_tempest_tests/tests/e2e/test_basic_actions.py index e0bb946e2..db6f4f55d 100644 --- a/vitrage_tempest_tests/tests/e2e/test_basic_actions.py +++ b/vitrage_tempest_tests/tests/e2e/test_basic_actions.py @@ -178,8 +178,8 @@ class TestBasicActions(TestActionsBase): all_tenants=True) self.assertEqual(True, len(alarms) >= 2, 'alarms %s' % str(alarms)) - deduced = g_utils.get_first_match(alarms, **DEDUCED_PROPS) - trigger = g_utils.get_first_match(alarms, **TRIGGER_ALARM_2_PROPS) + deduced = g_utils.first_match(alarms, **DEDUCED_PROPS) + trigger = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS) # Get Rca for the deduced rca = TempestClients.vitrage().rca.get( diff --git a/vitrage_tempest_tests/tests/e2e/test_overlapping_actions.py b/vitrage_tempest_tests/tests/e2e/test_overlapping_actions.py index 102f2ae90..435dfabaa 100644 --- a/vitrage_tempest_tests/tests/e2e/test_overlapping_actions.py +++ b/vitrage_tempest_tests/tests/e2e/test_overlapping_actions.py @@ -170,9 +170,9 @@ class TestOverlappingcActions(TestActionsBase): vitrage_id=self.orig_host.get(VProps.VITRAGE_ID), all_tenants=True) - deduced = g_utils.get_first_match(alarms, **DEDUCED_PROPS) - trigger1 = g_utils.get_first_match(alarms, **TRIGGER_ALARM_1_PROPS) - trigger2 = g_utils.get_first_match(alarms, **TRIGGER_ALARM_2_PROPS) + deduced = g_utils.first_match(alarms, **DEDUCED_PROPS) + trigger1 = g_utils.first_match(alarms, **TRIGGER_ALARM_1_PROPS) + trigger2 = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS) # Get Rca for the deduced rca = TempestClients.vitrage().rca.get( @@ -195,8 +195,8 @@ class TestOverlappingcActions(TestActionsBase): vitrage_id=self.orig_host.get(VProps.VITRAGE_ID), all_tenants=True) - deduced = g_utils.get_first_match(alarms, **DEDUCED_PROPS) - trigger2 = g_utils.get_first_match(alarms, **TRIGGER_ALARM_2_PROPS) + deduced = g_utils.first_match(alarms, **DEDUCED_PROPS) + trigger2 = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS) # Get Rca for the deduced rca = TempestClients.vitrage().rca.get( @@ -215,15 +215,15 @@ class TestOverlappingcActions(TestActionsBase): all_tenants=True) self.assertEqual( 0, - len(g_utils.get_all_matches(alarms, **TRIGGER_ALARM_1_PROPS)), + len(g_utils.all_matches(alarms, **TRIGGER_ALARM_1_PROPS)), 'trigger alarm 1 should have been removed') self.assertEqual( 0, - len(g_utils.get_all_matches(alarms, **TRIGGER_ALARM_2_PROPS)), + len(g_utils.all_matches(alarms, **TRIGGER_ALARM_2_PROPS)), 'trigger alarm 2 should have been removed') self.assertEqual( 0, - len(g_utils.get_all_matches(alarms, **DEDUCED_PROPS)), + len(g_utils.all_matches(alarms, **DEDUCED_PROPS)), 'deduced alarm should have been removed') except Exception as e: