blob: 3fa902e572616f9b0deba2bdeb1cce42891be0fa [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 Walbran7559fcf2019-05-09 17:11:20 +0100180 parser.add_argument("--out_initrd")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000181 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100182 parser.add_argument("--suite")
183 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000184 parser.add_argument("--vm_args")
Andrew Walbran98656252019-03-14 14:52:29 +0000185 parser.add_argument("--fvp", type=bool)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100186 args = parser.parse_args()
187 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000188 image = os.path.join(args.out, args.image + ".bin")
189 initrd = None
190 suite = args.image
191 if args.initrd:
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100192 initrd = os.path.join(args.out_initrd, "obj", args.initrd, "initrd.img")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000193 suite += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000194 vm_args = args.vm_args or ""
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000195 log = os.path.join(args.log, suite)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100196 ensure_dir(log)
197 print("Logs saved under", log)
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100198 log_file = os.path.join(log, "sponge_log.log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100199 with open(log_file, "w") as full_log:
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100200 # Query the tests in the image.
Andrew Scull845fc9b2019-04-03 12:44:26 +0100201 out = emulator(args.fvp, image, initrd, vm_args + " json",
202 os.path.join(log, "json.log"))
203 full_log.write(out)
204 full_log.write("\r\n\r\n")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100205 hftest_json = "\n".join(hftest_lines(out))
206 tests = json.loads(hftest_json)
207 # Run the selected tests.
208 tests_run = 0
209 failures = 0
210 suite_re = re.compile(args.suite or ".*")
211 test_re = re.compile(args.test or ".*")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100212 suites_xml = ET.Element("testsuites")
213 suites_xml.set("name", suite)
214 suites_xml.set(
Andrew Scull04502e42018-09-03 14:54:52 +0100215 "timestamp",
216 datetime.datetime.now().replace(microsecond=0).isoformat())
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100217 for suite in tests["suites"]:
218 if not suite_re.match(suite["name"]):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100219 continue
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100220 tests_run_from_suite = 0
221 failures_from_suite = 0
Andrew Scull845fc9b2019-04-03 12:44:26 +0100222 suite_xml = ET.SubElement(suites_xml, "testsuite")
223 suite_xml.set("name", suite["name"])
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100224 for test in suite["tests"]:
225 if not test_re.match(test):
226 continue
Andrew Scull845fc9b2019-04-03 12:44:26 +0100227 test_xml = ET.SubElement(suite_xml, "testcase")
228 test_xml.set("name", test)
229 test_xml.set("classname", suite['name'])
230 test_xml.set("status", "run")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100231 tests_run_from_suite += 1
232 if tests_run_from_suite == 1:
233 print(" SUITE", suite["name"])
234 print(" RUN", test)
235 test_log = os.path.join(log,
236 suite["name"] + "." + test + ".log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100237 out = emulator(
238 args.fvp, image, initrd,
239 vm_args + " run {} {}".format(suite["name"], test),
240 test_log)
241 full_log.write(out)
242 full_log.write("\r\n\r\n")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100243 hftest_out = hftest_lines(out)
Andrew Scull845fc9b2019-04-03 12:44:26 +0100244 if len(
245 hftest_out
246 ) > 0 and hftest_out[-1] == HFTEST_LOG_FINISHED and not any(
247 l.startswith(HFTEST_LOG_FAILURE_PREFIX)
248 for l in hftest_out):
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100249 print(" PASS")
250 else:
251 failures_from_suite += 1
Andrew Scull845fc9b2019-04-03 12:44:26 +0100252 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100253 # TODO: set a meaningful message and put log in CDATA
Andrew Scull845fc9b2019-04-03 12:44:26 +0100254 failure_xml.set("message", "Test failed")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100255 print("[x] FAIL --", test_log)
256 tests_run += tests_run_from_suite
257 failures += failures_from_suite
Andrew Scull845fc9b2019-04-03 12:44:26 +0100258 suite_xml.set("tests", str(tests_run_from_suite))
259 suite_xml.set("failures", str(failures_from_suite))
260 suites_xml.set("tests", str(tests_run))
261 suites_xml.set("failures", str(failures))
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100262 with open(os.path.join(log, "sponge_log.xml"), "w") as f:
Andrew Scull845fc9b2019-04-03 12:44:26 +0100263 ET.ElementTree(suites_xml).write(
264 f, encoding='utf-8', xml_declaration=True)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100265 # If none were run, this is probably a mistake.
266 if tests_run == 0:
267 print("Error: no tests match")
268 return 10
269 # Exit with 0 on success and 1 if any test failed.
270 if failures:
271 print("[x] FAIL:", failures, "of", tests_run, "tests failed")
272 return 1
273 else:
274 print(" PASS: all", tests_run, "tests passed")
275 return 0
276
277
278if __name__ == "__main__":
279 sys.exit(Main())