Reformat MON to standardized format
[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.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
31
32 vnfr_record_mock = {
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",
37 "vdur": [
38 {
39 "interfaces": [
40 {
41 "mac-address": "fa:16:3e:71:fd:b8",
42 "name": "eth0",
43 "ip-address": "192.168.160.2",
44 }
45 ],
46 "status": "ACTIVE",
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",
52 }
53 ],
54 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
55 "vnfd-ref": "cirros_vdu_scaling_vnf",
56 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
57 "_admin": {
58 "created": 1535392482.0067868,
59 "projects_read": ["admin"],
60 "modified": 1535392482.0067868,
61 "projects_write": ["admin"],
62 },
63 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
64 "member-vnf-index-ref": "1",
65 "connection-point": [{"name": "eth0", "id": None, "connection-point-id": None}],
66 }
67
68 vnfd_record_mock = {
69 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
70 "id": "cirros_vdu_scaling_vnf",
71 "_admin": {},
72 "product-name": "cirros_vdu_scaling_vnf",
73 "version": "1.0",
74 "vdu": [
75 {
76 "id": "cirros_vnfd-VM",
77 "name": "cirros_vnfd-VM",
78 "int-cpd": [
79 {
80 "virtual-network-interface-requirement": [{"name": "vdu-eth0"}],
81 "id": "vdu-eth0-int",
82 }
83 ],
84 "virtual-compute-desc": "cirros_vnfd-VM-compute",
85 "virtual-storage-desc": ["cirros_vnfd-VM-storage"],
86 "sw-image-desc": "cirros034",
87 "monitoring-parameter": [
88 {
89 "id": "cirros_vnfd-VM_memory_util",
90 "name": "cirros_vnfd-VM_memory_util",
91 "performance-metric": "average_memory_utilization",
92 }
93 ],
94 }
95 ],
96 "virtual-compute-desc": [
97 {
98 "id": "cirros_vnfd-VM-compute",
99 "virtual-cpu": {"num-virtual-cpu": 1},
100 "virtual-memory": {"size": 1},
101 }
102 ],
103 "virtual-storage-desc": [{"id": "cirros_vnfd-VM-storage", "size-of-storage": 2}],
104 "sw-image-desc": [{"id": "cirros034", "name": "cirros034", "image": "cirros034"}],
105 "ext-cpd": [
106 {
107 "int-cpd": {"vdu-id": "cirros_vnfd-VM", "cpd": "vdu-eth0-int"},
108 "id": "vnf-cp0-ext",
109 }
110 ],
111 "df": [
112 {
113 "id": "default-df",
114 "vdu-profile": [{"id": "cirros_vnfd-VM"}],
115 "instantiation-level": [
116 {
117 "id": "default-instantiation-level",
118 "vdu-level": [{"vdu-id": "cirros_vnfd-VM"}],
119 }
120 ],
121 }
122 ],
123 "description": "Simple VNF example with a cirros and a scaling group descriptor",
124 "mgmt-cp": "vnf-cp0-ext",
125 }
126
127
128 @mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None)
129 @mock.patch.object(MessageBusClient, "__init__", lambda *args, **kwargs: None)
130 class EvaluatorTest(TestCase):
131 def setUp(self):
132 super().setUp()
133 self.config = Config()
134
135 @mock.patch.object(EvaluatorService, "_get_metric_value")
136 def test_evaluate_metric(self, get_metric_value):
137 mock_alarm = mock.Mock()
138 mock_alarm.operation = "gt"
139 mock_alarm.threshold = 50.0
140 mock_alarm.metric = "metric_name"
141 get_metric_value.return_value = 100.0
142
143 service = EvaluatorService(self.config)
144 service.queue = mock.Mock()
145 service._evaluate_metric(mock_alarm)
146 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.ALARM))
147 service.queue.reset_mock()
148
149 mock_alarm.operation = "lt"
150 service._evaluate_metric(mock_alarm)
151 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.OK))
152 service.queue.reset_mock()
153
154 get_metric_value.return_value = None
155 service._evaluate_metric(mock_alarm)
156 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.INSUFFICIENT))
157
158 @mock.patch("multiprocessing.Process")
159 @mock.patch.object(EvaluatorService, "_evaluate_metric")
160 @mock.patch.object(CommonDbClient, "get_vnfd")
161 @mock.patch.object(CommonDbClient, "get_vnfr")
162 @mock.patch.object(CommonDbClient, "get_alarms")
163 def test_evaluate_alarms(
164 self, alarm_list, get_vnfr, get_vnfd, evaluate_metric, process
165 ):
166 mock_alarm = mock.Mock()
167 mock_alarm.vdur_name = "cirros_ns-1-cirros_vnfd-VM-1"
168 mock_alarm.monitoring_param = "cirros_vnf_memory_util"
169 mock_alarm.tags = {"name": "value"}
170 alarm_list.return_value = [mock_alarm]
171 get_vnfr.return_value = vnfr_record_mock
172 get_vnfd.return_value = vnfd_record_mock
173
174 evaluator = EvaluatorService(self.config)
175 evaluator.evaluate_alarms()
176
177 process.assert_called_with(target=evaluate_metric, args=(mock_alarm,))
178
179 @mock.patch.object(PrometheusBackend, "get_metric_value")
180 def test_get_metric_value_prometheus(self, get_metric_value):
181 self.config.set("evaluator", "backend", "prometheus")
182 evaluator = EvaluatorService(self.config)
183 evaluator._get_metric_value("test", {})
184
185 get_metric_value.assert_called_with("test", {})