run_java_test.py 5.2 KB

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