blob: 8f6dc6af53c7c90198d0ddf5b6e53b84fbc961b0 [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 Walbran58954f92019-05-09 17:11:20 +0100168 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 Scullbc7189d2018-08-14 09:35:13 +0100175
176
177def Main():
178 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000179 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100180 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100181 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100182 parser.add_argument("--out_initrd")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000183 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100184 parser.add_argument("--suite")
185 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000186 parser.add_argument("--vm_args")
Andrew Walbran98656252019-03-14 14:52:29 +0000187 parser.add_argument("--fvp", type=bool)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100188 args = parser.parse_args()
189 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000190 image = os.path.join(args.out, args.image + ".bin")
191 initrd = None
192 suite = args.image
193 if args.initrd:
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100194 initrd = os.path.join(args.out_initrd, "obj", args.initrd, "initrd.img")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000195 suite += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000196 vm_args = args.vm_args or ""
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000197 log = os.path.join(args.log, suite)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100198 ensure_dir(log)
199 print("Logs saved under", log)
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100200 log_file = os.path.join(log, "sponge_log.log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100201 with open(log_file, "w") as full_log:
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100202 # Query the tests in the image.
Andrew Scull845fc9b2019-04-03 12:44:26 +0100203 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 Scull3b62f2b2018-08-21 14:26:12 +0100207 hftest_json = "\n".join(hftest_lines(out))
Fuad Tabbaa1b550c2019-08-02 14:14:30 +0100208 try:
209 tests = json.loads(hftest_json)
210 except ValueError:
211 print(hftest_json)
212 return 2
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100213 # 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 Scull845fc9b2019-04-03 12:44:26 +0100218 suites_xml = ET.Element("testsuites")
219 suites_xml.set("name", suite)
220 suites_xml.set(
Andrew Scull04502e42018-09-03 14:54:52 +0100221 "timestamp",
222 datetime.datetime.now().replace(microsecond=0).isoformat())
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100223 for suite in tests["suites"]:
224 if not suite_re.match(suite["name"]):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100225 continue
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100226 tests_run_from_suite = 0
227 failures_from_suite = 0
Andrew Scull845fc9b2019-04-03 12:44:26 +0100228 suite_xml = ET.SubElement(suites_xml, "testsuite")
229 suite_xml.set("name", suite["name"])
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100230 for test in suite["tests"]:
231 if not test_re.match(test):
232 continue
Andrew Scull845fc9b2019-04-03 12:44:26 +0100233 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 Scull3b62f2b2018-08-21 14:26:12 +0100237 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 Scull845fc9b2019-04-03 12:44:26 +0100243 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 Scull3b62f2b2018-08-21 14:26:12 +0100249 hftest_out = hftest_lines(out)
Andrew Scull845fc9b2019-04-03 12:44:26 +0100250 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 Scull3b62f2b2018-08-21 14:26:12 +0100255 print(" PASS")
256 else:
257 failures_from_suite += 1
Andrew Scull845fc9b2019-04-03 12:44:26 +0100258 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100259 # TODO: set a meaningful message and put log in CDATA
Andrew Scull845fc9b2019-04-03 12:44:26 +0100260 failure_xml.set("message", "Test failed")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100261 print("[x] FAIL --", test_log)
262 tests_run += tests_run_from_suite
263 failures += failures_from_suite
Andrew Scull845fc9b2019-04-03 12:44:26 +0100264 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 Scull3b62f2b2018-08-21 14:26:12 +0100268 with open(os.path.join(log, "sponge_log.xml"), "w") as f:
Andrew Scull845fc9b2019-04-03 12:44:26 +0100269 ET.ElementTree(suites_xml).write(
270 f, encoding='utf-8', xml_declaration=True)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100271 # 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
284if __name__ == "__main__":
285 sys.exit(Main())