| #!/usr/bin/env -S python3 -B | |
| # Copyright (c) 2022 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 logging | |
| import os | |
| import queue | |
| import re | |
| import shlex | |
| import signal | |
| import subprocess | |
| import sys | |
| import click | |
| import coloredlogs | |
| from colorama import Fore, Style | |
| from java.base import DumpProgramOutputToQueue | |
| from java.bdx_test import BDXTest | |
| from java.commissioning_test import CommissioningTest | |
| from java.discover_test import DiscoverTest | |
| from java.im_test import IMTest | |
| from java.ota_test import OTATest | |
| log = logging.getLogger(__name__) | |
| @click.command() | |
| @click.option("--app", type=click.Path(exists=True), default=None, | |
| help='Path to local application to use, omit to use external apps.') | |
| @click.option("--app-args", type=str, default='', | |
| help='The extra arguments passed to the device.') | |
| @click.option("--tool-path", type=click.Path(exists=True), default=None, | |
| help='Path to java-matter-controller.') | |
| @click.option("--tool-cluster", type=str, default='pairing', | |
| help='The cluster name passed to the java-matter-controller.') | |
| @click.option("--tool-args", type=str, default='', | |
| help='The arguments passed to the java-matter-controller.') | |
| @click.option("--factoryreset", is_flag=True, | |
| help='Remove app configs (/tmp/chip*) before running the tests.') | |
| def main(app: str, app_args: str, tool_path: str, tool_cluster: str, tool_args: str, factoryreset: bool): | |
| if factoryreset: | |
| # Remove native app config | |
| retcode = subprocess.call("rm -rf /tmp/chip*", shell=True) | |
| if retcode != 0: | |
| raise RuntimeError("Failed to remove /tmp/chip* for factory reset.") | |
| # Remove native app KVS if that was used | |
| kvs_match = re.search(r"--KVS (?P<kvs_path>[^ ]+)", app_args) | |
| if kvs_match: | |
| kvs_path_to_remove = kvs_match.group("kvs_path") | |
| retcode = subprocess.call(f"rm -f {kvs_path_to_remove}", shell=True) | |
| print(f"Trying to remove KVS path {kvs_path_to_remove}") | |
| if retcode != 0: | |
| raise RuntimeError(f"Failed to remove {kvs_path_to_remove} for factory reset.") | |
| coloredlogs.install(level='INFO') | |
| log_queue = queue.Queue() | |
| log_cooking_threads = [] | |
| if tool_path: | |
| if not os.path.exists(tool_path): | |
| if tool_path is None: | |
| raise FileNotFoundError(f"{tool_path} not found") | |
| app_process = None | |
| if app: | |
| if not os.path.exists(app): | |
| if app is None: | |
| raise FileNotFoundError(f"{app} not found") | |
| app_args = [app] + shlex.split(app_args) | |
| log.info("Execute: %s", shlex.join(app_args)) | |
| if '--crash_log' in app_args: | |
| index = app_args.index('--crash_log') | |
| fileName = app_args[index + 1] | |
| log.info("Crash Log FileName: '%s'", fileName) | |
| with open(fileName, 'w') as f: | |
| for i in range(1, 1000): | |
| data = f"{i}\n" | |
| f.write(data) | |
| if '--network_diagnostics_log' in app_args: | |
| index = app_args.index('--network_diagnostics_log') | |
| fileName = app_args[index + 1] | |
| log.info("Network Diag Log FileName: '%s'", fileName) | |
| with open(fileName, 'w') as f: | |
| for i in range(1, 500): | |
| data = f"{i}\n" | |
| f.write(data) | |
| if '--end_user_support_log' in app_args: | |
| index = app_args.index('--end_user_support_log') | |
| fileName = app_args[index + 1] | |
| log.info("EndUser Support Log FileName: '%s'", fileName) | |
| with open(fileName, 'w') as f: | |
| for i in range(1, 10): | |
| data = f"{i}\n" | |
| f.write(data) | |
| app_process = subprocess.Popen( | |
| app_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) | |
| DumpProgramOutputToQueue( | |
| log_cooking_threads, Fore.GREEN + "APP " + Style.RESET_ALL, app_process, log_queue) | |
| command = ['java', | |
| f'-Djava.library.path={tool_path}/lib/jni', | |
| '-cp', | |
| ':'.join([ | |
| f'{tool_path}/lib/*', | |
| f'{tool_path}/lib/third_party/connectedhomeip/src/controller/java/*', | |
| f'{tool_path}/bin/java-matter-controller', | |
| ]), | |
| 'com.matter.controller.MainKt'] | |
| if tool_cluster == 'pairing': | |
| log.info("Testing pairing cluster") | |
| test = CommissioningTest(log_cooking_threads, log_queue, command, tool_args) | |
| try: | |
| test.RunTest() | |
| except Exception as e: | |
| log.exception(e) | |
| sys.exit(1) | |
| elif tool_cluster == 'discover': | |
| log.info("Testing discover cluster") | |
| test = DiscoverTest(log_cooking_threads, log_queue, command, tool_args) | |
| try: | |
| test.RunTest() | |
| except Exception as e: | |
| log.exception(e) | |
| sys.exit(1) | |
| elif tool_cluster == 'im': | |
| log.info("Testing IM") | |
| test = IMTest(log_cooking_threads, log_queue, command, tool_args) | |
| try: | |
| test.RunTest() | |
| except Exception as e: | |
| log.exception(e) | |
| sys.exit(1) | |
| elif tool_cluster == 'bdx': | |
| log.info("Testing BDX") | |
| test = BDXTest(log_cooking_threads, log_queue, command, tool_args) | |
| try: | |
| test.RunTest() | |
| except Exception as e: | |
| log.exception(e) | |
| sys.exit(1) | |
| elif tool_cluster == 'ota': | |
| log.info("Testing OTA") | |
| test = OTATest(log_cooking_threads, log_queue, command, tool_args) | |
| try: | |
| test.RunTest() | |
| except Exception as e: | |
| log.exception(e) | |
| sys.exit(1) | |
| app_exit_code = 0 | |
| if app_process: | |
| log.warning("Stopping app with SIGINT") | |
| app_process.send_signal(signal.SIGINT.value) | |
| app_exit_code = app_process.wait() | |
| # There are some logs not cooked, so we wait until we have processed all logs. | |
| # This procedure should be very fast since the related processes are finished. | |
| for thread in log_cooking_threads: | |
| thread.join() | |
| # We expect app should exit with 0 | |
| sys.exit(app_exit_code) | |
| if __name__ == '__main__': | |
| main() |