monasca-notification/tests/test_notification_processor.py

156 lines
5.7 KiB
Python
Raw Normal View History

# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# 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.
"""Tests NotificationProcessor"""
import mock
import multiprocessing
import time
import unittest
from monasca_notification.notification import Notification
from monasca_notification.processors import notification_processor
class smtpStub(object):
def __init__(self, log_queue):
self.queue = log_queue
def sendmail(self, from_addr, to_addr, msg):
self.queue.put("%s %s %s" % (from_addr, to_addr, msg))
class requestsResponse(object):
def __init__(self, status):
self.status_code = status
class TestStateTracker(unittest.TestCase):
def setUp(self):
self.trap = []
self.notification_queue = multiprocessing.Queue(10)
self.sent_notification_queue = multiprocessing.Queue(10)
self.finished_queue = multiprocessing.Queue(10)
self.log_queue = multiprocessing.Queue(10)
self.email_config = {'server': 'my.smtp.server',
'port': 25,
'user': None,
'password': None,
'timeout': 60,
'from_addr': 'hpcs.mon@hp.com'}
def tearDown(self):
self.assertTrue(self.log_queue.empty())
self.assertTrue(self.sent_notification_queue.empty())
self.assertTrue(self.finished_queue.empty())
self.processor.terminate()
# ------------------------------------------------------------------------
# Test helper functions
# ------------------------------------------------------------------------
@mock.patch('monasca_notification.types.notifiers.email_notifier.smtplib')
@mock.patch('monasca_notification.processors.notification_processor.notifiers.log')
def _start_processor(self, mock_log, mock_smtp):
"""Start the processor with the proper mocks
"""
# Since the log runs in another thread I can mock it directly, instead change the methods to put to a queue
mock_log.warn = self.log_queue.put
mock_log.error = self.log_queue.put
mock_smtp.SMTP = self._smtpStub
config = {}
config["email"] = self.email_config
nprocessor = (notification_processor.
NotificationProcessor(self.notification_queue,
self.sent_notification_queue,
self.finished_queue,
config))
self.processor = multiprocessing.Process(target=nprocessor.run)
self.processor.start()
def _smtpStub(self, *arg, **kwargs):
return smtpStub(self.log_queue)
def email_setup(self, metric):
alarm_dict = {"tenantId": "0",
"alarmId": "0",
"alarmName": "test Alarm",
"oldState": "OK",
"newState": "ALARM",
"stateChangeReason": "I am alarming!",
"timestamp": time.time(),
"metrics": metric}
notification = Notification('email', 0, 1, 'email notification', 'me@here.com', alarm_dict)
self.notification_queue.put([notification])
self._start_processor()
def _logQueueToTrap(self):
while True:
try:
self.trap.append(self.log_queue.get(timeout=1))
except Exception:
return
# ------------------------------------------------------------------------
# Unit tests
# ------------------------------------------------------------------------
def test_invalid_notification(self):
"""Verify invalid notification type is rejected.
"""
alarm_dict = {"tenantId": "0", "alarmId": "0", "alarmName": "test Alarm", "oldState": "OK", "newState": "ALARM",
"stateChangeReason": "I am alarming!", "timestamp": time.time(), "metrics": "cpu_util"}
invalid_notification = Notification('invalid', 0, 1, 'test notification', 'me@here.com', alarm_dict)
self.notification_queue.put([invalid_notification])
self._start_processor()
finished = self.finished_queue.get(timeout=2)
self._logQueueToTrap()
self.assertTrue(finished == (0, 1))
self.assertIn('attempting to send unconfigured notification: invalid', self.trap)
def test_email_notification_single_host(self):
"""Email with single host
"""
metrics = []
metric_data = {'dimensions': {'hostname': 'foo1', 'service': 'bar1'}}
metrics.append(metric_data)
self.email_setup(metrics)
self._logQueueToTrap()
for msg in self.trap:
if "From: hpcs.mon@hp.com" in msg:
self.assertRegexpMatches(msg, "From: hpcs.mon@hp.com")
self.assertRegexpMatches(msg, "To: me@here.com")
self.assertRegexpMatches(msg, "Content-Type: text/plain")
self.assertRegexpMatches(msg, "Alarm .test Alarm.")
self.assertRegexpMatches(msg, "On host .foo1.")
notification_msg = self.sent_notification_queue.get(timeout=3)
self.assertNotEqual(notification_msg, None)