Revert "Migrates alarms to MongoDB"
[osm/MON.git] / osm_mon / tests / unit / evaluator / test_evaluator_service.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 from unittest import TestCase, mock
24
25 from osm_mon.core.common_db import CommonDbClient
26 from osm_mon.core.config import Config
27 from osm_mon.core.database import AlarmRepository, AlarmTag
28 from osm_mon.core.message_bus_client import MessageBusClient
29 from osm_mon.evaluator.backends.prometheus import PrometheusBackend
30 from osm_mon.evaluator.evaluator import AlarmStatus
31 from osm_mon.evaluator.service import EvaluatorService
32
33 vnfr_record_mock = {
34 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
35 "ip-address": "192.168.160.2",
36 "created-time": 1535392482.0044956,
37 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
38 "vdur": [
39 {
40 "interfaces": [
41 {
42 "mac-address": "fa:16:3e:71:fd:b8",
43 "name": "eth0",
44 "ip-address": "192.168.160.2"
45 }
46 ],
47 "status": "ACTIVE",
48 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
49 "name": "cirros_ns-1-cirros_vnfd-VM-1",
50 "status-detailed": None,
51 "ip-address": "192.168.160.2",
52 "vdu-id-ref": "cirros_vnfd-VM"
53 }
54 ],
55 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
56 "vnfd-ref": "cirros_vdu_scaling_vnf",
57 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
58 "_admin": {
59 "created": 1535392482.0067868,
60 "projects_read": [
61 "admin"
62 ],
63 "modified": 1535392482.0067868,
64 "projects_write": [
65 "admin"
66 ]
67 },
68 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
69 "member-vnf-index-ref": "1",
70 "connection-point": [
71 {
72 "name": "eth0",
73 "id": None,
74 "connection-point-id": None
75 }
76 ]
77 }
78
79 vnfd_record_mock = {
80 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
81 "name": "cirros_vdu_scaling_vnf",
82 "vendor": "OSM",
83 "vdu": [
84 {
85 "name": "cirros_vnfd-VM",
86 "monitoring-param": [
87 {
88 "id": "cirros_vnfd-VM_memory_util",
89 "nfvi-metric": "average_memory_utilization"
90 }
91 ],
92 "vm-flavor": {
93 "vcpu-count": 1,
94 "memory-mb": 256,
95 "storage-gb": 2
96 },
97 "description": "cirros_vnfd-VM",
98 "count": 1,
99 "id": "cirros_vnfd-VM",
100 "interface": [
101 {
102 "name": "eth0",
103 "external-connection-point-ref": "eth0",
104 "type": "EXTERNAL",
105 "virtual-interface": {
106 "bandwidth": "0",
107 "type": "VIRTIO",
108 "vpci": "0000:00:0a.0"
109 }
110 }
111 ],
112 "image": "cirros034"
113 }
114 ],
115 "monitoring-param": [
116 {
117 "id": "cirros_vnf_memory_util",
118 "name": "cirros_vnf_memory_util",
119 "aggregation-type": "AVERAGE",
120 "vdu-monitoring-param": {
121 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
122 "vdu-ref": "cirros_vnfd-VM"
123 }
124 }
125 ],
126 "description": "Simple VNF example with a cirros and a scaling group descriptor",
127 "id": "cirros_vdu_scaling_vnf",
128 "logo": "cirros-64.png",
129 "version": "1.0",
130 "connection-point": [
131 {
132 "name": "eth0",
133 "type": "VPORT"
134 }
135 ],
136 "mgmt-interface": {
137 "cp": "eth0"
138 },
139 "short-name": "cirros_vdu_scaling_vnf",
140 "_admin": {}
141 }
142
143
144 @mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None)
145 @mock.patch.object(MessageBusClient, "__init__", lambda *args, **kwargs: None)
146 class EvaluatorTest(TestCase):
147 def setUp(self):
148 super().setUp()
149 self.config = Config()
150
151 @mock.patch.object(EvaluatorService, "_get_metric_value")
152 @mock.patch('osm_mon.core.database.db')
153 def test_evaluate_metric(self, db, get_metric_value):
154 mock_alarm = mock.Mock()
155 mock_alarm.operation = 'gt'
156 mock_alarm.threshold = 50.0
157 mock_alarm.metric = 'metric_name'
158 get_metric_value.return_value = 100.0
159
160 service = EvaluatorService(self.config)
161 service.queue = mock.Mock()
162 service._evaluate_metric(mock_alarm, {})
163 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.ALARM))
164 service.queue.reset_mock()
165
166 mock_alarm.operation = 'lt'
167 service._evaluate_metric(mock_alarm, {})
168 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.OK))
169 service.queue.reset_mock()
170
171 get_metric_value.return_value = None
172 service._evaluate_metric(mock_alarm, {})
173 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.INSUFFICIENT))
174
175 @mock.patch('multiprocessing.Process')
176 @mock.patch.object(EvaluatorService, "_evaluate_metric")
177 @mock.patch.object(CommonDbClient, "get_vnfd")
178 @mock.patch.object(CommonDbClient, "get_vnfr")
179 @mock.patch.object(AlarmRepository, "list")
180 @mock.patch('osm_mon.core.database.db')
181 def test_evaluate(self, db, alarm_list, get_vnfr, get_vnfd, evaluate_metric, process):
182 mock_alarm = mock.Mock()
183 mock_alarm.vdur_name = 'cirros_ns-1-cirros_vnfd-VM-1'
184 mock_alarm.monitoring_param = 'cirros_vnf_memory_util'
185 mock_tag = AlarmTag()
186 mock_tag.name = 'name'
187 mock_tag.value = 'value'
188 mock_alarm.tags = [mock_tag]
189 alarm_list.return_value = [mock_alarm]
190 get_vnfr.return_value = vnfr_record_mock
191 get_vnfd.return_value = vnfd_record_mock
192
193 evaluator = EvaluatorService(self.config)
194 evaluator.evaluate_alarms()
195
196 process.assert_called_with(target=evaluate_metric, args=(mock_alarm, {'name': 'value'}))
197
198 @mock.patch.object(PrometheusBackend, "get_metric_value")
199 @mock.patch('osm_mon.core.database.db')
200 def test_get_metric_value_prometheus(self, db, get_metric_value):
201 self.config.set('evaluator', 'backend', 'prometheus')
202 evaluator = EvaluatorService(self.config)
203 evaluator._get_metric_value('test', {})
204
205 get_metric_value.assert_called_with('test', {})