blob: 60a90a5c074822e0a1dca835e218189a8cb414ed [file] [log] [blame]
Lorenz Brun547b33f2020-04-23 15:27:06 +02001# Copyright 2020 The Monogon Project Authors.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
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# http://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
17"""
Serge Bazanski662b5b32020-12-21 13:49:00 +010018Ktest provides a macro to run tests under a normal Metropolis node kernel
Lorenz Brun547b33f2020-04-23 15:27:06 +020019"""
20
Tim Windelschmidtbed76d92025-02-18 03:04:14 +010021load("//osbase/build:def.bzl", "build_pure_transition", "build_static_transition")
22load("//osbase/build/fsspec:def.bzl", "FSSpecInfo", "fsspec_core_impl")
Lorenz Brunddd6caf2021-03-04 17:16:04 +010023
Lorenz Brunb69a71c2024-12-23 14:12:46 +010024_KTEST_SCRIPT = """
25#!/usr/bin/env bash
Lorenz Brunddd6caf2021-03-04 17:16:04 +010026
Lorenz Brunb69a71c2024-12-23 14:12:46 +010027exec "{ktest}" -initrd-path "{initrd}" -kernel-path "{kernel}" -cmdline "{cmdline}"
28"""
29
30def _ktest_impl(ctx):
31 initramfs_name = ctx.label.name + ".cpio.zst"
32 initramfs = ctx.actions.declare_file(initramfs_name)
33
Tim Windelschmidt24bf6fd2025-02-12 04:48:24 +010034 fsspec_core_impl(ctx, ctx.executable._mkcpio, initramfs, [("/init", ctx.attr._ktest_init[0]), ("/tester", ctx.attr.tester[0])], [ctx.attr._earlydev])
Lorenz Brunb69a71c2024-12-23 14:12:46 +010035
36 script_file = ctx.actions.declare_file(ctx.label.name + ".sh")
37
38 ctx.actions.write(
39 output = script_file,
40 content = _KTEST_SCRIPT.format(
41 ktest = ctx.executable._ktest.short_path,
42 initrd = initramfs.short_path,
43 kernel = ctx.file.kernel.short_path,
44 cmdline = ctx.attr.cmdline,
45 ),
46 is_executable = True,
Lorenz Brun547b33f2020-04-23 15:27:06 +020047 )
48
Lorenz Brunb69a71c2024-12-23 14:12:46 +010049 return [DefaultInfo(
50 executable = script_file,
51 runfiles = ctx.runfiles(
52 files = [ctx.files._ktest[0], initramfs, ctx.file.kernel, ctx.file.tester],
53 ),
54 )]
55
56k_test = rule(
57 implementation = _ktest_impl,
58 doc = """
59 Run a given test program under the Monogon kernel.
60 """,
61 attrs = {
62 "tester": attr.label(
63 mandatory = True,
64 executable = True,
65 allow_single_file = True,
66 # Runs inside the given kernel, needs to be build for Linux/static
67 cfg = build_static_transition,
68 ),
Tim Windelschmidt24bf6fd2025-02-12 04:48:24 +010069 "files": attr.string_keyed_label_dict(
Lorenz Brunb69a71c2024-12-23 14:12:46 +010070 allow_files = True,
71 doc = """
72 Dictionary of Labels to String, placing a given Label's output file in the initramfs at the location
73 specified by the String value. The specified labels must only have a single output.
74 """,
75 # Attach pure transition to ensure all binaries added to the initramfs are pure/static binaries.
76 cfg = build_pure_transition,
77 ),
Tim Windelschmidt24bf6fd2025-02-12 04:48:24 +010078 "files_cc": attr.string_keyed_label_dict(
Lorenz Brunb69a71c2024-12-23 14:12:46 +010079 allow_files = True,
80 doc = """
81 Special case of 'files' for compilation targets that need to be built with the musl toolchain like
82 go_binary targets which need cgo or cc_binary targets.
83 """,
84 # Attach static transition to all files_cc inputs to ensure they are built with musl and static.
85 cfg = build_static_transition,
86 ),
87 "symlinks": attr.string_dict(
88 default = {},
89 doc = """
90 Symbolic links to create. Similar format as in files and files_cc, so the target of the symlink is the
91 key and the value of it is the location of the symlink itself. Only raw strings are allowed as targets,
92 labels are not permitted. Include the file using files or files_cc, then symlink to its location.
93 """,
94 ),
95 "fsspecs": attr.label_list(
96 default = [],
97 doc = """
98 List of file system specs (osbase.build.fsspec.FSSpec) to also include in the resulting image.
99 These will be merged with all other given attributes.
100 """,
101 providers = [FSSpecInfo],
102 allow_files = True,
103 ),
104 "kernel": attr.label(
105 default = Label("//osbase/test/ktest:linux-testing"),
106 cfg = "exec",
107 allow_single_file = True,
108 ),
109 "cmdline": attr.string(
110 default = "",
111 ),
112 # Tool
113 "_ktest": attr.label(
114 default = Label("//osbase/test/ktest"),
115 cfg = "exec",
116 executable = True,
117 allow_files = True,
118 ),
119 "_ktest_init": attr.label(
120 default = Label("//osbase/test/ktest/init"),
121 cfg = build_pure_transition,
122 executable = True,
123 allow_single_file = True,
124 ),
125 "_mkcpio": attr.label(
126 default = Label("//osbase/build/mkcpio"),
127 executable = True,
128 cfg = "exec",
129 ),
130 "_earlydev": attr.label(
131 default = Label("//osbase/build:earlydev.fsspec"),
132 allow_files = True,
133 ),
134 },
135 test = True,
136)