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 _getConfiguration(self, content=None): if not content: content = ( '[log]\n' 'log_file: Disabled\n' ) proxy = manufacture.makeFileConfigurationProxy( content=content, defaults=LOG_SECTION_DEFAULTS) return manufacture.makeLogConfigurationSection(proxy=proxy)
def _getSection(self, content=None): """ Return a log configuration section. """ if content is None: content = ( '[log]\n' 'log_file: Disable\n' ) proxy = manufacture.makeFileConfigurationProxy( content=content, defaults=LOG_SECTION_DEFAULTS) return manufacture.makeLogConfigurationSection(proxy=proxy)
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_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")