blob: f160318f0ee74db1953b6012eb21d43467a70ddf [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
David Brazdil2df24082019-09-05 11:55:08 +010017"""Script which drives invocation of tests and parsing their output to produce
18a results report.
Andrew Scullbc7189d2018-08-14 09:35:13 +010019"""
20
21from __future__ import print_function
22
Andrew Scull3b62f2b2018-08-21 14:26:12 +010023import xml.etree.ElementTree as ET
24
Andrew Scullbc7189d2018-08-14 09:35:13 +010025import argparse
David Brazdil2df24082019-09-05 11:55:08 +010026import collections
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
David Brazdil2df24082019-09-05 11:55:08 +010038HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
39 os.path.abspath(__file__))))
David Brazdil5715f042019-08-27 11:11:51 +010040DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py")
David Brazdil2df24082019-09-05 11:55:08 +010041FVP_BINARY = os.path.join(
42 os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMv8A_pkg", "models",
43 "Linux64_GCC-4.9", "FVP_Base_RevC-2xAEMv8A")
David Brazdil21204ae2019-10-30 19:22:38 +000044FVP_PREBUILTS_ROOT = os.path.join(
45 HF_ROOT, "prebuilts", "linux-aarch64", "arm-trusted-firmware", "fvp")
David Brazdil2df24082019-09-05 11:55:08 +010046FVP_PREBUILT_DTS = os.path.join(
David Brazdil21204ae2019-10-30 19:22:38 +000047 FVP_PREBUILTS_ROOT, "fvp-base-gicv3-psci-1t.dts")
48FVP_PREBUILT_BL31 = os.path.join(FVP_PREBUILTS_ROOT, "bl31.bin")
Andrew Scull845fc9b2019-04-03 12:44:26 +010049
David Brazdil2df24082019-09-05 11:55:08 +010050def read_file(path):
51 with open(path, "r") as f:
52 return f.read()
Andrew Scull845fc9b2019-04-03 12:44:26 +010053
David Brazdil2df24082019-09-05 11:55:08 +010054def write_file(path, to_write, append=False):
55 with open(path, "a" if append else "w") as f:
56 f.write(to_write)
57
58def append_file(path, to_write):
59 write_file(path, to_write, append=True)
60
61def join_if_not_None(*args):
62 return " ".join(filter(lambda x: x, args))
63
64class ArtifactsManager:
65 """Class which manages folder with test artifacts."""
66
67 def __init__(self, log_dir):
68 self.created_files = []
69 self.log_dir = log_dir
70
71 # Create directory.
Andrew Scull845fc9b2019-04-03 12:44:26 +010072 try:
David Brazdil2df24082019-09-05 11:55:08 +010073 os.makedirs(self.log_dir)
74 except OSError:
75 if not os.path.isdir(self.log_dir):
76 raise
77 print("Logs saved under", log_dir)
78
79 # Create files expected by the Sponge test result parser.
80 self.sponge_log_path = self.create_file("sponge_log", ".log")
81 self.sponge_xml_path = self.create_file("sponge_log", ".xml")
82
David Brazdil623b6812019-09-09 11:41:08 +010083 def gen_file_path(self, basename, extension):
84 """Generate path to a file in the log directory."""
85 return os.path.join(self.log_dir, basename + extension)
86
David Brazdil2df24082019-09-05 11:55:08 +010087 def create_file(self, basename, extension):
88 """Create and touch a new file in the log folder. Ensure that no other
89 file of the same name was created by this instance of ArtifactsManager.
90 """
91 # Determine the path of the file.
David Brazdil623b6812019-09-09 11:41:08 +010092 path = self.gen_file_path(basename, extension)
David Brazdil2df24082019-09-05 11:55:08 +010093
94 # Check that the path is unique.
95 assert(path not in self.created_files)
96 self.created_files += [ path ]
97
98 # Touch file.
99 with open(path, "w") as f:
100 pass
101
102 return path
Andrew Scullbc7189d2018-08-14 09:35:13 +0100103
David Brazdil623b6812019-09-09 11:41:08 +0100104 def get_file(self, basename, extension):
105 """Return path to a file in the log folder. Assert that it was created
106 by this instance of ArtifactsManager."""
107 path = self.gen_file_path(basename, extension)
108 assert(path in self.created_files)
109 return path
110
Andrew Scullbc7189d2018-08-14 09:35:13 +0100111
David Brazdil2df24082019-09-05 11:55:08 +0100112# Tuple holding the arguments common to all driver constructors.
113# This is to avoid having to pass arguments from subclasses to superclasses.
114DriverArgs = collections.namedtuple("DriverArgs", [
115 "artifacts",
116 "kernel",
117 "initrd",
David Brazdil0dbb41f2019-09-09 18:03:35 +0100118 "manifest",
David Brazdil2df24082019-09-05 11:55:08 +0100119 "vm_args",
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000120 "cpu"
David Brazdil2df24082019-09-05 11:55:08 +0100121 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100122
Andrew Walbran98656252019-03-14 14:52:29 +0000123
David Brazdil2df24082019-09-05 11:55:08 +0100124# State shared between the common Driver class and its subclasses during
125# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100126class DriverRunState:
127 def __init__(self, log_path):
128 self.log_path = log_path
129 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000130
David Brazdil7325eaf2019-09-27 13:04:51 +0100131 def set_ret_code(self, ret_code):
132 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000133
David Brazdil0dbb41f2019-09-09 18:03:35 +0100134class DriverRunException(Exception):
135 """Exception thrown if subprocess invoked by a driver returned non-zero
136 status code. Used to fast-exit from a driver command sequence."""
137 pass
138
139
David Brazdil2df24082019-09-05 11:55:08 +0100140class Driver:
141 """Parent class of drivers for all testable platforms."""
142
143 def __init__(self, args):
144 self.args = args
145
David Brazdil623b6812019-09-09 11:41:08 +0100146 def get_run_log(self, run_name):
147 """Return path to the main log of a given test run."""
148 return self.args.artifacts.get_file(run_name, ".log")
149
David Brazdil2df24082019-09-05 11:55:08 +0100150 def start_run(self, run_name):
151 """Hook called by Driver subclasses before they invoke the target
152 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100153 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100154
155 def exec_logged(self, run_state, exec_args):
156 """Run a subprocess on behalf of a Driver subclass and append its
157 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100158 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100159 with open(run_state.log_path, "a") as f:
160 f.write("$ {}\r\n".format(" ".join(exec_args)))
161 f.flush()
David Brazdil0dbb41f2019-09-09 18:03:35 +0100162 ret_code = subprocess.call(exec_args, stdout=f, stderr=f)
163 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100164 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100165 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100166
David Brazdil0dbb41f2019-09-09 18:03:35 +0100167 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100168 """Hook called by Driver subclasses after they finished running the
169 target platform. `ret_code` argument is the return code of the main
170 command run by the driver. A corresponding log message is printed."""
171 # Decode return code and add a message to the log.
172 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100173 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100174 f.write("\r\n{}{} timed out\r\n".format(
175 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100176 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100177 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100178 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
179 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100180
181 # Append log of this run to full test log.
182 log_content = read_file(run_state.log_path)
183 append_file(
184 self.args.artifacts.sponge_log_path,
185 log_content + "\r\n\r\n")
186 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000187
David Brazdil0dbb41f2019-09-09 18:03:35 +0100188 def overlay_dtb(self, run_state, base_dtb, overlay_dtb, out_dtb):
189 """Overlay `overlay_dtb` over `base_dtb` into `out_dtb`."""
190 dtc_args = [
191 DTC_SCRIPT, "overlay",
192 out_dtb, base_dtb, overlay_dtb,
193 ]
194 self.exec_logged(run_state, dtc_args)
195
Andrew Walbran98656252019-03-14 14:52:29 +0000196
David Brazdil2df24082019-09-05 11:55:08 +0100197class QemuDriver(Driver):
198 """Driver which runs tests in QEMU."""
199
200 def __init__(self, args):
201 Driver.__init__(self, args)
202
David Brazdil3cc24aa2019-09-27 10:24:41 +0100203 def gen_exec_args(self, test_args, is_long_running, dtb_path=None,
204 dumpdtb_path=None):
David Brazdil2df24082019-09-05 11:55:08 +0100205 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100206 time_limit = "120s" if is_long_running else "10s"
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000207 # If no CPU configuration is selected, then test against the maximum
208 # configuration, "max", supported by QEMU.
209 cpu = self.args.cpu or "max"
David Brazdil2df24082019-09-05 11:55:08 +0100210 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100211 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100212 "./prebuilts/linux-x64/qemu/qemu-system-aarch64",
Andrew Scull2925e422019-10-04 13:29:53 +0100213 "-machine", "virt,virtualization=on,gic_version=3",
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000214 "-cpu", cpu, "-smp", "4", "-m", "64M",
David Brazdil2df24082019-09-05 11:55:08 +0100215 "-nographic", "-nodefaults", "-serial", "stdio",
Andrew Scull2925e422019-10-04 13:29:53 +0100216 "-d", "unimp", "-kernel", self.args.kernel,
David Brazdil2df24082019-09-05 11:55:08 +0100217 ]
218
David Brazdil0dbb41f2019-09-09 18:03:35 +0100219 if dtb_path:
220 exec_args += ["-dtb", dtb_path]
221
222 if dumpdtb_path:
223 exec_args += ["-machine", "dumpdtb=" + dumpdtb_path]
224
David Brazdil2df24082019-09-05 11:55:08 +0100225 if self.args.initrd:
226 exec_args += ["-initrd", self.args.initrd]
227
228 vm_args = join_if_not_None(self.args.vm_args, test_args)
229 if vm_args:
230 exec_args += ["-append", vm_args]
231
232 return exec_args
233
David Brazdil0dbb41f2019-09-09 18:03:35 +0100234 def dump_dtb(self, run_state, test_args, path):
David Brazdil3cc24aa2019-09-27 10:24:41 +0100235 dumpdtb_args = self.gen_exec_args(test_args, False, dumpdtb_path=path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100236 self.exec_logged(run_state, dumpdtb_args)
237
David Brazdil3cc24aa2019-09-27 10:24:41 +0100238 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100239 """Run test given by `test_args` in QEMU."""
240 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100241
242 try:
243 dtb_path = None
244
245 # If manifest DTBO specified, dump DTB from QEMU and overlay them.
246 if self.args.manifest:
247 base_dtb_path = self.args.artifacts.create_file(
248 run_name, ".base.dtb")
249 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
250 self.dump_dtb(run_state, test_args, base_dtb_path)
251 self.overlay_dtb(
252 run_state, base_dtb_path, self.args.manifest, dtb_path)
253
254 # Execute test in QEMU..
David Brazdil3cc24aa2019-09-27 10:24:41 +0100255 exec_args = self.gen_exec_args(test_args, is_long_running,
256 dtb_path=dtb_path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100257 self.exec_logged(run_state, exec_args)
258 except DriverRunException:
259 pass
260
261 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100262
263
David Brazdil2df24082019-09-05 11:55:08 +0100264class FvpDriver(Driver):
Andrew Walbran20215742019-11-18 11:35:05 +0000265 """Driver which runs tests in Arm FVP emulator."""
David Brazdil2df24082019-09-05 11:55:08 +0100266
267 def __init__(self, args):
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000268 if args.cpu:
269 raise ValueError("FVP emulator does not support the --cpu option.")
David Brazdil2df24082019-09-05 11:55:08 +0100270 Driver.__init__(self, args)
271
272 def gen_dts(self, dts_path, test_args, initrd_start, initrd_end):
273 """Create a DeviceTree source which will be compiled into a DTB and
274 passed to FVP for a test run."""
275 vm_args = join_if_not_None(self.args.vm_args, test_args)
276 write_file(dts_path, read_file(FVP_PREBUILT_DTS))
277 append_file(dts_path, """
278 / {{
279 chosen {{
280 bootargs = "{}";
281 stdout-path = "serial0:115200n8";
282 linux,initrd-start = <{}>;
283 linux,initrd-end = <{}>;
284 }};
285 }};
286 """.format(vm_args, initrd_start, initrd_end))
287
288 def gen_fvp_args(
289 self, initrd_start, uart0_log_path, uart1_log_path, dtb_path):
290 """Generate command line arguments for FVP."""
291 fvp_args = [
292 "timeout", "--foreground", "40s",
293 FVP_BINARY,
294 "-C", "pctl.startup=0.0.0.0",
295 "-C", "bp.secure_memory=0",
296 "-C", "cluster0.NUM_CORES=4",
297 "-C", "cluster1.NUM_CORES=4",
298 "-C", "cache_state_modelled=0",
299 "-C", "bp.vis.disable_visualisation=true",
300 "-C", "bp.vis.rate_limit-enable=false",
301 "-C", "bp.terminal_0.start_telnet=false",
302 "-C", "bp.terminal_1.start_telnet=false",
303 "-C", "bp.terminal_2.start_telnet=false",
304 "-C", "bp.terminal_3.start_telnet=false",
305 "-C", "bp.pl011_uart0.untimed_fifos=1",
306 "-C", "bp.pl011_uart0.unbuffered_output=1",
307 "-C", "bp.pl011_uart0.out_file=" + uart0_log_path,
308 "-C", "bp.pl011_uart1.out_file=" + uart1_log_path,
309 "-C", "cluster0.cpu0.RVBAR=0x04020000",
310 "-C", "cluster0.cpu1.RVBAR=0x04020000",
311 "-C", "cluster0.cpu2.RVBAR=0x04020000",
312 "-C", "cluster0.cpu3.RVBAR=0x04020000",
313 "-C", "cluster1.cpu0.RVBAR=0x04020000",
314 "-C", "cluster1.cpu1.RVBAR=0x04020000",
315 "-C", "cluster1.cpu2.RVBAR=0x04020000",
316 "-C", "cluster1.cpu3.RVBAR=0x04020000",
David Brazdil21204ae2019-10-30 19:22:38 +0000317 "--data", "cluster0.cpu0=" + FVP_PREBUILT_BL31 + "@0x04020000",
David Brazdil2df24082019-09-05 11:55:08 +0100318 "--data", "cluster0.cpu0=" + dtb_path + "@0x82000000",
319 "--data", "cluster0.cpu0=" + self.args.kernel + "@0x80000000",
320 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
321 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
322 ]
323
324 if self.args.initrd:
325 fvp_args += [
326 "--data",
327 "cluster0.cpu0={}@{}".format(
328 self.args.initrd, hex(initrd_start))
329 ]
330
331 return fvp_args
332
David Brazdil3cc24aa2019-09-27 10:24:41 +0100333 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100334 run_state = self.start_run(run_name)
335
David Brazdil0dbb41f2019-09-09 18:03:35 +0100336 base_dts_path = self.args.artifacts.create_file(run_name, ".base.dts")
337 base_dtb_path = self.args.artifacts.create_file(run_name, ".base.dtb")
David Brazdil2df24082019-09-05 11:55:08 +0100338 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
339 uart0_log_path = self.args.artifacts.create_file(run_name, ".uart0.log")
340 uart1_log_path = self.args.artifacts.create_file(run_name, ".uart1.log")
341
342 initrd_start = 0x84000000
343 if self.args.initrd:
344 initrd_end = initrd_start + os.path.getsize(self.args.initrd)
345 else:
346 initrd_end = 0x85000000 # Default value
347
David Brazdil0dbb41f2019-09-09 18:03:35 +0100348 try:
349 # Create a DT to pass to FVP.
350 self.gen_dts(base_dts_path, test_args, initrd_start, initrd_end)
David Brazdil2df24082019-09-05 11:55:08 +0100351
David Brazdil0dbb41f2019-09-09 18:03:35 +0100352 # Compile DTS to DTB.
353 dtc_args = [
354 DTC_SCRIPT, "compile", "-i", base_dts_path, "-o", base_dtb_path,
355 ]
356 self.exec_logged(run_state, dtc_args)
357
358 # If manifest DTBO specified, overlay it.
359 if self.args.manifest:
360 self.overlay_dtb(
361 run_state, base_dtb_path, self.args.manifest, dtb_path)
362 else:
363 dtb_path = base_dtb_path
364
365 # Run FVP.
366 fvp_args = self.gen_fvp_args(
367 initrd_start, uart0_log_path, uart1_log_path, dtb_path)
368 self.exec_logged(run_state, fvp_args)
369 except DriverRunException:
370 pass
David Brazdil2df24082019-09-05 11:55:08 +0100371
372 # Append UART0 output to main log.
373 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100374 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100375
376
377# Tuple used to return information about the results of running a set of tests.
378TestRunnerResult = collections.namedtuple("TestRunnerResult", [
379 "tests_run",
380 "tests_failed",
381 ])
382
383
384class TestRunner:
385 """Class which communicates with a test platform to obtain a list of
386 available tests and driving their execution."""
387
David Brazdil3cc24aa2019-09-27 10:24:41 +0100388 def __init__(self, artifacts, driver, image_name, suite_regex, test_regex,
389 skip_long_running_tests):
David Brazdil2df24082019-09-05 11:55:08 +0100390 self.artifacts = artifacts
391 self.driver = driver
392 self.image_name = image_name
David Brazdil3cc24aa2019-09-27 10:24:41 +0100393 self.skip_long_running_tests = skip_long_running_tests
David Brazdil2df24082019-09-05 11:55:08 +0100394
395 self.suite_re = re.compile(suite_regex or ".*")
396 self.test_re = re.compile(test_regex or ".*")
397
398 def extract_hftest_lines(self, raw):
399 """Extract hftest-specific lines from a raw output from an invocation
400 of the test platform."""
401 lines = []
402 for line in raw.splitlines():
403 if line.startswith("VM "):
404 line = line[len("VM 0: "):]
405 if line.startswith(HFTEST_LOG_PREFIX):
406 lines.append(line[len(HFTEST_LOG_PREFIX):])
407 return lines
408
409 def get_test_json(self):
410 """Invoke the test platform and request a JSON of available test and
411 test suites."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100412 out = self.driver.run("json", "json", False)
David Brazdil2df24082019-09-05 11:55:08 +0100413 hf_out = "\n".join(self.extract_hftest_lines(out))
414 try:
415 return json.loads(hf_out)
416 except ValueError as e:
417 print(out)
418 raise e
419
420 def collect_results(self, fn, it, xml_node):
421 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
422 Insert "tests" and "failures" nodes to `xml_node`."""
423 tests_run = 0
424 tests_failed = 0
425 for i in it:
426 sub_result = fn(i)
427 assert(sub_result.tests_run >= sub_result.tests_failed)
428 tests_run += sub_result.tests_run
429 tests_failed += sub_result.tests_failed
430
431 xml_node.set("tests", str(tests_run))
432 xml_node.set("failures", str(tests_failed))
433 return TestRunnerResult(tests_run, tests_failed)
434
435 def is_passed_test(self, test_out):
436 """Parse the output of a test and return True if it passed."""
437 return \
438 len(test_out) > 0 and \
439 test_out[-1] == HFTEST_LOG_FINISHED and \
440 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
441
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000442 def get_log_name(self, suite, test):
443 """Returns a string with a generated log name for the test."""
444 log_name = ""
445
446 cpu = self.driver.args.cpu
447 if cpu:
448 log_name += cpu + "."
449
450 log_name += suite["name"] + "." + test["name"]
451
452 return log_name
453
David Brazdil2df24082019-09-05 11:55:08 +0100454 def run_test(self, suite, test, suite_xml):
455 """Invoke the test platform and request to run a given `test` in given
456 `suite`. Create a new XML node with results under `suite_xml`.
457 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100458 if not self.test_re.match(test["name"]):
David Brazdil2df24082019-09-05 11:55:08 +0100459 return TestRunnerResult(tests_run=0, tests_failed=0)
460
David Brazdil3cc24aa2019-09-27 10:24:41 +0100461 if self.skip_long_running_tests and test["is_long_running"]:
462 print(" SKIP", test["name"])
463 return TestRunnerResult(tests_run=0, tests_failed=0)
464
465 print(" RUN", test["name"])
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000466 log_name = self.get_log_name(suite, test)
David Brazdil2df24082019-09-05 11:55:08 +0100467
468 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100469 test_xml.set("name", test["name"])
470 test_xml.set("classname", suite["name"])
David Brazdil2df24082019-09-05 11:55:08 +0100471 test_xml.set("status", "run")
472
473 out = self.extract_hftest_lines(self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100474 log_name, "run {} {}".format(suite["name"], test["name"]),
475 test["is_long_running"]))
David Brazdil2df24082019-09-05 11:55:08 +0100476
477 if self.is_passed_test(out):
478 print(" PASS")
479 return TestRunnerResult(tests_run=1, tests_failed=0)
480 else:
David Brazdil623b6812019-09-09 11:41:08 +0100481 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100482 failure_xml = ET.SubElement(test_xml, "failure")
483 # TODO: set a meaningful message and put log in CDATA
484 failure_xml.set("message", "Test failed")
485 return TestRunnerResult(tests_run=1, tests_failed=1)
486
487 def run_suite(self, suite, xml):
488 """Invoke the test platform and request to run all matching tests in
489 `suite`. Create new XML nodes with results under `xml`.
490 Suite skipped if it does not match the regex given to constructor."""
491 if not self.suite_re.match(suite["name"]):
492 return TestRunnerResult(tests_run=0, tests_failed=0)
493
494 print(" SUITE", suite["name"])
495 suite_xml = ET.SubElement(xml, "testsuite")
496 suite_xml.set("name", suite["name"])
497
498 return self.collect_results(
499 lambda test: self.run_test(suite, test, suite_xml),
500 suite["tests"],
501 suite_xml)
502
503 def run_tests(self):
504 """Run all suites and tests matching regexes given to constructor.
505 Write results to sponge log XML. Return the number of run and failed
506 tests."""
507
508 test_spec = self.get_test_json()
509 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
510
511 xml = ET.Element("testsuites")
512 xml.set("name", self.image_name)
513 xml.set("timestamp", timestamp)
514
515 result = self.collect_results(
516 lambda suite: self.run_suite(suite, xml),
517 test_spec["suites"],
518 xml)
519
520 # Write XML to file.
521 with open(self.artifacts.sponge_xml_path, "w") as f:
522 ET.ElementTree(xml).write(f, encoding='utf-8', xml_declaration=True)
523
524 if result.tests_failed > 0:
525 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
526 "tests failed")
527 elif result.tests_run > 0:
528 print(" PASS: all", result.tests_run, "tests passed")
529
530 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100531
532
533def Main():
534 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000535 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100536 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100537 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100538 parser.add_argument("--out_initrd")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000539 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100540 parser.add_argument("--suite")
541 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000542 parser.add_argument("--vm_args")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100543 parser.add_argument("--fvp", action="store_true")
544 parser.add_argument("--skip-long-running-tests", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000545 parser.add_argument("--cpu",
546 help="Selects the CPU configuration for the run environment.")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100547 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100548
Andrew Scullbc7189d2018-08-14 09:35:13 +0100549 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000550 image = os.path.join(args.out, args.image + ".bin")
551 initrd = None
David Brazdil0dbb41f2019-09-09 18:03:35 +0100552 manifest = None
David Brazdil2df24082019-09-05 11:55:08 +0100553 image_name = args.image
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000554 if args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100555 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
556 initrd = os.path.join(initrd_dir, "initrd.img")
557 manifest = os.path.join(initrd_dir, "manifest.dtbo")
David Brazdil2df24082019-09-05 11:55:08 +0100558 image_name += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000559 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100560
561 # Create class which will manage all test artifacts.
562 artifacts = ArtifactsManager(os.path.join(args.log, image_name))
563
564 # Create a driver for the platform we want to test on.
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000565 driver_args = DriverArgs(artifacts, image, initrd, manifest, vm_args,
566 args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100567 if args.fvp:
568 driver = FvpDriver(driver_args)
569 else:
570 driver = QemuDriver(driver_args)
571
572 # Create class which will drive test execution.
David Brazdil3cc24aa2019-09-27 10:24:41 +0100573 runner = TestRunner(artifacts, driver, image_name, args.suite, args.test,
574 args.skip_long_running_tests)
David Brazdil2df24082019-09-05 11:55:08 +0100575
576 # Run tests.
577 runner_result = runner.run_tests()
578
579 # Print error message if no tests were run as this is probably unexpected.
580 # Return suitable error code.
581 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100582 print("Error: no tests match")
583 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100584 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100585 return 1
586 else:
David Brazdil2df24082019-09-05 11:55:08 +0100587 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100588
589if __name__ == "__main__":
590 sys.exit(Main())