Skip to content

Commit

Permalink
[matter_yamltests] Add an abstract interface for pseudo clusters and …
Browse files Browse the repository at this point in the history
…a base class to register/use them
  • Loading branch information
vivien-apple committed Jan 24, 2023
1 parent 7c17463 commit 044297d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scripts/py_matter_yamltests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pw_python_package("matter_yamltests") {
"matter_yamltests/fixes.py",
"matter_yamltests/parser.py",
"matter_yamltests/pics_checker.py",
"matter_yamltests/test_cluster.py",
"matter_yamltests/test_clusters.py",
]

python_deps = [ "${chip_root}/scripts/py_matter_idl:matter_idl" ]
Expand Down
53 changes: 53 additions & 0 deletions scripts/py_matter_yamltests/matter_yamltests/test_cluster.py
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 scripts/py_matter_yamltests/matter_yamltests/test_clusters.py
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

0 comments on commit 044297d

Please sign in to comment.