-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[matter_yamltests] Add an abstract interface for pseudo clusters and …
…a base class to register/use them
- Loading branch information
1 parent
7c17463
commit 044297d
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
scripts/py_matter_yamltests/matter_yamltests/test_cluster.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) 2023 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. | ||
|
||
from abc import ABC, abstractproperty | ||
|
||
|
||
class TestCluster(ABC): | ||
""" | ||
TestCluster is an abstract interface that custom pseudo clusters | ||
should inherit from. | ||
The interface expose a name property that is used while looking | ||
where to dispatch the test step to. | ||
The implementation should then expose methods where the name match | ||
the name used in the YAML test file. | ||
For example, the 'CustomCommands' pseudo cluster can be implemented as: | ||
class CustomCommand(TestCluster): | ||
name = 'CustomCommands' | ||
async def MyCustomMethod(self, request): | ||
pass | ||
async def MyCustomMethod(self, request): | ||
pass | ||
It can then be called from any test step as: | ||
- label: "Call a custom method" | ||
cluster: "CustomCommands" | ||
command: "MyCustomMethod" | ||
arguments: | ||
values: | ||
- name: "MyCustomParameter" | ||
value: "this_is_a_custom_value" | ||
""" | ||
|
||
@abstractproperty | ||
def name(self): | ||
pass |
43 changes: 43 additions & 0 deletions
43
scripts/py_matter_yamltests/matter_yamltests/test_clusters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2023 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. | ||
|
||
from matter_yamltests.test_cluster import TestCluster | ||
|
||
|
||
class TestClusters: | ||
__clusters: list[TestCluster] | ||
|
||
def __init__(self, clusters: list[TestCluster]): | ||
self.__clusters = clusters | ||
|
||
def supports(self, request): | ||
return False if self.__get_command(request) is None else True | ||
|
||
async def execute(self, request): | ||
status = {'error': 'FAILURE'} | ||
|
||
command = self.__get_command(request) | ||
if command: | ||
status = await command(request) | ||
# If the command does not returns an error, it is considered a success. | ||
if status == None: | ||
status = {} | ||
|
||
return status, [] | ||
|
||
def __get_command(self, request): | ||
for cluster in self.__clusters: | ||
if request.cluster == cluster.name and getattr(cluster, request.command, None): | ||
return getattr(cluster, request.command) | ||
return None |