From 0cd4e14b4a4b65818a1aec1c14d84b28b946fcd5 Mon Sep 17 00:00:00 2001 From: Benjamin Diaz Date: Fri, 12 Apr 2019 12:53:05 -0300 Subject: [PATCH] Fixes error during alarm evaluation Adds unit test Change-Id: I2679210b7ce13f8ffd714183c220b617236003f8 Signed-off-by: Benjamin Diaz --- osm_mon/evaluator/service.py | 10 ++++++---- .../unit/evaluator/test_evaluator_service.py | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/osm_mon/evaluator/service.py b/osm_mon/evaluator/service.py index 0868e32..3865b17 100644 --- a/osm_mon/evaluator/service.py +++ b/osm_mon/evaluator/service.py @@ -55,15 +55,17 @@ class EvaluatorService: def _get_metric_value(self, nsr_id: str, - vnf_member_index: int, + vnf_member_index: str, vdur_name: str, metric_name: str): - return BACKENDS[self.conf.get('evaluator', 'backend')]().get_metric_value(metric_name, nsr_id, vdur_name, - vnf_member_index) + return BACKENDS[self.conf.get('evaluator', 'backend')](self.conf).get_metric_value(metric_name, + nsr_id, + vdur_name, + vnf_member_index) def _evaluate_metric(self, nsr_id: str, - vnf_member_index: int, + vnf_member_index: str, vdur_name: str, metric_name: str, alarm: Alarm): diff --git a/osm_mon/tests/unit/evaluator/test_evaluator_service.py b/osm_mon/tests/unit/evaluator/test_evaluator_service.py index 88e3b4d..b34c221 100644 --- a/osm_mon/tests/unit/evaluator/test_evaluator_service.py +++ b/osm_mon/tests/unit/evaluator/test_evaluator_service.py @@ -26,6 +26,7 @@ from osm_mon.core.common_db import CommonDbClient from osm_mon.core.config import Config from osm_mon.core.database import AlarmRepository from osm_mon.core.message_bus_client import MessageBusClient +from osm_mon.evaluator.backends.prometheus import PrometheusBackend from osm_mon.evaluator.evaluator import AlarmStatus from osm_mon.evaluator.service import EvaluatorService @@ -157,17 +158,17 @@ class EvaluatorTest(TestCase): service = EvaluatorService(self.config) service.queue = mock.Mock() - service._evaluate_metric('test_id', 1, 'test_name', 'test_metric_name', mock_alarm) + service._evaluate_metric('test_id', '1', 'test_name', 'test_metric_name', mock_alarm) service.queue.put.assert_called_with((mock_alarm, AlarmStatus.ALARM)) service.queue.reset_mock() mock_alarm.operation = 'lt' - service._evaluate_metric('test_id', 1, 'test_name', 'test_metric_name', mock_alarm) + service._evaluate_metric('test_id', '1', 'test_name', 'test_metric_name', mock_alarm) service.queue.put.assert_called_with((mock_alarm, AlarmStatus.OK)) service.queue.reset_mock() get_metric_value.return_value = None - service._evaluate_metric('test_id', 1, 'test_name', 'test_metric_name', mock_alarm) + service._evaluate_metric('test_id', '1', 'test_name', 'test_metric_name', mock_alarm) service.queue.put.assert_called_with((mock_alarm, AlarmStatus.INSUFFICIENT)) @mock.patch('multiprocessing.Process') @@ -190,3 +191,12 @@ class EvaluatorTest(TestCase): proccess.assert_called_with(target=evaluate_metric, args=( '87776f33-b67c-417a-8119-cb08e4098951', '1', 'cirros_ns-1-cirros_vnfd-VM-1', 'average_memory_utilization', mock_alarm)) + + @mock.patch.object(PrometheusBackend, "get_metric_value") + @mock.patch('osm_mon.core.database.db') + def test_get_metric_value_prometheus(self, db, get_metric_value): + self.config.set('evaluator', 'backend', 'prometheus') + evaluator = EvaluatorService(self.config) + evaluator._get_metric_value('test_id', 'test_vnf_member_index', 'test_vdur_name', 'test_metric_name') + + get_metric_value.assert_called_with('test_metric_name', 'test_id', 'test_vdur_name', 'test_vnf_member_index') -- 2.25.1