David Brazdil | ee5e25d | 2020-01-24 14:17:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 2 | # |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 3 | # Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | # https://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 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 17 | """Script which drives invocation of tests and parsing their output to produce |
| 18 | a results report. |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 19 | """ |
| 20 | |
| 21 | from __future__ import print_function |
| 22 | |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 23 | import xml.etree.ElementTree as ET |
| 24 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 25 | import argparse |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 26 | import collections |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 27 | import datetime |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 28 | import json |
| 29 | import os |
| 30 | import re |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 31 | import serial |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 32 | import subprocess |
| 33 | import sys |
| 34 | |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 35 | HFTEST_LOG_PREFIX = "[hftest] " |
| 36 | HFTEST_LOG_FAILURE_PREFIX = "Failure:" |
| 37 | HFTEST_LOG_FINISHED = "FINISHED" |
| 38 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 39 | HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]" |
| 40 | HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]" |
| 41 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 42 | HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname( |
| 43 | os.path.abspath(__file__)))) |
David Brazdil | 5715f04 | 2019-08-27 11:11:51 +0100 | [diff] [blame] | 44 | DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 45 | FVP_BINARY = os.path.join( |
| 46 | os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMv8A_pkg", "models", |
| 47 | "Linux64_GCC-4.9", "FVP_Base_RevC-2xAEMv8A") |
David Brazdil | 21204ae | 2019-10-30 19:22:38 +0000 | [diff] [blame] | 48 | FVP_PREBUILTS_ROOT = os.path.join( |
| 49 | HF_ROOT, "prebuilts", "linux-aarch64", "arm-trusted-firmware", "fvp") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 50 | FVP_PREBUILT_DTS = os.path.join( |
David Brazdil | 21204ae | 2019-10-30 19:22:38 +0000 | [diff] [blame] | 51 | FVP_PREBUILTS_ROOT, "fvp-base-gicv3-psci-1t.dts") |
| 52 | FVP_PREBUILT_BL31 = os.path.join(FVP_PREBUILTS_ROOT, "bl31.bin") |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 53 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 54 | def read_file(path): |
| 55 | with open(path, "r") as f: |
| 56 | return f.read() |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 57 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 58 | def write_file(path, to_write, append=False): |
| 59 | with open(path, "a" if append else "w") as f: |
| 60 | f.write(to_write) |
| 61 | |
| 62 | def append_file(path, to_write): |
| 63 | write_file(path, to_write, append=True) |
| 64 | |
| 65 | def join_if_not_None(*args): |
| 66 | return " ".join(filter(lambda x: x, args)) |
| 67 | |
| 68 | class ArtifactsManager: |
| 69 | """Class which manages folder with test artifacts.""" |
| 70 | |
| 71 | def __init__(self, log_dir): |
| 72 | self.created_files = [] |
| 73 | self.log_dir = log_dir |
| 74 | |
| 75 | # Create directory. |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 76 | try: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 77 | os.makedirs(self.log_dir) |
| 78 | except OSError: |
| 79 | if not os.path.isdir(self.log_dir): |
| 80 | raise |
| 81 | print("Logs saved under", log_dir) |
| 82 | |
| 83 | # Create files expected by the Sponge test result parser. |
| 84 | self.sponge_log_path = self.create_file("sponge_log", ".log") |
| 85 | self.sponge_xml_path = self.create_file("sponge_log", ".xml") |
| 86 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 87 | def gen_file_path(self, basename, extension): |
| 88 | """Generate path to a file in the log directory.""" |
| 89 | return os.path.join(self.log_dir, basename + extension) |
| 90 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 91 | def create_file(self, basename, extension): |
| 92 | """Create and touch a new file in the log folder. Ensure that no other |
| 93 | file of the same name was created by this instance of ArtifactsManager. |
| 94 | """ |
| 95 | # Determine the path of the file. |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 96 | path = self.gen_file_path(basename, extension) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 97 | |
| 98 | # Check that the path is unique. |
| 99 | assert(path not in self.created_files) |
| 100 | self.created_files += [ path ] |
| 101 | |
| 102 | # Touch file. |
| 103 | with open(path, "w") as f: |
| 104 | pass |
| 105 | |
| 106 | return path |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 107 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 108 | def get_file(self, basename, extension): |
| 109 | """Return path to a file in the log folder. Assert that it was created |
| 110 | by this instance of ArtifactsManager.""" |
| 111 | path = self.gen_file_path(basename, extension) |
| 112 | assert(path in self.created_files) |
| 113 | return path |
| 114 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 115 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 116 | # Tuple holding the arguments common to all driver constructors. |
| 117 | # This is to avoid having to pass arguments from subclasses to superclasses. |
| 118 | DriverArgs = collections.namedtuple("DriverArgs", [ |
| 119 | "artifacts", |
| 120 | "kernel", |
| 121 | "initrd", |
| 122 | "vm_args", |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 123 | "cpu", |
| 124 | "serial_dev", |
| 125 | "serial_baudrate", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 126 | ]) |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 127 | |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 128 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 129 | # State shared between the common Driver class and its subclasses during |
| 130 | # a single invocation of the target platform. |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 131 | class DriverRunState: |
| 132 | def __init__(self, log_path): |
| 133 | self.log_path = log_path |
| 134 | self.ret_code = 0 |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 135 | |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 136 | def set_ret_code(self, ret_code): |
| 137 | self.ret_code = ret_code |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 138 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 139 | class DriverRunException(Exception): |
| 140 | """Exception thrown if subprocess invoked by a driver returned non-zero |
| 141 | status code. Used to fast-exit from a driver command sequence.""" |
| 142 | pass |
| 143 | |
| 144 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 145 | class Driver: |
| 146 | """Parent class of drivers for all testable platforms.""" |
| 147 | |
| 148 | def __init__(self, args): |
| 149 | self.args = args |
| 150 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 151 | def get_run_log(self, run_name): |
| 152 | """Return path to the main log of a given test run.""" |
| 153 | return self.args.artifacts.get_file(run_name, ".log") |
| 154 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 155 | def start_run(self, run_name): |
| 156 | """Hook called by Driver subclasses before they invoke the target |
| 157 | platform.""" |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 158 | return DriverRunState(self.args.artifacts.create_file(run_name, ".log")) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 159 | |
| 160 | def exec_logged(self, run_state, exec_args): |
| 161 | """Run a subprocess on behalf of a Driver subclass and append its |
| 162 | stdout and stderr to the main log.""" |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 163 | assert(run_state.ret_code == 0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 164 | with open(run_state.log_path, "a") as f: |
| 165 | f.write("$ {}\r\n".format(" ".join(exec_args))) |
| 166 | f.flush() |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 167 | ret_code = subprocess.call(exec_args, stdout=f, stderr=f) |
| 168 | if ret_code != 0: |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 169 | run_state.set_ret_code(ret_code) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 170 | raise DriverRunException() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 171 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 172 | def finish_run(self, run_state): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 173 | """Hook called by Driver subclasses after they finished running the |
| 174 | target platform. `ret_code` argument is the return code of the main |
| 175 | command run by the driver. A corresponding log message is printed.""" |
| 176 | # Decode return code and add a message to the log. |
| 177 | with open(run_state.log_path, "a") as f: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 178 | if run_state.ret_code == 124: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 179 | f.write("\r\n{}{} timed out\r\n".format( |
| 180 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 181 | elif run_state.ret_code != 0: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 182 | f.write("\r\n{}{} process return code {}\r\n".format( |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 183 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX, |
| 184 | run_state.ret_code)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 185 | |
| 186 | # Append log of this run to full test log. |
| 187 | log_content = read_file(run_state.log_path) |
| 188 | append_file( |
| 189 | self.args.artifacts.sponge_log_path, |
| 190 | log_content + "\r\n\r\n") |
| 191 | return log_content |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 192 | |
| 193 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 194 | class QemuDriver(Driver): |
| 195 | """Driver which runs tests in QEMU.""" |
| 196 | |
| 197 | def __init__(self, args): |
| 198 | Driver.__init__(self, args) |
| 199 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 200 | def gen_exec_args(self, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 201 | """Generate command line arguments for QEMU.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 202 | time_limit = "120s" if is_long_running else "10s" |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 203 | # If no CPU configuration is selected, then test against the maximum |
| 204 | # configuration, "max", supported by QEMU. |
| 205 | cpu = self.args.cpu or "max" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 206 | exec_args = [ |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 207 | "timeout", "--foreground", time_limit, |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 208 | "./prebuilts/linux-x64/qemu/qemu-system-aarch64", |
Andrew Scull | 2925e42 | 2019-10-04 13:29:53 +0100 | [diff] [blame] | 209 | "-machine", "virt,virtualization=on,gic_version=3", |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 210 | "-cpu", cpu, "-smp", "4", "-m", "64M", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 211 | "-nographic", "-nodefaults", "-serial", "stdio", |
Andrew Scull | 2925e42 | 2019-10-04 13:29:53 +0100 | [diff] [blame] | 212 | "-d", "unimp", "-kernel", self.args.kernel, |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 213 | ] |
| 214 | |
| 215 | if self.args.initrd: |
| 216 | exec_args += ["-initrd", self.args.initrd] |
| 217 | |
| 218 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 219 | if vm_args: |
| 220 | exec_args += ["-append", vm_args] |
| 221 | |
| 222 | return exec_args |
| 223 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 224 | def run(self, run_name, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 225 | """Run test given by `test_args` in QEMU.""" |
| 226 | run_state = self.start_run(run_name) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 227 | |
| 228 | try: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 229 | # Execute test in QEMU.. |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 230 | exec_args = self.gen_exec_args(test_args, is_long_running) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 231 | self.exec_logged(run_state, exec_args) |
| 232 | except DriverRunException: |
| 233 | pass |
| 234 | |
| 235 | return self.finish_run(run_state) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 236 | |
| 237 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 238 | class FvpDriver(Driver): |
Andrew Walbran | 2021574 | 2019-11-18 11:35:05 +0000 | [diff] [blame] | 239 | """Driver which runs tests in Arm FVP emulator.""" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 240 | |
| 241 | def __init__(self, args): |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 242 | if args.cpu: |
| 243 | raise ValueError("FVP emulator does not support the --cpu option.") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 244 | Driver.__init__(self, args) |
| 245 | |
| 246 | def gen_dts(self, dts_path, test_args, initrd_start, initrd_end): |
| 247 | """Create a DeviceTree source which will be compiled into a DTB and |
| 248 | passed to FVP for a test run.""" |
| 249 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 250 | write_file(dts_path, read_file(FVP_PREBUILT_DTS)) |
| 251 | append_file(dts_path, """ |
| 252 | / {{ |
| 253 | chosen {{ |
| 254 | bootargs = "{}"; |
| 255 | stdout-path = "serial0:115200n8"; |
| 256 | linux,initrd-start = <{}>; |
| 257 | linux,initrd-end = <{}>; |
| 258 | }}; |
| 259 | }}; |
| 260 | """.format(vm_args, initrd_start, initrd_end)) |
| 261 | |
| 262 | def gen_fvp_args( |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 263 | self, is_long_running, initrd_start, uart0_log_path, uart1_log_path, |
| 264 | dtb_path): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 265 | """Generate command line arguments for FVP.""" |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 266 | time_limit = "80s" if is_long_running else "40s" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 267 | fvp_args = [ |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 268 | "timeout", "--foreground", time_limit, |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 269 | FVP_BINARY, |
| 270 | "-C", "pctl.startup=0.0.0.0", |
| 271 | "-C", "bp.secure_memory=0", |
| 272 | "-C", "cluster0.NUM_CORES=4", |
| 273 | "-C", "cluster1.NUM_CORES=4", |
| 274 | "-C", "cache_state_modelled=0", |
| 275 | "-C", "bp.vis.disable_visualisation=true", |
| 276 | "-C", "bp.vis.rate_limit-enable=false", |
| 277 | "-C", "bp.terminal_0.start_telnet=false", |
| 278 | "-C", "bp.terminal_1.start_telnet=false", |
| 279 | "-C", "bp.terminal_2.start_telnet=false", |
| 280 | "-C", "bp.terminal_3.start_telnet=false", |
| 281 | "-C", "bp.pl011_uart0.untimed_fifos=1", |
| 282 | "-C", "bp.pl011_uart0.unbuffered_output=1", |
| 283 | "-C", "bp.pl011_uart0.out_file=" + uart0_log_path, |
| 284 | "-C", "bp.pl011_uart1.out_file=" + uart1_log_path, |
| 285 | "-C", "cluster0.cpu0.RVBAR=0x04020000", |
| 286 | "-C", "cluster0.cpu1.RVBAR=0x04020000", |
| 287 | "-C", "cluster0.cpu2.RVBAR=0x04020000", |
| 288 | "-C", "cluster0.cpu3.RVBAR=0x04020000", |
| 289 | "-C", "cluster1.cpu0.RVBAR=0x04020000", |
| 290 | "-C", "cluster1.cpu1.RVBAR=0x04020000", |
| 291 | "-C", "cluster1.cpu2.RVBAR=0x04020000", |
| 292 | "-C", "cluster1.cpu3.RVBAR=0x04020000", |
David Brazdil | 21204ae | 2019-10-30 19:22:38 +0000 | [diff] [blame] | 293 | "--data", "cluster0.cpu0=" + FVP_PREBUILT_BL31 + "@0x04020000", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 294 | "--data", "cluster0.cpu0=" + dtb_path + "@0x82000000", |
| 295 | "--data", "cluster0.cpu0=" + self.args.kernel + "@0x80000000", |
| 296 | "-C", "bp.ve_sysregs.mmbSiteDefault=0", |
| 297 | "-C", "bp.ve_sysregs.exit_on_shutdown=1", |
| 298 | ] |
| 299 | |
| 300 | if self.args.initrd: |
| 301 | fvp_args += [ |
| 302 | "--data", |
| 303 | "cluster0.cpu0={}@{}".format( |
| 304 | self.args.initrd, hex(initrd_start)) |
| 305 | ] |
| 306 | |
| 307 | return fvp_args |
| 308 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 309 | def run(self, run_name, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 310 | run_state = self.start_run(run_name) |
| 311 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 312 | dts_path = self.args.artifacts.create_file(run_name, ".dts") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 313 | dtb_path = self.args.artifacts.create_file(run_name, ".dtb") |
| 314 | uart0_log_path = self.args.artifacts.create_file(run_name, ".uart0.log") |
| 315 | uart1_log_path = self.args.artifacts.create_file(run_name, ".uart1.log") |
| 316 | |
| 317 | initrd_start = 0x84000000 |
| 318 | if self.args.initrd: |
| 319 | initrd_end = initrd_start + os.path.getsize(self.args.initrd) |
| 320 | else: |
| 321 | initrd_end = 0x85000000 # Default value |
| 322 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 323 | try: |
| 324 | # Create a DT to pass to FVP. |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 325 | self.gen_dts(dts_path, test_args, initrd_start, initrd_end) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 326 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 327 | # Compile DTS to DTB. |
| 328 | dtc_args = [ |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 329 | DTC_SCRIPT, "compile", "-i", dts_path, "-o", dtb_path, |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 330 | ] |
| 331 | self.exec_logged(run_state, dtc_args) |
| 332 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 333 | # Run FVP. |
| 334 | fvp_args = self.gen_fvp_args( |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 335 | is_long_running, initrd_start, uart0_log_path, uart1_log_path, |
| 336 | dtb_path) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 337 | self.exec_logged(run_state, fvp_args) |
| 338 | except DriverRunException: |
| 339 | pass |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 340 | |
| 341 | # Append UART0 output to main log. |
| 342 | append_file(run_state.log_path, read_file(uart0_log_path)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 343 | return self.finish_run(run_state) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 344 | |
| 345 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 346 | class SerialDriver(Driver): |
| 347 | """Driver which communicates with a device over the serial port.""" |
| 348 | |
| 349 | def __init__(self, args): |
| 350 | Driver.__init__(self, args) |
| 351 | self.tty_file = self.args.serial_dev |
| 352 | self.baudrate = self.args.serial_baudrate |
| 353 | input("Press ENTER and then reset the device...") |
| 354 | |
| 355 | def run(self, run_name, test_args, is_long_running): |
| 356 | """Communicate `test_args` to the device over the serial port.""" |
| 357 | run_state = self.start_run(run_name) |
| 358 | |
| 359 | with serial.Serial(self.tty_file, self.baudrate, timeout=10) as ser: |
| 360 | with open(run_state.log_path, "a") as f: |
| 361 | while True: |
| 362 | # Read one line from the serial port. |
| 363 | line = ser.readline().decode('utf-8') |
| 364 | if len(line) == 0: |
| 365 | # Timeout |
| 366 | run_state.set_ret_code(124) |
| 367 | input("Timeout. " + |
| 368 | "Press ENTER and then reset the device...") |
| 369 | break |
| 370 | # Write the line to the log file. |
| 371 | f.write(line) |
| 372 | if HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 373 | # Device is waiting for `test_args`. |
| 374 | ser.write(test_args.encode('ascii')) |
| 375 | ser.write(b'\r') |
| 376 | elif HFTEST_CTRL_FINISHED in line: |
| 377 | # Device has finished running this test and will reboot. |
| 378 | break |
| 379 | return self.finish_run(run_state) |
| 380 | |
| 381 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 382 | # Tuple used to return information about the results of running a set of tests. |
| 383 | TestRunnerResult = collections.namedtuple("TestRunnerResult", [ |
| 384 | "tests_run", |
| 385 | "tests_failed", |
| 386 | ]) |
| 387 | |
| 388 | |
| 389 | class TestRunner: |
| 390 | """Class which communicates with a test platform to obtain a list of |
| 391 | available tests and driving their execution.""" |
| 392 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 393 | def __init__(self, artifacts, driver, image_name, suite_regex, test_regex, |
| 394 | skip_long_running_tests): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 395 | self.artifacts = artifacts |
| 396 | self.driver = driver |
| 397 | self.image_name = image_name |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 398 | self.skip_long_running_tests = skip_long_running_tests |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 399 | |
| 400 | self.suite_re = re.compile(suite_regex or ".*") |
| 401 | self.test_re = re.compile(test_regex or ".*") |
| 402 | |
| 403 | def extract_hftest_lines(self, raw): |
| 404 | """Extract hftest-specific lines from a raw output from an invocation |
| 405 | of the test platform.""" |
| 406 | lines = [] |
| 407 | for line in raw.splitlines(): |
| 408 | if line.startswith("VM "): |
| 409 | line = line[len("VM 0: "):] |
| 410 | if line.startswith(HFTEST_LOG_PREFIX): |
| 411 | lines.append(line[len(HFTEST_LOG_PREFIX):]) |
| 412 | return lines |
| 413 | |
| 414 | def get_test_json(self): |
| 415 | """Invoke the test platform and request a JSON of available test and |
| 416 | test suites.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 417 | out = self.driver.run("json", "json", False) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 418 | hf_out = "\n".join(self.extract_hftest_lines(out)) |
| 419 | try: |
| 420 | return json.loads(hf_out) |
| 421 | except ValueError as e: |
| 422 | print(out) |
| 423 | raise e |
| 424 | |
| 425 | def collect_results(self, fn, it, xml_node): |
| 426 | """Run `fn` on every entry in `it` and collect their TestRunnerResults. |
| 427 | Insert "tests" and "failures" nodes to `xml_node`.""" |
| 428 | tests_run = 0 |
| 429 | tests_failed = 0 |
| 430 | for i in it: |
| 431 | sub_result = fn(i) |
| 432 | assert(sub_result.tests_run >= sub_result.tests_failed) |
| 433 | tests_run += sub_result.tests_run |
| 434 | tests_failed += sub_result.tests_failed |
| 435 | |
| 436 | xml_node.set("tests", str(tests_run)) |
| 437 | xml_node.set("failures", str(tests_failed)) |
| 438 | return TestRunnerResult(tests_run, tests_failed) |
| 439 | |
| 440 | def is_passed_test(self, test_out): |
| 441 | """Parse the output of a test and return True if it passed.""" |
| 442 | return \ |
| 443 | len(test_out) > 0 and \ |
| 444 | test_out[-1] == HFTEST_LOG_FINISHED and \ |
| 445 | not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out) |
| 446 | |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 447 | def get_log_name(self, suite, test): |
| 448 | """Returns a string with a generated log name for the test.""" |
| 449 | log_name = "" |
| 450 | |
| 451 | cpu = self.driver.args.cpu |
| 452 | if cpu: |
| 453 | log_name += cpu + "." |
| 454 | |
| 455 | log_name += suite["name"] + "." + test["name"] |
| 456 | |
| 457 | return log_name |
| 458 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 459 | def run_test(self, suite, test, suite_xml): |
| 460 | """Invoke the test platform and request to run a given `test` in given |
| 461 | `suite`. Create a new XML node with results under `suite_xml`. |
| 462 | Test only invoked if it matches the regex given to constructor.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 463 | if not self.test_re.match(test["name"]): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 464 | return TestRunnerResult(tests_run=0, tests_failed=0) |
| 465 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 466 | if self.skip_long_running_tests and test["is_long_running"]: |
| 467 | print(" SKIP", test["name"]) |
| 468 | return TestRunnerResult(tests_run=0, tests_failed=0) |
| 469 | |
| 470 | print(" RUN", test["name"]) |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 471 | log_name = self.get_log_name(suite, test) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 472 | |
| 473 | test_xml = ET.SubElement(suite_xml, "testcase") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 474 | test_xml.set("name", test["name"]) |
| 475 | test_xml.set("classname", suite["name"]) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 476 | test_xml.set("status", "run") |
| 477 | |
| 478 | out = self.extract_hftest_lines(self.driver.run( |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 479 | log_name, "run {} {}".format(suite["name"], test["name"]), |
| 480 | test["is_long_running"])) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 481 | |
| 482 | if self.is_passed_test(out): |
| 483 | print(" PASS") |
| 484 | return TestRunnerResult(tests_run=1, tests_failed=0) |
| 485 | else: |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 486 | print("[x] FAIL --", self.driver.get_run_log(log_name)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 487 | failure_xml = ET.SubElement(test_xml, "failure") |
| 488 | # TODO: set a meaningful message and put log in CDATA |
| 489 | failure_xml.set("message", "Test failed") |
| 490 | return TestRunnerResult(tests_run=1, tests_failed=1) |
| 491 | |
| 492 | def run_suite(self, suite, xml): |
| 493 | """Invoke the test platform and request to run all matching tests in |
| 494 | `suite`. Create new XML nodes with results under `xml`. |
| 495 | Suite skipped if it does not match the regex given to constructor.""" |
| 496 | if not self.suite_re.match(suite["name"]): |
| 497 | return TestRunnerResult(tests_run=0, tests_failed=0) |
| 498 | |
| 499 | print(" SUITE", suite["name"]) |
| 500 | suite_xml = ET.SubElement(xml, "testsuite") |
| 501 | suite_xml.set("name", suite["name"]) |
| 502 | |
| 503 | return self.collect_results( |
| 504 | lambda test: self.run_test(suite, test, suite_xml), |
| 505 | suite["tests"], |
| 506 | suite_xml) |
| 507 | |
| 508 | def run_tests(self): |
| 509 | """Run all suites and tests matching regexes given to constructor. |
| 510 | Write results to sponge log XML. Return the number of run and failed |
| 511 | tests.""" |
| 512 | |
| 513 | test_spec = self.get_test_json() |
| 514 | timestamp = datetime.datetime.now().replace(microsecond=0).isoformat() |
| 515 | |
| 516 | xml = ET.Element("testsuites") |
| 517 | xml.set("name", self.image_name) |
| 518 | xml.set("timestamp", timestamp) |
| 519 | |
| 520 | result = self.collect_results( |
| 521 | lambda suite: self.run_suite(suite, xml), |
| 522 | test_spec["suites"], |
| 523 | xml) |
| 524 | |
| 525 | # Write XML to file. |
David Brazdil | ee5e25d | 2020-01-24 14:17:45 +0000 | [diff] [blame] | 526 | ET.ElementTree(xml).write(self.artifacts.sponge_xml_path, |
| 527 | encoding='utf-8', xml_declaration=True) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 528 | |
| 529 | if result.tests_failed > 0: |
| 530 | print("[x] FAIL:", result.tests_failed, "of", result.tests_run, |
| 531 | "tests failed") |
| 532 | elif result.tests_run > 0: |
| 533 | print(" PASS: all", result.tests_run, "tests passed") |
| 534 | |
| 535 | return result |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 536 | |
| 537 | |
| 538 | def Main(): |
| 539 | parser = argparse.ArgumentParser() |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 540 | parser.add_argument("image") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 541 | parser.add_argument("--out", required=True) |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 542 | parser.add_argument("--log", required=True) |
Andrew Walbran | 7559fcf | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 543 | parser.add_argument("--out_initrd") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 544 | parser.add_argument("--initrd") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 545 | parser.add_argument("--suite") |
| 546 | parser.add_argument("--test") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 547 | parser.add_argument("--vm_args") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 548 | parser.add_argument("--driver", default="qemu") |
| 549 | parser.add_argument("--serial-dev", default="/dev/ttyUSB0") |
| 550 | parser.add_argument("--serial-baudrate", type=int, default=115200) |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 551 | parser.add_argument("--skip-long-running-tests", action="store_true") |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 552 | parser.add_argument("--cpu", |
| 553 | help="Selects the CPU configuration for the run environment.") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 554 | args = parser.parse_args() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 555 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 556 | # Resolve some paths. |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 557 | image = os.path.join(args.out, args.image + ".bin") |
| 558 | initrd = None |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 559 | image_name = args.image |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 560 | if args.initrd: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 561 | initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd) |
| 562 | initrd = os.path.join(initrd_dir, "initrd.img") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 563 | image_name += "_" + args.initrd |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 564 | vm_args = args.vm_args or "" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 565 | |
| 566 | # Create class which will manage all test artifacts. |
| 567 | artifacts = ArtifactsManager(os.path.join(args.log, image_name)) |
| 568 | |
| 569 | # Create a driver for the platform we want to test on. |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 570 | driver_args = DriverArgs(artifacts, image, initrd, vm_args, args.cpu, |
| 571 | args.serial_dev, args.serial_baudrate) |
| 572 | |
| 573 | if args.driver == "qemu": |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 574 | driver = QemuDriver(driver_args) |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame^] | 575 | elif args.driver == "fvp": |
| 576 | driver = FvpDriver(driver_args) |
| 577 | elif args.driver == "serial": |
| 578 | driver = SerialDriver(driver_args) |
| 579 | else: |
| 580 | raise Exception("Unknown driver name: {}".format(args.driver)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 581 | |
| 582 | # Create class which will drive test execution. |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 583 | runner = TestRunner(artifacts, driver, image_name, args.suite, args.test, |
| 584 | args.skip_long_running_tests) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 585 | |
| 586 | # Run tests. |
| 587 | runner_result = runner.run_tests() |
| 588 | |
| 589 | # Print error message if no tests were run as this is probably unexpected. |
| 590 | # Return suitable error code. |
| 591 | if runner_result.tests_run == 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 592 | print("Error: no tests match") |
| 593 | return 10 |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 594 | elif runner_result.tests_failed > 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 595 | return 1 |
| 596 | else: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 597 | return 0 |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 598 | |
| 599 | if __name__ == "__main__": |
| 600 | sys.exit(Main()) |