2016-08-08 09:12:21 -06:00
|
|
|
# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
|
2014-05-01 12:27:06 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-03-21 10:04:51 -06:00
|
|
|
"""Tests the AlarmProcessor"""
|
|
|
|
|
|
|
|
import collections
|
|
|
|
import json
|
|
|
|
import mock
|
2014-03-21 12:23:12 -06:00
|
|
|
import time
|
2014-03-21 10:04:51 -06:00
|
|
|
import unittest
|
|
|
|
|
2014-07-16 13:51:48 -06:00
|
|
|
from monasca_notification.notification import Notification
|
|
|
|
from monasca_notification.processors import alarm_processor
|
2014-03-21 10:04:51 -06:00
|
|
|
|
|
|
|
alarm_tuple = collections.namedtuple('alarm_tuple', ['offset', 'message'])
|
2014-03-21 12:23:12 -06:00
|
|
|
message_tuple = collections.namedtuple('message_tuple', ['value'])
|
|
|
|
|
2014-03-21 10:04:51 -06:00
|
|
|
|
|
|
|
class TestAlarmProcessor(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2015-01-28 15:34:52 -07:00
|
|
|
self.trap = []
|
2014-03-21 10:04:51 -06:00
|
|
|
|
2014-03-21 12:23:12 -06:00
|
|
|
def _create_raw_alarm(self, partition, offset, message):
|
|
|
|
"""Create a raw alarm, with the given message dictionary.
|
|
|
|
"""
|
|
|
|
json_msg = json.dumps({'alarm-transitioned': message})
|
|
|
|
msg_tuple = message_tuple(json_msg)
|
|
|
|
return [partition, alarm_tuple(offset, msg_tuple)]
|
|
|
|
|
2015-10-06 08:56:11 +02:00
|
|
|
@mock.patch('pymysql.connect')
|
2014-07-16 13:51:48 -06:00
|
|
|
@mock.patch('monasca_notification.processors.alarm_processor.log')
|
2015-01-28 15:34:52 -07:00
|
|
|
def _run_alarm_processor(self, alarm, sql_response, mock_log, mock_mysql):
|
2014-03-21 14:21:37 -06:00
|
|
|
"""Runs a mocked alarm processor reading from queue while running, returns (queue_message, log_message)
|
2014-03-21 12:23:12 -06:00
|
|
|
"""
|
|
|
|
# Since the log runs in another thread I can mock it directly, instead change the methods to put to a queue
|
2015-01-28 15:34:52 -07:00
|
|
|
mock_log.warn = self.trap.append
|
|
|
|
mock_log.error = self.trap.append
|
|
|
|
mock_log.exception = self.trap.append
|
2014-03-21 12:23:12 -06:00
|
|
|
|
|
|
|
# Setup the sql response
|
|
|
|
if sql_response is not None:
|
|
|
|
mock_mysql.return_value = mock_mysql
|
|
|
|
mock_mysql.cursor.return_value = mock_mysql
|
|
|
|
mock_mysql.__iter__.return_value = sql_response
|
|
|
|
|
2015-08-04 22:34:57 +02:00
|
|
|
mysql_config = {'mysql': {'ssl': None,
|
|
|
|
'host': 'mysql_host',
|
2016-02-24 09:45:51 -07:00
|
|
|
'port': 'mysql_port',
|
2015-08-04 22:34:57 +02:00
|
|
|
'user': 'mysql_user',
|
|
|
|
'db': 'dbname',
|
|
|
|
'passwd': 'mysql_passwd'}}
|
|
|
|
|
|
|
|
processor = alarm_processor.AlarmProcessor(600, mysql_config)
|
2014-03-21 10:04:51 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
return processor.to_notification(alarm)
|
2014-03-21 10:04:51 -06:00
|
|
|
|
2014-03-21 12:23:12 -06:00
|
|
|
def test_invalid_alarm(self):
|
2014-03-21 17:13:06 -06:00
|
|
|
"""Invalid Alarms, should log and error and push to the finished queue."""
|
2015-01-28 15:34:52 -07:00
|
|
|
alarm = self._create_raw_alarm(0, 1, {'invalid': 'invalid_alarm'})
|
|
|
|
notifications, partition, offset = self._run_alarm_processor(alarm, None)
|
|
|
|
self.assertEqual(notifications, [])
|
|
|
|
self.assertEqual(partition, 0)
|
|
|
|
self.assertEqual(offset, 1)
|
|
|
|
|
|
|
|
invalid_msg = ('Invalid Alarm format skipping partition 0, offset 1\n'
|
|
|
|
'ErrorAlarm data missing field actionsEnabled')
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
self.assertIn(invalid_msg, self.trap)
|
2014-03-21 10:04:51 -06:00
|
|
|
|
2014-03-21 12:23:12 -06:00
|
|
|
def test_old_timestamp(self):
|
2014-03-21 17:13:06 -06:00
|
|
|
"""Should cause the alarm_ttl to fire log a warning and push to finished queue."""
|
2015-06-29 10:41:49 +02:00
|
|
|
timestamp = 1375346830042
|
2014-09-30 11:55:18 -06:00
|
|
|
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
|
|
|
|
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
|
2016-01-26 13:56:10 -07:00
|
|
|
"timestamp": timestamp, "actionsEnabled": 1, "metrics": "cpu_util",
|
|
|
|
"severity": "LOW", "link": "http://some-place.com", "lifecycleState": "OPEN"}
|
2015-01-28 15:34:52 -07:00
|
|
|
alarm = self._create_raw_alarm(0, 2, alarm_dict)
|
2015-06-29 10:41:49 +02:00
|
|
|
expected_datetime = time.ctime(timestamp / 1000)
|
2015-01-28 15:34:52 -07:00
|
|
|
|
|
|
|
notifications, partition, offset = self._run_alarm_processor(alarm, None)
|
|
|
|
|
|
|
|
self.assertEqual(notifications, [])
|
|
|
|
self.assertEqual(partition, 0)
|
|
|
|
self.assertEqual(offset, 2)
|
|
|
|
|
|
|
|
old_msg = ('Received alarm older than the ttl, skipping. '
|
2015-06-29 10:41:49 +02:00
|
|
|
'Alarm from {datetime}'.format(datetime=expected_datetime))
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
self.assertIn(old_msg, self.trap)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
|
|
|
def test_no_notifications(self):
|
|
|
|
"""Test an alarm with no defined notifications
|
|
|
|
"""
|
2014-09-30 11:55:18 -06:00
|
|
|
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
|
|
|
|
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
|
2016-01-26 13:56:10 -07:00
|
|
|
"timestamp": time.time() * 1000, "actionsEnabled": 1, "metrics": "cpu_util",
|
|
|
|
"severity": "LOW", "link": "http://some-place.com", "lifecycleState": "OPEN"}
|
2015-01-28 15:34:52 -07:00
|
|
|
alarm = self._create_raw_alarm(0, 3, alarm_dict)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
notifications, partition, offset = self._run_alarm_processor(alarm, None)
|
|
|
|
|
|
|
|
self.assertEqual(notifications, [])
|
|
|
|
self.assertEqual(partition, 0)
|
|
|
|
self.assertEqual(offset, 3)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
|
|
|
def test_valid_notification(self):
|
|
|
|
"""Test a valid notification, being put onto the notification_queue
|
|
|
|
"""
|
2014-09-30 11:55:18 -06:00
|
|
|
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
|
|
|
|
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
|
2016-01-26 13:56:10 -07:00
|
|
|
"timestamp": time.time() * 1000, "actionsEnabled": 1, "metrics": "cpu_util",
|
|
|
|
"severity": "LOW", "link": "http://some-place.com", "lifecycleState": "OPEN"}
|
2015-01-28 15:34:52 -07:00
|
|
|
alarm = self._create_raw_alarm(0, 4, alarm_dict)
|
|
|
|
|
2016-08-08 09:12:21 -06:00
|
|
|
sql_response = [[1, 'EMAIL', 'test notification', 'me@here.com', 0]]
|
2015-01-28 15:34:52 -07:00
|
|
|
notifications, partition, offset = self._run_alarm_processor(alarm, sql_response)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2016-08-08 09:12:21 -06:00
|
|
|
test_notification = Notification(1, 'email', 'test notification', 'me@here.com', 0, 0, alarm_dict)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
self.assertEqual(notifications, [test_notification])
|
|
|
|
self.assertEqual(partition, 0)
|
|
|
|
self.assertEqual(offset, 4)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
|
|
|
def test_two_valid_notifications(self):
|
2014-09-30 11:55:18 -06:00
|
|
|
alarm_dict = {"tenantId": "0", "alarmDefinitionId": "0", "alarmId": "1", "alarmName": "test Alarm",
|
|
|
|
"oldState": "OK", "newState": "ALARM", "stateChangeReason": "I am alarming!",
|
2016-01-26 13:56:10 -07:00
|
|
|
"timestamp": time.time() * 1000, "actionsEnabled": 1, "metrics": "cpu_util",
|
|
|
|
"severity": "LOW", "link": "http://some-place.com", "lifecycleState": "OPEN"}
|
2015-01-28 15:34:52 -07:00
|
|
|
|
|
|
|
alarm = self._create_raw_alarm(0, 5, alarm_dict)
|
|
|
|
|
2016-08-08 09:12:21 -06:00
|
|
|
sql_response = [[1, 'EMAIL', 'test notification', 'me@here.com', 0],
|
|
|
|
[2, 'EMAIL', 'test notification2', 'me@here.com', 0]]
|
2015-01-28 15:34:52 -07:00
|
|
|
notifications, partition, offset = self._run_alarm_processor(alarm, sql_response)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2016-08-08 09:12:21 -06:00
|
|
|
test_notification = Notification(1, 'email', 'test notification', 'me@here.com', 0, 0, alarm_dict)
|
|
|
|
test_notification2 = Notification(2, 'email', 'test notification2', 'me@here.com', 0, 0, alarm_dict)
|
2014-03-21 12:23:12 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
self.assertEqual(notifications, [test_notification, test_notification2])
|
|
|
|
self.assertEqual(partition, 0)
|
|
|
|
self.assertEqual(offset, 5)
|