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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
25 from unittest
import TestCase
, mock
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
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
):
40 self
.config
= Config()
41 self
.loop
= asyncio
.new_event_loop()
42 self
.payload
= {"notify_details": {"alarm_number": 0}}
43 self
.headers
= {"content-type": "application/json"}
45 @mock.patch
.object(VnfAlarmRepository
, "get")
46 @mock.patch("requests.post")
47 @mock.patch("osm_policy_module.core.database.db")
48 def test_handle_alarm_suppression(self
, database
, requests_post
, get_alarm
):
49 alert_timeout
= int(self
.config
.get("alert", "timeout"))
50 mock_alarm
= self
._build
_mock
_alarm
("test_id", last_action
="ok")
51 get_alarm
.return_value
= mock_alarm
52 service
= AlarmingService(self
.config
)
53 if bool(self
.config
.get("alert", "enhanced_alarms")):
54 self
.loop
.run_until_complete(
55 service
.handle_alarm("test_id", "alarm", self
.payload
)
57 requests_post
.assert_called_once_with(
58 url
="http://alarm-url/",
59 data
='{"notify_details": {"alarm_number": 1}}',
60 headers
={"content-type": "application/json"},
62 timeout
=alert_timeout
,
65 self
.loop
.run_until_complete(service
.handle_alarm("test_id", "alarm", {}))
66 requests_post
.assert_called_once_with(
67 json
="{}", url
="http://alarm-url/", timeout
=alert_timeout
70 @mock.patch
.object(VnfAlarmRepository
, "get")
71 @mock.patch("requests.post")
72 @mock.patch("osm_policy_module.core.database.db")
73 def test_handle_ok_suppression(self
, database
, requests_post
, get_alarm
):
74 alert_timeout
= int(self
.config
.get("alert", "timeout"))
75 mock_alarm
= self
._build
_mock
_alarm
("test_id", last_action
="alarm")
76 get_alarm
.return_value
= mock_alarm
77 service
= AlarmingService(self
.config
)
78 if bool(self
.config
.get("alert", "enhanced_alarms")):
79 self
.loop
.run_until_complete(
80 service
.handle_alarm("test_id", "ok", self
.payload
)
82 requests_post
.assert_called_once_with(
84 data
='{"notify_details": {"alarm_number": 0}}',
85 headers
={"content-type": "application/json"},
87 timeout
=alert_timeout
,
90 self
.loop
.run_until_complete(service
.handle_alarm("test_id", "ok", {}))
91 requests_post
.assert_called_once_with(
92 json
="{}", url
="http://ok-url/", timeout
=alert_timeout
95 @mock.patch
.object(VnfAlarmRepository
, "get")
96 @mock.patch("requests.post")
97 @mock.patch("osm_policy_module.core.database.db")
98 def test_handle_insufficientalarm(self
, database
, requests_post
, get_alarm
):
99 alert_timeout
= int(self
.config
.get("alert", "timeout"))
100 mock_alarm
= self
._build
_mock
_alarm
("test_id")
101 get_alarm
.return_value
= mock_alarm
102 service
= AlarmingService(self
.config
)
103 if bool(self
.config
.get("alert", "enhanced_alarms")):
104 self
.loop
.run_until_complete(
105 service
.handle_alarm("test_id", "insufficient-data", self
.payload
)
107 requests_post
.assert_called_once_with(
108 url
="http://insufficient-data-url/",
109 data
='{"notify_details": {"alarm_number": 0}}',
110 headers
={"content-type": "application/json"},
112 timeout
=alert_timeout
,
115 self
.loop
.run_until_complete(
116 service
.handle_alarm("test_id", "insufficient-data", {})
118 requests_post
.assert_called_once_with(
119 json
="{}", url
="http://insufficient-data-url/", timeout
=alert_timeout
122 @mock.patch
.object(VnfAlarmRepository
, "get")
123 @mock.patch("requests.post")
124 @mock.patch("osm_policy_module.core.database.db")
125 def test_handle_alarm_unknown_status(self
, database
, requests_post
, get_alarm
):
126 mock_alarm
= self
._build
_mock
_alarm
("test_id")
127 get_alarm
.return_value
= mock_alarm
128 service
= AlarmingService(self
.config
)
129 self
.loop
.run_until_complete(service
.handle_alarm("test_id", "unknown", {}))
130 requests_post
.assert_not_called()
132 def _build_mock_alarm(
135 alarm_url
="http://alarm-url/",
136 insufficient_data_url
="http://insufficient-data-url/",
137 ok_url
="http://ok-url/",
139 last_action
="insufficient-data",
141 mock_alarm
= mock
.Mock()
142 mock_alarm
.alarm_id
= alarm_id
143 insufficient_data_action
= mock
.Mock()
144 insufficient_data_action
.type = "insufficient-data"
145 insufficient_data_action
.url
= insufficient_data_url
146 alarm_action
= mock
.Mock()
147 alarm_action
.type = "alarm"
148 alarm_action
.url
= alarm_url
149 ok_action
= mock
.Mock()
150 ok_action
.type = "ok"
151 ok_action
.url
= ok_url
152 mock_alarm
.ok_ack
= False
153 mock_alarm
.alarm_ack
= False
154 mock_alarm
.id_suffix
= id_suffix
155 mock_alarm
.last_action
= last_action
156 mock_alarm
.actions
= [insufficient_data_action
, alarm_action
, ok_action
]