blob: 7834c4d8fc63abeea80ae40644e94f70a61bf09d [file] [log] [blame]
Andrew Scullbc7189d2018-08-14 09:35:13 +01001#!/usr/bin/env python
Andrew Scull18834872018-10-12 11:48:09 +01002#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
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 Scullbc7189d2018-08-14 09:35:13 +010017"""Run tests.
18
19Runs tests on QEMU.
20"""
21
22from __future__ import print_function
23
Andrew Scull3b62f2b2018-08-21 14:26:12 +010024import xml.etree.ElementTree as ET
25
Andrew Scullbc7189d2018-08-14 09:35:13 +010026import argparse
Andrew Scull04502e42018-09-03 14:54:52 +010027import datetime
Andrew Scullbc7189d2018-08-14 09:35:13 +010028import json
29import os
30import re
31import subprocess
32import sys
33
Andrew Scull845fc9b2019-04-03 12:44:26 +010034HFTEST_LOG_PREFIX = "[hftest] "
35HFTEST_LOG_FAILURE_PREFIX = "Failure:"
36HFTEST_LOG_FINISHED = "FINISHED"
37
38
39def 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 Scullbc7189d2018-08-14 09:35:13 +010049
Andrew Scull7fd4bb72018-12-08 23:40:12 +000050def qemu(image, initrd, args, log):
Andrew Scullbc7189d2018-08-14 09:35:13 +010051 qemu_args = [
Andrew Walbranbc342d42019-02-05 16:56:02 +000052 "timeout", "--foreground", "10s",
Andrew Scullf0551c82018-12-15 20:38:47 +000053 "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt,gic_version=3",
Andrew Walbranbc342d42019-02-05 16:56:02 +000054 "-cpu", "cortex-a57", "-smp", "4", "-m", "64M", "-machine", "virtualization=true",
Andrew Scull7fd4bb72018-12-08 23:40:12 +000055 "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", image,
Andrew Scullbc7189d2018-08-14 09:35:13 +010056 ]
Andrew Scull7fd4bb72018-12-08 23:40:12 +000057 if initrd:
Andrew Scullf0551c82018-12-15 20:38:47 +000058 qemu_args += ["-initrd", initrd]
Andrew Scullbc7189d2018-08-14 09:35:13 +010059 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 Scull845fc9b2019-04-03 12:44:26 +010065 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 Scullbc7189d2018-08-14 09:35:13 +010069 # Return that log for processing.
70 with open(log, "r") as f:
71 return f.read()
72
73
Andrew Walbran98656252019-03-14 14:52:29 +000074def 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 Bonnici0a125632019-04-01 13:46:52 +010083 "timeout", "--foreground", "40s",
Andrew Walbran98656252019-03-14 14:52:29 +000084 "../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 Bonnici0a125632019-04-01 13:46:52 +0100114 initrd_start = 0x84000000
115 initrd_end = 0x85000000 # Default value
Andrew Walbran98656252019-03-14 14:52:29 +0000116 if initrd:
Marc Bonnici0a125632019-04-01 13:46:52 +0100117 fvp_args += ["--data", "cluster0.cpu0={}@{}".format(initrd, hex(initrd_start))]
118 initrd_end = initrd_start + os.path.getsize(initrd)
119
Andrew Walbran98656252019-03-14 14:52:29 +0000120
121 with open(log, "w") as f:
122 f.write("$ {}\r\n".format(" ".join(dtc_args)))
123 f.flush()
Andrew Scull845fc9b2019-04-03 12:44:26 +0100124 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 Walbran98656252019-03-14 14:52:29 +0000129 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 Bonnici0a125632019-04-01 13:46:52 +0100134 dtc.stdin.write(" linux,initrd-start = <{}>;\n".format(initrd_start))
135 dtc.stdin.write(" linux,initrd-end = <{}>;\n".format(initrd_end))
Andrew Walbran98656252019-03-14 14:52:29 +0000136 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 Scull845fc9b2019-04-03 12:44:26 +0100143 returncode = subprocess.call(fvp_args, stdout=f, stderr=f)
Andrew Walbran98656252019-03-14 14:52:29 +0000144 with open(uart0_log, "r") as g:
145 f.write(g.read())
Andrew Scull845fc9b2019-04-03 12:44:26 +0100146 log_timeout_returncode(f, returncode)
Andrew Walbran98656252019-03-14 14:52:29 +0000147
148 with open(log, "r") as f:
149 return f.read()
150
151
152def 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 Scullbc7189d2018-08-14 09:35:13 +0100159def ensure_dir(path):
160 try:
161 os.makedirs(path)
162 except OSError:
163 if not os.path.isdir(path):
164 raise
165
166
167def hftest_lines(raw):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100168 return [
Andrew Scull845fc9b2019-04-03 12:44:26 +0100169 line[len(HFTEST_LOG_PREFIX):]
Andrew Scullbc7189d2018-08-14 09:35:13 +0100170 for line in raw.splitlines()
Andrew Scull845fc9b2019-04-03 12:44:26 +0100171 if line.startswith(HFTEST_LOG_PREFIX)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100172 ]
173
174
175def Main():
176 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000177 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100178 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100179 parser.add_argument("--log", required=True)
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000180 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100181 parser.add_argument("--suite")
182 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000183 parser.add_argument("--vm_args")
Andrew Walbran98656252019-03-14 14:52:29 +0000184 parser.add_argument("--fvp", type=bool)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100185 args = parser.parse_args()
186 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000187 image = os.path.join(args.out, args.image + ".bin")
188 initrd = None
189 suite = args.image
190 if args.initrd:
Andrew Walbran377bd8b2019-02-04 17:51:04 +0000191 initrd = os.path.join(args.out, "obj", args.initrd, "initrd.img")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000192 suite += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000193 vm_args = args.vm_args or ""
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000194 log = os.path.join(args.log, suite)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100195 ensure_dir(log)
196 print("Logs saved under", log)
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100197 log_file = os.path.join(log, "sponge_log.log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100198 with open(log_file, "w") as full_log:
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100199 # Query the tests in the image.
Andrew Scull845fc9b2019-04-03 12:44:26 +0100200 out = emulator(args.fvp, image, initrd, vm_args + " json",
201 os.path.join(log, "json.log"))
202 full_log.write(out)
203 full_log.write("\r\n\r\n")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100204 hftest_json = "\n".join(hftest_lines(out))
205 tests = json.loads(hftest_json)
206 # Run the selected tests.
207 tests_run = 0
208 failures = 0
209 suite_re = re.compile(args.suite or ".*")
210 test_re = re.compile(args.test or ".*")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100211 suites_xml = ET.Element("testsuites")
212 suites_xml.set("name", suite)
213 suites_xml.set(
Andrew Scull04502e42018-09-03 14:54:52 +0100214 "timestamp",
215 datetime.datetime.now().replace(microsecond=0).isoformat())
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100216 for suite in tests["suites"]:
217 if not suite_re.match(suite["name"]):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100218 continue
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100219 tests_run_from_suite = 0
220 failures_from_suite = 0
Andrew Scull845fc9b2019-04-03 12:44:26 +0100221 suite_xml = ET.SubElement(suites_xml, "testsuite")
222 suite_xml.set("name", suite["name"])
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100223 for test in suite["tests"]:
224 if not test_re.match(test):
225 continue
Andrew Scull845fc9b2019-04-03 12:44:26 +0100226 test_xml = ET.SubElement(suite_xml, "testcase")
227 test_xml.set("name", test)
228 test_xml.set("classname", suite['name'])
229 test_xml.set("status", "run")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100230 tests_run_from_suite += 1
231 if tests_run_from_suite == 1:
232 print(" SUITE", suite["name"])
233 print(" RUN", test)
234 test_log = os.path.join(log,
235 suite["name"] + "." + test + ".log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100236 out = emulator(
237 args.fvp, image, initrd,
238 vm_args + " run {} {}".format(suite["name"], test),
239 test_log)
240 full_log.write(out)
241 full_log.write("\r\n\r\n")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100242 hftest_out = hftest_lines(out)
Andrew Scull845fc9b2019-04-03 12:44:26 +0100243 if len(
244 hftest_out
245 ) > 0 and hftest_out[-1] == HFTEST_LOG_FINISHED and not any(
246 l.startswith(HFTEST_LOG_FAILURE_PREFIX)
247 for l in hftest_out):
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100248 print(" PASS")
249 else:
250 failures_from_suite += 1
Andrew Scull845fc9b2019-04-03 12:44:26 +0100251 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100252 # TODO: set a meaningful message and put log in CDATA
Andrew Scull845fc9b2019-04-03 12:44:26 +0100253 failure_xml.set("message", "Test failed")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100254 print("[x] FAIL --", test_log)
255 tests_run += tests_run_from_suite
256 failures += failures_from_suite
Andrew Scull845fc9b2019-04-03 12:44:26 +0100257 suite_xml.set("tests", str(tests_run_from_suite))
258 suite_xml.set("failures", str(failures_from_suite))
259 suites_xml.set("tests", str(tests_run))
260 suites_xml.set("failures", str(failures))
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100261 with open(os.path.join(log, "sponge_log.xml"), "w") as f:
Andrew Scull845fc9b2019-04-03 12:44:26 +0100262 ET.ElementTree(suites_xml).write(
263 f, encoding='utf-8', xml_declaration=True)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100264 # If none were run, this is probably a mistake.
265 if tests_run == 0:
266 print("Error: no tests match")
267 return 10
268 # Exit with 0 on success and 1 if any test failed.
269 if failures:
270 print("[x] FAIL:", failures, "of", tests_run, "tests failed")
271 return 1
272 else:
273 print(" PASS: all", tests_run, "tests passed")
274 return 0
275
276
277if __name__ == "__main__":
278 sys.exit(Main())