blob: a8da870312af34fd8b6e5d56d1ca77da10944ea1 [file] [log] [blame]
# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/images/vbmeta.gni")
import("//build/testing/test_spec.gni")
import("//build/testing/zbi_test_success.gni")
import("//build/zbi/zbi.gni")
import("//build/zbi/zbi_input.gni")
declare_args() {
# Set this variable to true to include zbi_tests into tests.json.
include_zbi_host_tests = false
}
# Composes a host test to check for the success of the zbi tests.
#
# The test target is constructed differently based on whether it is run against
# hardware or an emulator, so the template needs to be called with device_types
# that are either only hardware or only emulators.
#
# Parameters
#
# is_emu
# [bool] Whether the device_types provided are all emulator types. If false,
# all device_types are expected to be hardware.
#
# device_types
# [list of strings] Devices the test should run on.
# Allowable values are those among the entries in
# //build/testing/platforms.gni; others will be ignored.
#
# zbi_image, vbmeta_image, qemu_kernel
# Passed through to the environments of the associated test_spec.
# See //build/testing/test_spec.gni.
#
template("zbi_host_test") {
forward_variables_from(invoker, [ "is_emu" ])
toolchain = host_toolchain
os = host_os
cpu = host_cpu
if (is_emu) {
toolchain = host_toolchain_for_target_cpu
os = host_os
cpu = target_cpu
}
# Create a script to run the seriallistener tool with the appropriate args.
_script_file = "${target_out_dir}/${target_name}.sh"
_test_script_target = "${target_name}_script"
action(_test_script_target) {
testonly = true
script = "//build/testing/create_test.sh"
binary_path = get_label_info("//tools/testing/seriallistener($toolchain)",
"root_out_dir") + "/seriallistener"
args = [
rebase_path(_script_file, root_build_dir),
rebase_path(binary_path, root_build_dir),
]
args += [
"-success-str",
zbi_test_success_string,
]
if (!is_emu) {
args += [ "-stdout=True" ]
}
outputs = [ "${_script_file}" ]
visibility = [ ":*" ]
deps = [ "//tools/testing/seriallistener($toolchain)" ]
metadata = {
test_runtime_deps = [
_script_file,
binary_path,
]
}
}
test_spec(target_name) {
target = get_label_info(":${target_name}", "label_with_toolchain")
path = _script_file
os = os
cpu = cpu
isolated = true
# Default timeout for zbi tests is 10 minutes.
# TODO(ihuh): Once we have more data, we can override this with a more
# sensible timeout for each test.
timeout_secs = 600
forward_variables_from(invoker, [ "visibility" ])
deps = [ ":${_test_script_target}" ]
environments = []
if (defined(invoker.device_types)) {
foreach(dt, invoker.device_types) {
environments += [
{
dimensions = {
device_type = dt
}
image_overrides = {
zbi_image = {
name = invoker.zbi_image
}
if (!is_emu && defined(invoker.vbmeta_image)) {
vbmeta_image = {
name = invoker.vbmeta_image
}
}
if (is_emu) {
if (defined(invoker.qemu_kernel)) {
qemu_kernel = {
label = get_label_info(invoker.qemu_kernel,
"label_with_toolchain")
}
} else { # If qemu_kernel is not provided, use the default.
qemu_kernel = {
name = "qemu-kernel"
}
}
}
}
},
]
}
}
}
}
# Composes a test zbi out of Zircon artifacts.
#
# The $target_name target is a group() that holds the metadata and is what
# should be used in deps. The actual zbi() target is defined as
# "$target_name.zbi" so that e.g. get_target_outputs(":$target_name.zbi")
# can be used.
#
# If `use_vbmeta` is true, an associated VBMeta image (of the same base name)
# will be output alongside the ZBI.
#
# Parameters
#
# args (optional)
# [list of strings] Command line arguments for the resulting zbi. ZBI tests
# using the kernel and a non-default `userboot.next` options must add
# `userboot.{shutdown,reboot}` as well in order to guarantee that a success
# string is printed. See //zircon/system/utest/core:core-tests for a
# canonical example.
#
# device_types
# [list of strings] Devices the test should run on.
# Allowable values are those among the entries in
# //build/testing/platforms.gni; others will be ignored.
# Defaults to [ "QEMU" ].
#
# qemu_kernel
# [label_with_toolchain] Label corresponding to the qemu-kernel image
# in images.json that should be used with the zbi_image. This will appear
# as the qemu_kernel_label in a zbi_tests.json entry.
#
# deps, data_deps, replace, complete, compress
# See //build/zbi/zbi.gni.
template("zbi_test") {
main_target = target_name
zbi_target = "${target_name}.zbi"
not_needed(invoker,
[
"qemu_kernel",
"timeout",
])
input_deps = []
group_deps = []
entries = []
if (defined(invoker.args)) {
entries += invoker.args
}
if (entries != []) {
input_target = "${target_name}_input"
input_deps += [ ":$input_target" ]
zbi_input(input_target) {
forward_variables_from(invoker, [ "deps" ])
testonly = true
type = "cmdline"
args = []
foreach(entry, entries) {
args += [ "--entry=$entry" ]
}
}
}
forward_variables_from(invoker, [ "output_name" ])
if (!defined(output_name)) {
output_name = main_target
}
zbi(zbi_target) {
testonly = true
data_deps = []
forward_variables_from(invoker,
[
"cpu",
"compress",
"data_deps",
"output_dir",
"output_extension",
"visibility",
])
if (defined(visibility)) {
visibility += [ ":$main_target" ]
}
deps = invoker.deps + input_deps
if (defined(invoker.qemu_kernel)) {
data_deps += [ invoker.qemu_kernel ]
}
_output_dir = target_out_dir
if (defined(output_dir)) {
_output_dir = output_dir
}
_output_extension = "zbi"
if (defined(output_extension)) {
_output_extension = output_extension
}
_output_file = "${_output_dir}/${output_name}"
if (_output_extension != "") {
_output_file += ".${_output_extension}"
}
metadata = {
images = [
{
label = get_label_info(":$zbi_target", "label_with_toolchain")
name = output_name
path = rebase_path(_output_file, root_build_dir)
type = "zbi"
cpu = current_cpu
compressed =
!defined(invoker.compressed) ||
(invoker.compressed != false && invoker.compressed != "none")
if (defined(testonly) && testonly) {
testonly = true
}
forward_variables_from(invoker, [ "tags" ])
},
]
}
}
group_deps += [ ":$zbi_target" ]
output_zbi = target_name
if (defined(invoker.output_name)) {
output_zbi = invoker.output_name
}
if (defined(invoker.output_extension)) {
if (invoker.output_extension != "") {
output_zbi += ".${invoker.output_extension}"
}
} else {
output_zbi += ".zbi"
}
if (defined(invoker.output_dir)) {
output_zbi = "${invoker.output_dir}/$output_zbi"
} else {
output_zbi = "$target_out_dir/$output_zbi"
}
if (defined(invoker.device_types)) {
_device_types = invoker.device_types
} else {
_device_types = [
"AEMU",
"QEMU",
]
}
if (use_vbmeta) {
vbmeta_target = "${target_name}.vbmeta"
vbmeta(vbmeta_target) {
forward_variables_from(invoker, [ "visibility" ])
testonly = true
zbi = ":$zbi_target"
}
vbmeta_outputs = get_target_outputs(":${vbmeta_target}")
output_vbmeta = vbmeta_outputs[0]
group_deps += [ ":${vbmeta_target}" ]
}
if (include_zbi_host_tests) {
emu_types = []
hardware_types = []
foreach(dt, _device_types) {
if (dt == "QEMU" || dt == "AEMU") {
emu_types += [ dt ]
} else {
hardware_types += [ dt ]
}
}
host_test_name = "${main_target}_host_test"
if (emu_types != []) {
zbi_host_test(host_test_name) {
is_emu = true
zbi_image = output_name
if (defined(invoker.qemu_kernel)) {
qemu_kernel = invoker.qemu_kernel
}
device_types = emu_types
}
group_deps += [ ":$host_test_name" ]
}
if (hardware_types != []) {
host_test_hw_name = "${host_test_name}_for_hardware"
zbi_host_test(host_test_hw_name) {
is_emu = false
zbi_image = output_name
if (use_vbmeta) {
vbmeta_image = vbmeta_target
}
device_types = hardware_types
}
group_deps += [ ":$host_test_hw_name" ]
}
}
group(main_target) {
forward_variables_from(invoker,
[
"assert_no_deps",
"visibility",
])
testonly = true
public_deps = group_deps
metadata = {
# Used by the zbi() template to ensure that this is not included into
# any ZBI that depends on this target.
zbi_input_barrier = []
# Used by build_api_module("zbi_tests") in //BUILD.gn
zbi_tests = [
{
cpu = current_cpu
disabled = defined(invoker.tags) && invoker.tags + [ "disabled" ] -
[ "disabled" ] != invoker.tags
name = main_target
label = get_label_info(":$target_name", "label_with_toolchain")
path = rebase_path(output_zbi, root_build_dir)
if (use_vbmeta) {
vbmeta = rebase_path(output_vbmeta, root_build_dir)
}
bootserver_netboot = [ "--boot" ]
success_string = zbi_test_success_string
device_types = _device_types
if (defined(invoker.qemu_kernel)) {
assert(device_types == [ "QEMU" ],
"`qemu_kernel` tests can only run on QEMU")
qemu_kernel_label =
get_label_info(invoker.qemu_kernel, "label_with_toolchain")
}
if (defined(invoker.timeout) && invoker.timeout != false) {
timeout = invoker.timeout
}
},
]
}
}
}