def test_serializer(self): transport = _FakeTransport(self.conf) serializer = msg_serializer.NoOpSerializer() notifier = messaging.Notifier(transport, 'test.localhost', driver='test', topic='test', serializer=serializer) message_id = uuid.uuid4() self.mox.StubOutWithMock(uuid, 'uuid4') uuid.uuid4().AndReturn(message_id) timeutils.set_time_override() self.mox.StubOutWithMock(serializer, 'serialize_entity') serializer.serialize_entity({}, 'bar').AndReturn('sbar') self.mox.ReplayAll() notifier.info({}, 'test.notify', 'bar') message = { 'message_id': str(message_id), 'publisher_id': 'test.localhost', 'event_type': 'test.notify', 'priority': 'INFO', 'payload': 'sbar', 'timestamp': str(timeutils.utcnow.override_time), } self.assertEquals(_impl_test.NOTIFICATIONS, [({}, message, 'INFO')])
def test_notifier(self): self.config(notification_driver=['log']) transport = _FakeTransport(self.conf) notifier = messaging.Notifier(transport, 'test.localhost') message_id = uuid.uuid4() self.mox.StubOutWithMock(uuid, 'uuid4') uuid.uuid4().AndReturn(message_id) timeutils.set_time_override() message = { 'message_id': str(message_id), 'publisher_id': 'test.localhost', 'event_type': 'test.notify', 'priority': 'INFO', 'payload': 'bar', 'timestamp': str(timeutils.utcnow.override_time), } logger = self.mox.CreateMockAnything() self.mox.StubOutWithMock(logging, 'getLogger') logging.getLogger('oslo.messaging.notification.test.notify').\ AndReturn(logger) logger.info(jsonutils.dumps(message)) self.mox.ReplayAll() notifier.info({}, 'test.notify', 'bar')
def test_notifier(self): drivers = [] if self.v1: drivers.append('messaging') if self.v2: drivers.append('messagingv2') self.config(notification_driver=drivers) self.config(notification_topics=self.topics) transport = _FakeTransport(self.conf) if hasattr(self, 'ctor_pub_id'): notifier = messaging.Notifier(transport, publisher_id=self.ctor_pub_id) else: notifier = messaging.Notifier(transport) if hasattr(self, 'prep_pub_id'): notifier = notifier.prepare(publisher_id=self.prep_pub_id) self.mox.StubOutWithMock(transport, '_send_notification') message_id = uuid.uuid4() self.mox.StubOutWithMock(uuid, 'uuid4') uuid.uuid4().AndReturn(message_id) timeutils.set_time_override() message = { 'message_id': str(message_id), 'publisher_id': self.expected_pub_id, 'event_type': 'test.notify', 'priority': self.priority.upper(), 'payload': self.payload, 'timestamp': str(timeutils.utcnow.override_time), } sends = [] if self.v1: sends.append(dict(version=1.0)) if self.v2: sends.append(dict(version=2.0)) for send_kwargs in sends: for topic in self.topics: target = messaging.Target(topic='%s.%s' % (topic, self.priority)) transport._send_notification(target, self.ctxt, message, **send_kwargs) self.mox.ReplayAll() method = getattr(notifier, self.priority) method(self.ctxt, 'test.notify', self.payload)
def test_logging_conf(self): with mock.patch('oslo.messaging.transport.get_transport', return_value=test_notifier._FakeTransport(self.conf)): logging.config.dictConfig({ 'version': 1, 'handlers': { 'notification': { 'class': 'oslo.messaging.LoggingNotificationHandler', 'level': self.priority.upper(), 'url': 'test://', }, }, 'loggers': { 'default': { 'handlers': ['notification'], 'level': self.priority.upper(), }, }, }) timeutils.set_time_override() levelno = getattr(logging, self.priority.upper()) logger = logging.getLogger('default') logger.log(levelno, 'foobar') n = messaging.notify._impl_test.NOTIFICATIONS[0][1] self.assertEqual(n['priority'], getattr(self, 'queue', self.priority.upper())) self.assertEqual(n['event_type'], 'logrecord') self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time)) self.assertEqual(n['publisher_id'], None) self.assertDictEqual( n['payload'], {'process': os.getpid(), 'funcName': 'test_logging_conf', 'name': 'default', 'thread': logging.thread.get_ident() if logging.thread else None, 'levelno': levelno, 'processName': 'MainProcess', 'pathname': __file__[:-1], # Remove the 'c' of .pyc 'lineno': 121, 'msg': 'foobar', 'exc_info': None, 'levelname': logging.getLevelName(levelno), 'extra': None})
def test_logger(self): with mock.patch('oslo.messaging.transport.get_transport', return_value=test_notifier._FakeTransport(self.conf)): self.logger = messaging.LoggingNotificationHandler('test://') timeutils.set_time_override() levelno = getattr(logging, self.priority.upper(), 42) record = logging.LogRecord('foo', levelno, '/foo/bar', 42, 'Something happened', None, None) self.logger.emit(record) n = messaging.notify._impl_test.NOTIFICATIONS[0][1] self.assertEqual(n['priority'], getattr(self, 'queue', self.priority.upper())) self.assertEqual(n['event_type'], 'logrecord') self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time)) self.assertEqual(n['publisher_id'], None) self.assertEqual( n['payload'], {'process': os.getpid(), 'funcName': None, 'name': 'foo', 'thread': logging.thread.get_ident() if logging.thread else None, 'levelno': levelno, 'processName': 'MainProcess', 'pathname': '/foo/bar', 'lineno': 42, 'msg': 'Something happened', 'exc_info': None, 'levelname': logging.getLevelName(levelno), 'extra': None})
def test_serializer(self): transport = _FakeTransport(self.conf) serializer = msg_serializer.NoOpSerializer() notifier = messaging.Notifier(transport, 'test.localhost', driver='test', topic='test', serializer=serializer) message_id = uuid.uuid4() self.mox.StubOutWithMock(uuid, 'uuid4') uuid.uuid4().AndReturn(message_id) timeutils.set_time_override() self.mox.StubOutWithMock(serializer, 'serialize_context') self.mox.StubOutWithMock(serializer, 'serialize_entity') serializer.serialize_context(dict(user='******')).\ AndReturn(dict(user='******')) serializer.serialize_entity(dict(user='******'), 'bar').AndReturn('sbar') self.mox.ReplayAll() notifier.info(dict(user='******'), 'test.notify', 'bar') message = { 'message_id': str(message_id), 'publisher_id': 'test.localhost', 'event_type': 'test.notify', 'priority': 'INFO', 'payload': 'sbar', 'timestamp': str(timeutils.utcnow.override_time), } self.assertEquals(_impl_test.NOTIFICATIONS, [(dict(user='******'), message, 'INFO')])