blob: 4ee07098fab8f0a827aaa1ecbdf7aec172fedb7c [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")
44FVP_PREBUILT_DTS = os.path.join(
45 HF_ROOT, "prebuilts", "linux-aarch64", "arm-trusted-firmware",
46 "fvp-base-gicv3-psci-1t.dts")
Andrew Scull845fc9b2019-04-03 12:44:26 +010047
David Brazdil2df24082019-09-05 11:55:08 +010048def read_file(path):
49 with open(path, "r") as f:
50 return f.read()
Andrew Scull845fc9b2019-04-03 12:44:26 +010051
David Brazdil2df24082019-09-05 11:55:08 +010052def write_file(path, to_write, append=False):
53 with open(path, "a" if append else "w") as f:
54 f.write(to_write)
55
56def append_file(path, to_write):
57 write_file(path, to_write, append=True)
58
59def join_if_not_None(*args):
60 return " ".join(filter(lambda x: x, args))
61
62class ArtifactsManager:
63 """Class which manages folder with test artifacts."""
64
65 def __init__(self, log_dir):
66 self.created_files = []
67 self.log_dir = log_dir
68
69 # Create directory.
Andrew Scull845fc9b2019-04-03 12:44:26 +010070 try:
David Brazdil2df24082019-09-05 11:55:08 +010071 os.makedirs(self.log_dir)
72 except OSError:
73 if not os.path.isdir(self.log_dir):
74 raise
75 print("Logs saved under", log_dir)
76
77 # Create files expected by the Sponge test result parser.
78 self.sponge_log_path = self.create_file("sponge_log", ".log")
79 self.sponge_xml_path = self.create_file("sponge_log", ".xml")
80
David Brazdil623b6812019-09-09 11:41:08 +010081 def gen_file_path(self, basename, extension):
82 """Generate path to a file in the log directory."""
83 return os.path.join(self.log_dir, basename + extension)
84
David Brazdil2df24082019-09-05 11:55:08 +010085 def create_file(self, basename, extension):
86 """Create and touch a new file in the log folder. Ensure that no other
87 file of the same name was created by this instance of ArtifactsManager.
88 """
89 # Determine the path of the file.
David Brazdil623b6812019-09-09 11:41:08 +010090 path = self.gen_file_path(basename, extension)
David Brazdil2df24082019-09-05 11:55:08 +010091
92 # Check that the path is unique.
93 assert(path not in self.created_files)
94 self.created_files += [ path ]
95
96 # Touch file.
97 with open(path, "w") as f:
98 pass
99
100 return path
Andrew Scullbc7189d2018-08-14 09:35:13 +0100101
David Brazdil623b6812019-09-09 11:41:08 +0100102 def get_file(self, basename, extension):
103 """Return path to a file in the log folder. Assert that it was created
104 by this instance of ArtifactsManager."""
105 path = self.gen_file_path(basename, extension)
106 assert(path in self.created_files)
107 return path
108
Andrew Scullbc7189d2018-08-14 09:35:13 +0100109
David Brazdil2df24082019-09-05 11:55:08 +0100110# Tuple holding the arguments common to all driver constructors.
111# This is to avoid having to pass arguments from subclasses to superclasses.
112DriverArgs = collections.namedtuple("DriverArgs", [
113 "artifacts",
114 "kernel",
115 "initrd",
David Brazdil0dbb41f2019-09-09 18:03:35 +0100116 "manifest",
David Brazdil2df24082019-09-05 11:55:08 +0100117 "vm_args",
118 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100119
Andrew Walbran98656252019-03-14 14:52:29 +0000120
David Brazdil2df24082019-09-05 11:55:08 +0100121# State shared between the common Driver class and its subclasses during
122# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100123class DriverRunState:
124 def __init__(self, log_path):
125 self.log_path = log_path
126 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000127
David Brazdil7325eaf2019-09-27 13:04:51 +0100128 def set_ret_code(self, ret_code):
129 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000130
David Brazdil0dbb41f2019-09-09 18:03:35 +0100131class DriverRunException(Exception):
132 """Exception thrown if subprocess invoked by a driver returned non-zero
133 status code. Used to fast-exit from a driver command sequence."""
134 pass
135
136
David Brazdil2df24082019-09-05 11:55:08 +0100137class Driver:
138 """Parent class of drivers for all testable platforms."""
139
140 def __init__(self, args):
141 self.args = args
142
David Brazdil623b6812019-09-09 11:41:08 +0100143 def get_run_log(self, run_name):
144 """Return path to the main log of a given test run."""
145 return self.args.artifacts.get_file(run_name, ".log")
146
David Brazdil2df24082019-09-05 11:55:08 +0100147 def start_run(self, run_name):
148 """Hook called by Driver subclasses before they invoke the target
149 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100150 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100151
152 def exec_logged(self, run_state, exec_args):
153 """Run a subprocess on behalf of a Driver subclass and append its
154 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100155 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100156 with open(run_state.log_path, "a") as f:
157 f.write("$ {}\r\n".format(" ".join(exec_args)))
158 f.flush()
David Brazdil0dbb41f2019-09-09 18:03:35 +0100159 ret_code = subprocess.call(exec_args, stdout=f, stderr=f)
160 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100161 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100162 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100163
David Brazdil0dbb41f2019-09-09 18:03:35 +0100164 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100165 """Hook called by Driver subclasses after they finished running the
166 target platform. `ret_code` argument is the return code of the main
167 command run by the driver. A corresponding log message is printed."""
168 # Decode return code and add a message to the log.
169 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100170 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100171 f.write("\r\n{}{} timed out\r\n".format(
172 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100173 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100174 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100175 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
176 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100177
178 # Append log of this run to full test log.
179 log_content = read_file(run_state.log_path)
180 append_file(
181 self.args.artifacts.sponge_log_path,
182 log_content + "\r\n\r\n")
183 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000184
David Brazdil0dbb41f2019-09-09 18:03:35 +0100185 def overlay_dtb(self, run_state, base_dtb, overlay_dtb, out_dtb):
186 """Overlay `overlay_dtb` over `base_dtb` into `out_dtb`."""
187 dtc_args = [
188 DTC_SCRIPT, "overlay",
189 out_dtb, base_dtb, overlay_dtb,
190 ]
191 self.exec_logged(run_state, dtc_args)
192
Andrew Walbran98656252019-03-14 14:52:29 +0000193
David Brazdil2df24082019-09-05 11:55:08 +0100194class QemuDriver(Driver):
195 """Driver which runs tests in QEMU."""
196
197 def __init__(self, args):
198 Driver.__init__(self, args)
199
David Brazdil3cc24aa2019-09-27 10:24:41 +0100200 def gen_exec_args(self, test_args, is_long_running, dtb_path=None,
201 dumpdtb_path=None):
David Brazdil2df24082019-09-05 11:55:08 +0100202 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100203 time_limit = "120s" if is_long_running else "10s"
David Brazdil2df24082019-09-05 11:55:08 +0100204 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100205 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100206 "./prebuilts/linux-x64/qemu/qemu-system-aarch64",
Andrew Scull2925e422019-10-04 13:29:53 +0100207 "-machine", "virt,virtualization=on,gic_version=3",
David Brazdil2df24082019-09-05 11:55:08 +0100208 "-cpu", "cortex-a57", "-smp", "4", "-m", "64M",
David Brazdil2df24082019-09-05 11:55:08 +0100209 "-nographic", "-nodefaults", "-serial", "stdio",
Andrew Scull2925e422019-10-04 13:29:53 +0100210 "-d", "unimp", "-kernel", self.args.kernel,
David Brazdil2df24082019-09-05 11:55:08 +0100211 ]
212
David Brazdil0dbb41f2019-09-09 18:03:35 +0100213 if dtb_path:
214 exec_args += ["-dtb", dtb_path]
215
216 if dumpdtb_path:
217 exec_args += ["-machine", "dumpdtb=" + dumpdtb_path]
218
David Brazdil2df24082019-09-05 11:55:08 +0100219 if self.args.initrd:
220 exec_args += ["-initrd", self.args.initrd]
221
222 vm_args = join_if_not_None(self.args.vm_args, test_args)
223 if vm_args:
224 exec_args += ["-append", vm_args]
225
226 return exec_args
227
David Brazdil0dbb41f2019-09-09 18:03:35 +0100228 def dump_dtb(self, run_state, test_args, path):
David Brazdil3cc24aa2019-09-27 10:24:41 +0100229 dumpdtb_args = self.gen_exec_args(test_args, False, dumpdtb_path=path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100230 self.exec_logged(run_state, dumpdtb_args)
231
David Brazdil3cc24aa2019-09-27 10:24:41 +0100232 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100233 """Run test given by `test_args` in QEMU."""
234 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100235
236 try:
237 dtb_path = None
238
239 # If manifest DTBO specified, dump DTB from QEMU and overlay them.
240 if self.args.manifest:
241 base_dtb_path = self.args.artifacts.create_file(
242 run_name, ".base.dtb")
243 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
244 self.dump_dtb(run_state, test_args, base_dtb_path)
245 self.overlay_dtb(
246 run_state, base_dtb_path, self.args.manifest, dtb_path)
247
248 # Execute test in QEMU..
David Brazdil3cc24aa2019-09-27 10:24:41 +0100249 exec_args = self.gen_exec_args(test_args, is_long_running,
250 dtb_path=dtb_path)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100251 self.exec_logged(run_state, exec_args)
252 except DriverRunException:
253 pass
254
255 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100256
257
David Brazdil2df24082019-09-05 11:55:08 +0100258class FvpDriver(Driver):
259 """Driver which runs tests in ARM FVP emulator."""
260
261 def __init__(self, args):
262 Driver.__init__(self, args)
263
264 def gen_dts(self, dts_path, test_args, initrd_start, initrd_end):
265 """Create a DeviceTree source which will be compiled into a DTB and
266 passed to FVP for a test run."""
267 vm_args = join_if_not_None(self.args.vm_args, test_args)
268 write_file(dts_path, read_file(FVP_PREBUILT_DTS))
269 append_file(dts_path, """
270 / {{
271 chosen {{
272 bootargs = "{}";
273 stdout-path = "serial0:115200n8";
274 linux,initrd-start = <{}>;
275 linux,initrd-end = <{}>;
276 }};
277 }};
278 """.format(vm_args, initrd_start, initrd_end))
279
280 def gen_fvp_args(
281 self, initrd_start, uart0_log_path, uart1_log_path, dtb_path):
282 """Generate command line arguments for FVP."""
283 fvp_args = [
284 "timeout", "--foreground", "40s",
285 FVP_BINARY,
286 "-C", "pctl.startup=0.0.0.0",
287 "-C", "bp.secure_memory=0",
288 "-C", "cluster0.NUM_CORES=4",
289 "-C", "cluster1.NUM_CORES=4",
290 "-C", "cache_state_modelled=0",
291 "-C", "bp.vis.disable_visualisation=true",
292 "-C", "bp.vis.rate_limit-enable=false",
293 "-C", "bp.terminal_0.start_telnet=false",
294 "-C", "bp.terminal_1.start_telnet=false",
295 "-C", "bp.terminal_2.start_telnet=false",
296 "-C", "bp.terminal_3.start_telnet=false",
297 "-C", "bp.pl011_uart0.untimed_fifos=1",
298 "-C", "bp.pl011_uart0.unbuffered_output=1",
299 "-C", "bp.pl011_uart0.out_file=" + uart0_log_path,
300 "-C", "bp.pl011_uart1.out_file=" + uart1_log_path,
301 "-C", "cluster0.cpu0.RVBAR=0x04020000",
302 "-C", "cluster0.cpu1.RVBAR=0x04020000",
303 "-C", "cluster0.cpu2.RVBAR=0x04020000",
304 "-C", "cluster0.cpu3.RVBAR=0x04020000",
305 "-C", "cluster1.cpu0.RVBAR=0x04020000",
306 "-C", "cluster1.cpu1.RVBAR=0x04020000",
307 "-C", "cluster1.cpu2.RVBAR=0x04020000",
308 "-C", "cluster1.cpu3.RVBAR=0x04020000",
309 "--data", "cluster0.cpu0=prebuilts/linux-aarch64/arm-trusted-firmware/bl31.bin@0x04020000",
310 "--data", "cluster0.cpu0=" + dtb_path + "@0x82000000",
311 "--data", "cluster0.cpu0=" + self.args.kernel + "@0x80000000",
312 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
313 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
314 ]
315
316 if self.args.initrd:
317 fvp_args += [
318 "--data",
319 "cluster0.cpu0={}@{}".format(
320 self.args.initrd, hex(initrd_start))
321 ]
322
323 return fvp_args
324
David Brazdil3cc24aa2019-09-27 10:24:41 +0100325 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100326 run_state = self.start_run(run_name)
327
David Brazdil0dbb41f2019-09-09 18:03:35 +0100328 base_dts_path = self.args.artifacts.create_file(run_name, ".base.dts")
329 base_dtb_path = self.args.artifacts.create_file(run_name, ".base.dtb")
David Brazdil2df24082019-09-05 11:55:08 +0100330 dtb_path = self.args.artifacts.create_file(run_name, ".dtb")
331 uart0_log_path = self.args.artifacts.create_file(run_name, ".uart0.log")
332 uart1_log_path = self.args.artifacts.create_file(run_name, ".uart1.log")
333
334 initrd_start = 0x84000000
335 if self.args.initrd:
336 initrd_end = initrd_start + os.path.getsize(self.args.initrd)
337 else:
338 initrd_end = 0x85000000 # Default value
339
David Brazdil0dbb41f2019-09-09 18:03:35 +0100340 try:
341 # Create a DT to pass to FVP.
342 self.gen_dts(base_dts_path, test_args, initrd_start, initrd_end)
David Brazdil2df24082019-09-05 11:55:08 +0100343
David Brazdil0dbb41f2019-09-09 18:03:35 +0100344 # Compile DTS to DTB.
345 dtc_args = [
346 DTC_SCRIPT, "compile", "-i", base_dts_path, "-o", base_dtb_path,
347 ]
348 self.exec_logged(run_state, dtc_args)
349
350 # If manifest DTBO specified, overlay it.
351 if self.args.manifest:
352 self.overlay_dtb(
353 run_state, base_dtb_path, self.args.manifest, dtb_path)
354 else:
355 dtb_path = base_dtb_path
356
357 # Run FVP.
358 fvp_args = self.gen_fvp_args(
359 initrd_start, uart0_log_path, uart1_log_path, dtb_path)
360 self.exec_logged(run_state, fvp_args)
361 except DriverRunException:
362 pass
David Brazdil2df24082019-09-05 11:55:08 +0100363
364 # Append UART0 output to main log.
365 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100366 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100367
368
369# Tuple used to return information about the results of running a set of tests.
370TestRunnerResult = collections.namedtuple("TestRunnerResult", [
371 "tests_run",
372 "tests_failed",
373 ])
374
375
376class TestRunner:
377 """Class which communicates with a test platform to obtain a list of
378 available tests and driving their execution."""
379
David Brazdil3cc24aa2019-09-27 10:24:41 +0100380 def __init__(self, artifacts, driver, image_name, suite_regex, test_regex,
381 skip_long_running_tests):
David Brazdil2df24082019-09-05 11:55:08 +0100382 self.artifacts = artifacts
383 self.driver = driver
384 self.image_name = image_name
David Brazdil3cc24aa2019-09-27 10:24:41 +0100385 self.skip_long_running_tests = skip_long_running_tests
David Brazdil2df24082019-09-05 11:55:08 +0100386
387 self.suite_re = re.compile(suite_regex or ".*")
388 self.test_re = re.compile(test_regex or ".*")
389
390 def extract_hftest_lines(self, raw):
391 """Extract hftest-specific lines from a raw output from an invocation
392 of the test platform."""
393 lines = []
394 for line in raw.splitlines():
395 if line.startswith("VM "):
396 line = line[len("VM 0: "):]
397 if line.startswith(HFTEST_LOG_PREFIX):
398 lines.append(line[len(HFTEST_LOG_PREFIX):])
399 return lines
400
401 def get_test_json(self):
402 """Invoke the test platform and request a JSON of available test and
403 test suites."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100404 out = self.driver.run("json", "json", False)
David Brazdil2df24082019-09-05 11:55:08 +0100405 hf_out = "\n".join(self.extract_hftest_lines(out))
406 try:
407 return json.loads(hf_out)
408 except ValueError as e:
409 print(out)
410 raise e
411
412 def collect_results(self, fn, it, xml_node):
413 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
414 Insert "tests" and "failures" nodes to `xml_node`."""
415 tests_run = 0
416 tests_failed = 0
417 for i in it:
418 sub_result = fn(i)
419 assert(sub_result.tests_run >= sub_result.tests_failed)
420 tests_run += sub_result.tests_run
421 tests_failed += sub_result.tests_failed
422
423 xml_node.set("tests", str(tests_run))
424 xml_node.set("failures", str(tests_failed))
425 return TestRunnerResult(tests_run, tests_failed)
426
427 def is_passed_test(self, test_out):
428 """Parse the output of a test and return True if it passed."""
429 return \
430 len(test_out) > 0 and \
431 test_out[-1] == HFTEST_LOG_FINISHED and \
432 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
433
434 def run_test(self, suite, test, suite_xml):
435 """Invoke the test platform and request to run a given `test` in given
436 `suite`. Create a new XML node with results under `suite_xml`.
437 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100438 if not self.test_re.match(test["name"]):
David Brazdil2df24082019-09-05 11:55:08 +0100439 return TestRunnerResult(tests_run=0, tests_failed=0)
440
David Brazdil3cc24aa2019-09-27 10:24:41 +0100441 if self.skip_long_running_tests and test["is_long_running"]:
442 print(" SKIP", test["name"])
443 return TestRunnerResult(tests_run=0, tests_failed=0)
444
445 print(" RUN", test["name"])
446 log_name = suite["name"] + "." + test["name"]
David Brazdil2df24082019-09-05 11:55:08 +0100447
448 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100449 test_xml.set("name", test["name"])
450 test_xml.set("classname", suite["name"])
David Brazdil2df24082019-09-05 11:55:08 +0100451 test_xml.set("status", "run")
452
453 out = self.extract_hftest_lines(self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100454 log_name, "run {} {}".format(suite["name"], test["name"]),
455 test["is_long_running"]))
David Brazdil2df24082019-09-05 11:55:08 +0100456
457 if self.is_passed_test(out):
458 print(" PASS")
459 return TestRunnerResult(tests_run=1, tests_failed=0)
460 else:
David Brazdil623b6812019-09-09 11:41:08 +0100461 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100462 failure_xml = ET.SubElement(test_xml, "failure")
463 # TODO: set a meaningful message and put log in CDATA
464 failure_xml.set("message", "Test failed")
465 return TestRunnerResult(tests_run=1, tests_failed=1)
466
467 def run_suite(self, suite, xml):
468 """Invoke the test platform and request to run all matching tests in
469 `suite`. Create new XML nodes with results under `xml`.
470 Suite skipped if it does not match the regex given to constructor."""
471 if not self.suite_re.match(suite["name"]):
472 return TestRunnerResult(tests_run=0, tests_failed=0)
473
474 print(" SUITE", suite["name"])
475 suite_xml = ET.SubElement(xml, "testsuite")
476 suite_xml.set("name", suite["name"])
477
478 return self.collect_results(
479 lambda test: self.run_test(suite, test, suite_xml),
480 suite["tests"],
481 suite_xml)
482
483 def run_tests(self):
484 """Run all suites and tests matching regexes given to constructor.
485 Write results to sponge log XML. Return the number of run and failed
486 tests."""
487
488 test_spec = self.get_test_json()
489 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
490
491 xml = ET.Element("testsuites")
492 xml.set("name", self.image_name)
493 xml.set("timestamp", timestamp)
494
495 result = self.collect_results(
496 lambda suite: self.run_suite(suite, xml),
497 test_spec["suites"],
498 xml)
499
500 # Write XML to file.
501 with open(self.artifacts.sponge_xml_path, "w") as f:
502 ET.ElementTree(xml).write(f, encoding='utf-8', xml_declaration=True)
503
504 if result.tests_failed > 0:
505 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
506 "tests failed")
507 elif result.tests_run > 0:
508 print(" PASS: all", result.tests_run, "tests passed")
509
510 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100511
512
513def Main():
514 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000515 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100516 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100517 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100518 parser.add_argument("--out_initrd")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000519 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100520 parser.add_argument("--suite")
521 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000522 parser.add_argument("--vm_args")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100523 parser.add_argument("--fvp", action="store_true")
524 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100525 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100526
Andrew Scullbc7189d2018-08-14 09:35:13 +0100527 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000528 image = os.path.join(args.out, args.image + ".bin")
529 initrd = None
David Brazdil0dbb41f2019-09-09 18:03:35 +0100530 manifest = None
David Brazdil2df24082019-09-05 11:55:08 +0100531 image_name = args.image
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000532 if args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100533 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
534 initrd = os.path.join(initrd_dir, "initrd.img")
535 manifest = os.path.join(initrd_dir, "manifest.dtbo")
David Brazdil2df24082019-09-05 11:55:08 +0100536 image_name += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000537 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100538
539 # Create class which will manage all test artifacts.
540 artifacts = ArtifactsManager(os.path.join(args.log, image_name))
541
542 # Create a driver for the platform we want to test on.
David Brazdil0dbb41f2019-09-09 18:03:35 +0100543 driver_args = DriverArgs(artifacts, image, initrd, manifest, vm_args)
David Brazdil2df24082019-09-05 11:55:08 +0100544 if args.fvp:
545 driver = FvpDriver(driver_args)
546 else:
547 driver = QemuDriver(driver_args)
548
549 # Create class which will drive test execution.
David Brazdil3cc24aa2019-09-27 10:24:41 +0100550 runner = TestRunner(artifacts, driver, image_name, args.suite, args.test,
551 args.skip_long_running_tests)
David Brazdil2df24082019-09-05 11:55:08 +0100552
553 # Run tests.
554 runner_result = runner.run_tests()
555
556 # Print error message if no tests were run as this is probably unexpected.
557 # Return suitable error code.
558 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100559 print("Error: no tests match")
560 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100561 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100562 return 1
563 else:
David Brazdil2df24082019-09-05 11:55:08 +0100564 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100565
566if __name__ == "__main__":
567 sys.exit(Main())