From 0005dfcec9e8da69a10ae73772347a553805cc85 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 17 Jan 2023 13:14:24 +0100 Subject: [PATCH] Add matter_yamlclusters with a set of default clusters for running some of the yaml tests --- scripts/BUILD.gn | 5 +- scripts/py_matter_yamlclusters/BUILD.gn | 41 +++++++ .../matter_yamlclusters/__init__.py | 0 .../matter_yamlclusters/cluster/__init__.py | 0 .../cluster/delay_commands.py | 33 ++++++ .../cluster/log_commands.py | 26 +++++ .../cluster/system_commands.py | 106 ++++++++++++++++++ .../matter_yamlclusters/clusters.py | 26 +++++ scripts/py_matter_yamlclusters/pyproject.toml | 17 +++ scripts/py_matter_yamlclusters/setup.cfg | 24 ++++ scripts/py_matter_yamlclusters/setup.py | 20 ++++ 11 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 scripts/py_matter_yamlclusters/BUILD.gn create mode 100644 scripts/py_matter_yamlclusters/matter_yamlclusters/__init__.py create mode 100644 scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/__init__.py create mode 100644 scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/delay_commands.py create mode 100644 scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/log_commands.py create mode 100644 scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/system_commands.py create mode 100644 scripts/py_matter_yamlclusters/matter_yamlclusters/clusters.py create mode 100644 scripts/py_matter_yamlclusters/pyproject.toml create mode 100644 scripts/py_matter_yamlclusters/setup.cfg create mode 100644 scripts/py_matter_yamlclusters/setup.py diff --git a/scripts/BUILD.gn b/scripts/BUILD.gn index e14db546195de5..7699e4385ec7de 100644 --- a/scripts/BUILD.gn +++ b/scripts/BUILD.gn @@ -23,7 +23,10 @@ import("$dir_pw_build/python_dist.gni") # out/obj/matter_yamltests_distribution/ <- source files here # out/obj/matter_yamltests_distribution._build_wheel/matter_yamltests-0.0.1-py3-none-any.whl pw_python_distribution("matter_yamltests_distribution") { - packages = [ "${chip_root}/scripts/py_matter_yamltests:matter_yamltests" ] + packages = [ + "${chip_root}/scripts/py_matter_yamltests:matter_yamltests", + "${chip_root}/scripts/py_matter_yamlclusters:matter_yamlclusters", + ] generate_setup_cfg = { common_config_file = "common_setup.cfg" include_default_pyproject_file = true diff --git a/scripts/py_matter_yamlclusters/BUILD.gn b/scripts/py_matter_yamlclusters/BUILD.gn new file mode 100644 index 00000000000000..5f3235c0bba7f0 --- /dev/null +++ b/scripts/py_matter_yamlclusters/BUILD.gn @@ -0,0 +1,41 @@ +# 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. + +import("//build_overrides/build.gni") +import("//build_overrides/chip.gni") + +import("//build_overrides/pigweed.gni") +import("$dir_pw_build/python.gni") + +pw_python_package("matter_yamlclusters") { + setup = [ + "setup.py", + "setup.cfg", + "pyproject.toml", + ] + + sources = [ + "matter_yamlclusters/__init__.py", + "matter_yamlclusters/clusters.py", + "matter_yamlclusters/cluster/delay_commands.py", + "matter_yamlclusters/cluster/log_commands.py", + "matter_yamlclusters/cluster/system_commands.py", + ] + + python_deps = [ "${chip_root}/scripts/py_matter_yamltests:matter_yamltests" ] + + # TODO: at a future time consider enabling all (* or missing) here to get + # pylint checking these files + static_analysis = [] +} diff --git a/scripts/py_matter_yamlclusters/matter_yamlclusters/__init__.py b/scripts/py_matter_yamlclusters/matter_yamlclusters/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/__init__.py b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/delay_commands.py b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/delay_commands.py new file mode 100644 index 00000000000000..cab38dd5b891b5 --- /dev/null +++ b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/delay_commands.py @@ -0,0 +1,33 @@ +# +# 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. + +import sys +import time + +from matter_yamltests.test_cluster import TestCluster + + +class DelayCommands(TestCluster): + name = 'DelayCommands' + + async def WaitForMs(self, request): + duration_in_ms = 0 + + for argument in request.arguments['values']: + if argument['name'] == 'ms': + duration_in_ms = argument['value'] + + sys.stdout.flush() + time.sleep(duration_in_ms / 1000) diff --git a/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/log_commands.py b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/log_commands.py new file mode 100644 index 00000000000000..5564b2f3df6e5d --- /dev/null +++ b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/log_commands.py @@ -0,0 +1,26 @@ +# +# 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 LogCommands(TestCluster): + name = 'LogCommands' + + async def UserPrompt(self, request): + pass + + async def Log(self, request): + pass diff --git a/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/system_commands.py b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/system_commands.py new file mode 100644 index 00000000000000..265b82fe2cfdab --- /dev/null +++ b/scripts/py_matter_yamlclusters/matter_yamlclusters/cluster/system_commands.py @@ -0,0 +1,106 @@ +# +# 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. + +import sys +import xmlrpc.client + +from matter_yamltests.test_cluster import TestCluster + +DEFAULT_KEY = 'default' +IP = '127.0.0.1' +PORT = 9000 + +if sys.platform == 'linux': + IP = '10.10.10.5' + + +def make_url(): + return 'http://' + IP + ':' + str(PORT) + '/' + + +def get_register_key(request): + if request.arguments: + values = request.arguments['values'] + for item in values: + name = item['name'] + value = item['value'] + if name == 'registerKey': + return value + + return DEFAULT_KEY + + +def get_options(request): + options = [] + + if request.arguments: + values = request.arguments['values'] + for item in values: + name = item['name'] + value = item['value'] + + if name == 'discriminator': + options.append('--discriminator') + options.append(str(value)) + elif name == 'port': + options.append('--secured-device-port') + options.append(str(value)) + elif name == 'kvs': + options.append('--KVS') + options.append(str(value)) + elif name == 'minCommissioningTimeout': + options.append('--min_commissioning_timeout') + options.append(str(value)) + elif name == 'filepath': + options.append('--filepath') + options.append(str(value)) + elif name == 'otaDownloadPath': + options.append('--otaDownloadPath') + options.append(str(value)) + elif name == 'registerKey': + pass + else: + raise KeyError(f'Unknown key: {name}') + + return options + + +class SystemCommands(TestCluster): + name = 'SystemCommands' + + async def Start(self, request): + register_key = get_register_key(request) + options = get_options(request) + + with xmlrpc.client.ServerProxy(make_url(), allow_none=True) as proxy: + proxy.start(register_key, options) + + async def Stop(self, request): + register_key = get_register_key(request) + + with xmlrpc.client.ServerProxy(make_url(), allow_none=True) as proxy: + proxy.stop(register_key) + + async def Reboot(self, request): + register_key = get_register_key(request) + + with xmlrpc.client.ServerProxy(make_url(), allow_none=True) as proxy: + proxy.reboot(register_key) + + async def FactoryReset(self, request): + register_key = get_register_key(request) + + with xmlrpc.client.ServerProxy(make_url(), allow_none=True) as proxy: + proxy.factoryReset(register_key) diff --git a/scripts/py_matter_yamlclusters/matter_yamlclusters/clusters.py b/scripts/py_matter_yamlclusters/matter_yamlclusters/clusters.py new file mode 100644 index 00000000000000..06e56cb27e0b0d --- /dev/null +++ b/scripts/py_matter_yamlclusters/matter_yamlclusters/clusters.py @@ -0,0 +1,26 @@ +# +# 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_yamlclusters.cluster.delay_commands import DelayCommands +from matter_yamlclusters.cluster.log_commands import LogCommands +from matter_yamlclusters.cluster.system_commands import SystemCommands + + +def get_default_clusters(): + return [ + DelayCommands(), + LogCommands(), + SystemCommands() + ] diff --git a/scripts/py_matter_yamlclusters/pyproject.toml b/scripts/py_matter_yamlclusters/pyproject.toml new file mode 100644 index 00000000000000..9080987af6e7be --- /dev/null +++ b/scripts/py_matter_yamlclusters/pyproject.toml @@ -0,0 +1,17 @@ +# 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. + +[build-system] +requires = ['setuptools', 'wheel'] +build-backend = 'setuptools.build_meta' diff --git a/scripts/py_matter_yamlclusters/setup.cfg b/scripts/py_matter_yamlclusters/setup.cfg new file mode 100644 index 00000000000000..2f660cc66311bf --- /dev/null +++ b/scripts/py_matter_yamlclusters/setup.cfg @@ -0,0 +1,24 @@ +# 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. + +[metadata] +name = matter_yamlclusters +version = 0.0.1 +author = Project CHIP Authors +description = Implement YAML test clusters + +[options] +packages = find: + +zip_safe = False diff --git a/scripts/py_matter_yamlclusters/setup.py b/scripts/py_matter_yamlclusters/setup.py new file mode 100644 index 00000000000000..a05bcb1a7edebd --- /dev/null +++ b/scripts/py_matter_yamlclusters/setup.py @@ -0,0 +1,20 @@ +# 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. + + +"""The matter_yamlclusters package.""" + +import setuptools # type: ignore + +setuptools.setup() # Package definition in setup.cfg