blob: c588c625c70c020afc610f67c8a557647eb8d108 [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",
120 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100121
Andrew Walbran98656252019-03-14 14:52:29 +0000122
David Brazdil2df24082019-09-05 11:55:08 +0100123# State shared between the common Driver class and its subclasses during
124# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100125class DriverRunState:
126 def __init__(self, log_path):
127 self.log_path = log_path
128 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000129
David Brazdil7325eaf2019-09-27 13:04:51 +0100130 def set_ret_code(self, ret_code):
131 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000132
David Brazdil0dbb41f2019-09-09 18:03:35 +0100133class DriverRunException(Exception):
134 """Exception thrown if subprocess invoked by a driver returned non-zero
135 status code. Used to fast-exit from a driver command sequence."""
136 pass
137
138
David Brazdil2df24082019-09-05 11:55:08 +0100139class Driver:
140 """Parent class of drivers for all testable platforms."""
141
142 def __init__(self, args):
143 self.args = args
144
David Brazdil623b6812019-09-09 11:41:08 +0100145 def get_run_log(self, run_name):
146 """Return path to the main log of a given test run."""
147 return self.args.artifacts.get_file(run_name, ".log")
148
David Brazdil2df24082019-09-05 11:55:08 +0100149 def start_run(self, run_name):
150 """Hook called by Driver subclasses before they invoke the target
151 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100152 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100153
154 def exec_logged(self, run_state, exec_args):
155 """Run a subprocess on behalf of a Driver subclass and append its
156 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100157 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100158 with open(run_state.log_path, "a") as f:
159 f.write("$ {}\r\n".format(" ".join(exec_args)))
160 f.flush()
David Brazdil0dbb41f2019-09-09 18:03:35 +0100161 ret_code = subprocess.call(exec_args, stdout=f, stderr=f)
162 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100163 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100164 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100165
David Brazdil0dbb41f2019-09-09 18:03:35 +0100166 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100167 """Hook called by Driver subclasses after they finished running the
168 target platform. `ret_code` argument is the return code of the main
169 command run by the driver. A corresponding log message is printed."""
170 # Decode return code and add a message to the log.
171 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100172 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100173 f.write("\r\n{}{} timed out\r\n".format(
174 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100175 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100176 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100177 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
178 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100179
180 # Append log of this run to full test log.
181 log_content = read_file(run_state.log_path)
182 append_file(
183 self.args.artifacts.sponge_log_path,
184 log_content + "\r\n\r\n")
185 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000186
David Brazdil0dbb41f2019-09-09 18:03:35 +0100187 def overlay_dtb(self, run_state, base_dtb, overlay_dtb, out_dtb):
188 """Overlay `overlay_dtb` over `base_dtb` into `out_dtb`."""
189 dtc_args = [
190 DTC_SCRIPT, "overlay",
191 out_dtb, base_dtb, overlay_dtb,
192 ]
193 self.exec_logged(run_state, dtc_args)
194
Andrew Walbran98656252019-03-14 14:52:29 +0000195
David Brazdil2df24082019-09-05 11:55:08 +0100196class QemuDriver(Driver):
197 """Driver which runs tests in QEMU."""
198
199 def __init__(self, args):
200 Driver.__init__(self, args)
201
David Brazdil3cc24aa2019-09-27 10:24:41 +0100202 def gen_exec_args(self, test_args, is_long_running, dtb_path=None,
203 dumpdtb_path=None):
David Brazdil2df24082019-09-05 11:55:08 +0100204 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100205 time_limit = "120s" if is_long_running else "10s"
David Brazdil2df24082019-09-05 11:55:08 +0100206 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100207 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100208 "./prebuilts/linux-x64/qemu/qemu-system-aarch64",
Andrew Scull2925e422019-10-04 13:29:53 +0100209 "-machine", "virt,virtualization=on,gic_version=3",
David Brazdil2df24082019-09-05 11:55:08 +0100210 "-cpu", "cortex-a57", "-smp", "4", "-m", "64M",
David Brazdil2df24082019-09-05 11:55:08 +0100211 "-nographic", "-nodefaults", "-serial", "stdio",
Andrew Scull2925e422019-10-04 13:29:53 +0100212 "-d", "unimp", "-kernel", self.args.kernel,
David Brazdil2df24082019-09-05 11:55:08 +0100213 ]
214
David Brazdil0dbb41f2019-09-09 18:03:35 +0100215 if dtb_path:
216 exec_args += ["-dtb", dtb_path]
217
218 if dumpdtb_path:
219 exec_args += ["-machine", "dumpdtb=" + dumpdtb_path]
220
David Brazdil2df24082019-09-05 11:55:08 +0100221 if self.args.initrd:
222 exec_args += ["-initrd", self.args.initrd]
223
224 vm_args = join_if_not_None(self.args.vm_args, test_args)
225 if vm_args:
226 exec_args += ["-append", vm_args]
227
228 return exec_args
229
David Brazdil0dbb41f2019-09-09 18:03:35 +0100230 def dump_dtb(self, run_state, test_args, path):
David Brazdil3cc24aa2019-09-27 10:24:41 +0100231 dumpdtb_args = self.gen_exec_args(test_args, False, dumpdtb_path=path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100232 self.exec_logged(run_state, dumpdtb_args)
233
David Brazdil3cc24aa2019-09-27 10:24:41 +0100234 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100235 """Run test given by `test_args` in QEMU."""
236 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100237
238 try:
239 dtb_path = None
240
241 # If manifest DTBO specified, dump DTB from QEMU and overlay them.
242 if self.args.manifest:
243 base_dtb_path = self.args.artifacts.create_file(
244 run_name, ".base.dtb")
245 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
246 self.dump_dtb(run_state, test_args, base_dtb_path)
247 self.overlay_dtb(
248 run_state, base_dtb_path, self.args.manifest, dtb_path)
249
250 # Execute test in QEMU..
David Brazdil3cc24aa2019-09-27 10:24:41 +0100251 exec_args = self.gen_exec_args(test_args, is_long_running,
252 dtb_path=dtb_path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100253 self.exec_logged(run_state, exec_args)
254 except DriverRunException:
255 pass
256
257 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100258
259
David Brazdil2df24082019-09-05 11:55:08 +0100260class FvpDriver(Driver):
261 """Driver which runs tests in ARM FVP emulator."""
262
263 def __init__(self, args):
264 Driver.__init__(self, args)
265
266 def gen_dts(self, dts_path, test_args, initrd_start, initrd_end):
267 """Create a DeviceTree source which will be compiled into a DTB and
268 passed to FVP for a test run."""
269 vm_args = join_if_not_None(self.args.vm_args, test_args)
270 write_file(dts_path, read_file(FVP_PREBUILT_DTS))
271 append_file(dts_path, """
272 / {{
273 chosen {{
274 bootargs = "{}";
275 stdout-path = "serial0:115200n8";
276 linux,initrd-start = <{}>;
277 linux,initrd-end = <{}>;
278 }};
279 }};
280 """.format(vm_args, initrd_start, initrd_end))
281
282 def gen_fvp_args(
283 self, initrd_start, uart0_log_path, uart1_log_path, dtb_path):
284 """Generate command line arguments for FVP."""
285 fvp_args = [
286 "timeout", "--foreground", "40s",
287 FVP_BINARY,
288 "-C", "pctl.startup=0.0.0.0",
289 "-C", "bp.secure_memory=0",
290 "-C", "cluster0.NUM_CORES=4",
291 "-C", "cluster1.NUM_CORES=4",
292 "-C", "cache_state_modelled=0",
293 "-C", "bp.vis.disable_visualisation=true",
294 "-C", "bp.vis.rate_limit-enable=false",
295 "-C", "bp.terminal_0.start_telnet=false",
296 "-C", "bp.terminal_1.start_telnet=false",
297 "-C", "bp.terminal_2.start_telnet=false",
298 "-C", "bp.terminal_3.start_telnet=false",
299 "-C", "bp.pl011_uart0.untimed_fifos=1",
300 "-C", "bp.pl011_uart0.unbuffered_output=1",
301 "-C", "bp.pl011_uart0.out_file=" + uart0_log_path,
302 "-C", "bp.pl011_uart1.out_file=" + uart1_log_path,
303 "-C", "cluster0.cpu0.RVBAR=0x04020000",
304 "-C", "cluster0.cpu1.RVBAR=0x04020000",
305 "-C", "cluster0.cpu2.RVBAR=0x04020000",
306 "-C", "cluster0.cpu3.RVBAR=0x04020000",
307 "-C", "cluster1.cpu0.RVBAR=0x04020000",
308 "-C", "cluster1.cpu1.RVBAR=0x04020000",
309 "-C", "cluster1.cpu2.RVBAR=0x04020000",
310 "-C", "cluster1.cpu3.RVBAR=0x04020000",
David Brazdil21204ae2019-10-30 19:22:38 +0000311 "--data", "cluster0.cpu0=" + FVP_PREBUILT_BL31 + "@0x04020000",
David Brazdil2df24082019-09-05 11:55:08 +0100312 "--data", "cluster0.cpu0=" + dtb_path + "@0x82000000",
313 "--data", "cluster0.cpu0=" + self.args.kernel + "@0x80000000",
314 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
315 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
316 ]
317
318 if self.args.initrd:
319 fvp_args += [
320 "--data",
321 "cluster0.cpu0={}@{}".format(
322 self.args.initrd, hex(initrd_start))
323 ]
324
325 return fvp_args
326
David Brazdil3cc24aa2019-09-27 10:24:41 +0100327 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100328 run_state = self.start_run(run_name)
329
David Brazdil0dbb41f2019-09-09 18:03:35 +0100330 base_dts_path = self.args.artifacts.create_file(run_name, ".base.dts")
331 base_dtb_path = self.args.artifacts.create_file(run_name, ".base.dtb")
David Brazdil2df24082019-09-05 11:55:08 +0100332 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
333 uart0_log_path = self.args.artifacts.create_file(run_name, ".uart0.log")
334 uart1_log_path = self.args.artifacts.create_file(run_name, ".uart1.log")
335
336 initrd_start = 0x84000000
337 if self.args.initrd:
338 initrd_end = initrd_start + os.path.getsize(self.args.initrd)
339 else:
340 initrd_end = 0x85000000 # Default value
341
David Brazdil0dbb41f2019-09-09 18:03:35 +0100342 try:
343 # Create a DT to pass to FVP.
344 self.gen_dts(base_dts_path, test_args, initrd_start, initrd_end)
David Brazdil2df24082019-09-05 11:55:08 +0100345
David Brazdil0dbb41f2019-09-09 18:03:35 +0100346 # Compile DTS to DTB.
347 dtc_args = [
348 DTC_SCRIPT, "compile", "-i", base_dts_path, "-o", base_dtb_path,
349 ]
350 self.exec_logged(run_state, dtc_args)
351
352 # If manifest DTBO specified, overlay it.
353 if self.args.manifest:
354 self.overlay_dtb(
355 run_state, base_dtb_path, self.args.manifest, dtb_path)
356 else:
357 dtb_path = base_dtb_path
358
359 # Run FVP.
360 fvp_args = self.gen_fvp_args(
361 initrd_start, uart0_log_path, uart1_log_path, dtb_path)
362 self.exec_logged(run_state, fvp_args)
363 except DriverRunException:
364 pass
David Brazdil2df24082019-09-05 11:55:08 +0100365
366 # Append UART0 output to main log.
367 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100368 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100369
370
371# Tuple used to return information about the results of running a set of tests.
372TestRunnerResult = collections.namedtuple("TestRunnerResult", [
373 "tests_run",
374 "tests_failed",
375 ])
376
377
378class TestRunner:
379 """Class which communicates with a test platform to obtain a list of
380 available tests and driving their execution."""
381
David Brazdil3cc24aa2019-09-27 10:24:41 +0100382 def __init__(self, artifacts, driver, image_name, suite_regex, test_regex,
383 skip_long_running_tests):
David Brazdil2df24082019-09-05 11:55:08 +0100384 self.artifacts = artifacts
385 self.driver = driver
386 self.image_name = image_name
David Brazdil3cc24aa2019-09-27 10:24:41 +0100387 self.skip_long_running_tests = skip_long_running_tests
David Brazdil2df24082019-09-05 11:55:08 +0100388
389 self.suite_re = re.compile(suite_regex or ".*")
390 self.test_re = re.compile(test_regex or ".*")
391
392 def extract_hftest_lines(self, raw):
393 """Extract hftest-specific lines from a raw output from an invocation
394 of the test platform."""
395 lines = []
396 for line in raw.splitlines():
397 if line.startswith("VM "):
398 line = line[len("VM 0: "):]
399 if line.startswith(HFTEST_LOG_PREFIX):
400 lines.append(line[len(HFTEST_LOG_PREFIX):])
401 return lines
402
403 def get_test_json(self):
404 """Invoke the test platform and request a JSON of available test and
405 test suites."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100406 out = self.driver.run("json", "json", False)
David Brazdil2df24082019-09-05 11:55:08 +0100407 hf_out = "\n".join(self.extract_hftest_lines(out))
408 try:
409 return json.loads(hf_out)
410 except ValueError as e:
411 print(out)
412 raise e
413
414 def collect_results(self, fn, it, xml_node):
415 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
416 Insert "tests" and "failures" nodes to `xml_node`."""
417 tests_run = 0
418 tests_failed = 0
419 for i in it:
420 sub_result = fn(i)
421 assert(sub_result.tests_run >= sub_result.tests_failed)
422 tests_run += sub_result.tests_run
423 tests_failed += sub_result.tests_failed
424
425 xml_node.set("tests", str(tests_run))
426 xml_node.set("failures", str(tests_failed))
427 return TestRunnerResult(tests_run, tests_failed)
428
429 def is_passed_test(self, test_out):
430 """Parse the output of a test and return True if it passed."""
431 return \
432 len(test_out) > 0 and \
433 test_out[-1] == HFTEST_LOG_FINISHED and \
434 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
435
436 def run_test(self, suite, test, suite_xml):
437 """Invoke the test platform and request to run a given `test` in given
438 `suite`. Create a new XML node with results under `suite_xml`.
439 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100440 if not self.test_re.match(test["name"]):
David Brazdil2df24082019-09-05 11:55:08 +0100441 return TestRunnerResult(tests_run=0, tests_failed=0)
442
David Brazdil3cc24aa2019-09-27 10:24:41 +0100443 if self.skip_long_running_tests and test["is_long_running"]:
444 print(" SKIP", test["name"])
445 return TestRunnerResult(tests_run=0, tests_failed=0)
446
447 print(" RUN", test["name"])
448 log_name = suite["name"] + "." + test["name"]
David Brazdil2df24082019-09-05 11:55:08 +0100449
450 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100451 test_xml.set("name", test["name"])
452 test_xml.set("classname", suite["name"])
David Brazdil2df24082019-09-05 11:55:08 +0100453 test_xml.set("status", "run")
454
455 out = self.extract_hftest_lines(self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100456 log_name, "run {} {}".format(suite["name"], test["name"]),
457 test["is_long_running"]))
David Brazdil2df24082019-09-05 11:55:08 +0100458
459 if self.is_passed_test(out):
460 print(" PASS")
461 return TestRunnerResult(tests_run=1, tests_failed=0)
462 else:
David Brazdil623b6812019-09-09 11:41:08 +0100463 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100464 failure_xml = ET.SubElement(test_xml, "failure")
465 # TODO: set a meaningful message and put log in CDATA
466 failure_xml.set("message", "Test failed")
467 return TestRunnerResult(tests_run=1, tests_failed=1)
468
469 def run_suite(self, suite, xml):
470 """Invoke the test platform and request to run all matching tests in
471 `suite`. Create new XML nodes with results under `xml`.
472 Suite skipped if it does not match the regex given to constructor."""
473 if not self.suite_re.match(suite["name"]):
474 return TestRunnerResult(tests_run=0, tests_failed=0)
475
476 print(" SUITE", suite["name"])
477 suite_xml = ET.SubElement(xml, "testsuite")
478 suite_xml.set("name", suite["name"])
479
480 return self.collect_results(
481 lambda test: self.run_test(suite, test, suite_xml),
482 suite["tests"],
483 suite_xml)
484
485 def run_tests(self):
486 """Run all suites and tests matching regexes given to constructor.
487 Write results to sponge log XML. Return the number of run and failed
488 tests."""
489
490 test_spec = self.get_test_json()
491 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
492
493 xml = ET.Element("testsuites")
494 xml.set("name", self.image_name)
495 xml.set("timestamp", timestamp)
496
497 result = self.collect_results(
498 lambda suite: self.run_suite(suite, xml),
499 test_spec["suites"],
500 xml)
501
502 # Write XML to file.
503 with open(self.artifacts.sponge_xml_path, "w") as f:
504 ET.ElementTree(xml).write(f, encoding='utf-8', xml_declaration=True)
505
506 if result.tests_failed > 0:
507 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
508 "tests failed")
509 elif result.tests_run > 0:
510 print(" PASS: all", result.tests_run, "tests passed")
511
512 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100513
514
515def Main():
516 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000517 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100518 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100519 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100520 parser.add_argument("--out_initrd")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000521 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100522 parser.add_argument("--suite")
523 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000524 parser.add_argument("--vm_args")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100525 parser.add_argument("--fvp", action="store_true")
526 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100527 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100528
Andrew Scullbc7189d2018-08-14 09:35:13 +0100529 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000530 image = os.path.join(args.out, args.image + ".bin")
531 initrd = None
David Brazdil0dbb41f2019-09-09 18:03:35 +0100532 manifest = None
David Brazdil2df24082019-09-05 11:55:08 +0100533 image_name = args.image
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000534 if args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100535 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
536 initrd = os.path.join(initrd_dir, "initrd.img")
537 manifest = os.path.join(initrd_dir, "manifest.dtbo")
David Brazdil2df24082019-09-05 11:55:08 +0100538 image_name += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000539 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100540
541 # Create class which will manage all test artifacts.
542 artifacts = ArtifactsManager(os.path.join(args.log, image_name))
543
544 # Create a driver for the platform we want to test on.
David Brazdil0dbb41f2019-09-09 18:03:35 +0100545 driver_args = DriverArgs(artifacts, image, initrd, manifest, vm_args)
David Brazdil2df24082019-09-05 11:55:08 +0100546 if args.fvp:
547 driver = FvpDriver(driver_args)
548 else:
549 driver = QemuDriver(driver_args)
550
551 # Create class which will drive test execution.
David Brazdil3cc24aa2019-09-27 10:24:41 +0100552 runner = TestRunner(artifacts, driver, image_name, args.suite, args.test,
553 args.skip_long_running_tests)
David Brazdil2df24082019-09-05 11:55:08 +0100554
555 # Run tests.
556 runner_result = runner.run_tests()
557
558 # Print error message if no tests were run as this is probably unexpected.
559 # Return suitable error code.
560 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100561 print("Error: no tests match")
562 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100563 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100564 return 1
565 else:
David Brazdil2df24082019-09-05 11:55:08 +0100566 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100567
568if __name__ == "__main__":
569 sys.exit(Main())