blob: 812bf08ca2217894e284706d4fb0d291dad8448f [file] [log] [blame]
# Copyright 2019 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("$zx/public/gn/test/zbi_test_success.gni")
import("$zx/public/gn/zbi.gni")
# Build a ZBI file to be run as a standalone ZBI test.
#
# What we call a "ZBI test" is a test that consists of booting a ZBI and
# capturing its console log. The test is considered successful if the
# $zbi_test_success_string is seen in the console log. That string is emitted
# by userboot when given the userboot.shutdown kernel command line argument
# and the program it launched exits with a zero return code.
#
# zbi_test() is like a zbi() target, but specifically meant for ZBI tests.
# The ZBI always embeds kernel command line arguments to instruct userboot.
# Its metadata identifies this image as a ZBI test so it will be run, and
# prevents deps on a zbi_test() target from other zbi() targets from folding
# the ZBIs together (so a zbi_test() target can be thrown together with
# individual test() targets populating a full system image).
#
# 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.
#
# Parameters
#
# * args
# - Required: List of kernel command line arguments,
# e.g. `[ "userboot=bin/my-standalone-test" ]`.
# "userboot.shutdown" is always appended to the list; this tells
# userboot to wait for the launched process to exit and to log its
# exit status and (if that's zero) $zbi_test_success_string before
# shutting down. Note "userboot.reboot" can still be added at boot
# time to cause the machine to reboot instead of powering off.
# - Type: list(string)
#
# * tags
# - Optional: See zbi() for full details. The tag "disabled" here
# causes this ZBI test to be disabled for bot runs. It will still
# be built and can be run manually, but won't be run automatically.
# - Type: list(string)
#
# * device_types
# - Optional: The list of device types that this test should be run on.
# Allowable values are those among the entries in //build/testing/platforms.gni;
# others will be ignored.
# - Default: [ "QEMU" ]
# - Type: list(string)
#
# * qemu_kernel
# - Optional: Label of a target (image_binary() or similar,
# e.g. phys_executable()) that should be launched by QEMU as the
# "kernel" in place of the standard boot shim.
# - Type: label
#
# * timeout
# - Optional: Timeout for running the test, in seconds.
# - Type: seconds
#
# See zbi() for other parameters.
#
template("zbi_test") {
test_target = target_name
cmdline_target = "_zbi_test.$target_name.cmdline"
zbi_target = "$target_name.zbi"
zbi_input(cmdline_target) {
visibility = [ ":*" ]
testonly = true
type = "cmdline"
args = []
foreach(arg, invoker.args + [ "userboot.shutdown" ]) {
args += [ "--entry=$arg" ]
}
}
zbi(zbi_target) {
testonly = true
data_deps = []
forward_variables_from(invoker,
[
"cpu",
"compress",
"data_deps",
"output_dir",
"output_extension",
"output_name",
"tags",
"visibility",
])
if (defined(visibility)) {
visibility += [ ":$test_target" ]
}
if (!defined(output_name)) {
output_name = test_target
}
deps = invoker.deps + [ ":$cmdline_target" ]
if (defined(invoker.qemu_kernel)) {
data_deps += [ invoker.qemu_kernel ]
}
}
outputs = get_target_outputs(":$zbi_target")
output_path = outputs[0]
# The main target is a group that provides a metadata.zbi_barrier
# blocking collection of the zbi() target's metadata.zbi_input_args.
group(test_target) {
forward_variables_from(invoker,
[
"assert_no_deps",
"visibility",
])
testonly = true
deps = [ ":$zbi_target" ]
metadata = {
# The zbi_test() target will not affect the contents of any zbi()
# targets that depend on it (directly or indirectly).
zbi_barrier = []
if (defined(invoker.metadata)) {
forward_variables_from(invoker.metadata, "*")
}
zbi_tests = [
{
cpu = current_cpu
disabled = defined(invoker.tags) && invoker.tags + [ "disabled" ] -
[ "disabled" ] != invoker.tags
name = test_target
label = get_label_info(":$test_target", "label_with_toolchain")
path = rebase_path(output_path, root_build_dir)
bootserver_netboot = [ "--boot" ]
success_string = zbi_test_success_string
if (defined(invoker.device_types)) {
device_types = invoker.device_types
} else {
device_types = [ "QEMU" ]
}
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")
}
forward_variables_from(invoker, [ "timeout" ])
},
]
}
}
}