Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[matter_yamltests] Add a config_override property to the TestParser i… #24869

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions scripts/py_matter_yamltests/matter_yamltests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

import copy
from dataclasses import dataclass, field
from enum import Enum, auto

import yaml
Expand Down Expand Up @@ -837,16 +838,32 @@ 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)

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".
Expand All @@ -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:
Expand Down
35 changes: 30 additions & 5 deletions scripts/py_matter_yamltests/test_yaml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''<?xml version="1.0"?>
<configurator>
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions scripts/tests/chiptest/yamltest_with_chip_repl_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__), "..", "..", ".."))
Expand Down Expand Up @@ -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)

Expand Down