| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the YAML-based attribute container definitions file.""" |
| 4 |
|
| 5 |
import io |
| 6 |
import unittest |
| 7 |
|
| 8 |
from acstore import errors |
| 9 |
from acstore.helpers import yaml_definitions_file |
| 10 |
|
| 11 |
from tests import test_lib as shared_test_lib |
| 12 |
|
| 13 |
|
| 14 |
class YAMLAttributeContainerDefinitionsFileTest(shared_test_lib.BaseTestCase): |
| 15 |
"""Tests for the YAML-based attribute container definitions file.""" |
| 16 |
|
| 17 |
# pylint: disable=protected-access |
| 18 |
|
| 19 |
_FORMATTERS_YAML = { |
| 20 |
'name': 'windows_eventlog_message_file', |
| 21 |
'attributes': [ |
| 22 |
{'name': 'path', 'type': 'str'}, |
| 23 |
{'name': 'windows_path', 'type': 'str'}]} |
| 24 |
|
| 25 |
def testReadDefinition(self): |
| 26 |
"""Tests the _ReadDefinition function.""" |
| 27 |
test_definitions_file = ( |
| 28 |
yaml_definitions_file.YAMLAttributeContainerDefinitionsFile()) |
| 29 |
|
| 30 |
container_class = test_definitions_file._ReadDefinition( |
| 31 |
self._FORMATTERS_YAML) |
| 32 |
|
| 33 |
self.assertIsNotNone(container_class) |
| 34 |
self.assertEqual( |
| 35 |
container_class.CONTAINER_TYPE, 'windows_eventlog_message_file') |
| 36 |
self.assertEqual( |
| 37 |
container_class.SCHEMA, {'path': 'str', 'windows_path': 'str'}) |
| 38 |
|
| 39 |
with self.assertRaises(errors.ParseError): |
| 40 |
test_definitions_file._ReadDefinition({}) |
| 41 |
|
| 42 |
with self.assertRaises(errors.ParseError): |
| 43 |
test_definitions_file._ReadDefinition({ |
| 44 |
'name': 'windows_eventlog_message_file', |
| 45 |
'attributes': []}) |
| 46 |
|
| 47 |
with self.assertRaises(errors.ParseError): |
| 48 |
test_definitions_file._ReadDefinition({ |
| 49 |
'name': 'windows_eventlog_message_file', |
| 50 |
'attributes': [{'type': 'str'}]}) |
| 51 |
|
| 52 |
with self.assertRaises(errors.ParseError): |
| 53 |
test_definitions_file._ReadDefinition({ |
| 54 |
'name': 'windows_eventlog_message_file', |
| 55 |
'attributes': [{'name': 'path'}]}) |
| 56 |
|
| 57 |
with self.assertRaises(errors.ParseError): |
| 58 |
test_definitions_file._ReadDefinition({ |
| 59 |
'name': 'windows_eventlog_message_file', |
| 60 |
'attributes': [{'name': 'path', 'type': 'bogus'}]}) |
| 61 |
|
| 62 |
def testReadFromFileObject(self): |
| 63 |
"""Tests the _ReadFromFileObject function.""" |
| 64 |
test_file_path = self._GetTestFilePath(['definitions.yaml']) |
| 65 |
self._SkipIfPathNotExists(test_file_path) |
| 66 |
|
| 67 |
test_definitions_file = ( |
| 68 |
yaml_definitions_file.YAMLAttributeContainerDefinitionsFile()) |
| 69 |
|
| 70 |
with io.open(test_file_path, 'r', encoding='utf-8') as file_object: |
| 71 |
definitions = list(test_definitions_file._ReadFromFileObject(file_object)) |
| 72 |
|
| 73 |
self.assertEqual(len(definitions), 1) |
| 74 |
|
| 75 |
def testReadFromFile(self): |
| 76 |
"""Tests the ReadFromFile function.""" |
| 77 |
test_file_path = self._GetTestFilePath(['definitions.yaml']) |
| 78 |
self._SkipIfPathNotExists(test_file_path) |
| 79 |
|
| 80 |
test_definitions_file = ( |
| 81 |
yaml_definitions_file.YAMLAttributeContainerDefinitionsFile()) |
| 82 |
|
| 83 |
definitions = list(test_definitions_file.ReadFromFile(test_file_path)) |
| 84 |
|
| 85 |
self.assertEqual(len(definitions), 1) |
| 86 |
|
| 87 |
self.assertEqual( |
| 88 |
definitions[0].CONTAINER_TYPE, 'windows_eventlog_message_file') |
| 89 |
|
| 90 |
|
| 91 |
if __name__ == '__main__': |
| 92 |
unittest.main() |