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_load_EventDefinition_missing_group(self): """ Loading an EventDefinition with a reference to a non-existent group will raise UtilsError. """ event_id = manufacture.getUniqueString() group_1 = manufacture.getUniqueString() message = manufacture.getUniqueString() content = ''' { "groups" : {}, "events" : { "%s": { "message": "%s", "groups": ["%s"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' % (event_id, message, group_1) config = manufacture.makeEventsDefinition( content=content, load=False) with self.assertRaises(UtilsError): config.load()
def test_load_empty(self): """ An EventGroup with just a complex or simple description will load just fine. """ definitions = manufacture.makeEventsDefinition(content=u'') definitions.load() self.assertIsEmpty(definitions.getAllEventDefinitions()) self.assertIsEmpty(definitions.getAllEventGroupDefinitions())
def test_load_bad_config_file(self): """ Trying to configure from a bad formated configuration file will raise UtilsError. """ content = manufacture.getUniqueString() definitions = manufacture.makeEventsDefinition( content=content, load=False) with self.assertRaises(UtilsError) as context: definitions.load() self.assertExceptionID(u'1028', context.exception)
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")
def test_load_EventDefinition_good(self): """ An EventDefinition with a message and groups will load just fine. """ event_id = manufacture.getUniqueString() group_1 = manufacture.getUniqueString() group_2 = manufacture.getUniqueString() message = manufacture.getUniqueString() content = ''' { "groups" : { "%s": { "description": ""}, "%s": { "description": ""} }, "events" : { "%s": { "message": "%s", "groups": ["%s", "%s"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' % (group_1, group_2, event_id, message, group_1, group_2) config = manufacture.makeEventsDefinition( content=content, load=False) config.load() event_definition = config.getEventDefinition(id=event_id) self.assertEqual(event_id, event_definition.id) self.assertEqual(message, event_definition.message) self.assertEqual(2, len(event_definition.groups)) self.assertIsNone(event_definition.version_added) self.assertIsNone(event_definition.version_removed) self.assertIsNone(event_definition.description)
def test_getAllEventDefinitions_good(self): """ An getAllEventDefinitions with return a dictionary with all defined EventDefinitons keyed by event id. """ content = ''' { "groups" : { "group-1": { "description": ""} }, "events" : { "ev1": { "message": "something", "groups": ["group-1"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" }, "ev2": { "message": "something", "groups": ["group-1"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' config = manufacture.makeEventsDefinition(content=content) result = config.getAllEventDefinitions() self.assertEqual(2, len(result)) self.assertTrue(u'ev1' in result) self.assertTrue(u'ev2' in result)
def test_getAllEventGroupDefinitions_good(self): """ An getAllEventGroupDefinitions with return a dictionary with all defined EventGroups. """ name_1 = manufacture.getUniqueString() name_2 = manufacture.getUniqueString() description = manufacture.getUniqueString() content = ''' { "groups" : { "%s": { "description": "%s"}, "%s": { "description": ""} }, "events" : {} } ''' % (name_1, description, name_2) config = manufacture.makeEventsDefinition(content=content) result = config.getAllEventGroupDefinitions() self.assertEqual(2, len(result)) self.assertTrue(name_1 in result) self.assertTrue(name_2 in result)
def test_load_EventGroup_good(self): """ An EventGroup with just a complex or simple description will load just fine. """ name_1 = manufacture.getUniqueString() name_2 = manufacture.getUniqueString() description = manufacture.getUniqueString() content = ''' { "groups" : { "%s": { "description": "%s"}, "%s": { "description": ""} }, "events" : {} } ''' % (name_1, description, name_2) definitions = manufacture.makeEventsDefinition(content=content) group = definitions.getEventGroupDefinition(name=name_1) self.assertEqual(name_1, group.name) self.assertEqual(description, group.description) group = definitions.getEventGroupDefinition(name=name_2) self.assertEqual(name_2, group.name)
def test_generateDocumentation_good(self): """ The handler can generates a RESTructuredText file documenting the events and event groups. """ content = ''' { "groups" : { "group-1": { "description": ""}, "group-2": { "description": ""} }, "events" : { "ev1": { "message": "something", "groups": ["group-1", "group-2"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" }, "ev2": { "message": "something", "groups": ["group-1"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" }, "event3": { "message": "something", "groups": ["group-2"], "description": "", "version_removed": "", "version_added": "", "details": "", "data": "" } } } ''' template_content = ( 'Header\n' '=======\n' '{% for group_id in groups %}\n' '{{ groups[group_id].name }}\n' '----\n' 'description: {{ groups[group_id].description }}\n' '\n' '{% endfor %}\n' '\n' '{% for event_id in events %}\n' '{{ events[event_id].id }}\n' '----\n' 'message: {{ events[event_id].message }}\n' 'groups: {{ ", ".join(events[event_id].group_names) }}\n' '\n' '{% endfor %}\n' ) config = manufacture.makeEventsDefinition(content=content) templates_loader = DictLoader( {'events_documentation.rst': template_content}) jinja_environment = Environment(loader=templates_loader) template = jinja_environment.get_template('events_documentation.rst') result = config.generateDocumentation(template=template) self.assertTrue('ev1' in result) self.assertTrue('groups: group-1, group-2' in result)