From 1390674d799714afd0c9b52d8a3d2e00bf12022e Mon Sep 17 00:00:00 2001 From: C Freeman Date: Thu, 11 Apr 2024 14:45:52 -0400 Subject: [PATCH] TC-TMP-2.1: Fix (#32881) * TC-TMP-2.1: Fix The constraints here are actually relatively complex because the min and max can be null and fall back to default values. Updated the automation to python so we can do the appropriate tests, added tests for the various scenarios becuase there are a LOT and we don't cover any of them with our current examples. Please see https://github.com/CHIP-Specifications/chip-test-plans/pull/4096 for test plan updates * remove yaml * missed a change * Restyled by autopep8 * remove unused import * Apply suggestions from code review --------- Co-authored-by: Restyled.io --- .../suites/certification/Test_TC_TMP_2_1.yaml | 74 ------- src/python_testing/TC_TMP_2_1.py | 97 ++++++++++ src/python_testing/matter_testing_support.py | 27 ++- .../test_testing/MockTestRunner.py | 57 ++++++ .../test_testing/test_TC_TMP_2_1.py | 180 ++++++++++++++++++ 5 files changed, 352 insertions(+), 83 deletions(-) delete mode 100644 src/app/tests/suites/certification/Test_TC_TMP_2_1.yaml create mode 100644 src/python_testing/TC_TMP_2_1.py create mode 100644 src/python_testing/test_testing/MockTestRunner.py create mode 100644 src/python_testing/test_testing/test_TC_TMP_2_1.py diff --git a/src/app/tests/suites/certification/Test_TC_TMP_2_1.yaml b/src/app/tests/suites/certification/Test_TC_TMP_2_1.yaml deleted file mode 100644 index a239c77b01dd05..00000000000000 --- a/src/app/tests/suites/certification/Test_TC_TMP_2_1.yaml +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2021 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. - -name: 7.2.1. [TC-TMP-2.1] Attributes with server as DUT - -PICS: - - TMP.S - -config: - nodeId: 0x12344321 - cluster: "Temperature Measurement" - endpoint: 1 - -tests: - - label: "Step 1: Commission DUT to TH" - cluster: "DelayCommands" - command: "WaitForCommissionee" - arguments: - values: - - name: "nodeId" - value: nodeId - - - label: "Step 2: TH reads the MinMeasuredValue attribute from the DUT" - PICS: TMP.S.A0001 - command: "readAttribute" - attribute: "MinMeasuredValue" - response: - saveAs: CurrentMinMeasured - constraints: - type: int16s - minValue: -27315 - maxValue: 32766 - - - label: "Step 3: TH reads the MaxMeasuredValue attribute from the DUT" - PICS: TMP.S.A0002 - command: "readAttribute" - attribute: "MaxMeasuredValue" - response: - saveAs: CurrentMaxMeasured - constraints: - type: int16s - minValue: CurrentMinMeasured+1 - maxValue: 32767 - - - label: "Step 4: TH reads the MeasuredValue attribute from the DUT" - PICS: TMP.S.A0000 - command: "readAttribute" - attribute: "MeasuredValue" - response: - constraints: - type: int16s - minValue: CurrentMinMeasured - maxValue: CurrentMaxMeasured - - - label: "Step 5: TH reads the Tolerance attribute from the DUT" - PICS: TMP.S.A0003 - command: "readAttribute" - attribute: "Tolerance" - response: - constraints: - type: int16u - minValue: 0 - maxValue: 2048 diff --git a/src/python_testing/TC_TMP_2_1.py b/src/python_testing/TC_TMP_2_1.py new file mode 100644 index 00000000000000..f48b96991ef80d --- /dev/null +++ b/src/python_testing/TC_TMP_2_1.py @@ -0,0 +1,97 @@ +# +# Copyright (c) 2024 Project CHIP Authors +# All rights reserved. +# +# 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 chip.clusters as Clusters +from chip.clusters.Types import NullValue +from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main +from mobly import asserts + + +class TC_TMP_2_1(MatterBaseTest): + def desc_TC_TMP_2_1(self) -> str: + return "[TC-TMP-2.1] Attributes with Server as DUT" + + def pics_TC_TMP_2_1(self): + return ["TMP.S"] + + def steps_TC_TMP_2_1(self) -> list[TestStep]: + return [ + TestStep(1, "Commissioning, already done", is_commissioning=True), + TestStep( + 2, "Set default bounds `min_bound` = -27315, `max_bound` = 32767"), + TestStep(3, "TH reads the MinMeasuredValue attribute from the DUT and saves as `min_measured_value`. If `min_measured_value` is not null, set `min_bound` to `min_measured_value`"), + TestStep(4, "TH reads the MaxMeasuredValue attribute from the DUT and saves as `max_measured_value`. If `max_measured_value` is not null, set `max_bound` to `max_measured_value", + "Verify that `max_measured_value` is either null or an int16 where min_bound < `max_measured_value` ≤ 32767."), + TestStep(5, "If `min_measured_value` is not null, verify min measured value range", + "Verify that -27315 ≤ `min_measured_value` < `max_bound`"), + TestStep(6, "TH reads the MeasuredValue attribute from the DUT", + "Verify that the DUT response contains either null or a int16 where `min_bound` ≤ MeasuredValue ≤ `max_bound`."), + TestStep(7, "TH reads the Tolerance attribute from the DUT", + "Verify that Tolerance is in the range of 0 to 2048"), + ] + + @async_test_body + async def test_TC_TMP_2_1(self): + cluster = Clusters.TemperatureMeasurement + attr = Clusters.TemperatureMeasurement.Attributes + + # Commission DUT - already done + self.step(1) + + self.step(2) + min_bound = -27315 + max_bound = 32767 + + self.step(3) + min_measured_value = await self.read_single_attribute_check_success(cluster=cluster, attribute=attr.MinMeasuredValue) + if min_measured_value != NullValue: + min_bound = min_measured_value + + self.step(4) + max_measured_value = await self.read_single_attribute_check_success(cluster=cluster, attribute=attr.MaxMeasuredValue) + if max_measured_value != NullValue: + max_bound = max_measured_value + asserts.assert_greater(max_measured_value, min_bound, + "MaxMeasuredValue is not greater than the minimum bound") + asserts.assert_less_equal( + max_measured_value, 32767, "MaxMeasuredValue is not less than or equal to than 32767") + + self.step(5) + if min_measured_value != NullValue: + asserts.assert_greater_equal(min_measured_value, -27315, "MinMeasuredValue is out of range") + asserts.assert_less(min_measured_value, max_bound, "MinMeasuredValue is out of range") + else: + self.mark_current_step_skipped() + + self.step(6) + measured_value = await self.read_single_attribute_check_success(cluster=cluster, attribute=attr.MeasuredValue) + if measured_value != NullValue: + print(measured_value) + print(min_bound) + asserts.assert_greater_equal( + measured_value, min_bound, "Measured value is less than min bound") + asserts.assert_less_equal( + measured_value, max_bound, "Measured value is greater than max bound") + + self.step(7) + tolerance = await self.read_single_attribute_check_success(cluster=cluster, attribute=attr.Tolerance) + asserts.assert_greater_equal(tolerance, 0, "Tolerance is less than 0") + asserts.assert_less_equal( + tolerance, 2048, "Tolerance is greater than 2048") + + +if __name__ == "__main__": + default_matter_test_main() diff --git a/src/python_testing/matter_testing_support.py b/src/python_testing/matter_testing_support.py index f56acdcd61d040..9ec12dca5fb081 100644 --- a/src/python_testing/matter_testing_support.py +++ b/src/python_testing/matter_testing_support.py @@ -1656,7 +1656,7 @@ def get_test_info(test_class: MatterBaseTest, matter_test_config: MatterTestConf return info -def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, hooks: TestRunnerHooks) -> None: +def run_tests_no_exit(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, hooks: TestRunnerHooks, default_controller=None, external_stack=None) -> bool: get_test_info(test_class, matter_test_config) @@ -1668,7 +1668,10 @@ def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, if len(matter_test_config.tests) > 0: tests = matter_test_config.tests - stack = MatterStackState(matter_test_config) + if external_stack: + stack = external_stack + else: + stack = MatterStackState(matter_test_config) with TracingContext() as tracing_ctx: for destination in matter_test_config.trace_to: @@ -1678,12 +1681,12 @@ def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, # TODO: Steer to right FabricAdmin! # TODO: If CASE Admin Subject is a CAT tag range, then make sure to issue NOC with that CAT tag - - default_controller = stack.certificate_authorities[0].adminList[0].NewController( - nodeId=matter_test_config.controller_node_id, - paaTrustStorePath=str(matter_test_config.paa_trust_store_path), - catTags=matter_test_config.controller_cat_tags - ) + if not default_controller: + default_controller = stack.certificate_authorities[0].adminList[0].NewController( + nodeId=matter_test_config.controller_node_id, + paaTrustStorePath=str(matter_test_config.paa_trust_store_path), + catTags=matter_test_config.controller_cat_tags + ) test_config.user_params["default_controller"] = stash_globally(default_controller) test_config.user_params["matter_test_config"] = stash_globally(matter_test_config) @@ -1732,10 +1735,16 @@ def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, hooks.stop(duration=duration) # Shutdown the stack when all done - stack.Shutdown() + if not external_stack: + stack.Shutdown() if ok: logging.info("Final result: PASS !") else: logging.error("Final result: FAIL !") + return ok + + +def run_tests(test_class: MatterBaseTest, matter_test_config: MatterTestConfig, hooks: TestRunnerHooks, default_controller=None, external_stack=None) -> None: + if not run_tests_no_exit(test_class, matter_test_config, hooks, default_controller, external_stack): sys.exit(1) diff --git a/src/python_testing/test_testing/MockTestRunner.py b/src/python_testing/test_testing/MockTestRunner.py new file mode 100644 index 00000000000000..79f6e21e2b434e --- /dev/null +++ b/src/python_testing/test_testing/MockTestRunner.py @@ -0,0 +1,57 @@ +# +# Copyright (c) 2024 Project CHIP Authors +# All rights reserved. +# +# 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 importlib +import os +import sys +from pathlib import Path +from unittest.mock import MagicMock + +from chip.clusters import Attribute + +try: + from matter_testing_support import MatterStackState, MatterTestConfig, run_tests_no_exit +except ImportError: + sys.path.append(os.path.abspath( + os.path.join(os.path.dirname(__file__), '..'))) + from matter_testing_support import MatterStackState, MatterTestConfig, run_tests_no_exit + + +class AsyncMock(MagicMock): + async def __call__(self, *args, **kwargs): + return super(AsyncMock, self).__call__(*args, **kwargs) + + +class MockTestRunner(): + def __init__(self, filename: str, classname: str, test: str): + self.config = MatterTestConfig( + tests=[test], endpoint=1, dut_node_ids=[1]) + self.stack = MatterStackState(self.config) + self.default_controller = self.stack.certificate_authorities[0].adminList[0].NewController( + nodeId=self.config.controller_node_id, + paaTrustStorePath=str(self.config.paa_trust_store_path), + catTags=self.config.controller_cat_tags + ) + module = importlib.import_module(Path(os.path.basename(filename)).stem) + self.test_class = getattr(module, classname) + + def Shutdown(self): + self.stack.Shutdown() + + def run_test_with_mock_read(self, read_cache: Attribute.AsyncReadTransaction.ReadResponse): + self.default_controller.Read = AsyncMock(return_value=read_cache) + return run_tests_no_exit(self.test_class, self.config, None, self.default_controller, self.stack) diff --git a/src/python_testing/test_testing/test_TC_TMP_2_1.py b/src/python_testing/test_testing/test_TC_TMP_2_1.py new file mode 100644 index 00000000000000..5693aced8ba505 --- /dev/null +++ b/src/python_testing/test_testing/test_TC_TMP_2_1.py @@ -0,0 +1,180 @@ +#!/usr/bin/env -S python3 -B +# +# Copyright (c) 2024 Project CHIP Authors +# All rights reserved. +# +# 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 typing +from dataclasses import dataclass + +import chip.clusters as Clusters +from chip.clusters import Attribute +from chip.clusters.Types import NullValue +from MockTestRunner import MockTestRunner + + +@dataclass +class TestSpec(): + min: typing.Optional[int] + max: typing.Optional[int] + measured: int + tolerance: int + expect_pass: bool + + +TEST_CASES = [ + # ============================== + # Measured Test cases + # ============================== + # -------- + # Neither min nor max specified + # -------- + # Measured lowest + TestSpec(NullValue, NullValue, -27315, 10, True), + # Measured highest + TestSpec(NullValue, NullValue, 32767, 10, True), + # Measured below + TestSpec(NullValue, NullValue, -27316, 10, False), + # Measured above + TestSpec(NullValue, NullValue, 32768, 10, False), + # Measured null + TestSpec(NullValue, NullValue, NullValue, 10, True), + + # -------- + # min only + # -------- + # Measured lowest + TestSpec(5, NullValue, 5, 10, True), + # Measured highest + TestSpec(5, NullValue, 32767, 10, True), + # Measured below + TestSpec(5, NullValue, 4, 10, False), + # Measured above + TestSpec(5, NullValue, 32768, 10, False), + # Measured null + TestSpec(5, NullValue, NullValue, 10, True), + + # -------- + # max only + # -------- + # Measured lowest + TestSpec(NullValue, 5, -27315, 10, True), + # Measured highest + TestSpec(NullValue, 5, 5, 10, True), + # Measured below + TestSpec(NullValue, 5, -27316, 10, False), + # Measured above + TestSpec(NullValue, 5, 6, 10, False), + # Measured null + TestSpec(NullValue, 5, NullValue, 10, True), + + # -------- + # both + # -------- + # Measured lowest + TestSpec(-5, 5, -5, 10, True), + # Measured highest + TestSpec(-5, 5, 5, 10, True), + # Measured below + TestSpec(-5, 5, -6, 10, False), + # Measured above + TestSpec(-5, 5, 6, 10, False), + # Measured null + TestSpec(-5, 5, NullValue, 10, True), + + # ============================== + # Min Test cases + # ============================== + # Max not specified, min OK bottom + TestSpec(-27315, NullValue, 0, 10, True), + # Max not specified, min OK top + TestSpec(32766, NullValue, 32767, 10, True), + # Max not specified, min out of range below + TestSpec(-27316, NullValue, 0, 10, False), + # Max not specified, min out of range above + TestSpec(32767, NullValue, 32767, 10, False), + # Max specified, min OK bottom + TestSpec(-27315, 5, 0, 10, True), + # Max specified, min OK top + TestSpec(4, 5, 4, 10, True), + # Max specified, min out of range below + TestSpec(-27316, 5, 0, 10, False), + # Max specified, min out of range above + TestSpec(5, 5, 5, 10, False), + + # ============================== + # Min Test cases + # ============================== + # min not specified, max OK bottom + TestSpec(NullValue, -27314, -27315, 10, True), + # min not specified, max OK top + TestSpec(NullValue, 32767, 0, 10, True), + # min not specified, max out of range bottom + TestSpec(NullValue, -27315, -27315, 10, False), + # min not specified, max out of range top + TestSpec(NullValue, 32768, 0, 10, False), + # min specified, max OK bottom + TestSpec(0, 1, 0, 10, True), + # min specified, max OK top + TestSpec(0, 32767, 0, 10, True), + # min specified, max out of range bottom + TestSpec(0, 0, 0, 10, False), + # min specified, max out of range top + TestSpec(0, 32768, 0, 10, False), + + # ============================== + # Tolerance test cases + # ============================== + # Tolerance OK bottom + TestSpec(NullValue, NullValue, 0, 0, True), + # Tolerance OK top + TestSpec(NullValue, NullValue, 0, 2048, True), + # Tolerance out of range bottom + TestSpec(NullValue, NullValue, 0, -1, False), + # Tolerance out of range top + TestSpec(NullValue, NullValue, 0, 2049, False), + +] + + +def test_spec_to_attribute_cache(test_spec: TestSpec) -> Attribute.AsyncReadTransaction.ReadResponse: + c = Clusters.TemperatureMeasurement + attr = Clusters.TemperatureMeasurement.Attributes + resp = Attribute.AsyncReadTransaction.ReadResponse({}, [], {}) + resp.attributes = {1: {c: {attr.MaxMeasuredValue: test_spec.max, + attr.MinMeasuredValue: test_spec.min, attr.MeasuredValue: test_spec.measured, attr.Tolerance: test_spec.tolerance}}} + return resp + + +def main(): + test_runner = MockTestRunner('TC_TMP_2_1', 'TC_TMP_2_1', 'test_TC_TMP_2_1') + failures = [] + for idx, t in enumerate(TEST_CASES): + ok = test_runner.run_test_with_mock_read(test_spec_to_attribute_cache(t)) == t.expect_pass + if not ok: + failures.append(f"Measured test case failure: {idx} {t}") + + test_runner.Shutdown() + print( + f"Test of tests: run {len(TEST_CASES)}, test response correct: {len(TEST_CASES) - len(failures)} test response incorrect: {len(failures)}") + for f in failures: + print(f) + + return 1 if failures else 0 + + +if __name__ == "__main__": + sys.exit(main())