def test_emit_message_from_definition_bad_interpolation(self): """ When wrong data is provided the an additional message is logged together with the non-interpolated message. """ handler = EventsHandler() content = ''' { "groups" : { "group": { "description": "something"} }, "events" : { "100": { "message": "100 %(unknown_data)s some message", "groups": ["group"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' definitions = manufacture.makeEventsDefinition(content=content) log_configuration_section = manufacture.makeLogConfigurationSection() handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) handler.emit('100', data={'other': u'dontcare'}) self.assertLog(1025) self.assertLog(100, regex='100 %\(unknown_data\)s some message')
def test_emit_message_from_definition_no_data(self): """ If not message was defined for emit, the message from event definition will be used. When no data is provided the string will not be interpolated. """ handler = EventsHandler() content = ''' { "groups" : { "group": { "description": "something"} }, "events" : { "100": { "message": "100 %(replace)s some message", "groups": ["group"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' definitions = manufacture.makeEventsDefinition(content=content) log_configuration_section = manufacture.makeLogConfigurationSection() handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) handler.emit('100', data={'replace': 'test'}) self.assertLog(100, regex="100 test some m")
def test_emit_message_from_definition_with_data(self): """ When data is provided the message will be interpolated based on data and event_definition. """ handler = EventsHandler() content = ''' { "groups" : { "group": { "description": "something"} }, "events" : { "100": { "message": "100 %(data)s some message", "groups": ["group"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' definitions = manufacture.makeEventsDefinition(content=content) log_configuration_section = manufacture.makeLogConfigurationSection() handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) data_string = manufacture.getUniqueString() handler.emit('100', data={'data': data_string}) self.assertLog(100, regex="100 " + data_string + " some m")
def test_emit_with_string_id(self): """ Events can be emitted with string ids and are converted to unicode. """ handler = EventsHandler() message = u'Some message ' + manufacture.getUniqueString() with patch.object(handler, 'emitEvent') as patched: handler.emit(u'100', message=message) event = patched.call_args[0][0] self.assertEqual(u'100', event.id)
def test_emit_with_int_id(self): """ When event id is an integer, it will be converted to string. """ handler = EventsHandler() message = u'Some message ' + manufacture.getUniqueString() with patch.object(handler, 'emitEvent') as patched: handler.emit(100, message=message) event = patched.call_args[0][0] self.assertEqual(u'100', event.id)
def test_emit_without_configuration(self): """ If handler is not configured, all events will be logged using only data from the event. Event definitions and other configurations is not used. """ handler = EventsHandler() message = u'Some message ' + manufacture.getUniqueString() handler.emit('100', message=message) self.assertLog(100, regex=u'Some ')
def test_removeConfiguration(self): """ EventsHandler configurations can be removed using removeConfiguration. """ handler = EventsHandler() definitions = manufacture.makeEventsDefinition() log_configuration_section = manufacture.makeLogConfigurationSection() handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) self.assertTrue(handler.configured) handler.removeConfiguration() self.assertFalse(handler.configured)
def test_configure(self): """ EventsHandler can be configured. """ handler = EventsHandler() definitions = manufacture.makeEventsDefinition() log_configuration_section = manufacture.makeLogConfigurationSection() handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) self.assertTrue(handler.configured) self.assertIsNotNone(handler.definitions) self.assertEqual( [CONFIGURATION_ALL_LOG_ENABLED_GROUPS], handler.enabled_groups)
def test_emit_with_all(self): """ When 'all' group is enabled, logs will be enabled even if their groups is not explicitly enabled. """ handler = EventsHandler() content = ''' { "groups" : { "disabled": { "description": ""} }, "events" : { "100": { "message": "some message", "groups": ["disabled"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" }, "101": { "message": "other message", "groups": ["disabled"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' definitions = manufacture.makeEventsDefinition(content=content) log_configuration_section = manufacture.makeLogConfigurationSection() log_configuration_section.enabled_groups = [ CONFIGURATION_ALL_LOG_ENABLED_GROUPS] handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) handler.emit('101', message='101some message') handler.emit('100', message='100some message') self.assertLog(101, regex="101some m") self.assertLog(100, regex="100some m")
def test_emit_unknown_id(self): """ Emitting an event with unknown ID will log an error containing the text of the unknown id. """ handler = EventsHandler() message = u'Some message ' + manufacture.getUniqueString() definitions = manufacture.makeEventsDefinition() log_configuration_section = manufacture.makeLogConfigurationSection() handler.configure( definitions=definitions, log_configuration_section=log_configuration_section, ) handler.emit(u'100', message=message) self.assertLog( 1024, regex='Unknown event with id "100"')
def test_log(self): """ `log` method is here for transition and used the old Logger.log interface to emit an event. """ handler = EventsHandler() message = u'Some message ' + manufacture.getUniqueString() peer = manufacture.makeIPv4Address() avatar = manufacture.makeFilesystemApplicationAvatar() data = {} handler.log(100, message, peer=peer, avatar=avatar, data=data) log_message = self.popLog() self.assertEqual(100, log_message[0]) self.assertTrue('Some message' in log_message[1]) self.assertEqual(avatar, log_message[2]) self.assertEqual(peer, log_message[3]) self.assertEqual(data, log_message[4])
def test_emit_with_configuration(self): """ If handler is configured, the logs can be filterd. """ handler = EventsHandler() content = ''' { "groups" : { "enabled": { "description": ""}, "disabled": { "description": ""} }, "events" : { "100": { "message": "some message", "groups": ["enabled"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" }, "101": { "message": "other message", "groups": ["disabled"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' definitions = manufacture.makeEventsDefinition(content=content) log_configuration_section = manufacture.makeLogConfigurationSection() log_configuration_section.enabled_groups = ['enabled'] handler.configure( definitions=definitions, log_configuration_section=log_configuration_section) handler.emit('101', message='101some message') handler.emit('100', message='100some message') self.assertLog(100, regex="100some m")