| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import os |
| 20 |
import sys |
| 21 |
import logging |
| 22 |
from time import time |
| 23 |
import argparse |
| 24 |
from xml.etree import ElementTree |
| 25 |
import multiprocessing as mp |
| 26 |
import io |
| 27 |
from dataclasses import dataclass |
| 28 |
from typing import (IO, List, Optional) |
| 29 |
|
| 30 |
from pyocd.core.session import Session |
| 31 |
from pyocd.core.helpers import ConnectHelper |
| 32 |
from pyocd.probe.aggregator import DebugProbeAggregator |
| 33 |
|
| 34 |
from test_util import ( |
| 35 |
get_env_file_name, |
| 36 |
TestResult, |
| 37 |
Test, |
| 38 |
IOTee, |
| 39 |
RecordingLogHandler, |
| 40 |
get_session_options, |
| 41 |
ensure_output_dir, |
| 42 |
TEST_OUTPUT_DIR, |
| 43 |
) |
| 44 |
|
| 45 |
from basic_test import BasicTest |
| 46 |
from speed_test import SpeedTest |
| 47 |
from cortex_test import CortexTest |
| 48 |
from flash_test import FlashTest |
| 49 |
from flash_loader_test import FlashLoaderTest |
| 50 |
from gdb_test import GdbTest |
| 51 |
from json_lists_test import JsonListsTest |
| 52 |
from connect_test import ConnectTest |
| 53 |
from debug_context_test import DebugContextTest |
| 54 |
from concurrency_test import ConcurrencyTest |
| 55 |
from commands_test import CommandsTest |
| 56 |
from commander_test import CommanderTest |
| 57 |
from probeserver_test import ProbeserverTest |
| 58 |
from user_script_test import UserScriptTest |
| 59 |
|
| 60 |
XML_RESULTS_TEMPLATE = "test_results{}.xml" |
| 61 |
LOG_FILE_TEMPLATE = "automated_test_result{}.txt" |
| 62 |
SUMMARY_FILE_TEMPLATE = "automated_test_summary{}.txt" |
| 63 |
|
| 64 |
LOG_FORMAT = "%(relativeCreated)07d:%(levelname)s:%(module)s:%(message)s" |
| 65 |
|
| 66 |
JOB_TIMEOUT = 30 * 60 |
| 67 |
|
| 68 |
|
| 69 |
all_tests = [ |
| 70 |
BasicTest(), |
| 71 |
JsonListsTest(), |
| 72 |
ConnectTest(), |
| 73 |
SpeedTest(), |
| 74 |
CortexTest(), |
| 75 |
ConcurrencyTest(), |
| 76 |
FlashTest(), |
| 77 |
FlashLoaderTest(), |
| 78 |
DebugContextTest(), |
| 79 |
GdbTest(), |
| 80 |
CommandsTest(), |
| 81 |
CommanderTest(), |
| 82 |
ProbeserverTest(), |
| 83 |
UserScriptTest(), |
| 84 |
] |
| 85 |
|
| 86 |
|
| 87 |
test_list = [] |
| 88 |
|
| 89 |
|
| 90 |
IGNORE_FAILURE_TESTS = [ |
| 91 |
"Connect Test", |
| 92 |
"Gdb Test", |
| 93 |
] |
| 94 |
|
| 95 |
def print_summary(test_list, result_list, test_time, output_file=None): |
| 96 |
for test in test_list: |
| 97 |
test.print_perf_info(result_list, output_file=output_file) |
| 98 |
|
| 99 |
Test.print_results(result_list, output_file=output_file, ignored=IGNORE_FAILURE_TESTS) |
| 100 |
print("", file=output_file) |
| 101 |
print("Test Time: %.3f" % test_time, file=output_file) |
| 102 |
if Test.all_tests_pass(result_list): |
| 103 |
print("All tests passed", file=output_file) |
| 104 |
else: |
| 105 |
print("One or more tests has failed!", file=output_file) |
| 106 |
|
| 107 |
def split_results_by_board(result_list): |
| 108 |
boards = {} |
| 109 |
for result in result_list: |
| 110 |
if result.board_name in boards: |
| 111 |
boards[result.board_name].append(result) |
| 112 |
else: |
| 113 |
boards[result.board_name] = [result] |
| 114 |
return boards |
| 115 |
|
| 116 |
def generate_xml_results(result_list): |
| 117 |
board_results = split_results_by_board(result_list) |
| 118 |
|
| 119 |
suite_id = 0 |
| 120 |
total_failures = 0 |
| 121 |
total_tests = 0 |
| 122 |
total_time = 0 |
| 123 |
|
| 124 |
root = ElementTree.Element('testsuites', |
| 125 |
name="pyocd" |
| 126 |
) |
| 127 |
root.text = "\n" |
| 128 |
|
| 129 |
for board_name, results in board_results.items(): |
| 130 |
total = 0 |
| 131 |
failures = 0 |
| 132 |
suite_time = 0 |
| 133 |
suite = ElementTree.SubElement(root, 'testsuite', |
| 134 |
name=board_name, |
| 135 |
id=str(suite_id)) |
| 136 |
suite.text = "\n" |
| 137 |
suite.tail = "\n" |
| 138 |
suite_id += 1 |
| 139 |
|
| 140 |
for result in results: |
| 141 |
|
| 142 |
total += 1 |
| 143 |
if not result.passed: |
| 144 |
failures += 1 |
| 145 |
case = result.get_test_case() |
| 146 |
suite.append(case) |
| 147 |
suite_time += result.time |
| 148 |
|
| 149 |
suite.set('tests', str(total)) |
| 150 |
suite.set('failures', str(failures)) |
| 151 |
suite.set('time', "%.3f" % suite_time) |
| 152 |
total_tests += total |
| 153 |
total_failures += failures |
| 154 |
total_time += suite_time |
| 155 |
|
| 156 |
root.set('tests', str(total_tests)) |
| 157 |
root.set('failures', str(total_failures)) |
| 158 |
root.set('time', "%.3f" % total_time) |
| 159 |
|
| 160 |
xml_results = os.path.join(TEST_OUTPUT_DIR, XML_RESULTS_TEMPLATE.format(get_env_file_name())) |
| 161 |
ElementTree.ElementTree(root).write(xml_results, encoding="UTF-8", xml_declaration=True) |
| 162 |
|
| 163 |
def print_board_header(outputFile, board, n, includeDividers=True, includeLeadingNewline=False): |
| 164 |
header = "TESTING BOARD {name} [{target}] [{uid}] #{n}".format( |
| 165 |
name=board.name, target=board.target_type, uid=board.unique_id, n=n) |
| 166 |
if includeDividers: |
| 167 |
divider = "=" * len(header) |
| 168 |
if includeLeadingNewline: |
| 169 |
print("\n" + divider, file=outputFile) |
| 170 |
else: |
| 171 |
print(divider, file=outputFile) |
| 172 |
print(header, file=outputFile) |
| 173 |
if includeDividers: |
| 174 |
print(divider + "\n", file=outputFile) |
| 175 |
|
| 176 |
def print_test_header(output_file, board, test): |
| 177 |
header = f"Test: {test.name} | {board.name} #{test.n}" |
| 178 |
divider = "-" * 80 |
| 179 |
print("\n" + divider, file=output_file) |
| 180 |
print(header, file=output_file) |
| 181 |
print(divider, file=output_file) |
| 182 |
|
| 183 |
def clean_board_name(name: str) -> str: |
| 184 |
return "".join((c if c.isalnum() else "_") for c in name) |
| 185 |
|
| 186 |
@dataclass |
| 187 |
class BoardTestConfig: |
| 188 |
board_id: str |
| 189 |
n: int |
| 190 |
loglevel: int |
| 191 |
log_to_console: bool |
| 192 |
common_log_file: Optional[IO[str]] |
| 193 |
test_list: List[Test] |
| 194 |
|
| 195 |
def test_board(config: BoardTestConfig): |
| 196 |
"""@brief Run all tests on a given board. |
| 197 |
|
| 198 |
When multiple test jobs are being used, this function is the entry point executed in |
| 199 |
child processes. |
| 200 |
|
| 201 |
Always writes both stdout and log messages of tests to a board-specific log file, and saves |
| 202 |
the output for each test to a string that is stored in the TestResult object. Depending on |
| 203 |
the logToConsole and commonLogFile parameters, output may also be copied to the console |
| 204 |
(sys.stdout) and/or a common log file for all boards. |
| 205 |
|
| 206 |
@param board_id Unique ID of the board to test. |
| 207 |
@param n Unique index of the test run. |
| 208 |
@param loglevel Log level passed to logger instance. Usually INFO or DEBUG. |
| 209 |
@param logToConsole Boolean indicating whether output should be copied to sys.stdout. |
| 210 |
@param commonLogFile If not None, an open file object to which output should be copied. |
| 211 |
""" |
| 212 |
board_id = config.board_id |
| 213 |
n = config.n |
| 214 |
loglevel = config.loglevel |
| 215 |
logToConsole = config.log_to_console |
| 216 |
commonLogFile = config.common_log_file |
| 217 |
|
| 218 |
probe = DebugProbeAggregator.get_probe_with_id(board_id) |
| 219 |
assert probe is not None |
| 220 |
session = Session(probe, **get_session_options()) |
| 221 |
board = session.board |
| 222 |
|
| 223 |
originalStdout = sys.stdout |
| 224 |
originalStderr = sys.stderr |
| 225 |
|
| 226 |
|
| 227 |
env_name = (("_" + os.environ['TOX_ENV_NAME']) if ('TOX_ENV_NAME' in os.environ) else '') |
| 228 |
name_info = "{}_{}_{}".format(env_name, clean_board_name(board.name), n) |
| 229 |
log_filename = os.path.join(TEST_OUTPUT_DIR, LOG_FILE_TEMPLATE.format(name_info)) |
| 230 |
if os.path.exists(log_filename): |
| 231 |
os.remove(log_filename) |
| 232 |
|
| 233 |
|
| 234 |
if session.options['skip_test']: |
| 235 |
print("Skipping board %s due as specified in config" % board.unique_id) |
| 236 |
return [] |
| 237 |
|
| 238 |
if board.test_binary is None: |
| 239 |
print("Skipping board %s due to missing test binary" % board.unique_id) |
| 240 |
return [] |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
log_file = open(log_filename, "w", buffering=1, encoding='utf-8', errors='backslashreplace') |
| 246 |
|
| 247 |
|
| 248 |
log_handler = RecordingLogHandler(None) |
| 249 |
log_handler.setFormatter(logging.Formatter(LOG_FORMAT)) |
| 250 |
root_logger = logging.getLogger() |
| 251 |
root_logger.setLevel(loglevel) |
| 252 |
root_logger.addHandler(log_handler) |
| 253 |
|
| 254 |
result_list = [] |
| 255 |
try: |
| 256 |
|
| 257 |
print_board_header(log_file, board, n) |
| 258 |
if commonLogFile: |
| 259 |
print_board_header(commonLogFile, board, n, includeLeadingNewline=(n != 0)) |
| 260 |
print_board_header(originalStdout, board, n, logToConsole, includeLeadingNewline=(n != 0)) |
| 261 |
|
| 262 |
|
| 263 |
for test in config.test_list: |
| 264 |
print("{} #{}: starting {}...".format(board.name, n, test.name), file=originalStdout) |
| 265 |
|
| 266 |
|
| 267 |
test.n = n |
| 268 |
|
| 269 |
|
| 270 |
print_test_header(log_file, board, test) |
| 271 |
if commonLogFile is not None: |
| 272 |
print_test_header(commonLogFile, board, test) |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
testOutput = io.StringIO() |
| 278 |
tee = IOTee(log_file, testOutput) |
| 279 |
if logToConsole: |
| 280 |
tee.add(originalStdout) |
| 281 |
if commonLogFile is not None: |
| 282 |
tee.add(commonLogFile) |
| 283 |
log_handler.stream = tee |
| 284 |
sys.stdout = tee |
| 285 |
sys.stderr = tee |
| 286 |
|
| 287 |
test_start = time() |
| 288 |
result = test.run(board) |
| 289 |
test_stop = time() |
| 290 |
result.time = test_stop - test_start |
| 291 |
tee.flush() |
| 292 |
result.output = testOutput.getvalue() |
| 293 |
result_list.append(result) |
| 294 |
|
| 295 |
passFail = "PASSED" if result.passed else "FAILED" |
| 296 |
print("{} #{}: finished {}... {} ({:.3f} s)".format( |
| 297 |
board.name, n, test.name, passFail, result.time), |
| 298 |
file=originalStdout) |
| 299 |
finally: |
| 300 |
|
| 301 |
sys.stdout = originalStdout |
| 302 |
sys.stderr = originalStderr |
| 303 |
|
| 304 |
root_logger.removeHandler(log_handler) |
| 305 |
log_handler.flush() |
| 306 |
log_handler.close() |
| 307 |
return result_list |
| 308 |
|
| 309 |
def filter_tests(args): |
| 310 |
"""@brief Generate the list of tests to run based on arguments.""" |
| 311 |
if args.exclude_tests and args.include_tests: |
| 312 |
print("Please only include or exclude tests, not both simultaneously.") |
| 313 |
sys.exit(1) |
| 314 |
excludes = [t.strip().lower() for t in args.exclude_tests.split(',')] if args.exclude_tests else [] |
| 315 |
includes = [t.strip().lower() for t in args.include_tests.split(',')] if args.include_tests else [] |
| 316 |
|
| 317 |
for test in all_tests: |
| 318 |
if excludes: |
| 319 |
include_it = (test.name.lower() not in excludes) |
| 320 |
elif includes: |
| 321 |
include_it = (test.name.lower() in includes) |
| 322 |
else: |
| 323 |
include_it = True |
| 324 |
|
| 325 |
if include_it: |
| 326 |
test_list.append(test) |
| 327 |
|
| 328 |
def main(): |
| 329 |
parser = argparse.ArgumentParser(description='pyOCD automated testing') |
| 330 |
parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging') |
| 331 |
parser.add_argument('-q', '--quiet', action="store_true", help='Hide test progress for 1 job') |
| 332 |
parser.add_argument('-j', '--jobs', action="store", default=1, type=int, metavar="JOBS", |
| 333 |
help='Set number of concurrent board tests (default is 1)') |
| 334 |
parser.add_argument('-b', '--board', action="append", metavar="ID", help="Limit testing to boards with specified unique IDs. Multiple boards can be listed.") |
| 335 |
parser.add_argument('-l', '--list-tests', action="store_true", help="Print a list of tests that will be run.") |
| 336 |
parser.add_argument('-x', '--exclude-tests', metavar="TESTS", default="", help="Comma-separated list of tests to exclude.") |
| 337 |
parser.add_argument('-i', '--include-tests', metavar="TESTS", default="", help="Comma-separated list of tests to include.") |
| 338 |
args = parser.parse_args() |
| 339 |
|
| 340 |
|
| 341 |
if 'CI_JOBS' in os.environ: |
| 342 |
args.jobs = int(os.environ['CI_JOBS']) |
| 343 |
|
| 344 |
filter_tests(args) |
| 345 |
|
| 346 |
if args.list_tests: |
| 347 |
for test in test_list: |
| 348 |
print(test.name) |
| 349 |
return |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if args.jobs > 1 and sys.platform.startswith('darwin') and sys.version_info[0:2] < (3, 4): |
| 357 |
print("WARNING: Cannot support multiple jobs on macOS prior to Python 3.4. Forcing 1 job.") |
| 358 |
args.jobs = 1 |
| 359 |
|
| 360 |
ensure_output_dir() |
| 361 |
|
| 362 |
|
| 363 |
level = logging.DEBUG if args.debug else logging.INFO |
| 364 |
if args.jobs == 1 and not args.quiet: |
| 365 |
log_file = os.path.join(TEST_OUTPUT_DIR, LOG_FILE_TEMPLATE.format(get_env_file_name())) |
| 366 |
|
| 367 |
if os.path.exists(log_file): |
| 368 |
os.remove(log_file) |
| 369 |
logToConsole = True |
| 370 |
commonLogFile = open(log_file, "a") |
| 371 |
else: |
| 372 |
logToConsole = False |
| 373 |
commonLogFile = None |
| 374 |
|
| 375 |
board_list = [] |
| 376 |
result_list = [] |
| 377 |
|
| 378 |
|
| 379 |
board_list = ConnectHelper.get_all_connected_probes(blocking=False) |
| 380 |
board_id_list = sorted(b.unique_id for b in board_list) |
| 381 |
|
| 382 |
|
| 383 |
if args.board: |
| 384 |
|
| 385 |
board_id_list = [b for b in board_id_list if any(c for c in args.board if c.lower() in b.lower())] |
| 386 |
|
| 387 |
board_id_list += [a for a in args.board if a.startswith('remote:')] |
| 388 |
|
| 389 |
|
| 390 |
test_configs = [ |
| 391 |
BoardTestConfig( |
| 392 |
board_id=board_id, |
| 393 |
n=n, |
| 394 |
loglevel=level, |
| 395 |
log_to_console=logToConsole, |
| 396 |
common_log_file=commonLogFile, |
| 397 |
test_list=test_list, |
| 398 |
) |
| 399 |
for n, board_id in enumerate(board_id_list) |
| 400 |
] |
| 401 |
|
| 402 |
|
| 403 |
start = time() |
| 404 |
if args.jobs == 1: |
| 405 |
for config in test_configs: |
| 406 |
result_list += test_board(config) |
| 407 |
else: |
| 408 |
|
| 409 |
try: |
| 410 |
pool = mp.Pool(args.jobs) |
| 411 |
|
| 412 |
|
| 413 |
async_results = [pool.apply_async(test_board, (config,)) |
| 414 |
for config in test_configs] |
| 415 |
|
| 416 |
|
| 417 |
for r in async_results: |
| 418 |
result_list += r.get(timeout=JOB_TIMEOUT) |
| 419 |
finally: |
| 420 |
pool.close() |
| 421 |
pool.join() |
| 422 |
stop = time() |
| 423 |
test_time = (stop - start) |
| 424 |
|
| 425 |
print_summary(test_list, result_list, test_time) |
| 426 |
summary_file = os.path.join(TEST_OUTPUT_DIR, SUMMARY_FILE_TEMPLATE.format(get_env_file_name())) |
| 427 |
with open(summary_file, "w") as output_file: |
| 428 |
print_summary(test_list, result_list, test_time, output_file) |
| 429 |
generate_xml_results(result_list) |
| 430 |
|
| 431 |
exit_val = 0 if Test.all_tests_pass(result_list, ignored=IGNORE_FAILURE_TESTS) else -1 |
| 432 |
exit(exit_val) |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
if __name__ == "__main__": |
| 437 |
|
| 438 |
if sys.version_info[0:2] >= (3, 4): |
| 439 |
mp.set_start_method('spawn') |
| 440 |
main() |
| 441 |
|