Coverage for osm_mon/tests/unit/evaluator/test_evaluator_service.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-06 19:04 +0000

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 

24from unittest import TestCase, mock 

25 

26from osm_mon.core.common_db import CommonDbClient 

27from osm_mon.core.config import Config 

28from osm_mon.core.message_bus_client import MessageBusClient 

29from osm_mon.evaluator.backends.prometheus import PrometheusBackend 

30from osm_mon.evaluator.evaluator import AlarmStatus 

31from osm_mon.evaluator.service import EvaluatorService 

32 

33vnfr_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": ["admin"], 

61 "modified": 1535392482.0067868, 

62 "projects_write": ["admin"], 

63 }, 

64 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951", 

65 "member-vnf-index-ref": "1", 

66 "connection-point": [{"name": "eth0", "id": None, "connection-point-id": None}], 

67} 

68 

69vnfd_record_mock = { 

70 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0", 

71 "id": "cirros_vdu_scaling_vnf", 

72 "_admin": {}, 

73 "product-name": "cirros_vdu_scaling_vnf", 

74 "version": "1.0", 

75 "vdu": [ 

76 { 

77 "id": "cirros_vnfd-VM", 

78 "name": "cirros_vnfd-VM", 

79 "int-cpd": [ 

80 { 

81 "virtual-network-interface-requirement": [{"name": "vdu-eth0"}], 

82 "id": "vdu-eth0-int", 

83 } 

84 ], 

85 "virtual-compute-desc": "cirros_vnfd-VM-compute", 

86 "virtual-storage-desc": ["cirros_vnfd-VM-storage"], 

87 "sw-image-desc": "cirros034", 

88 "monitoring-parameter": [ 

89 { 

90 "id": "cirros_vnfd-VM_memory_util", 

91 "name": "cirros_vnfd-VM_memory_util", 

92 "performance-metric": "average_memory_utilization", 

93 } 

94 ], 

95 } 

96 ], 

97 "virtual-compute-desc": [ 

98 { 

99 "id": "cirros_vnfd-VM-compute", 

100 "virtual-cpu": {"num-virtual-cpu": 1}, 

101 "virtual-memory": {"size": 1}, 

102 } 

103 ], 

104 "virtual-storage-desc": [{"id": "cirros_vnfd-VM-storage", "size-of-storage": 2}], 

105 "sw-image-desc": [{"id": "cirros034", "name": "cirros034", "image": "cirros034"}], 

106 "ext-cpd": [ 

107 { 

108 "int-cpd": {"vdu-id": "cirros_vnfd-VM", "cpd": "vdu-eth0-int"}, 

109 "id": "vnf-cp0-ext", 

110 } 

111 ], 

112 "df": [ 

113 { 

114 "id": "default-df", 

115 "vdu-profile": [{"id": "cirros_vnfd-VM"}], 

116 "instantiation-level": [ 

117 { 

118 "id": "default-instantiation-level", 

119 "vdu-level": [{"vdu-id": "cirros_vnfd-VM"}], 

120 } 

121 ], 

122 } 

123 ], 

124 "description": "Simple VNF example with a cirros and a scaling group descriptor", 

125 "mgmt-cp": "vnf-cp0-ext", 

126} 

127 

128 

129@mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None) 

130@mock.patch.object(MessageBusClient, "__init__", lambda *args, **kwargs: None) 

131class EvaluatorTest(TestCase): 

132 def setUp(self): 

133 super().setUp() 

134 self.config = Config() 

135 

136 @mock.patch.object(EvaluatorService, "_get_metric_data") 

137 def test_evaluate_metric(self, get_metric_data): 

138 mock_alarm = mock.Mock() 

139 mock_alarm.operation = "gt" 

140 mock_alarm.threshold = 50.0 

141 mock_alarm.metric = "metric_name" 

142 get_metric_data.return_value = [ 

143 { 

144 "labels": { 

145 "vdu_name": "cirros_vnfd-VM", 

146 "ns_id": "87776f33-b67c-417a-8119-cb08e4098951", 

147 "vnf_member_index": "1", 

148 "extra_label": "run_time_added_label", 

149 }, 

150 "value": 100.0, 

151 } 

152 ] 

153 

154 service = EvaluatorService(self.config) 

155 service.queue = mock.Mock() 

156 service._evaluate_metric(mock_alarm) 

157 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.ALARM)) 

158 service.queue.reset_mock() 

159 

160 mock_alarm.operation = "lt" 

161 service._evaluate_metric(mock_alarm) 

162 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.OK)) 

163 service.queue.reset_mock() 

164 

165 get_metric_data.return_value = None 

166 service._evaluate_metric(mock_alarm) 

167 service.queue.put.assert_called_with((mock_alarm, AlarmStatus.INSUFFICIENT)) 

168 

169 @mock.patch("multiprocessing.Process") 

170 @mock.patch.object(EvaluatorService, "_evaluate_metric") 

171 @mock.patch.object(CommonDbClient, "get_vnfd") 

172 @mock.patch.object(CommonDbClient, "get_vnfr") 

173 @mock.patch.object(CommonDbClient, "get_alarms") 

174 def test_evaluate_alarms( 

175 self, alarm_list, get_vnfr, get_vnfd, evaluate_metric, process 

176 ): 

177 mock_alarm = mock.Mock() 

178 mock_alarm.vdur_name = "cirros_ns-1-cirros_vnfd-VM-1" 

179 mock_alarm.monitoring_param = "cirros_vnf_memory_util" 

180 mock_alarm.tags = {"name": "value"} 

181 alarm_list.return_value = [mock_alarm] 

182 get_vnfr.return_value = vnfr_record_mock 

183 get_vnfd.return_value = vnfd_record_mock 

184 

185 evaluator = EvaluatorService(self.config) 

186 evaluator.evaluate_alarms() 

187 

188 process.assert_called_with(target=evaluate_metric, args=(mock_alarm,)) 

189 

190 @mock.patch.object(PrometheusBackend, "get_metric_data") 

191 def test_get_metric_data_prometheus(self, get_metric_data): 

192 self.config.set("evaluator", "backend", "prometheus") 

193 evaluator = EvaluatorService(self.config) 

194 evaluator._get_metric_data("test", {}) 

195 

196 get_metric_data.assert_called_with("test", {})