| 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 | |
| Tim Windelschmidt | bed76d9 | 2025-02-18 03:04:14 +0100 | [diff] [blame] | 21 | load("//osbase/build/fsspec:def.bzl", "FSSpecInfo", "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 | |
| Jan Schär | 2b9a0a0 | 2025-07-09 07:54:12 +0000 | [diff] [blame] | 33 | fsspec_core_impl(ctx, ctx.executable._mkcpio, initramfs, [("/init", ctx.attr._ktest_init), ("/tester", ctx.attr.tester)], [ctx.attr._earlydev]) |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 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 | |
| Jan Schär | 2b9a0a0 | 2025-07-09 07:54:12 +0000 | [diff] [blame] | 48 | runfiles = ctx.runfiles(files = [ctx.file._ktest, initramfs, ctx.file.kernel]) |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 49 | runfiles = runfiles.merge(ctx.attr._ktest[DefaultInfo].default_runfiles) |
| 50 | |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 51 | return [DefaultInfo( |
| 52 | executable = script_file, |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 53 | runfiles = runfiles, |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 54 | )] |
| 55 | |
| 56 | k_test = rule( |
| 57 | implementation = _ktest_impl, |
| 58 | doc = """ |
| Jan Schär | 27da2e6 | 2025-04-28 16:11:16 +0000 | [diff] [blame] | 59 | Run a given test program under the Monogon kernel. |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 60 | """, |
| 61 | attrs = { |
| 62 | "tester": attr.label( |
| 63 | mandatory = True, |
| 64 | executable = True, |
| 65 | allow_single_file = True, |
| Jan Schär | 2b9a0a0 | 2025-07-09 07:54:12 +0000 | [diff] [blame] | 66 | cfg = "target", |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 67 | ), |
| Tim Windelschmidt | 24bf6fd | 2025-02-12 04:48:24 +0100 | [diff] [blame] | 68 | "files": attr.string_keyed_label_dict( |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 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 | """, |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 74 | ), |
| 75 | "symlinks": attr.string_dict( |
| 76 | default = {}, |
| 77 | doc = """ |
| Jan Schär | 0fd36f4 | 2025-04-29 10:26:03 +0000 | [diff] [blame] | 78 | Symbolic links to create. Similar format as in `files`, so the key is the location of the |
| Tim Windelschmidt | ad4d954 | 2025-03-24 20:20:13 +0100 | [diff] [blame] | 79 | symlink itself and the value of it is target of the symlink. Only raw strings are allowed as targets, |
| Jan Schär | 0fd36f4 | 2025-04-29 10:26:03 +0000 | [diff] [blame] | 80 | labels are not permitted. Include the file using `files`, then symlink to its location. |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 81 | """, |
| 82 | ), |
| 83 | "fsspecs": attr.label_list( |
| 84 | default = [], |
| 85 | doc = """ |
| 86 | List of file system specs (osbase.build.fsspec.FSSpec) to also include in the resulting image. |
| 87 | These will be merged with all other given attributes. |
| 88 | """, |
| 89 | providers = [FSSpecInfo], |
| 90 | allow_files = True, |
| 91 | ), |
| 92 | "kernel": attr.label( |
| 93 | default = Label("//osbase/test/ktest:linux-testing"), |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 94 | allow_single_file = True, |
| 95 | ), |
| 96 | "cmdline": attr.string( |
| 97 | default = "", |
| 98 | ), |
| 99 | # Tool |
| 100 | "_ktest": attr.label( |
| 101 | default = Label("//osbase/test/ktest"), |
| Jan Schär | 27da2e6 | 2025-04-28 16:11:16 +0000 | [diff] [blame] | 102 | cfg = "target", |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 103 | executable = True, |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 104 | allow_single_file = True, |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 105 | ), |
| 106 | "_ktest_init": attr.label( |
| 107 | default = Label("//osbase/test/ktest/init"), |
| Jan Schär | 2b9a0a0 | 2025-07-09 07:54:12 +0000 | [diff] [blame] | 108 | cfg = "target", |
| Lorenz Brun | b69a71c | 2024-12-23 14:12:46 +0100 | [diff] [blame] | 109 | executable = True, |
| 110 | allow_single_file = True, |
| 111 | ), |
| 112 | "_mkcpio": attr.label( |
| 113 | default = Label("//osbase/build/mkcpio"), |
| 114 | executable = True, |
| 115 | cfg = "exec", |
| 116 | ), |
| 117 | "_earlydev": attr.label( |
| 118 | default = Label("//osbase/build:earlydev.fsspec"), |
| 119 | allow_files = True, |
| 120 | ), |
| 121 | }, |
| 122 | test = True, |
| 123 | ) |