Notification Engine for Monasca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_notification_processor.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
  2. # Copyright 2017 Fujitsu LIMITED
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. # implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Tests NotificationProcessor"""
  17. import mock
  18. import time
  19. from monasca_notification import notification as m_notification
  20. from monasca_notification.plugins import email_notifier
  21. from monasca_notification.processors import notification_processor as np
  22. from tests import base
  23. class smtpStub(object):
  24. def __init__(self, log_queue):
  25. self.queue = log_queue
  26. def sendmail(self, from_addr, to_addr, msg):
  27. self.queue.put("%s %s %s" % (from_addr, to_addr, msg))
  28. class requestsResponse(object):
  29. def __init__(self, status):
  30. self.status_code = status
  31. class TestNotificationProcessor(base.BaseTestCase):
  32. def setUp(self):
  33. super(TestNotificationProcessor, self).setUp()
  34. self.trap = []
  35. email_notifier.register_opts(base.config.CONF)
  36. self.conf_default(group='email_notifier', server='my.smtp.server',
  37. port=25, user=None, password=None,
  38. timeout=60, from_addr='hpcs.mon@hp.com')
  39. self.conf_default(group='mysql', ssl=None, host='localhost',
  40. port='3306', user='mysql_user', db='dbname',
  41. passwd='mysql_passwd')
  42. self.conf_default(group='statsd', host='localhost', port=8125)
  43. self.conf_default(group='notification_types', enabled=[])
  44. # ------------------------------------------------------------------------
  45. # Test helper functions
  46. # ------------------------------------------------------------------------
  47. @mock.patch('pymysql.connect')
  48. @mock.patch('monasca_notification.common.utils.monascastatsd')
  49. @mock.patch('monasca_notification.types.notifiers.email_notifier.smtplib')
  50. @mock.patch('monasca_notification.processors.notification_processor.notifiers.log')
  51. def _start_processor(self, notifications, mock_log, mock_smtp, mock_statsd, mock_pymsql):
  52. """Start the processor with the proper mocks
  53. """
  54. # Since the log runs in another thread I can mock it directly,
  55. # instead change the methods to put to a queue
  56. mock_log.warn = self.trap.append
  57. mock_log.error = self.trap.append
  58. mock_smtp.SMTP = self._smtpStub
  59. np.NotificationProcessor.insert_configured_plugins = mock.Mock()
  60. processor = np.NotificationProcessor()
  61. processor.send(notifications)
  62. def _smtpStub(self, *arg, **kwargs):
  63. return smtpStub(self.trap)
  64. def email_setup(self, metric):
  65. alarm_dict = {"tenantId": "0",
  66. "alarmId": "0",
  67. "alarmName": "test Alarm",
  68. "oldState": "OK",
  69. "newState": "ALARM",
  70. "severity": "LOW",
  71. "link": "some-link",
  72. "lifecycleState": "OPEN",
  73. "stateChangeReason": "I am alarming!",
  74. "timestamp": time.time(),
  75. "metrics": metric}
  76. notification = m_notification.Notification(
  77. 0, 'email', 'email notification', 'me@here.com', 0, 0, alarm_dict)
  78. self._start_processor([notification])
  79. # ------------------------------------------------------------------------
  80. # Unit tests
  81. # ------------------------------------------------------------------------
  82. def test_invalid_notification(self):
  83. """Verify invalid notification type is rejected.
  84. """
  85. alarm_dict = {
  86. "tenantId": "0",
  87. "alarmId": "0",
  88. "alarmName": "test Alarm",
  89. "oldState": "OK",
  90. "newState": "ALARM",
  91. "stateChangeReason": "I am alarming!",
  92. "timestamp": time.time(),
  93. "metrics": "cpu_util",
  94. "severity": "LOW",
  95. "link": "http://some-place.com",
  96. "lifecycleState": "OPEN"}
  97. invalid_notification = m_notification.Notification(0, 'invalid', 'test notification',
  98. 'me@here.com', 0, 0, alarm_dict)
  99. self._start_processor([invalid_notification])
  100. self.assertIn('attempting to send unconfigured notification: invalid', self.trap)
  101. def test_email_notification_single_host(self):
  102. """Email with single host
  103. """
  104. metrics = []
  105. metric_data = {'dimensions': {'hostname': 'foo1', 'service': 'bar1'}}
  106. metrics.append(metric_data)
  107. self.email_setup(metrics)
  108. for msg in self.trap:
  109. if "From: hpcs.mon@hp.com" in msg:
  110. self.assertRegex(msg, "From: hpcs.mon@hp.com")
  111. self.assertRegex(msg, "To: me@here.com")
  112. self.assertRegex(msg, "Content-Type: text/plain")
  113. self.assertRegex(msg, "Alarm .test Alarm.")
  114. self.assertRegex(msg, "On host .foo1.")