Fixes error during alarm evaluation 08/7408/3
authorBenjamin Diaz <bdiaz@whitestack.com>
Fri, 12 Apr 2019 15:53:05 +0000 (12:53 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Fri, 12 Apr 2019 16:01:19 +0000 (13:01 -0300)
Adds unit test

Change-Id: I2679210b7ce13f8ffd714183c220b617236003f8
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
osm_mon/evaluator/service.py
osm_mon/tests/unit/evaluator/test_evaluator_service.py

index 0868e32..3865b17 100644 (file)
@@ -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):
index 88e3b4d..b34c221 100644 (file)
@@ -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')