forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
1359 lines (1113 loc) · 56.3 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import logging
import re
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import Optional
from . import fixes
from .constraints import get_constraints, is_typed_constraint
from .definitions import SpecDefinitions
from .errors import (TestStepEnumError, TestStepEnumSpecifierNotUnknownError, TestStepEnumSpecifierWrongError, TestStepError,
TestStepKeyError, TestStepValueNameError)
from .pics_checker import PICSChecker
from .yaml_loader import YamlLoader
ANY_COMMANDS_CLUSTER_NAME = 'AnyCommands'
ANY_COMMANDS_LIST = [
'CommandById',
'ReadById',
'WriteById',
'SubscribeById',
'ReadEventById',
'SubscribeEventById',
'ReadAll',
'SubscribeAll',
]
# If True, enum values should use a valid name instead of a raw value
STRICT_ENUM_VALUE_CHECK = False
class UnknownPathQualifierError(TestStepError):
"""Raise when an attribute/command/event name is not found in the definitions."""
def __init__(self, content, target_type, target_name, candidate_names=[]):
if candidate_names:
message = f'Unknown {target_type}: "{target_name}". Candidates are: "{candidate_names}"'
for candidate_name in candidate_names:
if candidate_name.lower() == target_name.lower():
message = f'Unknown {target_type}: "{target_name}". Did you mean "{candidate_name}" ?'
break
else:
message = f'The cluster does not have any {target_type}s.'
super().__init__(message)
self.tag_key_with_error(content, target_type)
class TestStepAttributeKeyError(UnknownPathQualifierError):
"""Raise when an attribute name is not found in the definitions."""
def __init__(self, content, target_name, candidate_names=[]):
super().__init__(content, 'attribute', target_name, candidate_names)
class TestStepCommandKeyError(UnknownPathQualifierError):
"""Raise when a command name is not found in the definitions."""
def __init__(self, content, target_name, candidate_names=[]):
super().__init__(content, 'command', target_name, candidate_names)
class TestStepEventKeyError(UnknownPathQualifierError):
"""Raise when an event name is not found in the definitions."""
def __init__(self, content, target_name, candidate_names=[]):
super().__init__(content, 'event', target_name, candidate_names)
class PostProcessCheckStatus(Enum):
'''Indicates the post processing check step status.'''
SUCCESS = 'success',
WARNING = 'warning',
ERROR = 'error'
class PostProcessCheckType(Enum):
'''Indicates the post processing check step type.'''
IM_STATUS = auto()
CLUSTER_STATUS = auto()
RESPONSE_VALIDATION = auto()
CONSTRAINT_VALIDATION = auto()
SAVE_AS_VARIABLE = auto()
WAIT_VALIDATION = auto()
class PostProcessCheck:
'''Information about a single post processing operation that was performed.
Each check has a helpful message, indicating what the post processing operation did and whether
it was successful or not.
'''
def __init__(self, state: PostProcessCheckStatus, category: PostProcessCheckType, message: str, exception=None):
self.state = state
self.category = category
self.message = message
self.exception = exception
def is_success(self) -> bool:
return self.state == PostProcessCheckStatus.SUCCESS
def is_warning(self) -> bool:
return self.state == PostProcessCheckStatus.WARNING
def is_error(self) -> bool:
return self.state == PostProcessCheckStatus.ERROR
class PostProcessResponseResult:
'''Post processing response result information.
There are multiple operations that occur when post processing a response. This contains all the
results for each operation performed. Note that the number and types of steps performed is
dependant on test step itself.
'''
def __init__(self):
self.entries = []
self.successes = 0
self.warnings = 0
self.errors = 0
def success(self, category: PostProcessCheckType, message: str):
'''Adds a success entry that occured when post processing response to results.'''
self._insert(PostProcessCheckStatus.SUCCESS, category, message)
self.successes += 1
def warning(self, category: PostProcessCheckType, message: str):
'''Adds a warning entry that occured when post processing response to results.'''
self._insert(PostProcessCheckStatus.WARNING, category, message)
self.warnings += 1
def error(self, category: PostProcessCheckType, message: str, exception: TestStepError = None):
'''Adds an error entry that occured when post processing response to results.'''
self._insert(PostProcessCheckStatus.ERROR,
category, message, exception)
self.errors += 1
def is_success(self):
# It is possible that post processing a response doesn't have any success entires added
# that is why we explicitly only search for if an error occurred.
return self.errors == 0
def is_failure(self):
return self.errors != 0
def _insert(self, state: PostProcessCheckStatus, category: PostProcessCheckType, message: str, exception: Exception = None):
log = PostProcessCheck(state, category, message, exception)
self.entries.append(log)
def _value_or_none(data, key):
return data[key] if key in data else None
def _value_or_config(data, key, config):
return data[key] if key in data else config.get(key)
class EnumType:
def __init__(self, enum: Enum):
self.type = enum.name
self.base_type = enum.base_type
self._codes = {}
self.entries_by_name = {}
self.entries_by_code = {}
self._compute_entries(enum)
def translate(self, key: str, value) -> int:
if self._codes.get(key) is not None and self._codes.get(key) == value:
return self._codes.get(key)
if type(value) is str:
code = self._get_code_by_name(value)
else:
code = self._get_code_by_value(value)
if code is None:
raise TestStepEnumError(value, self.entries_by_name)
self._codes[key] = code
return code
def _get_code_by_name(self, value):
# For readability the name could sometimes be written as "enum_name(enum_code)" instead of "enum_name"
# In this case the enum_code should be checked to ensure that it is correct, unless enum_name is UnknownEnumValue
# in which case only invalid enum_code are allowed.
specified_name, specified_code = self._extract_name_and_code(value)
if specified_name not in self.entries_by_name:
return None
enum_code = self.entries_by_name.get(specified_name)
if specified_code is None or specified_code == enum_code:
return enum_code
if specified_name != f'{self.type}.UnknownEnumValue':
raise TestStepEnumSpecifierWrongError(
specified_code, specified_name, enum_code)
enum_name = self.entries_by_code.get(specified_code)
if enum_name:
raise TestStepEnumSpecifierNotUnknownError(value, enum_name)
return specified_code
def _get_code_by_value(self, value):
enum_name = self.entries_by_code.get(value)
if not enum_name:
return None
if STRICT_ENUM_VALUE_CHECK:
raise TestStepEnumError(value, self.entries_by_name)
return value
def _compute_entries(self, enum: Enum):
enum_codes = []
for enum_entry in enum.entries:
name = f'{self.type}.{enum_entry.name}'
code = enum_entry.code
self.entries_by_name[name] = code
self.entries_by_code[code] = name
enum_codes.append(code)
# search for the first invalid entry if any
max_code = 0xFF + 1
if self.base_type == 'enum16':
max_code = 0xFFFF + 1
for code in range(0, max_code):
if code not in enum_codes:
name = f'{self.type}.UnknownEnumValue'
self.entries_by_name[name] = code
self.entries_by_code[code] = name
break
def _extract_name_and_code(self, enum_name: str):
match = re.match(r"([\w.]+)(?:\((\w+)\))?", enum_name)
if match:
name = match.group(1)
code = int(match.group(2)) if match.group(2) else None
return name, code
return None, None
@staticmethod
def is_valid_type(target_type: str):
return target_type == 'enum8' or target_type == 'enum16'
class _TestStepWithPlaceholders:
'''A single YAML test parsed, as is, from YAML.
Some YAML test steps contain placeholders for variable subsitution. The value of the variable
is only known after an earlier test step's has executed and the result successfully post
processed.
'''
def __init__(self, test: dict, config: dict, definitions: SpecDefinitions, pics_checker: PICSChecker):
# Disabled tests are not parsed in order to allow the test to be added to the test
# suite even if the feature is not implemented yet.
self.is_enabled = not ('disabled' in test and test['disabled'])
if not self.is_enabled:
return
self._parsing_config_variable_storage = config
self.label = _value_or_none(test, 'label')
self.node_id = _value_or_config(test, 'nodeId', config)
self.group_id = _value_or_config(test, 'groupId', config)
self.cluster = _value_or_config(test, 'cluster', config)
self.command = _value_or_config(test, 'command', config)
if not self.command:
self.command = _value_or_config(test, 'wait', config)
self.attribute = _value_or_none(test, 'attribute')
self.event = _value_or_none(test, 'event')
self.endpoint = _value_or_config(test, 'endpoint', config)
self.pics = _value_or_none(test, 'PICS')
self.is_pics_enabled = pics_checker.check(_value_or_none(test, 'PICS'))
self.identity = _value_or_none(test, 'identity')
self.fabric_filtered = _value_or_none(test, 'fabricFiltered')
self.min_interval = _value_or_none(test, 'minInterval')
self.max_interval = _value_or_none(test, 'maxInterval')
self.keep_subscriptions = _value_or_none(test, 'keepSubscriptions')
self.timed_interaction_timeout_ms = _value_or_none(
test, 'timedInteractionTimeoutMs')
self.timeout = _value_or_none(test, 'timeout')
self.data_version = _value_or_none(
test, 'dataVersion')
self.busy_wait_ms = _value_or_none(test, 'busyWaitMs')
self.wait_for = _value_or_none(test, 'wait')
self.event_number = _value_or_none(test, 'eventNumber')
self.run_if = _value_or_none(test, 'runIf')
self.save_response_as = _value_or_none(test, 'saveResponseAs')
self.is_attribute = self.__is_attribute_command()
self.is_event = self.__is_event_command()
arguments = _value_or_none(test, 'arguments')
self._convert_single_value_to_values(arguments)
self.arguments_with_placeholders = arguments
responses = _value_or_none(test, 'response')
# Test may expect multiple responses. For example reading events may
# trigger multiple event responses. Or reading multiple attributes
# at the same time, may trigger multiple responses too.
if responses is None:
# If no response is specified at all, it implies that the step expect
# a success with any associatied value(s). So the empty response is effectively
# replace by an array that contains an empty object to represent that.
responses = [{}]
elif not isinstance(responses, list):
# If a single response is specified, it is converted to a list of responses.
responses = [responses]
for response in responses:
self._convert_single_value_to_values(response)
self.responses_with_placeholders = responses
self._update_mappings(test, definitions)
self.update_arguments(self.arguments_with_placeholders)
self.update_responses(self.responses_with_placeholders)
# This performs a very basic sanity parse time check of constraints. This parsing happens
# again inside post processing response since at that time we will have required variables
# to substitute in. This parsing check here has value since some test can take a really
# long time to run so knowing earlier on that the test step would have failed at parsing
# time before the test step run occurs save developer time that building yaml tests.
for response in self.responses_with_placeholders:
for value in response:
if 'constraints' not in value:
continue
get_constraints(value['constraints'])
def _update_mappings(self, test: dict, definitions: SpecDefinitions):
cluster_name = self.cluster
if definitions is None or (not definitions.has_cluster_by_name(cluster_name) and cluster_name != ANY_COMMANDS_CLUSTER_NAME):
self.argument_mapping = None
self.response_mapping = None
self.response_mapping_name = None
return
argument_mapping = None
response_mapping = None
response_mapping_name = None
if self.is_attribute:
attribute_name = self.attribute
attribute = definitions.get_attribute_by_name(
cluster_name,
attribute_name
)
if not attribute:
targets = definitions.get_attribute_names(cluster_name)
raise TestStepAttributeKeyError(test, attribute_name, targets)
attribute_mapping = self._as_mapping(
definitions,
cluster_name,
attribute.definition.data_type.name
)
argument_mapping = attribute_mapping
response_mapping = attribute_mapping
response_mapping_name = attribute.definition.data_type.name
elif self.is_event:
event_name = self.event
event = definitions.get_event_by_name(
cluster_name,
event_name
)
if not event:
targets = definitions.get_event_names(cluster_name)
raise TestStepEventKeyError(test, event_name, targets)
event_mapping = self._as_mapping(
definitions,
cluster_name,
event_name
)
argument_mapping = event_mapping
response_mapping = event_mapping
response_mapping_name = event.name
elif cluster_name == ANY_COMMANDS_CLUSTER_NAME or self.command in ANY_COMMANDS_LIST:
# When the cluster is ANY_COMMANDS_CLUSTER_NAME the test step does not contain the direct mapping
# for the response in the cluster/command/attribute/event fields.
#
# When the command is part of ANY_COMMANDS_LIST the test step does not contain the direct mapping
# for the response in the command/attribute/event fields.
#
# NOTE: The logic for this paragraph has not yet be implemented.
# In some cases the response type can be inferred from the argument fields, if for example the command
# is a ReadById targetting a specific ClusterId/AttributeId that exists in the definitions.
#
# For the other cases, the response can NOT be inferred directly from the argumment fields, if for exammple
# the command is a ReadById using a wildcard in one of its fields. For this type of case, the test writer
# can add additional specifiers in the expected response type to help determine the response mapping.
mapping_names = []
for response in self.responses_with_placeholders:
for value in response.get('values'):
if 'constraints' not in value:
continue
mapping_name = None
cluster_name = self.cluster if self.cluster != ANY_COMMANDS_CLUSTER_NAME else response.get(
'cluster')
if cluster_name is not None:
attribute_name = response.get('attribute')
event_name = response.get('event')
command_name = response.get('command')
if attribute_name:
attribute = definitions.get_attribute_by_name(
cluster_name, attribute_name)
if not attribute:
targets = definitions.get_attribute_names(
cluster_name)
test['response'] = ['...', response, '...']
raise TestStepAttributeKeyError(
response, attribute_name, targets)
mapping_name = attribute.definition.data_type.name
elif event_name:
event = definitions.get_event_by_name(
cluster_name, event_name)
if not event:
targets = definitions.get_event_names(
cluster_name)
test['response'] = ['...', response, '...']
raise TestStepEventKeyError(
response, attribute_name, targets)
mapping_name = event.name
elif command_name:
command = definitions.get_command_by_name(
cluster_name, command_name)
if not command:
targets = definitions.get_command_names(
cluster_name)
test['response'] = ['...', response, '...']
raise TestStepCommandKeyError(
response, command_name, targets)
mapping_name = command.output_param
mapping_names.append(mapping_name)
# TODO: For now only the response_mapping_name is inferred, it allows to use the type constraint
# on the responses.
argument_mapping = None
response_mapping = None
response_mapping_name = mapping_names
else:
command_name = self.command
command = definitions.get_command_by_name(
cluster_name,
command_name
)
if not command:
targets = definitions.get_command_names(cluster_name)
raise TestStepCommandKeyError(test, command_name, targets)
if command.input_param is None:
argument_mapping = {}
else:
argument_mapping = self._as_mapping(
definitions,
cluster_name,
command.input_param
)
response_mapping = self._as_mapping(
definitions,
cluster_name,
command.output_param
)
response_mapping_name = command.output_param
self.argument_mapping = argument_mapping
self.response_mapping = response_mapping
self.response_mapping_name = response_mapping_name
def _convert_single_value_to_values(self, container):
if container is None or 'values' in container:
return
# Attribute tests pass a single value argument that does not carry a name but
# instead uses a generic 'value' keyword. Convert to keyword to be the single
# members of the 'values' array which is what is used for other tests.
value = {}
known_keys_to_copy = ['value', 'constraints',
'saveAs', 'saveDataVersionAs']
known_keys_to_allow = ['error', 'clusterError']
for key, item in list(container.items()):
if key in known_keys_to_copy:
value[key] = item
del container[key]
elif key in known_keys_to_allow:
# Nothing to do for those keys.
pass
else:
raise TestStepKeyError(item, key)
container['values'] = [value]
def _as_mapping(self, definitions, cluster_name, target_name):
element = definitions.get_type_by_name(cluster_name, target_name)
if hasattr(element, 'base_type'):
if EnumType.is_valid_type(element.base_type):
target_name = EnumType(element)
else:
target_name = element.base_type
elif hasattr(element, 'fields'):
target_name = {f.name: self._as_mapping(
definitions, cluster_name, f.data_type.name) for f in element.fields}
elif target_name:
target_name = target_name.lower()
return target_name
def update_arguments(self, arguments_with_placeholders):
self._update_with_definition(
arguments_with_placeholders, self.argument_mapping)
def update_responses(self, responses_with_placeholders):
for response in responses_with_placeholders:
self._update_with_definition(
response, self.response_mapping)
def _update_with_definition(self, container: dict, mapping_type):
if not container or mapping_type is None:
return
values = container['values']
if values is None:
return
for value in list(values):
for key, item_value in list(value.items()):
if self.is_attribute or self.is_event:
mapping = mapping_type
else:
target_key = value['name']
if mapping_type.get(target_key) is None:
raise TestStepValueNameError(
value, target_key, [key for key in mapping_type])
mapping = mapping_type[target_key]
if key == 'value':
value[key] = self._update_value_with_definition(
value,
key,
item_value,
mapping
)
elif key == 'saveAs' and type(item_value) is str and item_value not in self._parsing_config_variable_storage:
self._parsing_config_variable_storage[item_value] = None
elif key == 'saveDataVersionAs' and type(item_value) is str and item_value not in self._parsing_config_variable_storage:
self._parsing_config_variable_storage[item_value] = None
elif key == 'constraints':
for constraint, constraint_value in item_value.items():
# Only apply update_value_with_definition to constraints that have a value that depends on
# the the value type for the target field.
if is_typed_constraint(constraint):
value[key][constraint] = self._update_value_with_definition(
item_value,
constraint,
constraint_value,
mapping
)
else:
# This key, value pair does not rely on cluster specifications.
pass
def _update_value_with_definition(self, container: dict, key: str, value, mapping_type):
"""
Processes a given value based on a specified mapping type and returns the updated value.
This method does not modify the container in place; rather, it returns a new value that should be
used to update or process further as necessary.
The 'container' and 'key' parameters are primarily used for error tagging. If an error occurs
during the value processing, these parameters allow for the error to be precisely located and
reported, facilitating easier debugging and error tracking.
Parameters:
- container (dict): A dictionary that serves as a context for the operation. It is used for error
tagging if processing fails, by associating errors with specific locations within the data structure.
- key (str): The key related to the value being processed. It is used alongside 'container' to tag
errors, enabling precise identification of the error source.
- value: The value to be processed according to the mapping type.
- mapping_type: Dictates the processing or mapping logic to be applied to 'value'.
Returns:
The processed value, which is the result of applying the specified mapping type to the original 'value'.
This method does not update the 'container'; any necessary updates based on the processed value must
be handled outside this method.
Raises:
- TestStepError: If an error occurs during the processing of the value. The error includes details
from the 'container' and 'key' to facilitate error tracing and debugging.
"""
if not mapping_type:
return value
if type(value) is dict:
rv = {}
for item_key in value:
# FabricIndex is a special case where the framework requires it to be passed even
# if it is not part of the requested arguments per spec and not part of the XML
# definition.
if item_key == 'FabricIndex' or item_key == 'fabricIndex':
rv[item_key] = value[item_key] # int64u
else:
if not mapping_type.get(item_key):
raise TestStepKeyError(value, item_key)
mapping = mapping_type[item_key]
rv[item_key] = self._update_value_with_definition(
value,
item_key,
value[item_key],
mapping
)
return rv
if type(value) is list:
return [self._update_value_with_definition(container, key, entry, mapping_type) for entry in value]
# TODO currently unsure if the check of `value not in config` is sufficant. For
# example let's say value = 'foo + 1' and map type is 'int64u', we would arguably do
# the wrong thing below.
if value is not None and value not in self._parsing_config_variable_storage:
if type(mapping_type) is EnumType:
try:
value = mapping_type.translate(key, value)
except (TestStepEnumError, TestStepEnumSpecifierNotUnknownError, TestStepEnumSpecifierWrongError) as e:
e.tag_key_with_error(container, key)
raise e
elif mapping_type == 'int64u' or mapping_type == 'int64s' or mapping_type == 'bitmap64' or mapping_type == 'epoch_us':
value = fixes.try_apply_float_to_integer_fix(value)
value = fixes.try_apply_yaml_cpp_longlong_limitation_fix(value)
value = fixes.try_apply_yaml_unrepresentable_integer_for_javascript_fixes(
value)
elif mapping_type == 'single' or mapping_type == 'double':
value = fixes.try_apply_yaml_float_written_as_strings(value)
elif isinstance(value, float) and mapping_type != 'single' and mapping_type != 'double':
value = fixes.try_apply_float_to_integer_fix(value)
elif mapping_type == 'octet_string' or mapping_type == 'long_octet_string':
value = fixes.convert_yaml_octet_string_to_bytes(value)
elif mapping_type == 'boolean':
value = bool(value)
return value
def __is_attribute_command(self) -> bool:
commands = {
'readAttribute',
'writeAttribute',
'subscribeAttribute',
'waitForReport',
}
return self.attribute and (self.command in commands or self.wait_for in commands)
def __is_event_command(self) -> bool:
commands = {
'readEvent',
'subscribeEvent',
'waitForReport',
}
return self.event and (self.command in commands or self.wait_for in commands)
class TestStep:
'''A single YAML test action parsed from YAML.
This object contains all the information required for a test runner to execute the test step.
It also provide a function that is expected to be called by the test runner to post process
the recieved response from the accessory. Post processing both validates recieved response
and saves any variables that might be required but test step that have yet to be executed.
'''
def __init__(self, test: _TestStepWithPlaceholders, step_index: int, runtime_config_variable_storage: dict):
self._test = test
self._step_index = step_index
self._runtime_config_variable_storage = runtime_config_variable_storage
self.arguments = copy.deepcopy(test.arguments_with_placeholders)
self.responses = copy.deepcopy(test.responses_with_placeholders)
if test.is_pics_enabled:
self._update_placeholder_values(self.arguments)
self._update_placeholder_values(self.responses)
self._test.data_version = self._config_variable_substitution(
self._test.data_version)
self._test.node_id = self._config_variable_substitution(
self._test.node_id)
self._test.run_if = self._config_variable_substitution(
self._test.run_if)
self._test.event_number = self._config_variable_substitution(
self._test.event_number)
self._test.cluster = self._config_variable_substitution(
self._test.cluster)
self._test.command = self._config_variable_substitution(
self._test.command)
self._test.attribute = self._config_variable_substitution(
self._test.attribute)
self._test.event = self._config_variable_substitution(
self._test.event)
self._test.endpoint = self._config_variable_substitution(
self._test.endpoint)
self._test.group_id = self._config_variable_substitution(
self._test.group_id)
self._test.node_id = self._config_variable_substitution(
self._test.node_id)
test.update_arguments(self.arguments)
test.update_responses(self.responses)
@property
def step_index(self):
return self._step_index
@property
def is_enabled(self):
return self._test.is_enabled
@property
def is_pics_enabled(self):
return self._test.is_pics_enabled and (self._test.run_if is None or self._test.run_if)
@property
def is_attribute(self):
return self._test.is_attribute
@property
def is_event(self):
return self._test.is_event
@property
def label(self):
return self._test.label
@property
def node_id(self):
return self._test.node_id
@property
def group_id(self):
return self._test.group_id
@property
def cluster(self):
return self._test.cluster
@property
def command(self):
return self._test.command
@property
def attribute(self):
return self._test.attribute
@property
def event(self):
return self._test.event
@property
def endpoint(self):
return self._test.endpoint
@property
def identity(self):
return self._test.identity
@property
def fabric_filtered(self):
return self._test.fabric_filtered
@property
def min_interval(self):
return self._test.min_interval
@property
def max_interval(self):
return self._test.max_interval
@property
def keep_subscriptions(self):
return self._test.keep_subscriptions
@property
def timed_interaction_timeout_ms(self):
return self._test.timed_interaction_timeout_ms
@property
def timeout(self):
return self._test.timeout
@property
def data_version(self):
return self._test.data_version
@property
def busy_wait_ms(self):
return self._test.busy_wait_ms
@property
def wait_for(self):
return self._test.wait_for
@property
def event_number(self):
return self._test.event_number
@event_number.setter
def event_number(self, value):
self._test.event_number = value
@property
def pics(self):
return self._test.pics
def _get_last_event_number(self, responses) -> Optional[int]:
if not self.is_event:
return None
# find the largest event number in all responses
# This iterates over everything (not just last element) since some commands like
# `chip-tool any read-all` may return multiple replies
event_number = None
for response in responses:
if not isinstance(response, dict):
continue
received_event_number = response.get('eventNumber')
if not isinstance(received_event_number, int):
continue
if (event_number is None) or (event_number < received_event_number):
event_number = received_event_number
return event_number
def post_process_response(self, received_responses):
result = PostProcessResponseResult()
# A list of responses is what is expected, but for legacy, if the response
# does not comes up as a list, it is converted here.
# TODO It should be removed once all decoders returns a list.
if not isinstance(received_responses, list):
received_responses = [received_responses]
if self._test.save_response_as:
self._runtime_config_variable_storage[self._test.save_response_as] = received_responses
if self.is_event:
last_event_number = self._get_last_event_number(received_responses)
if last_event_number:
if 'LastReceivedEventNumber' in self._runtime_config_variable_storage:
if self._runtime_config_variable_storage['LastReceivedEventNumber'] > last_event_number:
logging.warning(
"Received an older event than expected: received %r < %r",
last_event_number,
self._runtime_config_variable_storage['LastReceivedEventNumber']
)
self._runtime_config_variable_storage['LastReceivedEventNumber'] = last_event_number
if self.wait_for is not None:
self._response_cluster_wait_validation(received_responses, result)
return result
check_type = PostProcessCheckType.RESPONSE_VALIDATION
error_failure_wrong_response_number = (f'The test expects {len(self.responses)} responses '
f'but got {len(received_responses)} responses.')
received_responses_copy = copy.deepcopy(received_responses)
for expected_response in self.responses:
if len(received_responses_copy) == 0:
result.error(check_type, error_failure_wrong_response_number)
return result
received_response = received_responses_copy.pop(0)
self._response_error_validation(
expected_response, received_response, result)
self._response_cluster_error_validation(
expected_response, received_response, result)
self._response_values_source_validation(
expected_response, received_response, result)
self._response_values_validation(
expected_response, received_response, result)
self._response_constraints_validation(
expected_response, received_response, result)
self._maybe_save_as('saveAs', 'value',
expected_response, received_response, result)
self._maybe_save_as('saveDataVersionAs', 'dataVersion',
expected_response, received_response, result)
# An empty response array in a test step (responses: []) implies that the test step does expect a response
# but without any associated value.
if self.responses == [] and received_responses_copy == [{}]:
# if the received responses is a simple success ([{}]), that is valid.
return result
# This is different from the case where no response is specified at all, which implies that the step expect
# a success with any associatied value(s).
elif self.responses == [{'values': [{}]}] and len(received_responses_copy):
# if there are multiple responses and the test specifies that it does not really care
# about which values are returned, that is valid too.
return result
# Anything more complex where the response field as been defined with some values and the number
# of expected responses differs from the number of received responses is an error.
elif len(received_responses_copy) != 0:
result.error(check_type, error_failure_wrong_response_number)
return result
def _response_cluster_wait_validation(self, received_responses, result):
"""Check if the response concrete path matches the configuration of the test step
and validate that the response type (e.g readAttribute/writeAttribute/...) matches
the expectation from the test step."""
check_type = PostProcessCheckType.WAIT_VALIDATION
error_success = 'The test expectation "{wait_for}" for "{cluster}.{wait_type}" on endpoint {endpoint} is true'
error_failure = 'The test expectation "{expected} == {received}" is false'
if len(received_responses) > 1:
result.error(check_type, error_failure.multiple_responses)
return
received_response = received_responses[0]
if self.is_attribute:
expected_wait_type = self.attribute
received_wait_type = received_response.get('attribute')
elif self.is_event:
expected_wait_type = self.event
received_wait_type = received_response.get('event')
else:
expected_wait_type = self.command
received_wait_type = received_response.get('command')
expected_values = [
self.wait_for,
self.endpoint,
# TODO The name in tests does not always use spaces
self.cluster.replace(' ', ''),
expected_wait_type
]
wait_for_str = received_response.get('wait_for')
if not wait_for_str:
wait_for_str = received_response.get('command')
received_values = [
wait_for_str,
received_response.get('endpoint'),
received_response.get('cluster'),
received_wait_type
]
success = True
for expected_value in expected_values:
received_value = received_values.pop(0)
if expected_value != received_value:
result.error(check_type, error_failure.format(
expected=expected_value, received=received_value))
success = False
if success: