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 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 5 | # Use of this source code is governed by a BSD-style |
| 6 | # license that can be found in the LICENSE file or at |
| 7 | # https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 8 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 9 | """Script which drives invocation of tests and parsing their output to produce |
| 10 | a results report. |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 11 | """ |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 15 | import xml.etree.ElementTree as ET |
| 16 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 17 | import argparse |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 18 | from abc import ABC, abstractmethod |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 19 | import collections |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 20 | import datetime |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 21 | import importlib |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 22 | import json |
| 23 | import os |
| 24 | import re |
| 25 | import subprocess |
| 26 | import sys |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 27 | import time |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 28 | import fdt |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 29 | |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 30 | HFTEST_LOG_PREFIX = "[hftest] " |
| 31 | HFTEST_LOG_FAILURE_PREFIX = "Failure:" |
| 32 | HFTEST_LOG_FINISHED = "FINISHED" |
| 33 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 34 | HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]" |
| 35 | HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]" |
| 36 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 37 | HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname( |
| 38 | os.path.abspath(__file__)))) |
David Brazdil | 5715f04 | 2019-08-27 11:11:51 +0100 | [diff] [blame] | 39 | DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 40 | FVP_BINARY = os.path.join( |
| 41 | os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMv8A_pkg", "models", |
Olivier Deprez | e415304 | 2020-10-02 15:24:59 +0200 | [diff] [blame] | 42 | "Linux64_GCC-6.4", "FVP_Base_RevC-2xAEMv8A") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 43 | HF_PREBUILTS = os.path.join(HF_ROOT, "prebuilts") |
| 44 | FVP_PREBUILTS_TFA_TRUSTY_ROOT = os.path.join( |
| 45 | HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", "fvp") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 46 | FVP_PREBUILT_DTS = os.path.join( |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 47 | FVP_PREBUILTS_TFA_TRUSTY_ROOT, "fvp-base-gicv3-psci-1t.dts") |
| 48 | |
| 49 | FVP_PREBUILT_TFA_ROOT = os.path.join( |
| 50 | HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a", "fvp") |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 51 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 52 | def read_file(path): |
| 53 | with open(path, "r") as f: |
| 54 | return f.read() |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 55 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 56 | def write_file(path, to_write, append=False): |
| 57 | with open(path, "a" if append else "w") as f: |
| 58 | f.write(to_write) |
| 59 | |
| 60 | def append_file(path, to_write): |
| 61 | write_file(path, to_write, append=True) |
| 62 | |
| 63 | def join_if_not_None(*args): |
| 64 | return " ".join(filter(lambda x: x, args)) |
| 65 | |
| 66 | class ArtifactsManager: |
| 67 | """Class which manages folder with test artifacts.""" |
| 68 | |
| 69 | def __init__(self, log_dir): |
| 70 | self.created_files = [] |
| 71 | self.log_dir = log_dir |
| 72 | |
| 73 | # Create directory. |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 74 | try: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 75 | os.makedirs(self.log_dir) |
| 76 | except OSError: |
| 77 | if not os.path.isdir(self.log_dir): |
| 78 | raise |
| 79 | print("Logs saved under", log_dir) |
| 80 | |
| 81 | # Create files expected by the Sponge test result parser. |
| 82 | self.sponge_log_path = self.create_file("sponge_log", ".log") |
| 83 | self.sponge_xml_path = self.create_file("sponge_log", ".xml") |
| 84 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 85 | def gen_file_path(self, basename, extension): |
| 86 | """Generate path to a file in the log directory.""" |
| 87 | return os.path.join(self.log_dir, basename + extension) |
| 88 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 89 | def create_file(self, basename, extension): |
| 90 | """Create and touch a new file in the log folder. Ensure that no other |
| 91 | file of the same name was created by this instance of ArtifactsManager. |
| 92 | """ |
| 93 | # Determine the path of the file. |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 94 | path = self.gen_file_path(basename, extension) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 95 | |
| 96 | # Check that the path is unique. |
| 97 | assert(path not in self.created_files) |
| 98 | self.created_files += [ path ] |
| 99 | |
| 100 | # Touch file. |
| 101 | with open(path, "w") as f: |
| 102 | pass |
| 103 | |
| 104 | return path |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 105 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 106 | def get_file(self, basename, extension): |
| 107 | """Return path to a file in the log folder. Assert that it was created |
| 108 | by this instance of ArtifactsManager.""" |
| 109 | path = self.gen_file_path(basename, extension) |
| 110 | assert(path in self.created_files) |
| 111 | return path |
| 112 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 113 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 114 | # Tuple holding the arguments common to all driver constructors. |
| 115 | # This is to avoid having to pass arguments from subclasses to superclasses. |
| 116 | DriverArgs = collections.namedtuple("DriverArgs", [ |
| 117 | "artifacts", |
| 118 | "kernel", |
| 119 | "initrd", |
| 120 | "vm_args", |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 121 | "cpu", |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 122 | "partitions" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 123 | ]) |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 124 | |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 125 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 126 | # State shared between the common Driver class and its subclasses during |
| 127 | # a single invocation of the target platform. |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 128 | class DriverRunState: |
| 129 | def __init__(self, log_path): |
| 130 | self.log_path = log_path |
| 131 | self.ret_code = 0 |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 132 | |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 133 | def set_ret_code(self, ret_code): |
| 134 | self.ret_code = ret_code |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 135 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 136 | class DriverRunException(Exception): |
| 137 | """Exception thrown if subprocess invoked by a driver returned non-zero |
| 138 | status code. Used to fast-exit from a driver command sequence.""" |
| 139 | pass |
| 140 | |
| 141 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 142 | class Driver: |
| 143 | """Parent class of drivers for all testable platforms.""" |
| 144 | |
| 145 | def __init__(self, args): |
| 146 | self.args = args |
| 147 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 148 | def get_run_log(self, run_name): |
| 149 | """Return path to the main log of a given test run.""" |
| 150 | return self.args.artifacts.get_file(run_name, ".log") |
| 151 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 152 | def start_run(self, run_name): |
| 153 | """Hook called by Driver subclasses before they invoke the target |
| 154 | platform.""" |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 155 | return DriverRunState(self.args.artifacts.create_file(run_name, ".log")) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 156 | |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 157 | def exec_logged(self, run_state, exec_args, cwd=None): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 158 | """Run a subprocess on behalf of a Driver subclass and append its |
| 159 | stdout and stderr to the main log.""" |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 160 | assert(run_state.ret_code == 0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 161 | with open(run_state.log_path, "a") as f: |
| 162 | f.write("$ {}\r\n".format(" ".join(exec_args))) |
| 163 | f.flush() |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 164 | ret_code = subprocess.call(exec_args, stdout=f, stderr=f, cwd=cwd) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 165 | if ret_code != 0: |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 166 | run_state.set_ret_code(ret_code) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 167 | raise DriverRunException() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 168 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 169 | def finish_run(self, run_state): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 170 | """Hook called by Driver subclasses after they finished running the |
| 171 | target platform. `ret_code` argument is the return code of the main |
| 172 | command run by the driver. A corresponding log message is printed.""" |
| 173 | # Decode return code and add a message to the log. |
| 174 | with open(run_state.log_path, "a") as f: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 175 | if run_state.ret_code == 124: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 176 | f.write("\r\n{}{} timed out\r\n".format( |
| 177 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 178 | elif run_state.ret_code != 0: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 179 | f.write("\r\n{}{} process return code {}\r\n".format( |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 180 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX, |
| 181 | run_state.ret_code)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 182 | |
| 183 | # Append log of this run to full test log. |
| 184 | log_content = read_file(run_state.log_path) |
| 185 | append_file( |
| 186 | self.args.artifacts.sponge_log_path, |
| 187 | log_content + "\r\n\r\n") |
| 188 | return log_content |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 189 | |
| 190 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 191 | class QemuDriver(Driver): |
| 192 | """Driver which runs tests in QEMU.""" |
| 193 | |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 194 | def __init__(self, args, qemu_wd, tfa): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 195 | Driver.__init__(self, args) |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 196 | self.qemu_wd = qemu_wd |
| 197 | self.tfa = tfa |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 198 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 199 | def gen_exec_args(self, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 200 | """Generate command line arguments for QEMU.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 201 | time_limit = "120s" if is_long_running else "10s" |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 202 | # If no CPU configuration is selected, then test against the maximum |
| 203 | # configuration, "max", supported by QEMU. |
| 204 | cpu = self.args.cpu or "max" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 205 | exec_args = [ |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 206 | "timeout", "--foreground", time_limit, |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 207 | os.path.abspath("prebuilts/linux-x64/qemu/qemu-system-aarch64"), |
Andrew Walbran | a081a29 | 2020-01-23 10:08:42 +0000 | [diff] [blame] | 208 | "-machine", "virt,virtualization=on,gic-version=3", |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 209 | "-cpu", cpu, "-smp", "4", "-m", "1G", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 210 | "-nographic", "-nodefaults", "-serial", "stdio", |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 211 | "-d", "unimp", "-kernel", os.path.abspath(self.args.kernel), |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 212 | ] |
| 213 | |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 214 | if self.tfa: |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 215 | bl1_path = os.path.join( |
| 216 | HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", |
| 217 | "qemu", "bl1.bin") |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 218 | exec_args += ["-bios", |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 219 | os.path.abspath(bl1_path), |
| 220 | "-machine", "secure=on", "-semihosting-config", |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 221 | "enable,target=native"] |
| 222 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 223 | if self.args.initrd: |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 224 | exec_args += ["-initrd", os.path.abspath(self.args.initrd)] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 225 | |
| 226 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 227 | if vm_args: |
| 228 | exec_args += ["-append", vm_args] |
| 229 | |
| 230 | return exec_args |
| 231 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 232 | def run(self, run_name, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 233 | """Run test given by `test_args` in QEMU.""" |
| 234 | run_state = self.start_run(run_name) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 235 | |
| 236 | try: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 237 | # Execute test in QEMU.. |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 238 | exec_args = self.gen_exec_args(test_args, is_long_running) |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 239 | self.exec_logged(run_state, exec_args, |
| 240 | cwd=self.qemu_wd) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 241 | except DriverRunException: |
| 242 | pass |
| 243 | |
| 244 | return self.finish_run(run_state) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 245 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 246 | def finish(self): |
| 247 | """Clean up after running tests.""" |
| 248 | pass |
| 249 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 250 | class FvpDriver(Driver, ABC): |
| 251 | """Base class for driver which runs tests in Arm FVP emulator.""" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 252 | |
| 253 | def __init__(self, args): |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 254 | if args.cpu: |
| 255 | raise ValueError("FVP emulator does not support the --cpu option.") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 256 | super().__init__(args) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 257 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 258 | @property |
| 259 | @abstractmethod |
| 260 | def CPU_START_ADDRESS(self): |
| 261 | pass |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 262 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 263 | @property |
| 264 | @abstractmethod |
| 265 | def DTB_ADDRESS(self): |
| 266 | pass |
| 267 | |
| 268 | @property |
| 269 | @abstractmethod |
| 270 | def FVP_PREBUILT_BL31(self): |
| 271 | pass |
| 272 | |
| 273 | @property |
| 274 | @abstractmethod |
| 275 | def KERNEL_ADDRESS(self): |
| 276 | pass |
| 277 | |
| 278 | def create_dt(self, run_name : str): |
| 279 | """Create DT related files, and return respective paths in a tuple |
| 280 | (dts,dtb)""" |
| 281 | return self.args.artifacts.create_file(run_name, ".dts"), \ |
| 282 | self.args.artifacts.create_file(run_name, ".dtb") |
| 283 | |
| 284 | def compile_dt(self, run_state, dts_path, dtb_path): |
| 285 | """Compile DT calling dtc.""" |
| 286 | dtc_args = [ |
| 287 | DTC_SCRIPT, "compile", "-i", dts_path, "-o", dtb_path, |
| 288 | ] |
| 289 | self.exec_logged(run_state, dtc_args) |
| 290 | |
| 291 | def create_uart_log(self, run_name : str, file_name : str): |
| 292 | """Create uart log file, and return path""" |
| 293 | return self.args.artifacts.create_file(run_name, file_name) |
| 294 | |
| 295 | def get_img_and_ldadd(self, partitions : dict): |
| 296 | ret = [] |
| 297 | for i, p in enumerate(partitions): |
| 298 | with open(p["dts"], "r") as dts: |
| 299 | manifest = fdt.parse_dts(dts.read()) |
| 300 | load_address = manifest.get_property("load_address", |
| 301 | f"/hypervisor/vm{str(i+1)}").value |
| 302 | ret.append((p["img"], load_address)) |
| 303 | return ret |
| 304 | |
| 305 | def get_manifests_from_json(self, partitions : list): |
| 306 | manifests = "" |
| 307 | if partitions is not None: |
| 308 | for p in partitions: |
| 309 | manifests += read_file(p["dts"]) |
| 310 | return manifests |
| 311 | |
| 312 | @abstractmethod |
| 313 | def gen_dts(self, dts_path, test_args): |
| 314 | """Abstract method to generate dts file. This specific to the use case |
| 315 | so should be implemented within derived driver""" |
| 316 | pass |
| 317 | |
| 318 | @abstractmethod |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 319 | def gen_fvp_args( |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 320 | self, is_long_running, uart0_log_path, uart1_log_path, dtb_path): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 321 | """Generate command line arguments for FVP.""" |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 322 | time_limit = "80s" if is_long_running else "40s" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 323 | fvp_args = [ |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 324 | "timeout", "--foreground", time_limit, |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 325 | FVP_BINARY, |
J-Alves | 10446d8 | 2021-04-26 11:52:57 +0100 | [diff] [blame] | 326 | "-C", "pci.pci_smmuv3.mmu.SMMU_AIDR=2", |
| 327 | "-C", "pci.pci_smmuv3.mmu.SMMU_IDR0=0x0046123B", |
| 328 | "-C", "pci.pci_smmuv3.mmu.SMMU_IDR1=0x00600002", |
| 329 | "-C", "pci.pci_smmuv3.mmu.SMMU_IDR3=0x1714", |
| 330 | "-C", "pci.pci_smmuv3.mmu.SMMU_IDR5=0xFFFF0472", |
| 331 | "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR1=0xA0000002", |
| 332 | "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR2=0", |
| 333 | "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR3=0", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 334 | "-C", "pctl.startup=0.0.0.0", |
| 335 | "-C", "bp.secure_memory=0", |
| 336 | "-C", "cluster0.NUM_CORES=4", |
| 337 | "-C", "cluster1.NUM_CORES=4", |
| 338 | "-C", "cache_state_modelled=0", |
| 339 | "-C", "bp.vis.disable_visualisation=true", |
| 340 | "-C", "bp.vis.rate_limit-enable=false", |
| 341 | "-C", "bp.terminal_0.start_telnet=false", |
| 342 | "-C", "bp.terminal_1.start_telnet=false", |
| 343 | "-C", "bp.terminal_2.start_telnet=false", |
| 344 | "-C", "bp.terminal_3.start_telnet=false", |
| 345 | "-C", "bp.pl011_uart0.untimed_fifos=1", |
| 346 | "-C", "bp.pl011_uart0.unbuffered_output=1", |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 347 | "-C", f"bp.pl011_uart0.out_file={uart0_log_path}", |
| 348 | "-C", f"bp.pl011_uart1.out_file={uart1_log_path}", |
| 349 | "-C", f"cluster0.cpu0.RVBAR={self.CPU_START_ADDRESS}", |
| 350 | "-C", f"cluster0.cpu1.RVBAR={self.CPU_START_ADDRESS}", |
| 351 | "-C", f"cluster0.cpu2.RVBAR={self.CPU_START_ADDRESS}", |
| 352 | "-C", f"cluster0.cpu3.RVBAR={self.CPU_START_ADDRESS}", |
| 353 | "-C", f"cluster1.cpu0.RVBAR={self.CPU_START_ADDRESS}", |
| 354 | "-C", f"cluster1.cpu1.RVBAR={self.CPU_START_ADDRESS}", |
| 355 | "-C", f"cluster1.cpu2.RVBAR={self.CPU_START_ADDRESS}", |
| 356 | "-C", f"cluster1.cpu3.RVBAR={self.CPU_START_ADDRESS}", |
| 357 | "--data", |
| 358 | f"cluster0.cpu0={self.FVP_PREBUILT_BL31}@{self.CPU_START_ADDRESS}", |
| 359 | "--data", f"cluster0.cpu0={dtb_path}@{self.DTB_ADDRESS}", |
| 360 | "--data", f"cluster0.cpu0={self.args.kernel}@{self.KERNEL_ADDRESS}", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 361 | "-C", "bp.ve_sysregs.mmbSiteDefault=0", |
| 362 | "-C", "bp.ve_sysregs.exit_on_shutdown=1", |
| 363 | ] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 364 | return fvp_args |
| 365 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 366 | def run(self, run_name, test_args, is_long_running): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 367 | """ Run test """ |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 368 | run_state = self.start_run(run_name) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 369 | dts_path, dtb_path = self.create_dt(run_name) |
| 370 | uart0_log_path = self.create_uart_log(run_name, ".uart0.log") |
| 371 | uart1_log_path = self.create_uart_log(run_name, ".uart1.log") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 372 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 373 | try: |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 374 | self.gen_dts(dts_path, test_args) |
| 375 | self.compile_dt(run_state, dts_path, dtb_path) |
| 376 | fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path, |
| 377 | uart1_log_path, dtb_path) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 378 | self.exec_logged(run_state, fvp_args) |
| 379 | except DriverRunException: |
| 380 | pass |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 381 | |
| 382 | # Append UART0 output to main log. |
| 383 | append_file(run_state.log_path, read_file(uart0_log_path)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 384 | return self.finish_run(run_state) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 385 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 386 | def finish(self): |
| 387 | """Clean up after running tests.""" |
| 388 | pass |
| 389 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 390 | class FvpDriverHypervisor(FvpDriver): |
| 391 | """ |
| 392 | Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor |
| 393 | """ |
| 394 | INITRD_START= 0x84000000 |
| 395 | INITRD_END = 0x85000000 #Default value, however may change if initrd in args |
| 396 | |
| 397 | def __init__(self, args): |
| 398 | self.vms_in_partitions_json = args.partitions and args.partitions["VMs"] |
| 399 | super().__init__(args) |
| 400 | |
| 401 | @property |
| 402 | def CPU_START_ADDRESS(self): |
| 403 | return "0x04020000" |
| 404 | |
| 405 | @property |
| 406 | def DTB_ADDRESS(self): |
| 407 | return "0x82000000" |
| 408 | |
| 409 | @property |
| 410 | def FVP_PREBUILT_BL31(self): |
| 411 | return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin") |
| 412 | |
| 413 | @property |
| 414 | def KERNEL_ADDRESS(self): |
| 415 | return "0x80000000" |
| 416 | |
| 417 | def gen_dts(self, dts_path, test_args): |
| 418 | """Create a DeviceTree source which will be compiled into a DTB and |
| 419 | passed to FVP for a test run.""" |
| 420 | |
| 421 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 422 | write_file(dts_path, read_file(FVP_PREBUILT_DTS)) |
| 423 | |
| 424 | # Write the vm arguments to the partition manifest |
| 425 | to_append = f""" |
| 426 | / {{ |
| 427 | chosen {{ |
| 428 | bootargs = "{vm_args}"; |
| 429 | stdout-path = "serial0:115200n8"; |
| 430 | linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>; |
| 431 | linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>; |
| 432 | }}; |
| 433 | }};""" |
| 434 | if self.vms_in_partitions_json: |
| 435 | to_append += self.get_manifests_from_json(self.args.partitions["VMs"]) |
| 436 | |
| 437 | append_file(dts_path, to_append) |
| 438 | |
| 439 | def gen_fvp_args( |
| 440 | self, is_long_running, uart0_log_path, uart1_log_path, dtb_path): |
| 441 | """Generate command line arguments for FVP.""" |
| 442 | |
| 443 | fvp_args = super().gen_fvp_args( |
| 444 | is_long_running, uart0_log_path, uart1_log_path, dtb_path) |
| 445 | |
| 446 | if self.vms_in_partitions_json: |
| 447 | img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"]) |
| 448 | for img, ldadd in img_ldadd: |
| 449 | fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"] |
| 450 | |
| 451 | if self.args.initrd: |
| 452 | fvp_args += [ |
| 453 | "--data", |
| 454 | f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}" |
| 455 | ] |
| 456 | return fvp_args |
| 457 | |
| 458 | class FvpDriverSPMC(FvpDriver): |
| 459 | """ |
| 460 | Driver which runs tests in Arm FVP emulator, with hafnium as SPMC |
| 461 | """ |
| 462 | FVP_PREBUILT_SECURE_DTS = os.path.join( |
| 463 | HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts") |
| 464 | HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds") |
| 465 | |
| 466 | def __init__(self, args): |
| 467 | if args.partitions is None or args.partitions["SPs"] is None: |
J-Alves | 10446d8 | 2021-04-26 11:52:57 +0100 | [diff] [blame] | 468 | raise Exception("Need to provide SPs in partitions_json") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 469 | super().__init__(args) |
| 470 | |
| 471 | @property |
| 472 | def CPU_START_ADDRESS(self): |
| 473 | return "0x04010000" |
| 474 | |
| 475 | @property |
| 476 | def DTB_ADDRESS(self): |
| 477 | return "0x0403f000" |
| 478 | |
| 479 | @property |
| 480 | def FVP_PREBUILT_BL31(self): |
| 481 | return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin") |
| 482 | |
| 483 | @property |
| 484 | def KERNEL_ADDRESS(self): |
| 485 | return "0x6000000" |
| 486 | |
| 487 | def gen_dts(self, dts_path, test_args): |
| 488 | """Create a DeviceTree source which will be compiled into a DTB and |
| 489 | passed to FVP for a test run.""" |
| 490 | to_append = self.get_manifests_from_json(self.args.partitions["SPs"]) |
| 491 | write_file(dts_path, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS)) |
| 492 | append_file(dts_path, to_append) |
| 493 | |
| 494 | def gen_fvp_args( |
| 495 | self, is_long_running, uart0_log_path, uart1_log_path, dtb_path): |
| 496 | """Generate command line arguments for FVP.""" |
| 497 | fvp_args = super().gen_fvp_args( |
| 498 | is_long_running, uart0_log_path, uart1_log_path, dtb_path) |
| 499 | fvp_args += [ |
| 500 | "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}", |
| 501 | "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"", |
J-Alves | b5a6399 | 2021-04-30 10:23:19 +0100 | [diff] [blame^] | 502 | "-C", "cluster0.has_arm_v8-5=1", |
| 503 | "-C", "cluster1.has_arm_v8-5=1", |
| 504 | "-C", "cluster0.has_branch_target_exception=1", |
| 505 | "-C", "cluster1.has_branch_target_exception=1", |
| 506 | "-C", "cluster0.restriction_on_speculative_execution=2", |
| 507 | "-C", "cluster1.restriction_on_speculative_execution=2", |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 508 | ] |
| 509 | img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"]) |
| 510 | for img, ldadd in img_ldadd: |
| 511 | fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"] |
| 512 | |
| 513 | return fvp_args |
| 514 | |
| 515 | def run(self, run_name, test_args, is_long_running): |
| 516 | with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f: |
| 517 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 518 | f.write(f"{vm_args}\n") |
| 519 | return super().run(run_name, test_args, is_long_running) |
| 520 | |
| 521 | def finish(self): |
| 522 | """Clean up after running tests.""" |
| 523 | os.remove(FvpDriverSPMC.HFTEST_CMD_FILE) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 524 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 525 | class SerialDriver(Driver): |
| 526 | """Driver which communicates with a device over the serial port.""" |
| 527 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 528 | def __init__(self, args, tty_file, baudrate, init_wait): |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 529 | Driver.__init__(self, args) |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 530 | self.tty_file = tty_file |
| 531 | self.baudrate = baudrate |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 532 | self.pyserial = importlib.import_module("serial") |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 533 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 534 | if init_wait: |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 535 | input("Press ENTER and then reset the device...") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 536 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 537 | def connect(self): |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 538 | return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10) |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 539 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 540 | def run(self, run_name, test_args, is_long_running): |
| 541 | """Communicate `test_args` to the device over the serial port.""" |
| 542 | run_state = self.start_run(run_name) |
| 543 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 544 | with self.connect() as ser: |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 545 | with open(run_state.log_path, "a") as f: |
| 546 | while True: |
| 547 | # Read one line from the serial port. |
| 548 | line = ser.readline().decode('utf-8') |
| 549 | if len(line) == 0: |
| 550 | # Timeout |
| 551 | run_state.set_ret_code(124) |
| 552 | input("Timeout. " + |
| 553 | "Press ENTER and then reset the device...") |
| 554 | break |
| 555 | # Write the line to the log file. |
| 556 | f.write(line) |
| 557 | if HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 558 | # Device is waiting for `test_args`. |
| 559 | ser.write(test_args.encode('ascii')) |
| 560 | ser.write(b'\r') |
| 561 | elif HFTEST_CTRL_FINISHED in line: |
| 562 | # Device has finished running this test and will reboot. |
| 563 | break |
| 564 | return self.finish_run(run_state) |
| 565 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 566 | def finish(self): |
| 567 | """Clean up after running tests.""" |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 568 | with self.connect() as ser: |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 569 | while True: |
| 570 | line = ser.readline().decode('utf-8') |
| 571 | if len(line) == 0: |
| 572 | input("Timeout. Press ENTER and then reset the device...") |
| 573 | elif HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 574 | # Device is waiting for a command. Instruct it to exit |
| 575 | # the test environment. |
| 576 | ser.write("exit".encode('ascii')) |
| 577 | ser.write(b'\r') |
| 578 | break |
| 579 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 580 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 581 | # Tuple used to return information about the results of running a set of tests. |
| 582 | TestRunnerResult = collections.namedtuple("TestRunnerResult", [ |
| 583 | "tests_run", |
| 584 | "tests_failed", |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 585 | "tests_skipped", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 586 | ]) |
| 587 | |
| 588 | |
| 589 | class TestRunner: |
| 590 | """Class which communicates with a test platform to obtain a list of |
| 591 | available tests and driving their execution.""" |
| 592 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 593 | def __init__(self, artifacts, driver, image_name, suite_regex, test_regex, |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 594 | skip_long_running_tests, force_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 595 | self.artifacts = artifacts |
| 596 | self.driver = driver |
| 597 | self.image_name = image_name |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 598 | self.skip_long_running_tests = skip_long_running_tests |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 599 | self.force_long_running = force_long_running |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 600 | |
| 601 | self.suite_re = re.compile(suite_regex or ".*") |
| 602 | self.test_re = re.compile(test_regex or ".*") |
| 603 | |
| 604 | def extract_hftest_lines(self, raw): |
| 605 | """Extract hftest-specific lines from a raw output from an invocation |
| 606 | of the test platform.""" |
| 607 | lines = [] |
| 608 | for line in raw.splitlines(): |
J-Alves | 3dbb856 | 2020-12-01 10:45:37 +0000 | [diff] [blame] | 609 | match = re.search(f"^VM \d+: ", line) |
| 610 | if match is not None: |
| 611 | line = line[match.end():] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 612 | if line.startswith(HFTEST_LOG_PREFIX): |
| 613 | lines.append(line[len(HFTEST_LOG_PREFIX):]) |
| 614 | return lines |
| 615 | |
| 616 | def get_test_json(self): |
| 617 | """Invoke the test platform and request a JSON of available test and |
| 618 | test suites.""" |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 619 | out = self.driver.run("json", "json", self.force_long_running) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 620 | hf_out = "\n".join(self.extract_hftest_lines(out)) |
| 621 | try: |
| 622 | return json.loads(hf_out) |
| 623 | except ValueError as e: |
| 624 | print(out) |
| 625 | raise e |
| 626 | |
| 627 | def collect_results(self, fn, it, xml_node): |
| 628 | """Run `fn` on every entry in `it` and collect their TestRunnerResults. |
| 629 | Insert "tests" and "failures" nodes to `xml_node`.""" |
| 630 | tests_run = 0 |
| 631 | tests_failed = 0 |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 632 | tests_skipped = 0 |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 633 | start_time = time.perf_counter() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 634 | for i in it: |
| 635 | sub_result = fn(i) |
| 636 | assert(sub_result.tests_run >= sub_result.tests_failed) |
| 637 | tests_run += sub_result.tests_run |
| 638 | tests_failed += sub_result.tests_failed |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 639 | tests_skipped += sub_result.tests_skipped |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 640 | elapsed_time = time.perf_counter() - start_time |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 641 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 642 | xml_node.set("tests", str(tests_run + tests_skipped)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 643 | xml_node.set("failures", str(tests_failed)) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 644 | xml_node.set("skipped", str(tests_skipped)) |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 645 | xml_node.set("time", str(elapsed_time)) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 646 | return TestRunnerResult(tests_run, tests_failed, tests_skipped) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 647 | |
| 648 | def is_passed_test(self, test_out): |
| 649 | """Parse the output of a test and return True if it passed.""" |
| 650 | return \ |
| 651 | len(test_out) > 0 and \ |
| 652 | test_out[-1] == HFTEST_LOG_FINISHED and \ |
| 653 | not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out) |
| 654 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 655 | def get_failure_message(self, test_out): |
| 656 | """Parse the output of a test and return the message of the first |
| 657 | assertion failure.""" |
| 658 | for i, line in enumerate(test_out): |
| 659 | if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out): |
| 660 | # The assertion message is on the line after the 'Failure:' |
| 661 | return test_out[i + 1].strip() |
| 662 | |
| 663 | return None |
| 664 | |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 665 | def get_log_name(self, suite, test): |
| 666 | """Returns a string with a generated log name for the test.""" |
| 667 | log_name = "" |
| 668 | |
| 669 | cpu = self.driver.args.cpu |
| 670 | if cpu: |
| 671 | log_name += cpu + "." |
| 672 | |
| 673 | log_name += suite["name"] + "." + test["name"] |
| 674 | |
| 675 | return log_name |
| 676 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 677 | def run_test(self, suite, test, suite_xml): |
| 678 | """Invoke the test platform and request to run a given `test` in given |
| 679 | `suite`. Create a new XML node with results under `suite_xml`. |
| 680 | Test only invoked if it matches the regex given to constructor.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 681 | if not self.test_re.match(test["name"]): |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 682 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 683 | |
| 684 | test_xml = ET.SubElement(suite_xml, "testcase") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 685 | test_xml.set("name", test["name"]) |
| 686 | test_xml.set("classname", suite["name"]) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 687 | |
| 688 | if self.skip_long_running_tests and test["is_long_running"]: |
| 689 | print(" SKIP", test["name"]) |
| 690 | test_xml.set("status", "notrun") |
| 691 | skipped_xml = ET.SubElement(test_xml, "skipped") |
| 692 | skipped_xml.set("message", "Long running") |
| 693 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1) |
| 694 | |
| 695 | print(" RUN", test["name"]) |
| 696 | log_name = self.get_log_name(suite, test) |
| 697 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 698 | test_xml.set("status", "run") |
| 699 | |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 700 | start_time = time.perf_counter() |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 701 | out = self.driver.run( |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 702 | log_name, "run {} {}".format(suite["name"], test["name"]), |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 703 | test["is_long_running"] or self.force_long_running) |
| 704 | hftest_out = self.extract_hftest_lines(out) |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 705 | elapsed_time = time.perf_counter() - start_time |
| 706 | |
| 707 | test_xml.set("time", str(elapsed_time)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 708 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 709 | system_out_xml = ET.SubElement(test_xml, "system-out") |
| 710 | system_out_xml.text = out |
| 711 | |
| 712 | if self.is_passed_test(hftest_out): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 713 | print(" PASS") |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 714 | return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 715 | else: |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 716 | print("[x] FAIL --", self.driver.get_run_log(log_name)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 717 | failure_xml = ET.SubElement(test_xml, "failure") |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 718 | failure_message = self.get_failure_message(hftest_out) or "Test failed" |
| 719 | failure_xml.set("message", failure_message) |
| 720 | failure_xml.text = '\n'.join(hftest_out) |
| 721 | return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 722 | |
| 723 | def run_suite(self, suite, xml): |
| 724 | """Invoke the test platform and request to run all matching tests in |
| 725 | `suite`. Create new XML nodes with results under `xml`. |
| 726 | Suite skipped if it does not match the regex given to constructor.""" |
| 727 | if not self.suite_re.match(suite["name"]): |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 728 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 729 | |
| 730 | print(" SUITE", suite["name"]) |
| 731 | suite_xml = ET.SubElement(xml, "testsuite") |
| 732 | suite_xml.set("name", suite["name"]) |
Andrew Walbran | 16ae62e | 2020-06-05 18:27:46 +0100 | [diff] [blame] | 733 | properties_xml = ET.SubElement(suite_xml, "properties") |
| 734 | |
| 735 | property_xml = ET.SubElement(properties_xml, "property") |
| 736 | property_xml.set("name", "driver") |
| 737 | property_xml.set("value", type(self.driver).__name__) |
| 738 | |
| 739 | if self.driver.args.cpu: |
| 740 | property_xml = ET.SubElement(properties_xml, "property") |
| 741 | property_xml.set("name", "cpu") |
| 742 | property_xml.set("value", self.driver.args.cpu) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 743 | |
| 744 | return self.collect_results( |
| 745 | lambda test: self.run_test(suite, test, suite_xml), |
| 746 | suite["tests"], |
| 747 | suite_xml) |
| 748 | |
| 749 | def run_tests(self): |
| 750 | """Run all suites and tests matching regexes given to constructor. |
| 751 | Write results to sponge log XML. Return the number of run and failed |
| 752 | tests.""" |
| 753 | |
| 754 | test_spec = self.get_test_json() |
| 755 | timestamp = datetime.datetime.now().replace(microsecond=0).isoformat() |
| 756 | |
| 757 | xml = ET.Element("testsuites") |
| 758 | xml.set("name", self.image_name) |
| 759 | xml.set("timestamp", timestamp) |
| 760 | |
| 761 | result = self.collect_results( |
| 762 | lambda suite: self.run_suite(suite, xml), |
| 763 | test_spec["suites"], |
| 764 | xml) |
| 765 | |
| 766 | # Write XML to file. |
David Brazdil | ee5e25d | 2020-01-24 14:17:45 +0000 | [diff] [blame] | 767 | ET.ElementTree(xml).write(self.artifacts.sponge_xml_path, |
| 768 | encoding='utf-8', xml_declaration=True) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 769 | |
| 770 | if result.tests_failed > 0: |
| 771 | print("[x] FAIL:", result.tests_failed, "of", result.tests_run, |
| 772 | "tests failed") |
| 773 | elif result.tests_run > 0: |
| 774 | print(" PASS: all", result.tests_run, "tests passed") |
| 775 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 776 | # Let the driver clean up. |
| 777 | self.driver.finish() |
| 778 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 779 | return result |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 780 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 781 | def Main(): |
| 782 | parser = argparse.ArgumentParser() |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 783 | parser.add_argument("image") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 784 | parser.add_argument("--out", required=True) |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 785 | parser.add_argument("--log", required=True) |
Andrew Walbran | 7559fcf | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 786 | parser.add_argument("--out_initrd") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 787 | parser.add_argument("--out_partitions") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 788 | parser.add_argument("--initrd") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 789 | parser.add_argument("--partitions_json") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 790 | parser.add_argument("--suite") |
| 791 | parser.add_argument("--test") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 792 | parser.add_argument("--vm_args") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 793 | parser.add_argument("--driver", default="qemu") |
| 794 | parser.add_argument("--serial-dev", default="/dev/ttyUSB0") |
| 795 | parser.add_argument("--serial-baudrate", type=int, default=115200) |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 796 | parser.add_argument("--serial-no-init-wait", action="store_true") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 797 | parser.add_argument("--skip-long-running-tests", action="store_true") |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 798 | parser.add_argument("--force-long-running", action="store_true") |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 799 | parser.add_argument("--cpu", |
| 800 | help="Selects the CPU configuration for the run environment.") |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 801 | parser.add_argument("--tfa", action="store_true") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 802 | parser.add_argument("--secure", action="store_true") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 803 | args = parser.parse_args() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 804 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 805 | # Resolve some paths. |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 806 | image = os.path.join(args.out, args.image + ".bin") |
| 807 | initrd = None |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 808 | image_name = args.image |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 809 | |
| 810 | if not args.secure and args.initrd: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 811 | initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd) |
| 812 | initrd = os.path.join(initrd_dir, "initrd.img") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 813 | image_name += "_" + args.initrd |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 814 | vm_args = args.vm_args or "" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 815 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 816 | partitions = None |
| 817 | if args.driver == "fvp" and args.partitions_json is not None: |
| 818 | partitions_dir = os.path.join(args.out_partitions, "obj", args.partitions_json) |
| 819 | partitions = json.load(open(partitions_dir, "r")) |
| 820 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 821 | # Create class which will manage all test artifacts. |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 822 | log_dir = os.path.join(args.log, "hafnium" if not args.secure else "spmc") |
| 823 | artifacts = ArtifactsManager(log_dir) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 824 | |
| 825 | # Create a driver for the platform we want to test on. |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 826 | driver_args = DriverArgs(artifacts, image, initrd, vm_args, args.cpu, |
| 827 | partitions) |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 828 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 829 | if args.secure: |
| 830 | if args.driver != "fvp": |
| 831 | raise Exception("Secure tests can only run with fvp driver") |
| 832 | driver = FvpDriverSPMC(driver_args) |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 833 | else: |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 834 | if args.driver == "qemu": |
| 835 | driver = QemuDriver(driver_args, args.out, args.tfa) |
| 836 | elif args.driver == "fvp": |
| 837 | driver = FvpDriverHypervisor(driver_args) |
| 838 | elif args.driver == "serial": |
| 839 | driver = SerialDriver(driver_args, args.serial_dev, |
| 840 | args.serial_baudrate, not args.serial_no_init_wait) |
| 841 | else: |
| 842 | raise Exception("Unknown driver name: {}".format(args.driver)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 843 | |
| 844 | # Create class which will drive test execution. |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 845 | runner = TestRunner(artifacts, driver, image_name, args.suite, args.test, |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 846 | args.skip_long_running_tests, args.force_long_running) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 847 | |
| 848 | # Run tests. |
| 849 | runner_result = runner.run_tests() |
| 850 | |
| 851 | # Print error message if no tests were run as this is probably unexpected. |
| 852 | # Return suitable error code. |
| 853 | if runner_result.tests_run == 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 854 | print("Error: no tests match") |
| 855 | return 10 |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 856 | elif runner_result.tests_failed > 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 857 | return 1 |
| 858 | else: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 859 | return 0 |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 860 | |
| 861 | if __name__ == "__main__": |
| 862 | sys.exit(Main()) |