def test_send_notification_unconfigured(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.info = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}} notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("No config data for type: pagerduty", self.trap) notifications = [] notifications.append(m_notification.Notification(0, 'pagerduty', 'pagerduty notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(failed, []) self.assertEqual(len(invalid), 1) self.assertIn("attempting to send unconfigured notification: pagerduty", self.trap)
def test_send_notification_failure(self, mock_im, mock_log, mock_smtp, mock_email): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ] ) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendFailureStub mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(len(failed), 1) self.assertEqual(invalid, [])
def test_statsd(self, mock_im, mock_log, mock_smtp, mock_email): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ]) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] notifiers.send_notifications(notifications) self.assertEqual(self.statsd.timer.timer_calls['email_time_start'], 3) self.assertEqual(self.statsd.timer.timer_calls['email_time_stop'], 3) self.assertEqual(self.statsd.counter.counter, 3)
def test_send_notification_exception(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'abc' } } notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append( Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifiers.send_notifications(notifications) self.assertIn("send_notification exception for email", self.trap)
def test_send_notification_failure(self, mock_im, mock_log, mock_smtp, mock_email): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ]) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendFailureStub mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(len(failed), 1) self.assertEqual(invalid, [])
def test_statsd(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'abc' } } notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append( Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifiers.send_notifications(notifications) self.assertEqual(self.statsd.timer.timer_calls['email_time_start'], 3) self.assertEqual(self.statsd.timer.timer_calls['email_time_stop'], 3) self.assertEqual(self.statsd.counter.counter, 3)
def test_send_notification_exception(self, mock_im, mock_log, mock_smtp, mock_email): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ] ) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] notifiers.send_notifications(notifications) self.assertIn("send_notification exception for email", self.trap)
def test_send_notification_unconfigured(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.info = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' } } notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("No config data for type: pagerduty", self.trap) notifications = [] notifications.append( Notification(0, 'pagerduty', 'pagerduty notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(failed, []) self.assertEqual(len(invalid), 1) self.assertIn( "attempting to send unconfigured notification: pagerduty", self.trap)
def test_send_notification_correct(self, mock_log, mock_smtp, mock_email, mock_time): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_time.time.return_value = 42 config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'abc'}} notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append(m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append(m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append(m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(len(sent), 3) self.assertEqual(failed, []) self.assertEqual(invalid, []) for n in sent: self.assertEqual(n.notification_timestamp, 42)
def test_send_notification_failure(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendFailureStub config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'abc' } } notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append( Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(len(failed), 1) self.assertEqual(invalid, [])
def test_statsd(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub notifiers.init(self.statsd) notifiers.config() notifications = [] notifications.append( m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifiers.send_notifications(notifications) self.assertEqual(self.statsd.timer.timer_calls['email_time_start'], 3) self.assertEqual(self.statsd.timer.timer_calls['email_time_stop'], 3) self.assertEqual(self.statsd.counter.counter, 3)
def test_send_notification_correct(self, mock_log, mock_smtp, mock_email, mock_time): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_time.time.return_value = 42 notifiers.init(self.statsd) notifiers.config() notifications = [] notifications.append( m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(len(sent), 3) self.assertEqual(failed, []) self.assertEqual(invalid, []) for n in sent: self.assertEqual(n.notification_timestamp, 42)
def test_statsd(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'abc'}} notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append(m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append(m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append(m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifiers.send_notifications(notifications) self.assertEqual(self.statsd.timer.timer_calls['email_time_start'], 3) self.assertEqual(self.statsd.timer.timer_calls['email_time_stop'], 3) self.assertEqual(self.statsd.counter.counter, 3)
def test_statsd(self, mock_im, mock_log, mock_smtp, mock_email): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ] ) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] notifiers.send_notifications(notifications) self.assertEqual(self.statsd.timer.timer_calls['email_time_start'], 3) self.assertEqual(self.statsd.timer.timer_calls['email_time_stop'], 3) self.assertEqual(self.statsd.counter.counter, 3)
def test_send_notification_exception(self, mock_im, mock_log, mock_smtp, mock_email): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ]) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] notifiers.send_notifications(notifications) self.assertIn("send_notification exception for email", self.trap)
def __init__(self, config): self.statsd = monascastatsd.Client(name='monasca', dimensions=BaseProcessor.dimensions) notifiers.init(self.statsd) notifiers.load_plugins(config['notification_types']) notifiers.config(config['notification_types']) self._db_repo = get_db_repo(config) self.insert_configured_plugins()
def __init__(self): self.statsd = get_statsd_client() notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() self._db_repo = get_db_repo() self.insert_configured_plugins()
def __init__(self, config): self.statsd = get_statsd_client(config) notifiers.init(self.statsd) notifiers.load_plugins(config['notification_types']) notifiers.config(config['notification_types']) self._db_repo = get_db_repo(config) self.insert_configured_plugins() self._invalid_type_count = self.statsd.get_counter(name='invalid_type_count') self._sent_failed_count = self.statsd.get_counter(name='sent_failed_count')
def test_enabled_notifications(self, mock_log, mock_smtp): notifiers.init(self.statsd) notifiers.config() notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 3) self.assertEqual(sorted(notifications), ["EMAIL", "PAGERDUTY", "WEBHOOK"])
def test_enabled_notifications_none(self): self.conf_override(group='notification_types', enabled=[]) notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 0)
def test_plugin_load(self): config_dict = {"plugins": ["monasca_notification.plugins.hipchat_notifier:HipChatNotifier", "monasca_notification.plugins.slack_notifier:SlackNotifier"]} notifiers.init(self.statsd) notifiers.load_plugins(config_dict) self.assertEqual(len(notifiers.possible_notifiers), 5) configured_plugins = ["email", "webhook", "pagerduty", "hipchat", "slack"] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_config_missing_data(self, mock_log, mock_smtp): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.info = self.trap.append config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}} notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("No config data for type: pagerduty", self.trap)
def test_enabled_notifications(self, mock_log, mock_smtp): config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'xyz.com'}} notifiers.init(self.statsd) notifiers.config(config_dict) notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 3) self.assertEqual(sorted(notifications), ["EMAIL", "PAGERDUTY", "WEBHOOK"])
def test_invalid_plugin_load_exception_ignored(self, mock_log): mock_log.exception = self.trap.append config_dict = {"plugins": ["monasca_notification.plugins.hipchat_notifier:UnknownPlugin", "monasca_notification.plugins.slack_notifier:SlackNotifier"]} notifiers.init(self.statsd) notifiers.load_plugins(config_dict) self.assertEqual(len(notifiers.possible_notifiers), 4) self.assertEqual(len(self.trap), 1) configured_plugins = ["email", "webhook", "pagerduty", "slack"] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_enabled_notifications_none(self): self.conf_override( group='notification_types', enabled=[] ) notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 0)
def test_plugin_load(self): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.hipchat_notifier:HipChatNotifier', 'monasca_notification.plugins.slack_notifier:SlackNotifier' ]) notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() self.assertEqual(len(notifiers.possible_notifiers), 2) self.assertItemsEqual(['hipchat', 'slack'], notifiers.configured_notifiers)
def test_config_exception(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._configExceptionStub config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'abc'}} notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("config exception for email", self.trap)
def test_config_correct(self, mock_log, mock_smtp): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.info = self.trap.append config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'abc'}} notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("email notification ready", self.trap) self.assertIn("webhook notification ready", self.trap) self.assertIn("pagerduty notification ready", self.trap)
def test_plugin_load(self): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.hipchat_notifier:HipChatNotifier', 'monasca_notification.plugins.slack_notifier:SlackNotifier' ] ) notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() self.assertEqual(len(notifiers.possible_notifiers), 2) self.assertItemsEqual(['hipchat', 'slack'], notifiers.configured_notifiers)
def test_config_missing_data(self, mock_log, mock_smtp): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.info = self.trap.append config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' } } notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("No config data for type: pagerduty", self.trap)
def test_invalid_plugin_load_exception_ignored(self, mock_log): mock_log.exception = self.trap.append self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.hipchat_notifier:UnknownPlugin', 'monasca_notification.plugins.slack_notifier:SlackNotifier' ]) notifiers.init(self.statsd) notifiers.load_plugins() self.assertEqual(len(notifiers.possible_notifiers), 1) self.assertEqual(len(self.trap), 1) configured_plugins = ["email", "webhook", "pagerduty", "slack"] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_plugin_load(self): config_dict = { "plugins": [ "monasca_notification.plugins.hipchat_notifier:HipChatNotifier", "monasca_notification.plugins.slack_notifier:SlackNotifier" ] } notifiers.init(self.statsd) notifiers.load_plugins(config_dict) self.assertEqual(len(notifiers.possible_notifiers), 5) configured_plugins = [ "email", "webhook", "pagerduty", "hipchat", "slack" ] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_plugin_load(self): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.hipchat_notifier:HipChatNotifier', 'monasca_notification.plugins.slack_notifier:SlackNotifier' ]) notifiers.init(self.statsd) notifiers.load_plugins() self.assertEqual(len(notifiers.possible_notifiers), 5) configured_plugins = [ "email", "webhook", "pagerduty", "hipchat", "slack" ] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_invalid_plugin_load_exception_ignored(self, mock_log): mock_log.exception = self.trap.append config_dict = { "plugins": [ "monasca_notification.plugins.hipchat_notifier:UnknownPlugin", "monasca_notification.plugins.slack_notifier:SlackNotifier" ] } notifiers.init(self.statsd) notifiers.load_plugins(config_dict) self.assertEqual(len(notifiers.possible_notifiers), 4) self.assertEqual(len(self.trap), 1) configured_plugins = ["email", "webhook", "pagerduty", "slack"] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_invalid_plugin_load_exception_ignored(self, mock_log): mock_log.exception = self.trap.append self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.hipchat_notifier:UnknownPlugin', 'monasca_notification.plugins.slack_notifier:SlackNotifier' ] ) notifiers.init(self.statsd) notifiers.load_plugins() self.assertEqual(len(notifiers.possible_notifiers), 1) self.assertEqual(len(self.trap), 1) configured_plugins = ["email", "webhook", "pagerduty", "slack"] for plugin in notifiers.configured_notifiers: self.asssertIn(plugin.type in configured_plugins)
def test_enabled_notifications(self, mock_log, mock_smtp): config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'xyz.com' } } notifiers.init(self.statsd) notifiers.config(config_dict) notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 3) self.assertEqual(sorted(notifications), ["EMAIL", "PAGERDUTY", "WEBHOOK"])
def test_send_notification_exception(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub notifiers.init(self.statsd) notifiers.config() notifications = [] notifications.append( m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifiers.send_notifications(notifications) self.assertIn("send_notification exception for email", self.trap)
def test_send_notification_failure(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendFailureStub notifiers.init(self.statsd) notifiers.config() notifications = [] notifications.append( m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(len(failed), 1) self.assertEqual(invalid, [])
def test_enabled_notifications(self): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier', 'monasca_notification.plugins.pagerduty_notifier:PagerdutyNotifier', 'monasca_notification.plugins.webhook_notifier:WebhookNotifier', 'monasca_notification.plugins.hipchat_notifier:HipChatNotifier', 'monasca_notification.plugins.slack_notifier:SlackNotifier', 'monasca_notification.plugins.jira_notifier:JiraNotifier' ]) notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 6) self.assertItemsEqual( notifications, ['EMAIL', 'PAGERDUTY', 'WEBHOOK', 'HIPCHAT', 'SLACK', 'JIRA'])
def test_send_notification_correct(self, mock_log, mock_smtp, mock_email, mock_time): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_time.time.return_value = 42 config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'abc' } } notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append( Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifications.append( Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(len(sent), 3) self.assertEqual(failed, []) self.assertEqual(invalid, []) for n in sent: self.assertEqual(n.notification_timestamp, 42)
def test_send_notification_exception(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendExceptionStub config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'abc'}} notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append(m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) notifiers.send_notifications(notifications) self.assertIn("send_notification exception for email", self.trap)
def test_enabled_notifications(self): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier', 'monasca_notification.plugins.pagerduty_notifier:PagerdutyNotifier', 'monasca_notification.plugins.webhook_notifier:WebhookNotifier', 'monasca_notification.plugins.hipchat_notifier:HipChatNotifier', 'monasca_notification.plugins.slack_notifier:SlackNotifier', 'monasca_notification.plugins.jira_notifier:JiraNotifier' ] ) notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = notifiers.enabled_notifications() self.assertEqual(len(notifications), 6) self.assertItemsEqual(notifications, ['EMAIL', 'PAGERDUTY', 'WEBHOOK', 'HIPCHAT', 'SLACK', 'JIRA'])
def test_config_exception(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._configExceptionStub config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'abc' } } notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("config exception for email", self.trap)
def test_config_correct(self, mock_log, mock_smtp): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.info = self.trap.append config_dict = { 'email': self.email_config, 'webhook': { 'address': 'xyz.com' }, 'pagerduty': { 'address': 'abc' } } notifiers.init(self.statsd) notifiers.config(config_dict) self.assertIn("email notification ready", self.trap) self.assertIn("webhook notification ready", self.trap) self.assertIn("pagerduty notification ready", self.trap)
def test_send_notification_failure(self, mock_log, mock_smtp, mock_email): mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_log.exception = self.trap.append mock_email.EmailNotifier = self._sendFailureStub config_dict = {'email': self.email_config, 'webhook': {'address': 'xyz.com'}, 'pagerduty': {'address': 'abc'}} notifiers.init(self.statsd) notifiers.config(config_dict) notifications = [] notifications.append(m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({}))) sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(sent, []) self.assertEqual(len(failed), 1) self.assertEqual(invalid, [])
def test_send_notification_correct(self, mock_im, mock_log, mock_smtp, mock_email, mock_time): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ] ) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_time.time.return_value = 42 mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(len(sent), 3) self.assertEqual(failed, []) self.assertEqual(invalid, []) for n in sent: self.assertEqual(n.notification_timestamp, 42)
def test_send_notification_correct(self, mock_im, mock_log, mock_smtp, mock_email, mock_time): self.conf_override( group='notification_types', enabled=[ 'monasca_notification.plugins.email_notifier:EmailNotifier' ]) mock_log.warn = self.trap.append mock_log.error = self.trap.append mock_email.EmailNotifier = self._goodSendStub mock_time.time.return_value = 42 mock_im.import_class.return_value = mock_email.EmailNotifier notifiers.init(self.statsd) notifiers.load_plugins() notifiers.config() notifications = [ m_notification.Notification(0, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(1, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})), m_notification.Notification(2, 'email', 'email notification', '*****@*****.**', 0, 0, alarm({})) ] sent, failed, invalid = notifiers.send_notifications(notifications) self.assertEqual(len(sent), 3) self.assertEqual(failed, []) self.assertEqual(invalid, []) for n in sent: self.assertEqual(n.notification_timestamp, 42)
def __init__(self, config): self.statsd = monascastatsd.Client(name='monasca', dimensions=BaseProcessor.dimensions) notifiers.init(self.statsd) notifiers.config(config)