def test_send_notifications_successfully(self): notification_driver = [ 'essential.notifier.no_op_notifier' ] self.config(notification_driver=notification_driver) notifier_api.notify('contextarg', 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3)) self.assertEqual(self.notify_count, 1) self.assertEqual(self.exception_count, 0)
def test_send_notification(self): self.notify_called = False def mock_notify(cls, *args): self.notify_called = True self.stubs.Set(no_op_notifier, 'notify', mock_notify) notifier_api.notify(ctxt, 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3)) self.assertEqual(self.notify_called, True)
def process_request(self, request): request.environ['HTTP_X_SERVICE_NAME'] = \ self.service_name or request.host payload = { 'request': self.environ_to_dict(request.environ), } api.notify(context.get_admin_context(), api.publisher_id(os.path.basename(sys.argv[0])), 'http.request', api.INFO, payload)
def test_when_driver_fails_to_import(self): notification_driver = [ 'essential.notifier.no_op_notifier', 'essential.notifier.logo_notifier', 'fdsjgsdfhjkhgsfkj' ] self.config(notification_driver=notification_driver) notifier_api.notify('contextarg', 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3)) self.assertEqual(self.exception_count, 2) self.assertEqual(self.notify_count, 1)
def _test_rpc_notify(self, driver, envelope=False): self.stubs.Set(self.CONF, 'notification_driver', [driver]) self.mock_notify = False self.envelope = False def mock_notify(cls, *args, **kwargs): self.mock_notify = True self.envelope = kwargs.get('envelope', False) self.stubs.Set(rpc, 'notify', mock_notify) notifier_api.notify(ctxt, 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3)) self.assertEqual(self.mock_notify, True) self.assertEqual(self.envelope, envelope)
def test_rpc_priority_queue(self): self.CONF.import_opt('notification_topics', 'essential.notifier.rpc_notifier') self.stubs.Set(self.CONF, 'notification_driver', ['essential.notifier.rpc_notifier']) self.stubs.Set(self.CONF, 'notification_topics', ['testnotify', ]) self.test_topic = None def mock_notify(context, topic, msg): self.test_topic = topic self.stubs.Set(rpc, 'notify', mock_notify) notifier_api.notify(ctxt, 'publisher_id', 'event_type', 'DEBUG', dict(a=3)) self.assertEqual(self.test_topic, 'testnotify.debug')
def test_verify_message_format(self): """A test to ensure changing the message format is prohibitively annoying. """ def message_assert(context, message): fields = [('publisher_id', 'publisher_id'), ('event_type', 'event_type'), ('priority', 'WARN'), ('payload', dict(a=3))] for k, v in fields: self.assertEqual(message[k], v) self.assertTrue(len(message['message_id']) > 0) self.assertTrue(len(message['timestamp']) > 0) self.assertEqual(context, ctxt) self.stubs.Set(no_op_notifier, 'notify', message_assert) notifier_api.notify(ctxt, 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3))
def process_response(self, request, response, exception=None, traceback=None): payload = { 'request': self.environ_to_dict(request.environ), } if response: payload['response'] = { 'status': response.status, 'headers': response.headers, } if exception: payload['exception'] = { 'value': repr(exception), 'traceback': tb.format_tb(traceback) } api.notify(context.get_admin_context(), api.publisher_id(os.path.basename(sys.argv[0])), 'http.response', api.INFO, payload)
def _notify(self, ctxt, event_type, payload, priority): notifier_api.notify(ctxt, self.publisher_id, event_type, priority, payload)