def testReadFormatterDefinition(self): """Tests the _ReadFormatterDefinition function.""" test_formatters_file = yaml_formatters_file.YAMLFormattersFile() formatter = test_formatters_file._ReadFormatterDefinition( self._FORMATTERS_YAML) self.assertIsNotNone(formatter) self.assertEqual(formatter.DATA_TYPE, 'test:fs:stat') with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition({}) with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition({'type': 'bogus'}) with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition( {'type': 'conditional'}) with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition({ 'type': 'conditional', 'data_type': 'test:fs:stat' }) with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition({ 'type': 'conditional', 'data_type': 'test:fs:stat', 'message': [ '{display_name}', 'Type: {file_entry_type}', '({unallocated})' ] }) with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition({ 'type': 'conditional', 'data_type': 'test:fs:stat', 'message': [ '{display_name}', 'Type: {file_entry_type}', '({unallocated})' ] }) with self.assertRaises(errors.ParseError): test_formatters_file._ReadFormatterDefinition({'bogus': 'error'})
def testReadFromFile(self): """Tests the ReadFromFile function.""" test_file_path = self._GetTestFilePath( ['formatters', 'format_test.yaml']) self._SkipIfPathNotExists(test_file_path) test_formatters_file = yaml_formatters_file.YAMLFormattersFile() formatters = test_formatters_file.ReadFromFile(test_file_path) self.assertEqual(len(formatters), 1) self.assertEqual(formatters[0].DATA_TYPE, 'fs:stat')
def testReadFromFileObject(self): """Tests the _ReadFromFileObject function.""" test_file_path = self._GetTestFilePath( ['formatters', 'format_test.yaml']) self._SkipIfPathNotExists(test_file_path) test_formatters_file = yaml_formatters_file.YAMLFormattersFile() with io.open(test_file_path, 'r', encoding='utf-8') as file_object: formatters = list( test_formatters_file._ReadFromFileObject(file_object)) self.assertEqual(len(formatters), 1)
def _ReadFormattersFile(cls, path): """Reads a formatters configuration file. Args: path (str): path of file that contains the formatters configuration. Raises: KeyError: if formatter class is already set for the corresponding data type. """ formatters_file = yaml_formatters_file.YAMLFormattersFile() for formatter in formatters_file.ReadFromFile(path): # TODO: refactor RegisterFormatter to only use formatter objects. cls.RegisterFormatter(formatter) data_type = formatter.DATA_TYPE.lower() cls._formatter_objects[data_type] = formatter cls._formatters_from_file.append(data_type)
def _ReadMessageFormattersFile(self, path): """Reads a message formatters configuration file. Args: path (str): path of file that contains the message formatters configuration. Raises: KeyError: if the message formatter is already set for the corresponding data type. """ message_formatters_file = yaml_formatters_file.YAMLFormattersFile() for message_formatter in message_formatters_file.ReadFromFile(path): for identifier in message_formatter.custom_helpers: custom_formatter_helper = ( formatters_manager.FormattersManager. GetEventFormatterHelper(identifier)) if custom_formatter_helper: message_formatter.AddHelper(custom_formatter_helper) self._message_formatters[ message_formatter.data_type] = message_formatter