blob: 1c889531b5d3cd3ea1d9369e89f455f6b37af00a [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(
Andrew Walbranee5418e2019-11-27 17:43:05 +0000289 self, is_long_running, initrd_start, uart0_log_path, uart1_log_path,
290 dtb_path):
David Brazdil2df24082019-09-05 11:55:08 +0100291 """Generate command line arguments for FVP."""
Andrew Walbranee5418e2019-11-27 17:43:05 +0000292 time_limit = "80s" if is_long_running else "40s"
David Brazdil2df24082019-09-05 11:55:08 +0100293 fvp_args = [
Andrew Walbranee5418e2019-11-27 17:43:05 +0000294 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100295 FVP_BINARY,
296 "-C", "pctl.startup=0.0.0.0",
297 "-C", "bp.secure_memory=0",
298 "-C", "cluster0.NUM_CORES=4",
299 "-C", "cluster1.NUM_CORES=4",
300 "-C", "cache_state_modelled=0",
301 "-C", "bp.vis.disable_visualisation=true",
302 "-C", "bp.vis.rate_limit-enable=false",
303 "-C", "bp.terminal_0.start_telnet=false",
304 "-C", "bp.terminal_1.start_telnet=false",
305 "-C", "bp.terminal_2.start_telnet=false",
306 "-C", "bp.terminal_3.start_telnet=false",
307 "-C", "bp.pl011_uart0.untimed_fifos=1",
308 "-C", "bp.pl011_uart0.unbuffered_output=1",
309 "-C", "bp.pl011_uart0.out_file=" + uart0_log_path,
310 "-C", "bp.pl011_uart1.out_file=" + uart1_log_path,
311 "-C", "cluster0.cpu0.RVBAR=0x04020000",
312 "-C", "cluster0.cpu1.RVBAR=0x04020000",
313 "-C", "cluster0.cpu2.RVBAR=0x04020000",
314 "-C", "cluster0.cpu3.RVBAR=0x04020000",
315 "-C", "cluster1.cpu0.RVBAR=0x04020000",
316 "-C", "cluster1.cpu1.RVBAR=0x04020000",
317 "-C", "cluster1.cpu2.RVBAR=0x04020000",
318 "-C", "cluster1.cpu3.RVBAR=0x04020000",
David Brazdil21204ae2019-10-30 19:22:38 +0000319 "--data", "cluster0.cpu0=" + FVP_PREBUILT_BL31 + "@0x04020000",
David Brazdil2df24082019-09-05 11:55:08 +0100320 "--data", "cluster0.cpu0=" + dtb_path + "@0x82000000",
321 "--data", "cluster0.cpu0=" + self.args.kernel + "@0x80000000",
322 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
323 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
324 ]
325
326 if self.args.initrd:
327 fvp_args += [
328 "--data",
329 "cluster0.cpu0={}@{}".format(
330 self.args.initrd, hex(initrd_start))
331 ]
332
333 return fvp_args
334
David Brazdil3cc24aa2019-09-27 10:24:41 +0100335 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100336 run_state = self.start_run(run_name)
337
David Brazdil0dbb41f2019-09-09 18:03:35 +0100338 base_dts_path = self.args.artifacts.create_file(run_name, ".base.dts")
339 base_dtb_path = self.args.artifacts.create_file(run_name, ".base.dtb")
David Brazdil2df24082019-09-05 11:55:08 +0100340 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
341 uart0_log_path = self.args.artifacts.create_file(run_name, ".uart0.log")
342 uart1_log_path = self.args.artifacts.create_file(run_name, ".uart1.log")
343
344 initrd_start = 0x84000000
345 if self.args.initrd:
346 initrd_end = initrd_start + os.path.getsize(self.args.initrd)
347 else:
348 initrd_end = 0x85000000 # Default value
349
David Brazdil0dbb41f2019-09-09 18:03:35 +0100350 try:
351 # Create a DT to pass to FVP.
352 self.gen_dts(base_dts_path, test_args, initrd_start, initrd_end)
David Brazdil2df24082019-09-05 11:55:08 +0100353
David Brazdil0dbb41f2019-09-09 18:03:35 +0100354 # Compile DTS to DTB.
355 dtc_args = [
356 DTC_SCRIPT, "compile", "-i", base_dts_path, "-o", base_dtb_path,
357 ]
358 self.exec_logged(run_state, dtc_args)
359
360 # If manifest DTBO specified, overlay it.
361 if self.args.manifest:
362 self.overlay_dtb(
363 run_state, base_dtb_path, self.args.manifest, dtb_path)
364 else:
365 dtb_path = base_dtb_path
366
367 # Run FVP.
368 fvp_args = self.gen_fvp_args(
Andrew Walbranee5418e2019-11-27 17:43:05 +0000369 is_long_running, initrd_start, uart0_log_path, uart1_log_path,
370 dtb_path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100371 self.exec_logged(run_state, fvp_args)
372 except DriverRunException:
373 pass
David Brazdil2df24082019-09-05 11:55:08 +0100374
375 # Append UART0 output to main log.
376 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100377 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100378
379
380# Tuple used to return information about the results of running a set of tests.
381TestRunnerResult = collections.namedtuple("TestRunnerResult", [
382 "tests_run",
383 "tests_failed",
384 ])
385
386
387class TestRunner:
388 """Class which communicates with a test platform to obtain a list of
389 available tests and driving their execution."""
390
David Brazdil3cc24aa2019-09-27 10:24:41 +0100391 def __init__(self, artifacts, driver, image_name, suite_regex, test_regex,
392 skip_long_running_tests):
David Brazdil2df24082019-09-05 11:55:08 +0100393 self.artifacts = artifacts
394 self.driver = driver
395 self.image_name = image_name
David Brazdil3cc24aa2019-09-27 10:24:41 +0100396 self.skip_long_running_tests = skip_long_running_tests
David Brazdil2df24082019-09-05 11:55:08 +0100397
398 self.suite_re = re.compile(suite_regex or ".*")
399 self.test_re = re.compile(test_regex or ".*")
400
401 def extract_hftest_lines(self, raw):
402 """Extract hftest-specific lines from a raw output from an invocation
403 of the test platform."""
404 lines = []
405 for line in raw.splitlines():
406 if line.startswith("VM "):
407 line = line[len("VM 0: "):]
408 if line.startswith(HFTEST_LOG_PREFIX):
409 lines.append(line[len(HFTEST_LOG_PREFIX):])
410 return lines
411
412 def get_test_json(self):
413 """Invoke the test platform and request a JSON of available test and
414 test suites."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100415 out = self.driver.run("json", "json", False)
David Brazdil2df24082019-09-05 11:55:08 +0100416 hf_out = "\n".join(self.extract_hftest_lines(out))
417 try:
418 return json.loads(hf_out)
419 except ValueError as e:
420 print(out)
421 raise e
422
423 def collect_results(self, fn, it, xml_node):
424 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
425 Insert "tests" and "failures" nodes to `xml_node`."""
426 tests_run = 0
427 tests_failed = 0
428 for i in it:
429 sub_result = fn(i)
430 assert(sub_result.tests_run >= sub_result.tests_failed)
431 tests_run += sub_result.tests_run
432 tests_failed += sub_result.tests_failed
433
434 xml_node.set("tests", str(tests_run))
435 xml_node.set("failures", str(tests_failed))
436 return TestRunnerResult(tests_run, tests_failed)
437
438 def is_passed_test(self, test_out):
439 """Parse the output of a test and return True if it passed."""
440 return \
441 len(test_out) > 0 and \
442 test_out[-1] == HFTEST_LOG_FINISHED and \
443 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
444
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000445 def get_log_name(self, suite, test):
446 """Returns a string with a generated log name for the test."""
447 log_name = ""
448
449 cpu = self.driver.args.cpu
450 if cpu:
451 log_name += cpu + "."
452
453 log_name += suite["name"] + "." + test["name"]
454
455 return log_name
456
David Brazdil2df24082019-09-05 11:55:08 +0100457 def run_test(self, suite, test, suite_xml):
458 """Invoke the test platform and request to run a given `test` in given
459 `suite`. Create a new XML node with results under `suite_xml`.
460 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100461 if not self.test_re.match(test["name"]):
David Brazdil2df24082019-09-05 11:55:08 +0100462 return TestRunnerResult(tests_run=0, tests_failed=0)
463
David Brazdil3cc24aa2019-09-27 10:24:41 +0100464 if self.skip_long_running_tests and test["is_long_running"]:
465 print(" SKIP", test["name"])
466 return TestRunnerResult(tests_run=0, tests_failed=0)
467
468 print(" RUN", test["name"])
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000469 log_name = self.get_log_name(suite, test)
David Brazdil2df24082019-09-05 11:55:08 +0100470
471 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100472 test_xml.set("name", test["name"])
473 test_xml.set("classname", suite["name"])
David Brazdil2df24082019-09-05 11:55:08 +0100474 test_xml.set("status", "run")
475
476 out = self.extract_hftest_lines(self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100477 log_name, "run {} {}".format(suite["name"], test["name"]),
478 test["is_long_running"]))
David Brazdil2df24082019-09-05 11:55:08 +0100479
480 if self.is_passed_test(out):
481 print(" PASS")
482 return TestRunnerResult(tests_run=1, tests_failed=0)
483 else:
David Brazdil623b6812019-09-09 11:41:08 +0100484 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100485 failure_xml = ET.SubElement(test_xml, "failure")
486 # TODO: set a meaningful message and put log in CDATA
487 failure_xml.set("message", "Test failed")
488 return TestRunnerResult(tests_run=1, tests_failed=1)
489
490 def run_suite(self, suite, xml):
491 """Invoke the test platform and request to run all matching tests in
492 `suite`. Create new XML nodes with results under `xml`.
493 Suite skipped if it does not match the regex given to constructor."""
494 if not self.suite_re.match(suite["name"]):
495 return TestRunnerResult(tests_run=0, tests_failed=0)
496
497 print(" SUITE", suite["name"])
498 suite_xml = ET.SubElement(xml, "testsuite")
499 suite_xml.set("name", suite["name"])
500
501 return self.collect_results(
502 lambda test: self.run_test(suite, test, suite_xml),
503 suite["tests"],
504 suite_xml)
505
506 def run_tests(self):
507 """Run all suites and tests matching regexes given to constructor.
508 Write results to sponge log XML. Return the number of run and failed
509 tests."""
510
511 test_spec = self.get_test_json()
512 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
513
514 xml = ET.Element("testsuites")
515 xml.set("name", self.image_name)
516 xml.set("timestamp", timestamp)
517
518 result = self.collect_results(
519 lambda suite: self.run_suite(suite, xml),
520 test_spec["suites"],
521 xml)
522
523 # Write XML to file.
524 with open(self.artifacts.sponge_xml_path, "w") as f:
525 ET.ElementTree(xml).write(f, encoding='utf-8', xml_declaration=True)
526
527 if result.tests_failed > 0:
528 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
529 "tests failed")
530 elif result.tests_run > 0:
531 print(" PASS: all", result.tests_run, "tests passed")
532
533 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100534
535
536def Main():
537 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000538 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100539 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100540 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100541 parser.add_argument("--out_initrd")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000542 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100543 parser.add_argument("--suite")
544 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000545 parser.add_argument("--vm_args")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100546 parser.add_argument("--fvp", action="store_true")
547 parser.add_argument("--skip-long-running-tests", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000548 parser.add_argument("--cpu",
549 help="Selects the CPU configuration for the run environment.")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100550 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100551
Andrew Scullbc7189d2018-08-14 09:35:13 +0100552 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000553 image = os.path.join(args.out, args.image + ".bin")
554 initrd = None
David Brazdil0dbb41f2019-09-09 18:03:35 +0100555 manifest = None
David Brazdil2df24082019-09-05 11:55:08 +0100556 image_name = args.image
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000557 if args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100558 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
559 initrd = os.path.join(initrd_dir, "initrd.img")
560 manifest = os.path.join(initrd_dir, "manifest.dtbo")
David Brazdil2df24082019-09-05 11:55:08 +0100561 image_name += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000562 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100563
564 # Create class which will manage all test artifacts.
565 artifacts = ArtifactsManager(os.path.join(args.log, image_name))
566
567 # Create a driver for the platform we want to test on.
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000568 driver_args = DriverArgs(artifacts, image, initrd, manifest, vm_args,
569 args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100570 if args.fvp:
571 driver = FvpDriver(driver_args)
572 else:
573 driver = QemuDriver(driver_args)
574
575 # Create class which will drive test execution.
David Brazdil3cc24aa2019-09-27 10:24:41 +0100576 runner = TestRunner(artifacts, driver, image_name, args.suite, args.test,
577 args.skip_long_running_tests)
David Brazdil2df24082019-09-05 11:55:08 +0100578
579 # Run tests.
580 runner_result = runner.run_tests()
581
582 # Print error message if no tests were run as this is probably unexpected.
583 # Return suitable error code.
584 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100585 print("Error: no tests match")
586 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100587 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100588 return 1
589 else:
David Brazdil2df24082019-09-05 11:55:08 +0100590 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100591
592if __name__ == "__main__":
593 sys.exit(Main())