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