|
| 1 | +# |
| 2 | +# Copyright (c) 2024 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 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 | + |
| 18 | +import argparse |
| 19 | +import glob |
| 20 | +import logging |
| 21 | +import os |
| 22 | +import subprocess |
| 23 | +import sys |
| 24 | +import time |
| 25 | +from pathlib import Path |
| 26 | +from typing import Any |
| 27 | + |
| 28 | +import serial # type: ignore |
| 29 | +from pw_hdlc import rpc |
| 30 | +from pw_unit_test.rpc import run_tests |
| 31 | + |
| 32 | +PW_LOG = logging.getLogger(__name__) |
| 33 | + |
| 34 | +PROTO = Path(os.environ["PW_ROOT"], |
| 35 | + "pw_unit_test/pw_unit_test_proto/unit_test.proto") |
| 36 | + |
| 37 | + |
| 38 | +class colors: |
| 39 | + HEADER = "\033[95m" |
| 40 | + OKBLUE = "\033[94m" |
| 41 | + OKCYAN = "\033[96m" |
| 42 | + OKGREEN = "\033[92m" |
| 43 | + WARNING = "\033[93m" |
| 44 | + FAIL = "\033[91m" |
| 45 | + ENDC = "\033[0m" |
| 46 | + BOLD = "\033[1m" |
| 47 | + |
| 48 | + |
| 49 | +PASS_STRING = colors.OKGREEN + "\N{check mark}" + colors.ENDC |
| 50 | +FAIL_STRING = colors.FAIL + "FAILED" + colors.ENDC |
| 51 | + |
| 52 | + |
| 53 | +def _parse_args(): |
| 54 | + """Parses and returns the command line arguments.""" |
| 55 | + parser = argparse.ArgumentParser( |
| 56 | + description="CHIP on device unit test runner.") |
| 57 | + parser.add_argument("-d", "--device", help="the serial port to use") |
| 58 | + parser.add_argument( |
| 59 | + "-b", "--baudrate", type=int, default=115200, help="the baud rate to use" |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "-f", |
| 63 | + "--flash_image", |
| 64 | + help="A firmware image which will be flashed berfore runnning the test. Or a directory containing firmware images, each of which will be flashed and then run.", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "-o", |
| 68 | + "--output", |
| 69 | + type=argparse.FileType("wb"), |
| 70 | + default=sys.stdout.buffer, |
| 71 | + help=( |
| 72 | + "The file to which to write device output (HDLC channel 1); " |
| 73 | + "provide - or omit for stdout." |
| 74 | + ), |
| 75 | + ) |
| 76 | + return parser.parse_args() |
| 77 | + |
| 78 | + |
| 79 | +def flash_device(device: str, flash_image: str): |
| 80 | + """flashes the EFR32 device using commander""" |
| 81 | + err = subprocess.call( |
| 82 | + ["commander", "flash", "--device", "EFR32", flash_image]) |
| 83 | + if err: |
| 84 | + raise Exception("flash failed") |
| 85 | + |
| 86 | + |
| 87 | +def get_hdlc_rpc_client(device: str, baudrate: int, output: Any, **kwargs): |
| 88 | + """Get the HdlcRpcClient based on arguments.""" |
| 89 | + serial_device = serial.Serial(device, baudrate, timeout=1) |
| 90 | + reader = rpc.SerialReader(serial_device, 8192) |
| 91 | + write = serial_device.write |
| 92 | + return rpc.HdlcRpcClient( |
| 93 | + reader, |
| 94 | + PROTO, |
| 95 | + rpc.default_channels(write), |
| 96 | + lambda data: rpc.write_to_file(data, output), |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def run(args) -> int: |
| 101 | + """Run the tests. Return the number of failed tests.""" |
| 102 | + with get_hdlc_rpc_client(**vars(args)) as client: |
| 103 | + test_records = run_tests(client.rpcs()) |
| 104 | + return len(test_records.failing_tests) |
| 105 | + |
| 106 | + |
| 107 | +def list_images(flash_directory: str) -> list[str]: |
| 108 | + filenames: list[str] = glob.glob(os.path.join(flash_directory, "*.s37")) |
| 109 | + return list(map(lambda x: os.path.join(flash_directory, x), filenames)) |
| 110 | + |
| 111 | + |
| 112 | +def main() -> int: |
| 113 | + args = _parse_args() |
| 114 | + |
| 115 | + failures = 0 |
| 116 | + if args.flash_image: |
| 117 | + if os.path.isdir(args.flash_image): |
| 118 | + images = list_images(args.flash_image) |
| 119 | + if not images: |
| 120 | + raise Exception(f"No images found in `{args.flash_image}`") |
| 121 | + elif os.path.isdir(args.flash_image): |
| 122 | + images = [args.flash_image] |
| 123 | + else: |
| 124 | + raise Exception(f"File or directory not found `{args.flash_image}`") |
| 125 | + |
| 126 | + for image in images: |
| 127 | + flash_device(args.device, image) |
| 128 | + time.sleep(1) # Give time for device to boot |
| 129 | + |
| 130 | + failures += run(args) |
| 131 | + else: # No image provided. Just run what's on the device. |
| 132 | + failures += run(args) |
| 133 | + |
| 134 | + return failures |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + sys.exit(main()) |
0 commit comments