Updates for Python 3.10 and Ubuntu 22.04
[osm/POL.git] / osm_policy_module / tests / unit / alarming / test_alarming_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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import asyncio
25 from unittest import TestCase, mock
26
27 from osm_policy_module.alarming.service import AlarmingService
28 from osm_policy_module.common.common_db_client import CommonDbClient
29 from osm_policy_module.common.lcm_client import LcmClient
30 from osm_policy_module.common.mon_client import MonClient
31 from osm_policy_module.core.config import Config
32 from osm_policy_module.core.database import VnfAlarmRepository
33
34
35 @mock.patch.object(LcmClient, "__init__", lambda *args, **kwargs: None)
36 @mock.patch.object(MonClient, "__init__", lambda *args, **kwargs: None)
37 @mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None)
38 class TestAlarmingService(TestCase):
39 def setUp(self):
40 self.config = Config()
41 self.payload = {"notify_details": {"alarm_number": 0}}
42 self.headers = {"content-type": "application/json"}
43
44 @mock.patch.object(VnfAlarmRepository, "get")
45 @mock.patch("requests.post")
46 @mock.patch("osm_policy_module.core.database.db")
47 def test_handle_alarm_suppression(self, database, requests_post, get_alarm):
48 alert_timeout = int(self.config.get("alert", "timeout"))
49 mock_alarm = self._build_mock_alarm("test_id", last_action="ok")
50 get_alarm.return_value = mock_alarm
51 service = AlarmingService(self.config)
52 if bool(self.config.get("alert", "enhanced_alarms")):
53 asyncio.run(service.handle_alarm("test_id", "alarm", self.payload))
54 requests_post.assert_called_once_with(
55 url="http://alarm-url/",
56 data='{"notify_details": {"alarm_number": 1}}',
57 headers={"content-type": "application/json"},
58 verify=False,
59 timeout=alert_timeout,
60 )
61 else:
62 asyncio.run(service.handle_alarm("test_id", "alarm", {}))
63 requests_post.assert_called_once_with(
64 json="{}", url="http://alarm-url/", timeout=alert_timeout
65 )
66
67 @mock.patch.object(VnfAlarmRepository, "get")
68 @mock.patch("requests.post")
69 @mock.patch("osm_policy_module.core.database.db")
70 def test_handle_ok_suppression(self, database, requests_post, get_alarm):
71 alert_timeout = int(self.config.get("alert", "timeout"))
72 mock_alarm = self._build_mock_alarm("test_id", last_action="alarm")
73 get_alarm.return_value = mock_alarm
74 service = AlarmingService(self.config)
75 if bool(self.config.get("alert", "enhanced_alarms")):
76 asyncio.run(service.handle_alarm("test_id", "ok", self.payload))
77 requests_post.assert_called_once_with(
78 url="http://ok-url/",
79 data='{"notify_details": {"alarm_number": 0}}',
80 headers={"content-type": "application/json"},
81 verify=False,
82 timeout=alert_timeout,
83 )
84 else:
85 asyncio.run(service.handle_alarm("test_id", "ok", {}))
86 requests_post.assert_called_once_with(
87 json="{}", url="http://ok-url/", timeout=alert_timeout
88 )
89
90 @mock.patch.object(VnfAlarmRepository, "get")
91 @mock.patch("requests.post")
92 @mock.patch("osm_policy_module.core.database.db")
93 def test_handle_insufficientalarm(self, database, requests_post, get_alarm):
94 alert_timeout = int(self.config.get("alert", "timeout"))
95 mock_alarm = self._build_mock_alarm("test_id")
96 get_alarm.return_value = mock_alarm
97 service = AlarmingService(self.config)
98 if bool(self.config.get("alert", "enhanced_alarms")):
99 asyncio.run(
100 service.handle_alarm("test_id", "insufficient-data", self.payload)
101 )
102 requests_post.assert_called_once_with(
103 url="http://insufficient-data-url/",
104 data='{"notify_details": {"alarm_number": 0}}',
105 headers={"content-type": "application/json"},
106 verify=False,
107 timeout=alert_timeout,
108 )
109 else:
110 asyncio.run(service.handle_alarm("test_id", "insufficient-data", {}))
111 requests_post.assert_called_once_with(
112 json="{}", url="http://insufficient-data-url/", timeout=alert_timeout
113 )
114
115 @mock.patch.object(VnfAlarmRepository, "get")
116 @mock.patch("requests.post")
117 @mock.patch("osm_policy_module.core.database.db")
118 def test_handle_alarm_unknown_status(self, database, requests_post, get_alarm):
119 mock_alarm = self._build_mock_alarm("test_id")
120 get_alarm.return_value = mock_alarm
121 service = AlarmingService(self.config)
122 asyncio.run(service.handle_alarm("test_id", "unknown", {}))
123 requests_post.assert_not_called()
124
125 def _build_mock_alarm(
126 self,
127 alarm_id="test_id",
128 alarm_url="http://alarm-url/",
129 insufficient_data_url="http://insufficient-data-url/",
130 ok_url="http://ok-url/",
131 id_suffix=0,
132 last_action="insufficient-data",
133 ):
134 mock_alarm = mock.Mock()
135 mock_alarm.alarm_id = alarm_id
136 insufficient_data_action = mock.Mock()
137 insufficient_data_action.type = "insufficient-data"
138 insufficient_data_action.url = insufficient_data_url
139 alarm_action = mock.Mock()
140 alarm_action.type = "alarm"
141 alarm_action.url = alarm_url
142 ok_action = mock.Mock()
143 ok_action.type = "ok"
144 ok_action.url = ok_url
145 mock_alarm.ok_ack = False
146 mock_alarm.alarm_ack = False
147 mock_alarm.id_suffix = id_suffix
148 mock_alarm.last_action = last_action
149 mock_alarm.actions = [insufficient_data_action, alarm_action, ok_action]
150 return mock_alarm