1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 from unittest
import TestCase
, mock
25 from osm_mon
.core
.common_db
import CommonDbClient
26 from osm_mon
.core
.config
import Config
27 from osm_mon
.core
.message_bus_client
import MessageBusClient
28 from osm_mon
.evaluator
.backends
.prometheus
import PrometheusBackend
29 from osm_mon
.evaluator
.evaluator
import AlarmStatus
30 from osm_mon
.evaluator
.service
import EvaluatorService
33 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
34 "ip-address": "192.168.160.2",
35 "created-time": 1535392482.0044956,
36 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
41 "mac-address": "fa:16:3e:71:fd:b8",
43 "ip-address": "192.168.160.2"
47 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
48 "name": "cirros_ns-1-cirros_vnfd-VM-1",
49 "status-detailed": None,
50 "ip-address": "192.168.160.2",
51 "vdu-id-ref": "cirros_vnfd-VM"
54 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
55 "vnfd-ref": "cirros_vdu_scaling_vnf",
56 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
58 "created": 1535392482.0067868,
62 "modified": 1535392482.0067868,
67 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
68 "member-vnf-index-ref": "1",
73 "connection-point-id": None
79 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
80 "name": "cirros_vdu_scaling_vnf",
84 "name": "cirros_vnfd-VM",
87 "id": "cirros_vnfd-VM_memory_util",
88 "nfvi-metric": "average_memory_utilization"
96 "description": "cirros_vnfd-VM",
98 "id": "cirros_vnfd-VM",
102 "external-connection-point-ref": "eth0",
104 "virtual-interface": {
107 "vpci": "0000:00:0a.0"
114 "monitoring-param": [
116 "id": "cirros_vnf_memory_util",
117 "name": "cirros_vnf_memory_util",
118 "aggregation-type": "AVERAGE",
119 "vdu-monitoring-param": {
120 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
121 "vdu-ref": "cirros_vnfd-VM"
125 "description": "Simple VNF example with a cirros and a scaling group descriptor",
126 "id": "cirros_vdu_scaling_vnf",
127 "logo": "cirros-64.png",
129 "connection-point": [
138 "short-name": "cirros_vdu_scaling_vnf",
143 @mock.patch
.object(CommonDbClient
, "__init__", lambda *args
, **kwargs
: None)
144 @mock.patch
.object(MessageBusClient
, "__init__", lambda *args
, **kwargs
: None)
145 class EvaluatorTest(TestCase
):
148 self
.config
= Config()
150 @mock.patch
.object(EvaluatorService
, "_get_metric_value")
151 def test_evaluate_metric(self
, get_metric_value
):
152 mock_alarm
= mock
.Mock()
153 mock_alarm
.operation
= 'gt'
154 mock_alarm
.threshold
= 50.0
155 mock_alarm
.metric
= 'metric_name'
156 get_metric_value
.return_value
= 100.0
158 service
= EvaluatorService(self
.config
)
159 service
.queue
= mock
.Mock()
160 service
._evaluate
_metric
(mock_alarm
)
161 service
.queue
.put
.assert_called_with((mock_alarm
, AlarmStatus
.ALARM
))
162 service
.queue
.reset_mock()
164 mock_alarm
.operation
= 'lt'
165 service
._evaluate
_metric
(mock_alarm
)
166 service
.queue
.put
.assert_called_with((mock_alarm
, AlarmStatus
.OK
))
167 service
.queue
.reset_mock()
169 get_metric_value
.return_value
= None
170 service
._evaluate
_metric
(mock_alarm
)
171 service
.queue
.put
.assert_called_with((mock_alarm
, AlarmStatus
.INSUFFICIENT
))
173 @mock.patch('multiprocessing.Process')
174 @mock.patch
.object(EvaluatorService
, "_evaluate_metric")
175 @mock.patch
.object(CommonDbClient
, "get_vnfd")
176 @mock.patch
.object(CommonDbClient
, "get_vnfr")
177 @mock.patch
.object(CommonDbClient
, "get_alarms")
178 def test_evaluate_alarms(self
, alarm_list
, get_vnfr
, get_vnfd
, evaluate_metric
, process
):
179 mock_alarm
= mock
.Mock()
180 mock_alarm
.vdur_name
= 'cirros_ns-1-cirros_vnfd-VM-1'
181 mock_alarm
.monitoring_param
= 'cirros_vnf_memory_util'
182 mock_alarm
.tags
= {'name': 'value'}
183 alarm_list
.return_value
= [mock_alarm
]
184 get_vnfr
.return_value
= vnfr_record_mock
185 get_vnfd
.return_value
= vnfd_record_mock
187 evaluator
= EvaluatorService(self
.config
)
188 evaluator
.evaluate_alarms()
190 process
.assert_called_with(target
=evaluate_metric
, args
=(mock_alarm
,))
192 @mock.patch
.object(PrometheusBackend
, "get_metric_value")
193 def test_get_metric_value_prometheus(self
, get_metric_value
):
194 self
.config
.set('evaluator', 'backend', 'prometheus')
195 evaluator
= EvaluatorService(self
.config
)
196 evaluator
._get
_metric
_value
('test', {})
198 get_metric_value
.assert_called_with('test', {})