From 86727720a322e3b9ff20ed534a1a86db0cb54c57 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 7 Feb 2023 10:39:43 +0100 Subject: [PATCH] [matter_yamltests] Add a config_override property to the TestParser in order to modify the test parameters (#24869) --- .../matter_yamltests/parser.py | 28 +++++++++++---- .../py_matter_yamltests/test_yaml_parser.py | 35 ++++++++++++++++--- .../yamltest_with_chip_repl_tester.py | 5 +-- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/scripts/py_matter_yamltests/matter_yamltests/parser.py b/scripts/py_matter_yamltests/matter_yamltests/parser.py index e60e0acd44bcaf..97cc0dc51e8c19 100644 --- a/scripts/py_matter_yamltests/matter_yamltests/parser.py +++ b/scripts/py_matter_yamltests/matter_yamltests/parser.py @@ -14,6 +14,7 @@ # limitations under the License. import copy +from dataclasses import dataclass, field from enum import Enum, auto import yaml @@ -837,8 +838,15 @@ def __next__(self) -> TestStep: raise StopIteration +@dataclass +class TestParserConfig: + pics: str = None + definitions: SpecDefinitions = None + config_override: dict = field(default_factory=dict) + + class TestParser: - def __init__(self, test_file, pics_file, definitions): + def __init__(self, test_file: str, parser_config: TestParserConfig = TestParserConfig()): data = self.__load_yaml(test_file) _check_valid_keys(data, _TESTS_SECTION) @@ -846,7 +854,16 @@ def __init__(self, test_file, pics_file, definitions): self.name = _value_or_none(data, 'name') self.PICS = _value_or_none(data, 'PICS') - self._parsing_config_variable_storage = data.get('config', {}) + config = data.get('config', {}) + for key, value in parser_config.config_override.items(): + if value is None: + continue + + if isinstance(config[key], dict) and 'defaultValue' in config[key]: + config[key]['defaultValue'] = value + else: + config[key] = value + self._parsing_config_variable_storage = config # These are a list of "KnownVariables". These are defaults the codegen used to use. This # is added for legacy support of tests that expect to uses these "defaults". @@ -855,13 +872,10 @@ def __init__(self, test_file, pics_file, definitions): self.__populate_default_config_if_missing('cluster', '') self.__populate_default_config_if_missing('timeout', '90') - pics_checker = PICSChecker(pics_file) + pics_checker = PICSChecker(parser_config.pics) tests = _value_or_none(data, 'tests') self.tests = YamlTests( - self._parsing_config_variable_storage, definitions, pics_checker, tests) - - def update_config(self, key, value): - self._parsing_config_variable_storage[key] = value + self._parsing_config_variable_storage, parser_config.definitions, pics_checker, tests) def __populate_default_config_if_missing(self, key, value): if key not in self._parsing_config_variable_storage: diff --git a/scripts/py_matter_yamltests/test_yaml_parser.py b/scripts/py_matter_yamltests/test_yaml_parser.py index 78df2c35fe64ad..325f4435d6ff7e 100644 --- a/scripts/py_matter_yamltests/test_yaml_parser.py +++ b/scripts/py_matter_yamltests/test_yaml_parser.py @@ -24,7 +24,7 @@ import unittest from matter_yamltests.definitions import * -from matter_yamltests.parser import TestParser +from matter_yamltests.parser import TestParser, TestParserConfig simple_test_description = ''' @@ -74,19 +74,44 @@ def setUp(self): self._temp_file = tempfile.NamedTemporaryFile(suffix='.yaml') with open(self._temp_file.name, 'w') as f: f.writelines(simple_test_yaml) - pics_file = None - self._yaml_parser = TestParser( - self._temp_file.name, pics_file, self._definitions) def test_able_to_iterate_over_all_parsed_tests(self): # self._yaml_parser.tests implements `__next__`, which does value substitution. We are # simply ensure there is no exceptions raise. + parser_config = TestParserConfig(None, self._definitions) + yaml_parser = TestParser(self._temp_file.name, parser_config) count = 0 - for idx, test_step in enumerate(self._yaml_parser.tests): + for idx, test_step in enumerate(yaml_parser.tests): count += 1 pass self.assertEqual(count, 3) + def test_config(self): + parser_config = TestParserConfig(None, self._definitions) + yaml_parser = TestParser(self._temp_file.name, parser_config) + for idx, test_step in enumerate(yaml_parser.tests): + self.assertEqual(test_step.node_id, 0x12344321) + self.assertEqual(test_step.cluster, 'Test') + self.assertEqual(test_step.endpoint, 1) + + def test_config_override(self): + config_override = {'nodeId': 12345, + 'cluster': 'TestOverride', 'endpoint': 4} + parser_config = TestParserConfig( + None, self._definitions, config_override) + yaml_parser = TestParser(self._temp_file.name, parser_config) + for idx, test_step in enumerate(yaml_parser.tests): + self.assertEqual(test_step.node_id, 12345) + self.assertEqual(test_step.cluster, 'TestOverride') + self.assertEqual(test_step.endpoint, 4) + + def test_config_override_unknown_field(self): + config_override = {'unknown_field': 1} + parser_config = TestParserConfig( + None, self._definitions, config_override) + self.assertRaises(KeyError, TestParser, + self._temp_file.name, parser_config) + def main(): unittest.main() diff --git a/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py b/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py index fada763dc15466..44dd27de13a185 100644 --- a/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py +++ b/scripts/tests/chiptest/yamltest_with_chip_repl_tester.py @@ -33,7 +33,7 @@ from chip.ChipStack import * from chip.yaml.runner import ReplTestRunner from matter_yamltests.definitions import SpecDefinitionsFromPaths -from matter_yamltests.parser import PostProcessCheckStatus, TestParser +from matter_yamltests.parser import PostProcessCheckStatus, TestParser, TestParserConfig _DEFAULT_CHIP_ROOT = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "..", "..")) @@ -100,7 +100,8 @@ def _StackShutDown(): ]) # Parsing YAML test and setting up chip-repl yamltests runner. - yaml = TestParser(yaml_path, pics_file, clusters_definitions) + parser_config = TestParserConfig(pics_file, clusters_definitions) + yaml = TestParser(yaml_path, parser_config) runner = ReplTestRunner( clusters_definitions, certificate_authority_manager, dev_ctrl)