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