Skip to content

Commit

Permalink
script support cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Aug 28, 2024
1 parent 377fe7b commit 016a545
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions src/python_testing/matter_testing_support.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2022 Project CHIP Authors
# Copyright (c) 2022-2024 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -520,6 +520,16 @@ def test_skipped(self, filename: str, name: str):

@dataclass
class MatterTestConfig:
def update(self, override_attr_dict: dict) -> None:
"""
Update the attributes of the MatterTestConfig instance with the values provided in the override_attr_dict.
This method allows for dynamic modification of the instance's attributes. If a key in override_attr_dict matches
an existing attribute name, the attribute's value is updated. If the key does not match any existing attribute,
a new attribute is created with the given key and value.
"""
self.__dict__.update(override_attr_dict)

storage_path: pathlib.Path = pathlib.Path(".")
logs_path: pathlib.Path = pathlib.Path(".")
paa_trust_store_path: Optional[pathlib.Path] = None
Expand All @@ -535,6 +545,7 @@ class MatterTestConfig:
endpoint: int = 0
app_pid: int = 0

pre_commissioning: bool = True
commissioning_method: Optional[str] = None
discriminators: Optional[List[int]] = None
setup_passcodes: Optional[List[int]] = None
Expand Down Expand Up @@ -573,6 +584,10 @@ class MatterTestConfig:

trace_to: List[str] = field(default_factory=list)

# Accepted Terms and Conditions if used
tc_version: int = None
tc_user_response: int = None


class ClusterMapper:
"""Describe clusters/attributes using schema names."""
Expand Down Expand Up @@ -842,6 +857,18 @@ def __init__(self, *args):
self.problems = []
self.is_commissioning = False

async def commission_devices(self) -> bool:
conf = self.matter_test_config

for commission_idx, node_id in enumerate(conf.dut_node_ids):
logging.info("Starting commissioning for root index %d, fabric ID 0x%016X, node ID 0x%016X" %
(conf.root_of_trust_index, conf.fabric_id, node_id))
logging.info("Commissioning method: %s" % conf.commissioning_method)

await CommissionDeviceTest.commission_device(self, commission_idx)

return True

def get_test_steps(self, test: str) -> list[TestStep]:
''' Retrieves the test step list for the given test
Expand Down Expand Up @@ -1666,6 +1693,9 @@ def convert_args_to_matter_config(args: argparse.Namespace) -> MatterTestConfig:
config.controller_node_id = args.controller_node_id
config.trace_to = args.trace_to

config.tc_version = args.tc_version
config.tc_user_response = args.tc_user_response

# Accumulate all command-line-passed named args
all_global_args = []
argsets = [item for item in (args.int_arg, args.float_arg, args.string_arg, args.json_arg,
Expand Down Expand Up @@ -1759,6 +1789,10 @@ def parse_matter_test_args(argv: Optional[List[str]] = None) -> MatterTestConfig
commission_group.add_argument('--commission-only', action="store_true", default=False,
help="If true, test exits after commissioning without running subsequent tests")

commission_group.add_argument('--tc-version', type=int, help="Terms and conditions version")

commission_group.add_argument('--tc-user-response', type=int, help="Terms and conditions acknowledgements")

code_group = parser.add_mutually_exclusive_group(required=False)

code_group.add_argument('-q', '--qr-code', type=str,
Expand Down Expand Up @@ -2022,20 +2056,21 @@ def __init__(self, *args):
self.is_commissioning = True

def test_run_commissioning(self):
conf = self.matter_test_config
for commission_idx, node_id in enumerate(conf.dut_node_ids):
logging.info("Starting commissioning for root index %d, fabric ID 0x%016X, node ID 0x%016X" %
(conf.root_of_trust_index, conf.fabric_id, node_id))
logging.info("Commissioning method: %s" % conf.commissioning_method)
if not asyncio.run(self.commission_devices()):
raise signals.TestAbortAll("Failed to commission node")

if not asyncio.run(self._commission_device(commission_idx)):
raise signals.TestAbortAll("Failed to commission node")
async def commission_device(instance: MatterBaseTest, i) -> bool:
dev_ctrl = instance.default_controller
conf = instance.matter_test_config

async def _commission_device(self, i) -> bool:
dev_ctrl = self.default_controller
conf = self.matter_test_config
info = instance.get_setup_payload_info()[i]

info = self.get_setup_payload_info()[i]
if conf.tc_version is not None and conf.tc_user_response is not None:
logging.debug(f"Setting TC Acknowledgements to version {conf.tc_version} with user response {conf.tc_user_response}.")
dev_ctrl.SetTCAcknowledgements(conf.tc_version, conf.tc_user_response)
dev_ctrl.SetTCRequired(True)
else:
dev_ctrl.SetTCRequired(False)

if conf.commissioning_method == "on-network":
try:
Expand Down Expand Up @@ -2091,7 +2126,7 @@ async def _commission_device(self, i) -> bool:
raise ValueError("Invalid commissioning method %s!" % conf.commissioning_method)


def default_matter_test_main():
def default_matter_test_main(override_matter_test_config: dict = {}):
"""Execute the test class in a test module.
This is the default entry point for running a test script file directly.
In this case, only one test class in a test script is allowed.
Expand All @@ -2103,7 +2138,11 @@ def default_matter_test_main():
default_matter_test_main.main()
"""

matter_test_config = parse_matter_test_args()
# Parse standard test configuration arguments
matter_test_config: MatterTestConfig = parse_matter_test_args()

# Apply test configuration overrides
matter_test_config.update(override_matter_test_config)

# Find the test class in the test script.
test_class = _find_test_class()
Expand Down Expand Up @@ -2177,7 +2216,7 @@ def run_tests_no_exit(test_class: MatterBaseTest, matter_test_config: MatterTest
testbed_name=test_config.testbed_name)

with runner.mobly_logger():
if matter_test_config.commissioning_method is not None:
if matter_test_config.pre_commissioning and matter_test_config.commissioning_method is not None:
runner.add_test_class(test_config, CommissionDeviceTest, None)

# Add the tests selected unless we have a commission-only request
Expand Down

0 comments on commit 016a545

Please sign in to comment.