Skip to content

Commit b281d1b

Browse files
authored
Couple of quick fixes to yamltests parser (#24331)
These fixes are needed for multi admin support. They were missed when doing some refactoring in the yamltests parser.
1 parent bfe87de commit b281d1b

File tree

2 files changed

+68
-36
lines changed

2 files changed

+68
-36
lines changed

scripts/py_matter_yamltests/matter_yamltests/fixes.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def try_add_yaml_support_for_scientific_notation_without_dot(loader):
9696
# accessory. But this state may not exist in the runner (as in it prevent to have multiple node ids
9797
# associated to a fabric...) so the 'nodeId' needs to be added back manually.
9898
def try_update_yaml_node_id_test_runner_state(tests, config):
99-
identities = {'alpha': None if 'nodeId' not in config else config['nodeId']}
99+
identities = {
100+
'alpha': None if 'nodeId' not in config else config['nodeId']}
100101

101102
for test in tests:
102103
if not test.is_enabled:
@@ -106,9 +107,9 @@ def try_update_yaml_node_id_test_runner_state(tests, config):
106107

107108
if test.cluster == 'CommissionerCommands':
108109
if test.command == 'PairWithCode':
109-
for item in test.arguments['values']:
110+
for item in test.arguments_with_placeholders['values']:
110111
if item['name'] == 'nodeId':
111112
identities[identity] = item['value']
112113
elif identity is not None and identity in identities:
113-
nodeId = identities[identity]
114-
test.nodeId = nodeId
114+
node_id = identities[identity]
115+
test.node_id = node_id

scripts/py_matter_yamltests/matter_yamltests/parser.py

+63-32
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def __init__(self, test: dict, config: dict, definitions):
206206
self.fabric_filtered = _value_or_none(test, 'fabricFiltered')
207207
self.min_interval = _value_or_none(test, 'minInterval')
208208
self.max_interval = _value_or_none(test, 'maxInterval')
209-
self.timed_interaction_timeout_ms = _value_or_none(test, 'timedInteractionTimeoutMs')
209+
self.timed_interaction_timeout_ms = _value_or_none(
210+
test, 'timedInteractionTimeoutMs')
210211
self.busy_wait_ms = _value_or_none(test, 'busyWaitMs')
211212

212213
self.is_attribute = self.command in _ATTRIBUTE_COMMANDS
@@ -215,8 +216,10 @@ def __init__(self, test: dict, config: dict, definitions):
215216
self.arguments_with_placeholders = _value_or_none(test, 'arguments')
216217
self.response_with_placeholders = _value_or_none(test, 'response')
217218

218-
_check_valid_keys(self.arguments_with_placeholders, _TEST_ARGUMENTS_SECTION)
219-
_check_valid_keys(self.response_with_placeholders, _TEST_RESPONSE_SECTION)
219+
_check_valid_keys(self.arguments_with_placeholders,
220+
_TEST_ARGUMENTS_SECTION)
221+
_check_valid_keys(self.response_with_placeholders,
222+
_TEST_RESPONSE_SECTION)
220223

221224
self._convert_single_value_to_values(self.arguments_with_placeholders)
222225
self._convert_single_value_to_values(self.response_with_placeholders)
@@ -225,20 +228,26 @@ def __init__(self, test: dict, config: dict, definitions):
225228
response_mapping = None
226229

227230
if self.is_attribute:
228-
attribute = definitions.get_attribute_by_name(self.cluster, self.attribute)
231+
attribute = definitions.get_attribute_by_name(
232+
self.cluster, self.attribute)
229233
if attribute:
230234
attribute_mapping = self._as_mapping(definitions, self.cluster,
231235
attribute.definition.data_type.name)
232236
argument_mapping = attribute_mapping
233237
response_mapping = attribute_mapping
234238
else:
235-
command = definitions.get_command_by_name(self.cluster, self.command)
239+
command = definitions.get_command_by_name(
240+
self.cluster, self.command)
236241
if command:
237-
argument_mapping = self._as_mapping(definitions, self.cluster, command.input_param)
238-
response_mapping = self._as_mapping(definitions, self.cluster, command.output_param)
242+
argument_mapping = self._as_mapping(
243+
definitions, self.cluster, command.input_param)
244+
response_mapping = self._as_mapping(
245+
definitions, self.cluster, command.output_param)
239246

240-
self._update_with_definition(self.arguments_with_placeholders, argument_mapping)
241-
self._update_with_definition(self.response_with_placeholders, response_mapping)
247+
self._update_with_definition(
248+
self.arguments_with_placeholders, argument_mapping)
249+
self._update_with_definition(
250+
self.response_with_placeholders, response_mapping)
242251

243252
# This performs a very basic sanity parse time check of constrains. This parsing happens
244253
# again inside post processing response since at that time we will have required variables
@@ -281,7 +290,8 @@ def _as_mapping(self, definitions, cluster_name, target_name):
281290
if hasattr(element, 'base_type'):
282291
target_name = element.base_type.lower()
283292
elif hasattr(element, 'fields'):
284-
target_name = {f.name: self._as_mapping(definitions, cluster_name, f.data_type.name) for f in element.fields}
293+
target_name = {f.name: self._as_mapping(
294+
definitions, cluster_name, f.data_type.name) for f in element.fields}
285295
elif target_name:
286296
target_name = target_name.lower()
287297

@@ -296,7 +306,8 @@ def _update_with_definition(self, container: dict, mapping_type):
296306
mapping = mapping_type if self.is_attribute else mapping_type[value['name']]
297307

298308
if key == 'value':
299-
value[key] = self._update_value_with_definition(item_value, mapping)
309+
value[key] = self._update_value_with_definition(
310+
item_value, mapping)
300311
elif key == 'saveAs' and type(item_value) is str and item_value not in self._parsing_config_variable_storage:
301312
self._parsing_config_variable_storage[item_value] = None
302313
elif key == 'constraints':
@@ -321,7 +332,8 @@ def _update_value_with_definition(self, value, mapping_type):
321332
rv[key] = value[key] # int64u
322333
else:
323334
mapping = mapping_type[key]
324-
rv[key] = self._update_value_with_definition(value[key], mapping)
335+
rv[key] = self._update_value_with_definition(
336+
value[key], mapping)
325337
return rv
326338
if type(value) is list:
327339
return [self._update_value_with_definition(entry, mapping_type) for entry in value]
@@ -331,7 +343,8 @@ def _update_value_with_definition(self, value, mapping_type):
331343
if value is not None and value not in self._parsing_config_variable_storage:
332344
if mapping_type == 'int64u' or mapping_type == 'int64s' or mapping_type == 'bitmap64' or mapping_type == 'epoch_us':
333345
value = fixes.try_apply_yaml_cpp_longlong_limitation_fix(value)
334-
value = fixes.try_apply_yaml_unrepresentable_integer_for_javascript_fixes(value)
346+
value = fixes.try_apply_yaml_unrepresentable_integer_for_javascript_fixes(
347+
value)
335348
elif mapping_type == 'single' or mapping_type == 'double':
336349
value = fixes.try_apply_yaml_float_written_as_strings(value)
337350
elif mapping_type == 'octet_string' or mapping_type == 'long_octet_string':
@@ -464,7 +477,7 @@ def _response_error_validation(self, response, result):
464477
error_success = 'The test expects the "{error}" error which occured successfully.'
465478
error_success_no_error = 'The test expects no error and no error occurred.'
466479
error_wrong_error = 'The test expects the "{error}" error but the "{value}" error occured.'
467-
error_unexpected_error = 'The test expects no error but the "{value}" error occured.'
480+
error_unexpected_error = 'The test expects no error but the "{error}" error occured.'
468481
error_unexpected_success = 'The test expects the "{error}" error but no error occured.'
469482

470483
expected_error = self.response.get('error') if self.response else None
@@ -478,14 +491,17 @@ def _response_error_validation(self, response, result):
478491
received_error = response.get('error')
479492

480493
if expected_error and received_error and expected_error == received_error:
481-
result.success(check_type, error_success.format(error=expected_error))
494+
result.success(check_type, error_success.format(
495+
error=expected_error))
482496
elif expected_error and received_error:
483497
result.error(check_type, error_wrong_error.format(
484498
error=expected_error, value=received_error))
485499
elif expected_error and not received_error:
486-
result.error(check_type, error_unexpected_success.format(error=expected_error))
500+
result.error(check_type, error_unexpected_success.format(
501+
error=expected_error))
487502
elif not expected_error and received_error:
488-
result.error(check_type, error_unexpected_error.format(error=received_error))
503+
result.error(check_type, error_unexpected_error.format(
504+
error=received_error))
489505
elif not expected_error and not received_error:
490506
result.success(check_type, error_success_no_error)
491507
else:
@@ -503,12 +519,14 @@ def _response_cluster_error_validation(self, response, result):
503519

504520
if expected_error:
505521
if received_error and expected_error == received_error:
506-
result.success(check_type, error_success.format(error=expected_error))
522+
result.success(check_type, error_success.format(
523+
error=expected_error))
507524
elif received_error:
508525
result.error(check_type, error_wrong_error.format(
509526
error=expected_error, value=received_error))
510527
else:
511-
result.error(check_type, error_unexpected_success.format(error=expected_error))
528+
result.error(check_type, error_unexpected_success.format(
529+
error=expected_error))
512530
else:
513531
# Nothing is logged here to not be redundant with the generic error checking code.
514532
pass
@@ -528,10 +546,12 @@ def _response_values_validation(self, response, result):
528546
if not self.is_attribute:
529547
expected_name = value.get('name')
530548
if expected_name not in received_value:
531-
result.error(check_type, error_name_does_not_exist.format(name=expected_name))
549+
result.error(check_type, error_name_does_not_exist.format(
550+
name=expected_name))
532551
continue
533552

534-
received_value = received_value.get(expected_name) if received_value else None
553+
received_value = received_value.get(
554+
expected_name) if received_value else None
535555

536556
# TODO Supports Array/List. See an exemple of failure in TestArmFailSafe.yaml
537557
expected_value = value.get('value')
@@ -557,10 +577,12 @@ def _response_constraints_validation(self, response, result):
557577
if not self.is_attribute:
558578
expected_name = value.get('name')
559579
if expected_name not in received_value:
560-
result.error(check_type, error_name_does_not_exist.format(name=expected_name))
580+
result.error(check_type, error_name_does_not_exist.format(
581+
name=expected_name))
561582
continue
562583

563-
received_value = received_value.get(expected_name) if received_value else None
584+
received_value = received_value.get(
585+
expected_name) if received_value else None
564586

565587
constraints = get_constraints(value['constraints'])
566588
if all([constraint.is_met(received_value) for constraint in constraints]):
@@ -583,14 +605,17 @@ def _maybe_save_as(self, response, result):
583605
if not self.is_attribute:
584606
expected_name = value.get('name')
585607
if expected_name not in received_value:
586-
result.error(check_type, error_name_does_not_exist.format(name=expected_name))
608+
result.error(check_type, error_name_does_not_exist.format(
609+
name=expected_name))
587610
continue
588611

589-
received_value = received_value.get(expected_name) if received_value else None
612+
received_value = received_value.get(
613+
expected_name) if received_value else None
590614

591615
save_as = value.get('saveAs')
592616
self._runtime_config_variable_storage[save_as] = received_value
593-
result.success(check_type, error_success.format(value=received_value, name=save_as))
617+
result.success(check_type, error_success.format(
618+
value=received_value, name=save_as))
594619

595620
def _update_placeholder_values(self, container):
596621
if not container:
@@ -600,7 +625,8 @@ def _update_placeholder_values(self, container):
600625

601626
for idx, item in enumerate(values):
602627
if 'value' in item:
603-
values[idx]['value'] = self._config_variable_substitution(item['value'])
628+
values[idx]['value'] = self._config_variable_substitution(
629+
item['value'])
604630

605631
if 'constraints' in item:
606632
for constraint, constraint_value in item['constraints'].items():
@@ -615,7 +641,8 @@ def _config_variable_substitution(self, value):
615641
elif type(value) is dict:
616642
mapped_value = {}
617643
for key in value:
618-
mapped_value[key] = self._config_variable_substitution(value[key])
644+
mapped_value[key] = self._config_variable_substitution(
645+
value[key])
619646
return mapped_value
620647
elif type(value) is str:
621648
# For most tests, a single config variable is used and it can be replaced as in.
@@ -669,7 +696,8 @@ def __init__(self, parsing_config_variable_storage: dict, definitions, tests: di
669696
fixes.try_update_yaml_node_id_test_runner_state(
670697
enabled_tests, self._parsing_config_variable_storage)
671698

672-
self._runtime_config_variable_storage = copy.deepcopy(parsing_config_variable_storage)
699+
self._runtime_config_variable_storage = copy.deepcopy(
700+
parsing_config_variable_storage)
673701
self._tests = enabled_tests
674702
self._index = 0
675703
self.count = len(self._tests)
@@ -692,18 +720,21 @@ def __init__(self, test_file, pics_file, definitions):
692720
# TODO Needs supports for PICS file
693721
with open(test_file) as f:
694722
loader = yaml.FullLoader
695-
loader = fixes.try_add_yaml_support_for_scientific_notation_without_dot(loader)
723+
loader = fixes.try_add_yaml_support_for_scientific_notation_without_dot(
724+
loader)
696725

697726
data = yaml.load(f, Loader=loader)
698727
_check_valid_keys(data, _TESTS_SECTION)
699728

700729
self.name = _value_or_none(data, 'name')
701730
self.PICS = _value_or_none(data, 'PICS')
702731

703-
self._parsing_config_variable_storage = _value_or_none(data, 'config')
732+
self._parsing_config_variable_storage = _value_or_none(
733+
data, 'config')
704734

705735
tests = _value_or_none(data, 'tests')
706-
self.tests = YamlTests(self._parsing_config_variable_storage, definitions, tests)
736+
self.tests = YamlTests(
737+
self._parsing_config_variable_storage, definitions, tests)
707738

708739
def update_config(self, key, value):
709740
self._parsing_config_variable_storage[key] = value

0 commit comments

Comments
 (0)