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 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 17 | """Run tests. |
| 18 | |
| 19 | Runs tests on QEMU. |
| 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 24 | import xml.etree.ElementTree as ET |
| 25 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 26 | import argparse |
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 | |
| 38 | |
| 39 | def log_timeout_returncode(f, returncode): |
| 40 | if returncode == 0: |
| 41 | return |
| 42 | elif returncode == 124: |
| 43 | f.write("\r\n{}{} timed out\r\n".format(HFTEST_LOG_PREFIX, |
| 44 | HFTEST_LOG_FAILURE_PREFIX)) |
| 45 | else: |
| 46 | f.write("\r\n{}{} process return code {}\r\n".format( |
| 47 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX, returncode)) |
| 48 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 49 | |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 50 | def qemu(image, initrd, args, log): |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 51 | qemu_args = [ |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 52 | "timeout", "--foreground", "10s", |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 53 | "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt,gic_version=3", |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 54 | "-cpu", "cortex-a57", "-smp", "4", "-m", "64M", "-machine", "virtualization=true", |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 55 | "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", image, |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 56 | ] |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 57 | if initrd: |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 58 | qemu_args += ["-initrd", initrd] |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 59 | if args: |
| 60 | qemu_args += ["-append", args] |
| 61 | # Save the log to a file. |
| 62 | with open(log, "w") as f: |
| 63 | f.write("$ {}\r\n".format(" ".join(qemu_args))) |
| 64 | f.flush() |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 65 | try: |
| 66 | subprocess.check_call(qemu_args, stdout=f, stderr=f) |
| 67 | except subprocess.CalledProcessError as e: |
| 68 | log_timeout_returncode(f, e.returncode) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 69 | # Return that log for processing. |
| 70 | with open(log, "r") as f: |
| 71 | return f.read() |
| 72 | |
| 73 | |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 74 | def fvp(image, initrd, args, log): |
| 75 | uart0_log = log + ".uart0" |
| 76 | uart1_log = log + ".uart1" |
| 77 | fdt = log + ".dtb" |
| 78 | dtc_args = [ |
| 79 | "dtc", "-I", "dts", "-O", "dtb", |
| 80 | "-o", fdt, |
| 81 | ] |
| 82 | fvp_args = [ |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 83 | "timeout", "--foreground", "40s", |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 84 | "../fvp/Base_RevC_AEMv8A_pkg/models/Linux64_GCC-4.9/FVP_Base_RevC-2xAEMv8A", |
| 85 | "-C", "pctl.startup=0.0.0.0", |
| 86 | "-C", "bp.secure_memory=0", |
| 87 | "-C", "cluster0.NUM_CORES=4", |
| 88 | "-C", "cluster1.NUM_CORES=4", |
| 89 | "-C", "cache_state_modelled=0", |
| 90 | "-C", "bp.vis.disable_visualisation=true", |
| 91 | "-C", "bp.vis.rate_limit-enable=false", |
| 92 | "-C", "bp.terminal_0.start_telnet=false", |
| 93 | "-C", "bp.terminal_1.start_telnet=false", |
| 94 | "-C", "bp.terminal_2.start_telnet=false", |
| 95 | "-C", "bp.terminal_3.start_telnet=false", |
| 96 | "-C", "bp.pl011_uart0.untimed_fifos=1", |
| 97 | "-C", "bp.pl011_uart0.unbuffered_output=1", |
| 98 | "-C", "bp.pl011_uart0.out_file=" + uart0_log, |
| 99 | "-C", "bp.pl011_uart1.out_file=" + uart1_log, |
| 100 | "-C", "cluster0.cpu0.RVBAR=0x04020000", |
| 101 | "-C", "cluster0.cpu1.RVBAR=0x04020000", |
| 102 | "-C", "cluster0.cpu2.RVBAR=0x04020000", |
| 103 | "-C", "cluster0.cpu3.RVBAR=0x04020000", |
| 104 | "-C", "cluster1.cpu0.RVBAR=0x04020000", |
| 105 | "-C", "cluster1.cpu1.RVBAR=0x04020000", |
| 106 | "-C", "cluster1.cpu2.RVBAR=0x04020000", |
| 107 | "-C", "cluster1.cpu3.RVBAR=0x04020000", |
| 108 | "--data", "cluster0.cpu0=prebuilts/linux-aarch64/arm-trusted-firmware/bl31.bin@0x04020000", |
| 109 | "--data", "cluster0.cpu0=" + fdt + "@0x82000000", |
| 110 | "--data", "cluster0.cpu0=" + image + "@0x80000000", |
| 111 | "-C", "bp.ve_sysregs.mmbSiteDefault=0", |
| 112 | "-C", "bp.ve_sysregs.exit_on_shutdown=1", |
| 113 | ] |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 114 | initrd_start = 0x84000000 |
| 115 | initrd_end = 0x85000000 # Default value |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 116 | if initrd: |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 117 | fvp_args += ["--data", "cluster0.cpu0={}@{}".format(initrd, hex(initrd_start))] |
| 118 | initrd_end = initrd_start + os.path.getsize(initrd) |
| 119 | |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 120 | |
| 121 | with open(log, "w") as f: |
| 122 | f.write("$ {}\r\n".format(" ".join(dtc_args))) |
| 123 | f.flush() |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 124 | dtc = subprocess.Popen( |
| 125 | dtc_args, stdout=f, stderr=f, stdin=subprocess.PIPE) |
| 126 | with open( |
| 127 | "prebuilts/linux-aarch64/arm-trusted-firmware/fvp-base-gicv3-psci-1t.dts", |
| 128 | "r") as base_dts: |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 129 | dtc.stdin.write(base_dts.read()) |
| 130 | dtc.stdin.write("/ {\n") |
| 131 | dtc.stdin.write(" chosen {\n") |
| 132 | dtc.stdin.write(" bootargs = \"" + args + "\";\n") |
| 133 | dtc.stdin.write(" stdout-path = \"serial0:115200n8\";\n") |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 134 | dtc.stdin.write(" linux,initrd-start = <{}>;\n".format(initrd_start)) |
| 135 | dtc.stdin.write(" linux,initrd-end = <{}>;\n".format(initrd_end)) |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 136 | dtc.stdin.write(" };\n") |
| 137 | dtc.stdin.write("};\n") |
| 138 | dtc.stdin.close() |
| 139 | dtc.wait() |
| 140 | |
| 141 | f.write("$ {}\r\n".format(" ".join(fvp_args))) |
| 142 | f.flush() |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 143 | returncode = subprocess.call(fvp_args, stdout=f, stderr=f) |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 144 | with open(uart0_log, "r") as g: |
| 145 | f.write(g.read()) |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 146 | log_timeout_returncode(f, returncode) |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 147 | |
| 148 | with open(log, "r") as f: |
| 149 | return f.read() |
| 150 | |
| 151 | |
| 152 | def emulator(use_fvp, image, initrd, args, log): |
| 153 | if use_fvp: |
| 154 | return fvp(image, initrd, args, log) |
| 155 | else: |
| 156 | return qemu(image, initrd, args, log) |
| 157 | |
| 158 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 159 | def ensure_dir(path): |
| 160 | try: |
| 161 | os.makedirs(path) |
| 162 | except OSError: |
| 163 | if not os.path.isdir(path): |
| 164 | raise |
| 165 | |
| 166 | |
| 167 | def hftest_lines(raw): |
Andrew Walbran | 58954f9 | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 168 | lines = [] |
| 169 | for line in raw.splitlines(): |
| 170 | if line.startswith("VM "): |
| 171 | line = line[len("VM 0: "):] |
| 172 | if line.startswith(HFTEST_LOG_PREFIX): |
| 173 | lines.append(line[len(HFTEST_LOG_PREFIX):]) |
| 174 | return lines |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 175 | |
| 176 | |
| 177 | def Main(): |
| 178 | parser = argparse.ArgumentParser() |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 179 | parser.add_argument("image") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 180 | parser.add_argument("--out", required=True) |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 181 | parser.add_argument("--log", required=True) |
Andrew Walbran | 7559fcf | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 182 | parser.add_argument("--out_initrd") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 183 | parser.add_argument("--initrd") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 184 | parser.add_argument("--suite") |
| 185 | parser.add_argument("--test") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 186 | parser.add_argument("--vm_args") |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 187 | parser.add_argument("--fvp", type=bool) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 188 | args = parser.parse_args() |
| 189 | # Resolve some paths. |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 190 | image = os.path.join(args.out, args.image + ".bin") |
| 191 | initrd = None |
| 192 | suite = args.image |
| 193 | if args.initrd: |
Andrew Walbran | 7559fcf | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 194 | initrd = os.path.join(args.out_initrd, "obj", args.initrd, "initrd.img") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 195 | suite += "_" + args.initrd |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 196 | vm_args = args.vm_args or "" |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 197 | log = os.path.join(args.log, suite) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 198 | ensure_dir(log) |
| 199 | print("Logs saved under", log) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 200 | log_file = os.path.join(log, "sponge_log.log") |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 201 | with open(log_file, "w") as full_log: |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 202 | # Query the tests in the image. |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 203 | out = emulator(args.fvp, image, initrd, vm_args + " json", |
| 204 | os.path.join(log, "json.log")) |
| 205 | full_log.write(out) |
| 206 | full_log.write("\r\n\r\n") |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 207 | hftest_json = "\n".join(hftest_lines(out)) |
Fuad Tabba | a1b550c | 2019-08-02 14:14:30 +0100 | [diff] [blame] | 208 | try: |
| 209 | tests = json.loads(hftest_json) |
| 210 | except ValueError: |
| 211 | print(hftest_json) |
| 212 | return 2 |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 213 | # Run the selected tests. |
| 214 | tests_run = 0 |
| 215 | failures = 0 |
| 216 | suite_re = re.compile(args.suite or ".*") |
| 217 | test_re = re.compile(args.test or ".*") |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 218 | suites_xml = ET.Element("testsuites") |
| 219 | suites_xml.set("name", suite) |
| 220 | suites_xml.set( |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 221 | "timestamp", |
| 222 | datetime.datetime.now().replace(microsecond=0).isoformat()) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 223 | for suite in tests["suites"]: |
| 224 | if not suite_re.match(suite["name"]): |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 225 | continue |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 226 | tests_run_from_suite = 0 |
| 227 | failures_from_suite = 0 |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 228 | suite_xml = ET.SubElement(suites_xml, "testsuite") |
| 229 | suite_xml.set("name", suite["name"]) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 230 | for test in suite["tests"]: |
| 231 | if not test_re.match(test): |
| 232 | continue |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 233 | test_xml = ET.SubElement(suite_xml, "testcase") |
| 234 | test_xml.set("name", test) |
| 235 | test_xml.set("classname", suite['name']) |
| 236 | test_xml.set("status", "run") |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 237 | tests_run_from_suite += 1 |
| 238 | if tests_run_from_suite == 1: |
| 239 | print(" SUITE", suite["name"]) |
| 240 | print(" RUN", test) |
| 241 | test_log = os.path.join(log, |
| 242 | suite["name"] + "." + test + ".log") |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 243 | out = emulator( |
| 244 | args.fvp, image, initrd, |
| 245 | vm_args + " run {} {}".format(suite["name"], test), |
| 246 | test_log) |
| 247 | full_log.write(out) |
| 248 | full_log.write("\r\n\r\n") |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 249 | hftest_out = hftest_lines(out) |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 250 | if len( |
| 251 | hftest_out |
| 252 | ) > 0 and hftest_out[-1] == HFTEST_LOG_FINISHED and not any( |
| 253 | l.startswith(HFTEST_LOG_FAILURE_PREFIX) |
| 254 | for l in hftest_out): |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 255 | print(" PASS") |
| 256 | else: |
| 257 | failures_from_suite += 1 |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 258 | failure_xml = ET.SubElement(test_xml, "failure") |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 259 | # TODO: set a meaningful message and put log in CDATA |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 260 | failure_xml.set("message", "Test failed") |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 261 | print("[x] FAIL --", test_log) |
| 262 | tests_run += tests_run_from_suite |
| 263 | failures += failures_from_suite |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 264 | suite_xml.set("tests", str(tests_run_from_suite)) |
| 265 | suite_xml.set("failures", str(failures_from_suite)) |
| 266 | suites_xml.set("tests", str(tests_run)) |
| 267 | suites_xml.set("failures", str(failures)) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 268 | with open(os.path.join(log, "sponge_log.xml"), "w") as f: |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 269 | ET.ElementTree(suites_xml).write( |
| 270 | f, encoding='utf-8', xml_declaration=True) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 271 | # If none were run, this is probably a mistake. |
| 272 | if tests_run == 0: |
| 273 | print("Error: no tests match") |
| 274 | return 10 |
| 275 | # Exit with 0 on success and 1 if any test failed. |
| 276 | if failures: |
| 277 | print("[x] FAIL:", failures, "of", tests_run, "tests failed") |
| 278 | return 1 |
| 279 | else: |
| 280 | print(" PASS: all", tests_run, "tests passed") |
| 281 | return 0 |
| 282 | |
| 283 | |
| 284 | if __name__ == "__main__": |
| 285 | sys.exit(Main()) |