blob: ce30a7f58c652a0e4503415c9d4de909c173ae34 [file] [log] [blame]
David Brazdilee5e25d2020-01-24 14:17:45 +00001#!/usr/bin/env python3
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#
Andrew Walbrane959ec12020-06-17 15:01:09 +01005# Use of this source code is governed by a BSD-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01008
David Brazdil2df24082019-09-05 11:55:08 +01009"""Script which drives invocation of tests and parsing their output to produce
10a results report.
Andrew Scullbc7189d2018-08-14 09:35:13 +010011"""
12
13from __future__ import print_function
14
Andrew Scull3b62f2b2018-08-21 14:26:12 +010015import xml.etree.ElementTree as ET
16
Andrew Scullbc7189d2018-08-14 09:35:13 +010017import argparse
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010018from abc import ABC, abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +010019import collections
Andrew Scull04502e42018-09-03 14:54:52 +010020import datetime
David Brazdil4f9cf9a2020-02-06 17:34:44 +000021import importlib
Andrew Scullbc7189d2018-08-14 09:35:13 +010022import json
23import os
24import re
25import subprocess
26import sys
Andrew Walbran42bf2842020-06-05 18:50:19 +010027import time
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010028import fdt
Andrew Scullbc7189d2018-08-14 09:35:13 +010029
Andrew Scull845fc9b2019-04-03 12:44:26 +010030HFTEST_LOG_PREFIX = "[hftest] "
31HFTEST_LOG_FAILURE_PREFIX = "Failure:"
32HFTEST_LOG_FINISHED = "FINISHED"
33
David Brazdil17e76652020-01-29 14:44:19 +000034HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]"
35HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]"
36
David Brazdil2df24082019-09-05 11:55:08 +010037HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
38 os.path.abspath(__file__))))
David Brazdil5715f042019-08-27 11:11:51 +010039DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py")
David Brazdil2df24082019-09-05 11:55:08 +010040FVP_BINARY = os.path.join(
41 os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMv8A_pkg", "models",
Olivier Depreze4153042020-10-02 15:24:59 +020042 "Linux64_GCC-6.4", "FVP_Base_RevC-2xAEMv8A")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010043HF_PREBUILTS = os.path.join(HF_ROOT, "prebuilts")
44FVP_PREBUILTS_TFA_TRUSTY_ROOT = os.path.join(
45 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", "fvp")
David Brazdil2df24082019-09-05 11:55:08 +010046FVP_PREBUILT_DTS = os.path.join(
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010047 FVP_PREBUILTS_TFA_TRUSTY_ROOT, "fvp-base-gicv3-psci-1t.dts")
48
49FVP_PREBUILT_TFA_ROOT = os.path.join(
50 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a", "fvp")
Andrew Scull845fc9b2019-04-03 12:44:26 +010051
J-Alves852fe742021-04-22 11:59:55 +010052VM_NODE_REGEX = "vm[1-9]"
53
David Brazdil2df24082019-09-05 11:55:08 +010054def read_file(path):
55 with open(path, "r") as f:
56 return f.read()
Andrew Scull845fc9b2019-04-03 12:44:26 +010057
David Brazdil2df24082019-09-05 11:55:08 +010058def write_file(path, to_write, append=False):
59 with open(path, "a" if append else "w") as f:
60 f.write(to_write)
61
62def append_file(path, to_write):
63 write_file(path, to_write, append=True)
64
65def join_if_not_None(*args):
66 return " ".join(filter(lambda x: x, args))
67
J-Alves852fe742021-04-22 11:59:55 +010068def get_vm_node_from_manifest(dts : str):
69 """ Get VM node string from Partition's extension to Partition Manager's
70 manifest."""
71 match = re.search(VM_NODE_REGEX, dts)
72 if not match:
73 raise Exception("Partition's node is not defined in its manifest.")
74 return match.group()
75
76def correct_vm_node(dts: str, node_index : int):
77 """ The vm node is being appended to the Partition Manager manifests.
78 Ideally, these files would be reused accross various test set-ups."""
79 return dts.replace(get_vm_node_from_manifest(dts), f"vm{node_index}")
80
J-Alves8cc7dbb2021-04-16 10:38:48 +010081DT = collections.namedtuple("DT", ["dts", "dtb"])
82
David Brazdil2df24082019-09-05 11:55:08 +010083class ArtifactsManager:
84 """Class which manages folder with test artifacts."""
85
86 def __init__(self, log_dir):
87 self.created_files = []
88 self.log_dir = log_dir
89
90 # Create directory.
Andrew Scull845fc9b2019-04-03 12:44:26 +010091 try:
David Brazdil2df24082019-09-05 11:55:08 +010092 os.makedirs(self.log_dir)
93 except OSError:
94 if not os.path.isdir(self.log_dir):
95 raise
96 print("Logs saved under", log_dir)
97
98 # Create files expected by the Sponge test result parser.
99 self.sponge_log_path = self.create_file("sponge_log", ".log")
100 self.sponge_xml_path = self.create_file("sponge_log", ".xml")
101
David Brazdil623b6812019-09-09 11:41:08 +0100102 def gen_file_path(self, basename, extension):
103 """Generate path to a file in the log directory."""
104 return os.path.join(self.log_dir, basename + extension)
105
David Brazdil2df24082019-09-05 11:55:08 +0100106 def create_file(self, basename, extension):
107 """Create and touch a new file in the log folder. Ensure that no other
108 file of the same name was created by this instance of ArtifactsManager.
109 """
110 # Determine the path of the file.
David Brazdil623b6812019-09-09 11:41:08 +0100111 path = self.gen_file_path(basename, extension)
David Brazdil2df24082019-09-05 11:55:08 +0100112
113 # Check that the path is unique.
114 assert(path not in self.created_files)
115 self.created_files += [ path ]
116
117 # Touch file.
118 with open(path, "w") as f:
119 pass
120
121 return path
Andrew Scullbc7189d2018-08-14 09:35:13 +0100122
David Brazdil623b6812019-09-09 11:41:08 +0100123 def get_file(self, basename, extension):
124 """Return path to a file in the log folder. Assert that it was created
125 by this instance of ArtifactsManager."""
126 path = self.gen_file_path(basename, extension)
127 assert(path in self.created_files)
128 return path
129
Andrew Scullbc7189d2018-08-14 09:35:13 +0100130
David Brazdil2df24082019-09-05 11:55:08 +0100131# Tuple holding the arguments common to all driver constructors.
132# This is to avoid having to pass arguments from subclasses to superclasses.
133DriverArgs = collections.namedtuple("DriverArgs", [
134 "artifacts",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100135 "hypervisor",
136 "spmc",
David Brazdil2df24082019-09-05 11:55:08 +0100137 "initrd",
138 "vm_args",
David Brazdil17e76652020-01-29 14:44:19 +0000139 "cpu",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100140 "partitions"
David Brazdil2df24082019-09-05 11:55:08 +0100141 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100142
David Brazdil2df24082019-09-05 11:55:08 +0100143# State shared between the common Driver class and its subclasses during
144# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100145class DriverRunState:
146 def __init__(self, log_path):
147 self.log_path = log_path
148 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000149
David Brazdil7325eaf2019-09-27 13:04:51 +0100150 def set_ret_code(self, ret_code):
151 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000152
David Brazdil0dbb41f2019-09-09 18:03:35 +0100153class DriverRunException(Exception):
154 """Exception thrown if subprocess invoked by a driver returned non-zero
155 status code. Used to fast-exit from a driver command sequence."""
156 pass
157
158
David Brazdil2df24082019-09-05 11:55:08 +0100159class Driver:
160 """Parent class of drivers for all testable platforms."""
161
162 def __init__(self, args):
163 self.args = args
164
David Brazdil623b6812019-09-09 11:41:08 +0100165 def get_run_log(self, run_name):
166 """Return path to the main log of a given test run."""
167 return self.args.artifacts.get_file(run_name, ".log")
168
David Brazdil2df24082019-09-05 11:55:08 +0100169 def start_run(self, run_name):
170 """Hook called by Driver subclasses before they invoke the target
171 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100172 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100173
Andrew Walbranf636b842020-01-10 11:46:12 +0000174 def exec_logged(self, run_state, exec_args, cwd=None):
David Brazdil2df24082019-09-05 11:55:08 +0100175 """Run a subprocess on behalf of a Driver subclass and append its
176 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100177 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100178 with open(run_state.log_path, "a") as f:
179 f.write("$ {}\r\n".format(" ".join(exec_args)))
180 f.flush()
Andrew Walbranf636b842020-01-10 11:46:12 +0000181 ret_code = subprocess.call(exec_args, stdout=f, stderr=f, cwd=cwd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100182 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100183 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100184 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100185
David Brazdil0dbb41f2019-09-09 18:03:35 +0100186 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100187 """Hook called by Driver subclasses after they finished running the
188 target platform. `ret_code` argument is the return code of the main
189 command run by the driver. A corresponding log message is printed."""
190 # Decode return code and add a message to the log.
191 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100192 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100193 f.write("\r\n{}{} timed out\r\n".format(
194 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100195 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100196 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100197 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
198 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100199
200 # Append log of this run to full test log.
201 log_content = read_file(run_state.log_path)
202 append_file(
203 self.args.artifacts.sponge_log_path,
204 log_content + "\r\n\r\n")
205 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000206
207
David Brazdil2df24082019-09-05 11:55:08 +0100208class QemuDriver(Driver):
209 """Driver which runs tests in QEMU."""
210
Andrew Walbranf636b842020-01-10 11:46:12 +0000211 def __init__(self, args, qemu_wd, tfa):
David Brazdil2df24082019-09-05 11:55:08 +0100212 Driver.__init__(self, args)
Andrew Walbranf636b842020-01-10 11:46:12 +0000213 self.qemu_wd = qemu_wd
214 self.tfa = tfa
David Brazdil2df24082019-09-05 11:55:08 +0100215
David Brazdila2358d42020-01-27 18:51:38 +0000216 def gen_exec_args(self, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100217 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100218 time_limit = "120s" if is_long_running else "10s"
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000219 # If no CPU configuration is selected, then test against the maximum
220 # configuration, "max", supported by QEMU.
221 cpu = self.args.cpu or "max"
David Brazdil2df24082019-09-05 11:55:08 +0100222 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100223 "timeout", "--foreground", time_limit,
Andrew Walbranf636b842020-01-10 11:46:12 +0000224 os.path.abspath("prebuilts/linux-x64/qemu/qemu-system-aarch64"),
Andrew Walbrana081a292020-01-23 10:08:42 +0000225 "-machine", "virt,virtualization=on,gic-version=3",
Andrew Walbranf636b842020-01-10 11:46:12 +0000226 "-cpu", cpu, "-smp", "4", "-m", "1G",
David Brazdil2df24082019-09-05 11:55:08 +0100227 "-nographic", "-nodefaults", "-serial", "stdio",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100228 "-d", "unimp", "-kernel", os.path.abspath(self.args.hypervisor),
David Brazdil2df24082019-09-05 11:55:08 +0100229 ]
230
Andrew Walbranf636b842020-01-10 11:46:12 +0000231 if self.tfa:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100232 bl1_path = os.path.join(
233 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty",
234 "qemu", "bl1.bin")
Andrew Walbranf636b842020-01-10 11:46:12 +0000235 exec_args += ["-bios",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100236 os.path.abspath(bl1_path),
237 "-machine", "secure=on", "-semihosting-config",
Andrew Walbranf636b842020-01-10 11:46:12 +0000238 "enable,target=native"]
239
David Brazdil2df24082019-09-05 11:55:08 +0100240 if self.args.initrd:
Andrew Walbranf636b842020-01-10 11:46:12 +0000241 exec_args += ["-initrd", os.path.abspath(self.args.initrd)]
David Brazdil2df24082019-09-05 11:55:08 +0100242
243 vm_args = join_if_not_None(self.args.vm_args, test_args)
244 if vm_args:
245 exec_args += ["-append", vm_args]
246
247 return exec_args
248
David Brazdil3cc24aa2019-09-27 10:24:41 +0100249 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100250 """Run test given by `test_args` in QEMU."""
251 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100252
253 try:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100254 # Execute test in QEMU..
David Brazdila2358d42020-01-27 18:51:38 +0000255 exec_args = self.gen_exec_args(test_args, is_long_running)
Andrew Walbranf636b842020-01-10 11:46:12 +0000256 self.exec_logged(run_state, exec_args,
257 cwd=self.qemu_wd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100258 except DriverRunException:
259 pass
260
261 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100262
David Brazdil94fd1e92020-02-03 16:45:20 +0000263 def finish(self):
264 """Clean up after running tests."""
265 pass
266
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100267class FvpDriver(Driver, ABC):
268 """Base class for driver which runs tests in Arm FVP emulator."""
David Brazdil2df24082019-09-05 11:55:08 +0100269
270 def __init__(self, args):
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000271 if args.cpu:
272 raise ValueError("FVP emulator does not support the --cpu option.")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100273 super().__init__(args)
David Brazdil2df24082019-09-05 11:55:08 +0100274
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100275 @property
276 @abstractmethod
277 def CPU_START_ADDRESS(self):
278 pass
David Brazdil2df24082019-09-05 11:55:08 +0100279
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100280 @property
281 @abstractmethod
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100282 def FVP_PREBUILT_BL31(self):
283 pass
284
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100285 def create_dt(self, run_name : str):
286 """Create DT related files, and return respective paths in a tuple
287 (dts,dtb)"""
J-Alves8cc7dbb2021-04-16 10:38:48 +0100288 return DT(self.args.artifacts.create_file(run_name, ".dts"),
289 self.args.artifacts.create_file(run_name, ".dtb"))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100290
J-Alves8cc7dbb2021-04-16 10:38:48 +0100291 def compile_dt(self, run_state, dt : DT):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100292 """Compile DT calling dtc."""
293 dtc_args = [
J-Alves8cc7dbb2021-04-16 10:38:48 +0100294 DTC_SCRIPT, "compile", "-i", dt.dts, "-o", dt.dtb,
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100295 ]
296 self.exec_logged(run_state, dtc_args)
297
298 def create_uart_log(self, run_name : str, file_name : str):
299 """Create uart log file, and return path"""
300 return self.args.artifacts.create_file(run_name, file_name)
301
302 def get_img_and_ldadd(self, partitions : dict):
303 ret = []
304 for i, p in enumerate(partitions):
J-Alves852fe742021-04-22 11:59:55 +0100305 with open(p["dts"], "r") as dt:
306 dts = dt.read()
307 manifest = fdt.parse_dts(dts)
308 vm_node = get_vm_node_from_manifest(dts)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100309 load_address = manifest.get_property("load_address",
J-Alves852fe742021-04-22 11:59:55 +0100310 f"/hypervisor/{vm_node}").value
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100311 ret.append((p["img"], load_address))
312 return ret
313
314 def get_manifests_from_json(self, partitions : list):
315 manifests = ""
316 if partitions is not None:
J-Alves852fe742021-04-22 11:59:55 +0100317 for i, p in enumerate(partitions):
318 manifests += correct_vm_node(read_file(p["dts"]), i + 1)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100319 return manifests
320
321 @abstractmethod
J-Alves8cc7dbb2021-04-16 10:38:48 +0100322 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100323 """Abstract method to generate dts file. This specific to the use case
324 so should be implemented within derived driver"""
325 pass
326
327 @abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +0100328 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100329 self, is_long_running, uart0_log_path, uart1_log_path, dt):
David Brazdil2df24082019-09-05 11:55:08 +0100330 """Generate command line arguments for FVP."""
Andrew Walbranee5418e2019-11-27 17:43:05 +0000331 time_limit = "80s" if is_long_running else "40s"
David Brazdil2df24082019-09-05 11:55:08 +0100332 fvp_args = [
Andrew Walbranee5418e2019-11-27 17:43:05 +0000333 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100334 FVP_BINARY,
J-Alves10446d82021-04-26 11:52:57 +0100335 "-C", "pci.pci_smmuv3.mmu.SMMU_AIDR=2",
336 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR0=0x0046123B",
337 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR1=0x00600002",
338 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR3=0x1714",
339 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR5=0xFFFF0472",
340 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR1=0xA0000002",
341 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR2=0",
342 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR3=0",
David Brazdil2df24082019-09-05 11:55:08 +0100343 "-C", "pctl.startup=0.0.0.0",
344 "-C", "bp.secure_memory=0",
345 "-C", "cluster0.NUM_CORES=4",
346 "-C", "cluster1.NUM_CORES=4",
347 "-C", "cache_state_modelled=0",
348 "-C", "bp.vis.disable_visualisation=true",
349 "-C", "bp.vis.rate_limit-enable=false",
350 "-C", "bp.terminal_0.start_telnet=false",
351 "-C", "bp.terminal_1.start_telnet=false",
352 "-C", "bp.terminal_2.start_telnet=false",
353 "-C", "bp.terminal_3.start_telnet=false",
354 "-C", "bp.pl011_uart0.untimed_fifos=1",
355 "-C", "bp.pl011_uart0.unbuffered_output=1",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100356 "-C", f"bp.pl011_uart0.out_file={uart0_log_path}",
357 "-C", f"bp.pl011_uart1.out_file={uart1_log_path}",
358 "-C", f"cluster0.cpu0.RVBAR={self.CPU_START_ADDRESS}",
359 "-C", f"cluster0.cpu1.RVBAR={self.CPU_START_ADDRESS}",
360 "-C", f"cluster0.cpu2.RVBAR={self.CPU_START_ADDRESS}",
361 "-C", f"cluster0.cpu3.RVBAR={self.CPU_START_ADDRESS}",
362 "-C", f"cluster1.cpu0.RVBAR={self.CPU_START_ADDRESS}",
363 "-C", f"cluster1.cpu1.RVBAR={self.CPU_START_ADDRESS}",
364 "-C", f"cluster1.cpu2.RVBAR={self.CPU_START_ADDRESS}",
365 "-C", f"cluster1.cpu3.RVBAR={self.CPU_START_ADDRESS}",
366 "--data",
367 f"cluster0.cpu0={self.FVP_PREBUILT_BL31}@{self.CPU_START_ADDRESS}",
David Brazdil2df24082019-09-05 11:55:08 +0100368 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
369 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
370 ]
David Brazdil2df24082019-09-05 11:55:08 +0100371 return fvp_args
372
David Brazdil3cc24aa2019-09-27 10:24:41 +0100373 def run(self, run_name, test_args, is_long_running):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100374 """ Run test """
David Brazdil2df24082019-09-05 11:55:08 +0100375 run_state = self.start_run(run_name)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100376 dt = self.create_dt(run_name)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100377 uart0_log_path = self.create_uart_log(run_name, ".uart0.log")
378 uart1_log_path = self.create_uart_log(run_name, ".uart1.log")
David Brazdil2df24082019-09-05 11:55:08 +0100379
David Brazdil0dbb41f2019-09-09 18:03:35 +0100380 try:
J-Alves8cc7dbb2021-04-16 10:38:48 +0100381 self.gen_dts(dt, test_args)
382 self.compile_dt(run_state, dt)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100383 fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path,
J-Alves8cc7dbb2021-04-16 10:38:48 +0100384 uart1_log_path, dt)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100385 self.exec_logged(run_state, fvp_args)
386 except DriverRunException:
387 pass
David Brazdil2df24082019-09-05 11:55:08 +0100388
389 # Append UART0 output to main log.
390 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100391 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100392
David Brazdil94fd1e92020-02-03 16:45:20 +0000393 def finish(self):
394 """Clean up after running tests."""
395 pass
396
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100397class FvpDriverHypervisor(FvpDriver):
398 """
399 Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor
400 """
401 INITRD_START= 0x84000000
Olivier Depreza516f482021-04-30 18:47:59 +0200402 INITRD_END = 0x86000000 #Default value, however may change if initrd in args
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100403
404 def __init__(self, args):
405 self.vms_in_partitions_json = args.partitions and args.partitions["VMs"]
406 super().__init__(args)
407
408 @property
409 def CPU_START_ADDRESS(self):
410 return "0x04020000"
411
412 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100413 def FVP_PREBUILT_BL31(self):
414 return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin")
415
416 @property
J-Alves38223dd2021-04-20 17:31:48 +0100417 def HYPERVISOR_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100418 return "0x80000000"
419
J-Alves38223dd2021-04-20 17:31:48 +0100420 @property
421 def HYPERVISOR_DTB_ADDRESS(self):
422 return "0x82000000"
423
J-Alves8cc7dbb2021-04-16 10:38:48 +0100424 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100425 """Create a DeviceTree source which will be compiled into a DTB and
426 passed to FVP for a test run."""
427
428 vm_args = join_if_not_None(self.args.vm_args, test_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100429 write_file(dt.dts, read_file(FVP_PREBUILT_DTS))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100430
431 # Write the vm arguments to the partition manifest
432 to_append = f"""
433/ {{
434 chosen {{
435 bootargs = "{vm_args}";
436 stdout-path = "serial0:115200n8";
437 linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>;
438 linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>;
439 }};
440}};"""
441 if self.vms_in_partitions_json:
442 to_append += self.get_manifests_from_json(self.args.partitions["VMs"])
443
J-Alves8cc7dbb2021-04-16 10:38:48 +0100444 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100445
446 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100447 self, is_long_running, uart0_log_path, uart1_log_path, dt, call_super = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100448 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100449 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt)
450 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100451
J-Alves8cc7dbb2021-04-16 10:38:48 +0100452 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100453 "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}",
454 "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100455 ]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100456
457 if self.vms_in_partitions_json:
458 img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"])
459 for img, ldadd in img_ldadd:
460 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
461
462 if self.args.initrd:
463 fvp_args += [
464 "--data",
465 f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}"
466 ]
467 return fvp_args
468
469class FvpDriverSPMC(FvpDriver):
470 """
471 Driver which runs tests in Arm FVP emulator, with hafnium as SPMC
472 """
473 FVP_PREBUILT_SECURE_DTS = os.path.join(
474 HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts")
475 HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds")
476
477 def __init__(self, args):
478 if args.partitions is None or args.partitions["SPs"] is None:
J-Alves10446d82021-04-26 11:52:57 +0100479 raise Exception("Need to provide SPs in partitions_json")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100480 super().__init__(args)
481
482 @property
483 def CPU_START_ADDRESS(self):
484 return "0x04010000"
485
486 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100487 def FVP_PREBUILT_BL31(self):
488 return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")
489
490 @property
J-Alves38223dd2021-04-20 17:31:48 +0100491 def SPMC_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100492 return "0x6000000"
493
J-Alves38223dd2021-04-20 17:31:48 +0100494 @property
495 def SPMC_DTB_ADDRESS(self):
496 return "0x0403f000"
497
J-Alves8cc7dbb2021-04-16 10:38:48 +0100498 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100499 """Create a DeviceTree source which will be compiled into a DTB and
500 passed to FVP for a test run."""
501 to_append = self.get_manifests_from_json(self.args.partitions["SPs"])
J-Alves8cc7dbb2021-04-16 10:38:48 +0100502 write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS))
503 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100504
505 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100506 self, is_long_running, uart0_log_path, uart1_log_path, dt,
J-Alves38223dd2021-04-20 17:31:48 +0100507 call_super = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100508 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100509 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb)
510 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
J-Alves8cc7dbb2021-04-16 10:38:48 +0100511
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100512 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100513 "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}",
514 "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}",
515 "-C", "cluster0.has_arm_v8-5=1",
516 "-C", "cluster1.has_arm_v8-5=1",
517 "-C", "cluster0.has_branch_target_exception=1",
518 "-C", "cluster1.has_branch_target_exception=1",
519 "-C", "cluster0.restriction_on_speculative_execution=2",
520 "-C", "cluster1.restriction_on_speculative_execution=2",
521 "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}",
522 "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100523 ]
524
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100525 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
526 for img, ldadd in img_ldadd:
527 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
528
529 return fvp_args
530
531 def run(self, run_name, test_args, is_long_running):
532 with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f:
533 vm_args = join_if_not_None(self.args.vm_args, test_args)
534 f.write(f"{vm_args}\n")
535 return super().run(run_name, test_args, is_long_running)
536
537 def finish(self):
538 """Clean up after running tests."""
539 os.remove(FvpDriverSPMC.HFTEST_CMD_FILE)
David Brazdil2df24082019-09-05 11:55:08 +0100540
J-Alves38223dd2021-04-20 17:31:48 +0100541class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC):
542 def __init__(self, args):
543 FvpDriverHypervisor.__init__(self, args)
544 FvpDriverSPMC.__init__(self, args)
545
546 @property
547 def CPU_START_ADDRESS(self):
548 return str(0x04010000)
549
550 @property
551 def FVP_PREBUILT_BL31(self):
552 return str(os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin"))
553
554 def create_dt(self, run_name):
555 dt = dict()
556 dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor")
557 dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc")
558 return dt
559
560 @property
561 def HYPERVISOR_ADDRESS(self):
562 return "0x88000000"
563
564 @property
565 def HYPERVISOR_DTB_ADDRESS(self):
566 return "0x80000000"
567
568 def compile_dt(self, run_state, dt):
569 FvpDriver.compile_dt(self, run_state, dt["hypervisor"])
570 FvpDriver.compile_dt(self, run_state, dt["spmc"])
571
572 def gen_dts(self, dt, test_args):
573 FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args)
574 FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args)
575
576 def gen_fvp_args(
577 self, is_long_running, uart0_log_path, uart1_log_path, dt):
578 """Generate command line arguments for FVP."""
579 common_args = (self, is_long_running, uart0_log_path, uart1_log_path)
580 fvp_args = FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"])
581 fvp_args += FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"], False)
582 return fvp_args
583
584 def finish(self):
585 """Clean up after running tests."""
586 pass
587
David Brazdil17e76652020-01-29 14:44:19 +0000588class SerialDriver(Driver):
589 """Driver which communicates with a device over the serial port."""
590
David Brazdil9d4ed962020-02-06 17:23:48 +0000591 def __init__(self, args, tty_file, baudrate, init_wait):
David Brazdil17e76652020-01-29 14:44:19 +0000592 Driver.__init__(self, args)
David Brazdil9d4ed962020-02-06 17:23:48 +0000593 self.tty_file = tty_file
594 self.baudrate = baudrate
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000595 self.pyserial = importlib.import_module("serial")
David Brazdild8013f92020-02-03 16:40:25 +0000596
David Brazdil9d4ed962020-02-06 17:23:48 +0000597 if init_wait:
David Brazdild8013f92020-02-03 16:40:25 +0000598 input("Press ENTER and then reset the device...")
David Brazdil17e76652020-01-29 14:44:19 +0000599
David Brazdil9d4ed962020-02-06 17:23:48 +0000600 def connect(self):
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000601 return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10)
David Brazdil9d4ed962020-02-06 17:23:48 +0000602
David Brazdil17e76652020-01-29 14:44:19 +0000603 def run(self, run_name, test_args, is_long_running):
604 """Communicate `test_args` to the device over the serial port."""
605 run_state = self.start_run(run_name)
606
David Brazdil9d4ed962020-02-06 17:23:48 +0000607 with self.connect() as ser:
David Brazdil17e76652020-01-29 14:44:19 +0000608 with open(run_state.log_path, "a") as f:
609 while True:
610 # Read one line from the serial port.
611 line = ser.readline().decode('utf-8')
612 if len(line) == 0:
613 # Timeout
614 run_state.set_ret_code(124)
615 input("Timeout. " +
616 "Press ENTER and then reset the device...")
617 break
618 # Write the line to the log file.
619 f.write(line)
620 if HFTEST_CTRL_GET_COMMAND_LINE in line:
621 # Device is waiting for `test_args`.
622 ser.write(test_args.encode('ascii'))
623 ser.write(b'\r')
624 elif HFTEST_CTRL_FINISHED in line:
625 # Device has finished running this test and will reboot.
626 break
627 return self.finish_run(run_state)
628
David Brazdil94fd1e92020-02-03 16:45:20 +0000629 def finish(self):
630 """Clean up after running tests."""
David Brazdil9d4ed962020-02-06 17:23:48 +0000631 with self.connect() as ser:
David Brazdil94fd1e92020-02-03 16:45:20 +0000632 while True:
633 line = ser.readline().decode('utf-8')
634 if len(line) == 0:
635 input("Timeout. Press ENTER and then reset the device...")
636 elif HFTEST_CTRL_GET_COMMAND_LINE in line:
637 # Device is waiting for a command. Instruct it to exit
638 # the test environment.
639 ser.write("exit".encode('ascii'))
640 ser.write(b'\r')
641 break
642
David Brazdil2df24082019-09-05 11:55:08 +0100643# Tuple used to return information about the results of running a set of tests.
644TestRunnerResult = collections.namedtuple("TestRunnerResult", [
645 "tests_run",
646 "tests_failed",
Andrew Walbranf9463922020-06-05 16:44:42 +0100647 "tests_skipped",
David Brazdil2df24082019-09-05 11:55:08 +0100648 ])
649
David Brazdil2df24082019-09-05 11:55:08 +0100650class TestRunner:
651 """Class which communicates with a test platform to obtain a list of
652 available tests and driving their execution."""
653
J-Alves8cc7dbb2021-04-16 10:38:48 +0100654 def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100655 skip_long_running_tests, force_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100656 self.artifacts = artifacts
657 self.driver = driver
J-Alves8cc7dbb2021-04-16 10:38:48 +0100658 self.test_set_up = test_set_up
David Brazdil3cc24aa2019-09-27 10:24:41 +0100659 self.skip_long_running_tests = skip_long_running_tests
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100660 self.force_long_running = force_long_running
David Brazdil2df24082019-09-05 11:55:08 +0100661
662 self.suite_re = re.compile(suite_regex or ".*")
663 self.test_re = re.compile(test_regex or ".*")
664
665 def extract_hftest_lines(self, raw):
666 """Extract hftest-specific lines from a raw output from an invocation
667 of the test platform."""
668 lines = []
J-Alves07be7bb2021-04-13 11:09:12 +0100669 lines_to_process = raw.splitlines()
670
671 try:
672 # If logs have logs of more than one VM, the loop below to extract
673 # lines won't work. Thus, extracting between starting and ending
674 # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED.
675 hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1
676 hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED)
677 except ValueError:
678 hftest_start = 0
679 hftest_end = len(lines_to_process)
680
681 lines_to_process = lines_to_process[hftest_start : hftest_end]
682
683 for line in lines_to_process:
J-Alves3dbb8562020-12-01 10:45:37 +0000684 match = re.search(f"^VM \d+: ", line)
685 if match is not None:
686 line = line[match.end():]
David Brazdil2df24082019-09-05 11:55:08 +0100687 if line.startswith(HFTEST_LOG_PREFIX):
688 lines.append(line[len(HFTEST_LOG_PREFIX):])
689 return lines
690
691 def get_test_json(self):
692 """Invoke the test platform and request a JSON of available test and
693 test suites."""
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100694 out = self.driver.run("json", "json", self.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100695 hf_out = "\n".join(self.extract_hftest_lines(out))
696 try:
697 return json.loads(hf_out)
698 except ValueError as e:
699 print(out)
700 raise e
701
702 def collect_results(self, fn, it, xml_node):
703 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
704 Insert "tests" and "failures" nodes to `xml_node`."""
705 tests_run = 0
706 tests_failed = 0
Andrew Walbranf9463922020-06-05 16:44:42 +0100707 tests_skipped = 0
Andrew Walbran42bf2842020-06-05 18:50:19 +0100708 start_time = time.perf_counter()
David Brazdil2df24082019-09-05 11:55:08 +0100709 for i in it:
710 sub_result = fn(i)
711 assert(sub_result.tests_run >= sub_result.tests_failed)
712 tests_run += sub_result.tests_run
713 tests_failed += sub_result.tests_failed
Andrew Walbranf9463922020-06-05 16:44:42 +0100714 tests_skipped += sub_result.tests_skipped
Andrew Walbran42bf2842020-06-05 18:50:19 +0100715 elapsed_time = time.perf_counter() - start_time
David Brazdil2df24082019-09-05 11:55:08 +0100716
Andrew Walbranf9463922020-06-05 16:44:42 +0100717 xml_node.set("tests", str(tests_run + tests_skipped))
David Brazdil2df24082019-09-05 11:55:08 +0100718 xml_node.set("failures", str(tests_failed))
Andrew Walbranf9463922020-06-05 16:44:42 +0100719 xml_node.set("skipped", str(tests_skipped))
Andrew Walbran42bf2842020-06-05 18:50:19 +0100720 xml_node.set("time", str(elapsed_time))
Andrew Walbranf9463922020-06-05 16:44:42 +0100721 return TestRunnerResult(tests_run, tests_failed, tests_skipped)
David Brazdil2df24082019-09-05 11:55:08 +0100722
723 def is_passed_test(self, test_out):
724 """Parse the output of a test and return True if it passed."""
725 return \
726 len(test_out) > 0 and \
727 test_out[-1] == HFTEST_LOG_FINISHED and \
728 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
729
Andrew Walbranf9463922020-06-05 16:44:42 +0100730 def get_failure_message(self, test_out):
731 """Parse the output of a test and return the message of the first
732 assertion failure."""
733 for i, line in enumerate(test_out):
734 if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out):
735 # The assertion message is on the line after the 'Failure:'
736 return test_out[i + 1].strip()
737
738 return None
739
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000740 def get_log_name(self, suite, test):
741 """Returns a string with a generated log name for the test."""
742 log_name = ""
743
744 cpu = self.driver.args.cpu
745 if cpu:
746 log_name += cpu + "."
747
748 log_name += suite["name"] + "." + test["name"]
749
750 return log_name
751
David Brazdil2df24082019-09-05 11:55:08 +0100752 def run_test(self, suite, test, suite_xml):
753 """Invoke the test platform and request to run a given `test` in given
754 `suite`. Create a new XML node with results under `suite_xml`.
755 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100756 if not self.test_re.match(test["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100757 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100758
759 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100760 test_xml.set("name", test["name"])
761 test_xml.set("classname", suite["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100762
763 if self.skip_long_running_tests and test["is_long_running"]:
764 print(" SKIP", test["name"])
765 test_xml.set("status", "notrun")
766 skipped_xml = ET.SubElement(test_xml, "skipped")
767 skipped_xml.set("message", "Long running")
768 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1)
769
770 print(" RUN", test["name"])
771 log_name = self.get_log_name(suite, test)
772
David Brazdil2df24082019-09-05 11:55:08 +0100773 test_xml.set("status", "run")
774
Andrew Walbran42bf2842020-06-05 18:50:19 +0100775 start_time = time.perf_counter()
Andrew Walbranf9463922020-06-05 16:44:42 +0100776 out = self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100777 log_name, "run {} {}".format(suite["name"], test["name"]),
Andrew Walbranf9463922020-06-05 16:44:42 +0100778 test["is_long_running"] or self.force_long_running)
779 hftest_out = self.extract_hftest_lines(out)
Andrew Walbran42bf2842020-06-05 18:50:19 +0100780 elapsed_time = time.perf_counter() - start_time
781
782 test_xml.set("time", str(elapsed_time))
David Brazdil2df24082019-09-05 11:55:08 +0100783
Andrew Walbranf9463922020-06-05 16:44:42 +0100784 system_out_xml = ET.SubElement(test_xml, "system-out")
785 system_out_xml.text = out
786
787 if self.is_passed_test(hftest_out):
David Brazdil2df24082019-09-05 11:55:08 +0100788 print(" PASS")
Andrew Walbranf9463922020-06-05 16:44:42 +0100789 return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100790 else:
David Brazdil623b6812019-09-09 11:41:08 +0100791 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100792 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Walbranf9463922020-06-05 16:44:42 +0100793 failure_message = self.get_failure_message(hftest_out) or "Test failed"
794 failure_xml.set("message", failure_message)
795 failure_xml.text = '\n'.join(hftest_out)
796 return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100797
798 def run_suite(self, suite, xml):
799 """Invoke the test platform and request to run all matching tests in
800 `suite`. Create new XML nodes with results under `xml`.
801 Suite skipped if it does not match the regex given to constructor."""
802 if not self.suite_re.match(suite["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100803 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100804
805 print(" SUITE", suite["name"])
806 suite_xml = ET.SubElement(xml, "testsuite")
807 suite_xml.set("name", suite["name"])
Andrew Walbran16ae62e2020-06-05 18:27:46 +0100808 properties_xml = ET.SubElement(suite_xml, "properties")
809
810 property_xml = ET.SubElement(properties_xml, "property")
811 property_xml.set("name", "driver")
812 property_xml.set("value", type(self.driver).__name__)
813
814 if self.driver.args.cpu:
815 property_xml = ET.SubElement(properties_xml, "property")
816 property_xml.set("name", "cpu")
817 property_xml.set("value", self.driver.args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100818
819 return self.collect_results(
820 lambda test: self.run_test(suite, test, suite_xml),
821 suite["tests"],
822 suite_xml)
823
824 def run_tests(self):
825 """Run all suites and tests matching regexes given to constructor.
826 Write results to sponge log XML. Return the number of run and failed
827 tests."""
828
829 test_spec = self.get_test_json()
830 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
831
832 xml = ET.Element("testsuites")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100833 xml.set("name", self.test_set_up)
David Brazdil2df24082019-09-05 11:55:08 +0100834 xml.set("timestamp", timestamp)
835
836 result = self.collect_results(
837 lambda suite: self.run_suite(suite, xml),
838 test_spec["suites"],
839 xml)
840
841 # Write XML to file.
David Brazdilee5e25d2020-01-24 14:17:45 +0000842 ET.ElementTree(xml).write(self.artifacts.sponge_xml_path,
843 encoding='utf-8', xml_declaration=True)
David Brazdil2df24082019-09-05 11:55:08 +0100844
845 if result.tests_failed > 0:
846 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
847 "tests failed")
848 elif result.tests_run > 0:
849 print(" PASS: all", result.tests_run, "tests passed")
850
David Brazdil94fd1e92020-02-03 16:45:20 +0000851 # Let the driver clean up.
852 self.driver.finish()
853
David Brazdil2df24082019-09-05 11:55:08 +0100854 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100855
Andrew Scullbc7189d2018-08-14 09:35:13 +0100856def Main():
857 parser = argparse.ArgumentParser()
J-Alves8cc7dbb2021-04-16 10:38:48 +0100858 parser.add_argument("--hypervisor")
859 parser.add_argument("--spmc")
Andrew Scull23e93a82018-10-26 14:56:04 +0100860 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100861 parser.add_argument("--out_initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100862 parser.add_argument("--out_partitions")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000863 parser.add_argument("--initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100864 parser.add_argument("--partitions_json")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100865 parser.add_argument("--suite")
866 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000867 parser.add_argument("--vm_args")
David Brazdil17e76652020-01-29 14:44:19 +0000868 parser.add_argument("--driver", default="qemu")
869 parser.add_argument("--serial-dev", default="/dev/ttyUSB0")
870 parser.add_argument("--serial-baudrate", type=int, default=115200)
David Brazdild8013f92020-02-03 16:40:25 +0000871 parser.add_argument("--serial-no-init-wait", action="store_true")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100872 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100873 parser.add_argument("--force-long-running", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000874 parser.add_argument("--cpu",
875 help="Selects the CPU configuration for the run environment.")
Andrew Walbranf636b842020-01-10 11:46:12 +0000876 parser.add_argument("--tfa", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100877 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100878
J-Alves8cc7dbb2021-04-16 10:38:48 +0100879 # Create class which will manage all test artifacts.
880 if args.hypervisor and args.spmc:
881 test_set_up = "hypervisor_and_spmc"
882 elif args.hypervisor:
883 test_set_up = "hypervisor"
884 elif args.spmc:
885 test_set_up = "spmc"
886 else:
887 raise Exception("No Hafnium image provided!\n")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100888
J-Alves8cc7dbb2021-04-16 10:38:48 +0100889 initrd = None
890 if args.hypervisor and args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100891 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
892 initrd = os.path.join(initrd_dir, "initrd.img")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100893 test_set_up += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000894 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100895
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100896 partitions = None
897 if args.driver == "fvp" and args.partitions_json is not None:
898 partitions_dir = os.path.join(args.out_partitions, "obj", args.partitions_json)
899 partitions = json.load(open(partitions_dir, "r"))
900
David Brazdil2df24082019-09-05 11:55:08 +0100901 # Create class which will manage all test artifacts.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100902 log_dir = os.path.join(args.log, test_set_up)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100903 artifacts = ArtifactsManager(log_dir)
David Brazdil2df24082019-09-05 11:55:08 +0100904
905 # Create a driver for the platform we want to test on.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100906 driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd,
907 vm_args, args.cpu, partitions)
David Brazdil17e76652020-01-29 14:44:19 +0000908
J-Alves8cc7dbb2021-04-16 10:38:48 +0100909 if args.spmc:
J-Alves38223dd2021-04-20 17:31:48 +0100910 # So far only FVP supports tests for SPMC.
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100911 if args.driver != "fvp":
912 raise Exception("Secure tests can only run with fvp driver")
J-Alves38223dd2021-04-20 17:31:48 +0100913
914 if args.hypervisor:
915 driver = FvpDriverBothWorlds(driver_args)
916 else:
917 driver = FvpDriverSPMC(driver_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100918 elif args.hypervisor:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100919 if args.driver == "qemu":
J-Alves8cc7dbb2021-04-16 10:38:48 +0100920 out = os.path.dirname(args.hypervisor)
921 driver = QemuDriver(driver_args, out, args.tfa)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100922 elif args.driver == "fvp":
923 driver = FvpDriverHypervisor(driver_args)
924 elif args.driver == "serial":
925 driver = SerialDriver(driver_args, args.serial_dev,
926 args.serial_baudrate, not args.serial_no_init_wait)
927 else:
928 raise Exception("Unknown driver name: {}".format(args.driver))
J-Alves8cc7dbb2021-04-16 10:38:48 +0100929 else:
930 raise Exception("No Hafnium image provided!\n")
David Brazdil2df24082019-09-05 11:55:08 +0100931
932 # Create class which will drive test execution.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100933 runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100934 args.skip_long_running_tests, args.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100935
936 # Run tests.
937 runner_result = runner.run_tests()
938
939 # Print error message if no tests were run as this is probably unexpected.
940 # Return suitable error code.
941 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100942 print("Error: no tests match")
943 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100944 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100945 return 1
946 else:
David Brazdil2df24082019-09-05 11:55:08 +0100947 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100948
949if __name__ == "__main__":
950 sys.exit(Main())