|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | + |
| 3 | +# Copyright (c) 2022 Project CHIP Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import click |
| 18 | +import coloredlogs |
| 19 | +import logging |
| 20 | +import os |
| 21 | +import pathlib |
| 22 | +import pty |
| 23 | +import queue |
| 24 | +import re |
| 25 | +import shlex |
| 26 | +import signal |
| 27 | +import subprocess |
| 28 | +import sys |
| 29 | +from java.base import DumpProgramOutputToQueue |
| 30 | +from java.commissioning_test import CommissioningTest |
| 31 | +from colorama import Fore, Style |
| 32 | + |
| 33 | + |
| 34 | +@click.command() |
| 35 | +@click.option("--app", type=click.Path(exists=True), default=None, help='Path to local application to use, omit to use external apps.') |
| 36 | +@click.option("--app-args", type=str, default='', help='The extra arguments passed to the device.') |
| 37 | +@click.option("--tool-path", type=click.Path(exists=True), default=None, help='Path to java-matter-controller.') |
| 38 | +@click.option("--tool-cluster", type=str, default='pairing', help='The cluster name passed to the java-matter-controller.') |
| 39 | +@click.option("--tool-args", type=str, default='', help='The arguments passed to the java-matter-controller.') |
| 40 | +@click.option("--factoryreset", is_flag=True, help='Remove app configs (/tmp/chip*) before running the tests.') |
| 41 | +def main(app: str, app_args: str, tool_path: str, tool_cluster: str, tool_args: str, factoryreset: bool): |
| 42 | + logging.info("Execute: {script_command}") |
| 43 | + |
| 44 | + if factoryreset: |
| 45 | + # Remove native app config |
| 46 | + retcode = subprocess.call("rm -rf /tmp/chip*", shell=True) |
| 47 | + if retcode != 0: |
| 48 | + raise Exception("Failed to remove /tmp/chip* for factory reset.") |
| 49 | + |
| 50 | + print("Contents of test directory: %s" % os.getcwd()) |
| 51 | + print(subprocess.check_output(["ls -l"], shell=True).decode('us-ascii')) |
| 52 | + |
| 53 | + # Remove native app KVS if that was used |
| 54 | + kvs_match = re.search(r"--KVS (?P<kvs_path>[^ ]+)", app_args) |
| 55 | + if kvs_match: |
| 56 | + kvs_path_to_remove = kvs_match.group("kvs_path") |
| 57 | + retcode = subprocess.call("rm -f %s" % kvs_path_to_remove, shell=True) |
| 58 | + print("Trying to remove KVS path %s" % kvs_path_to_remove) |
| 59 | + if retcode != 0: |
| 60 | + raise Exception("Failed to remove %s for factory reset." % kvs_path_to_remove) |
| 61 | + |
| 62 | + coloredlogs.install(level='INFO') |
| 63 | + |
| 64 | + log_queue = queue.Queue() |
| 65 | + log_cooking_threads = [] |
| 66 | + |
| 67 | + if tool_path: |
| 68 | + if not os.path.exists(tool_path): |
| 69 | + if tool_path is None: |
| 70 | + raise FileNotFoundError(f"{tool_path} not found") |
| 71 | + |
| 72 | + app_process = None |
| 73 | + if app: |
| 74 | + if not os.path.exists(app): |
| 75 | + if app is None: |
| 76 | + raise FileNotFoundError(f"{app} not found") |
| 77 | + app_args = [app] + shlex.split(app_args) |
| 78 | + logging.info(f"Execute: {app_args}") |
| 79 | + app_process = subprocess.Popen( |
| 80 | + app_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) |
| 81 | + DumpProgramOutputToQueue( |
| 82 | + log_cooking_threads, Fore.GREEN + "APP " + Style.RESET_ALL, app_process, log_queue) |
| 83 | + |
| 84 | + command = ['java', '-Djava.library.path=' + tool_path + '/lib/jni', '-jar', tool_path + '/bin/java-matter-controller'] |
| 85 | + |
| 86 | + if tool_cluster == 'pairing': |
| 87 | + logging.info("Testing pairing cluster") |
| 88 | + |
| 89 | + test = CommissioningTest(log_cooking_threads, log_queue, command, tool_args) |
| 90 | + controller_exit_code = test.RunTest() |
| 91 | + |
| 92 | + if controller_exit_code != 0: |
| 93 | + logging.error("Test script exited with error %r" % test_script_exit_code) |
| 94 | + |
| 95 | + app_exit_code = 0 |
| 96 | + if app_process: |
| 97 | + logging.warning("Stopping app with SIGINT") |
| 98 | + app_process.send_signal(signal.SIGINT.value) |
| 99 | + app_exit_code = app_process.wait() |
| 100 | + |
| 101 | + # There are some logs not cooked, so we wait until we have processed all logs. |
| 102 | + # This procedure should be very fast since the related processes are finished. |
| 103 | + for thread in log_cooking_threads: |
| 104 | + thread.join() |
| 105 | + |
| 106 | + if controller_exit_code != 0: |
| 107 | + sys.exit(controller_exit_code) |
| 108 | + else: |
| 109 | + # We expect both app and controller should exit with 0 |
| 110 | + sys.exit(app_exit_code) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + main() |
0 commit comments