forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_python_test.py
executable file
·225 lines (185 loc) · 9.89 KB
/
run_python_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/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 datetime
import glob
import io
import logging
import os
import os.path
import pathlib
import re
import shlex
import sys
import time
import click
import coloredlogs
from colorama import Fore, Style
from matter_testing_support.metadata import Metadata, MetadataReader
from matter_testing_support.tasks import Subprocess
DEFAULT_CHIP_ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', '..'))
MATTER_DEVELOPMENT_PAA_ROOT_CERTS = "credentials/development/paa-root-certs"
TAG_PROCESS_APP = f"[{Fore.GREEN}APP {Style.RESET_ALL}]".encode()
TAG_PROCESS_TEST = f"[{Fore.GREEN}TEST{Style.RESET_ALL}]".encode()
TAG_STDOUT = f"[{Fore.YELLOW}STDOUT{Style.RESET_ALL}]".encode()
TAG_STDERR = f"[{Fore.RED}STDERR{Style.RESET_ALL}]".encode()
# RegExp which matches the timestamp in the output of CHIP application
OUTPUT_TIMESTAMP_MATCH = re.compile(r'(?P<prefix>.*)\[(?P<ts>\d+\.\d+)\](?P<suffix>\[\d+:\d+\].*)'.encode())
def chip_output_extract_timestamp(line: bytes) -> (float, bytes):
"""Try to extract timestamp from a CHIP application output line."""
if match := OUTPUT_TIMESTAMP_MATCH.match(line):
return float(match.group(2)), match.group(1) + match.group(3) + b'\n'
return time.time(), line
def process_chip_output(line: bytes, is_stderr: bool, process_tag: bytes = b"") -> bytes:
"""Rewrite the output line to add the timestamp and the process tag."""
timestamp, line = chip_output_extract_timestamp(line)
timestamp = datetime.datetime.fromtimestamp(timestamp).isoformat(sep=' ')
return f"[{timestamp}]".encode() + process_tag + (TAG_STDERR if is_stderr else TAG_STDOUT) + line
def process_chip_app_output(line, is_stderr):
return process_chip_output(line, is_stderr, TAG_PROCESS_APP)
def process_test_script_output(line, is_stderr):
return process_chip_output(line, is_stderr, TAG_PROCESS_TEST)
@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("--factory-reset/--no-factory-reset", default=None,
help='Remove app config and repl configs (/tmp/chip* and /tmp/repl*) before running the tests.')
@click.option("--factory-reset-app-only/--no-factory-reset-app-only", default=None,
help='Remove app config and repl configs (/tmp/chip* and /tmp/repl*) before running the tests, but not the controller config')
@click.option("--app-args", type=str, default='',
help='The extra arguments passed to the device. Can use placeholders like {SCRIPT_BASE_NAME}')
@click.option("--app-ready-pattern", type=str, default=None,
help='Delay test script start until given regular expression pattern is found in the application output.')
@click.option("--script", type=click.Path(exists=True), default=os.path.join(DEFAULT_CHIP_ROOT,
'src',
'controller',
'python',
'test',
'test_scripts',
'mobile-device-test.py'), help='Test script to use.')
@click.option("--script-args", type=str, default='',
help='Script arguments, can use placeholders like {SCRIPT_BASE_NAME}.')
@click.option("--script-gdb", is_flag=True,
help='Run script through gdb')
@click.option("--quiet/--no-quiet", default=None,
help="Do not print output from passing tests. Use this flag in CI to keep GitHub log size manageable.")
@click.option("--load-from-env", default=None, help="YAML file that contains values for environment variables.")
def main(app: str, factory_reset: bool, factory_reset_app_only: bool, app_args: str,
app_ready_pattern: str, script: str, script_args: str, script_gdb: bool, quiet: bool, load_from_env):
if load_from_env:
reader = MetadataReader(load_from_env)
runs = reader.parse_script(script)
else:
runs = [
Metadata(
py_script_path=script,
run="cmd-run",
app=app,
app_args=app_args,
app_ready_pattern=app_ready_pattern,
script_args=script_args,
script_gdb=script_gdb,
)
]
if not runs:
raise click.ClickException(
"No valid runs were found. Make sure you add runs to your file, see "
"https://github.com/project-chip/connectedhomeip/blob/master/docs/testing/python.md document for reference/example.")
# Override runs Metadata with the command line arguments
for run in runs:
if factory_reset is not None:
run.factory_reset = factory_reset
if factory_reset_app_only is not None:
run.factory_reset_app_only = factory_reset_app_only
if quiet is not None:
run.quiet = quiet
for run in runs:
logging.info("Executing %s %s", run.py_script_path.split('/')[-1], run.run)
main_impl(run.app, run.factory_reset, run.factory_reset_app_only, run.app_args or "",
run.app_ready_pattern, run.py_script_path, run.script_args or "", run.script_gdb, run.quiet)
def main_impl(app: str, factory_reset: bool, factory_reset_app_only: bool, app_args: str,
app_ready_pattern: str, script: str, script_args: str, script_gdb: bool, quiet: bool):
app_args = app_args.replace('{SCRIPT_BASE_NAME}', os.path.splitext(os.path.basename(script))[0])
script_args = script_args.replace('{SCRIPT_BASE_NAME}', os.path.splitext(os.path.basename(script))[0])
if factory_reset or factory_reset_app_only:
# Remove native app config
for path in glob.glob('/tmp/chip*') + glob.glob('/tmp/repl*'):
pathlib.Path(path).unlink(missing_ok=True)
# Remove native app KVS if that was used
if match := re.search(r"--KVS (?P<path>[^ ]+)", app_args):
logging.info("Removing KVS path: %s" % match.group("path"))
pathlib.Path(match.group("path")).unlink(missing_ok=True)
if factory_reset:
# Remove Python test admin storage if provided
if match := re.search(r"--storage-path (?P<path>[^ ]+)", script_args):
logging.info("Removing storage path: %s" % match.group("path"))
pathlib.Path(match.group("path")).unlink(missing_ok=True)
app_process = None
app_exit_code = 0
app_pid = 0
stream_output = sys.stdout.buffer
if quiet:
stream_output = io.BytesIO()
if app:
if not os.path.exists(app):
if app is None:
raise FileNotFoundError(f"{app} not found")
if app_ready_pattern:
app_ready_pattern = re.compile(app_ready_pattern.encode())
app_process = Subprocess(app, *shlex.split(app_args),
output_cb=process_chip_app_output,
f_stdout=stream_output,
f_stderr=stream_output)
app_process.start(expected_output=app_ready_pattern, timeout=30)
app_process.p.stdin.close()
app_pid = app_process.p.pid
script_command = [script, "--paa-trust-store-path", os.path.join(DEFAULT_CHIP_ROOT, MATTER_DEVELOPMENT_PAA_ROOT_CERTS),
'--log-format', '%(message)s', "--app-pid", str(app_pid)] + shlex.split(script_args)
if script_gdb:
#
# When running through Popen, we need to preserve some space-delimited args to GDB as a single logical argument.
# To do that, let's use '|' as a placeholder for the space character so that the initial split will not tokenize them,
# and then replace that with the space char there-after.
#
script_command = ("gdb -batch -return-child-result -q -ex run -ex "
"thread|apply|all|bt --args python3".split() + script_command)
else:
script_command = "/usr/bin/env python3".split() + script_command
final_script_command = [i.replace('|', ' ') for i in script_command]
test_script_process = Subprocess(final_script_command[0], *final_script_command[1:],
output_cb=process_test_script_output,
f_stdout=stream_output,
f_stderr=stream_output)
test_script_process.start()
test_script_process.p.stdin.close()
test_script_exit_code = test_script_process.wait()
if test_script_exit_code != 0:
logging.error("Test script exited with returncode %d" % test_script_exit_code)
if app_process:
logging.info("Stopping app with SIGTERM")
app_process.terminate()
app_exit_code = app_process.returncode
# We expect both app and test script should exit with 0
exit_code = test_script_exit_code or app_exit_code
if quiet:
if exit_code:
sys.stdout.write(stream_output.getvalue().decode('utf-8'))
else:
logging.info("Test completed successfully")
if exit_code != 0:
sys.exit(exit_code)
if __name__ == '__main__':
coloredlogs.install(level='INFO')
main(auto_envvar_prefix='CHIP')