|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Test `uki_bootconfig_cmdline` in `imghelper`: the function that hard-codes |
| 4 | +# the core kit's systemd and FIPS bootconfig snippets |
| 5 | +# (/boot/boot-config.d/20-*, 21-*, 10-fips.conf) as literal UKI cmdline |
| 6 | +# tokens, since bootconfig.data is not yet attachable to a UKI's boot chain. |
| 7 | +# |
| 8 | +# FIPS-ness is derived from the variant name (the `-fips` suffix convention |
| 9 | +# used across every FIPS variant under bottlerocket/variants/), not from a |
| 10 | +# separate build-arg, so this test exercises that name-based branch directly. |
| 11 | +# |
| 12 | +# Run from the repo root: |
| 13 | +# bash twoliter/embedded/tests/test_uki_bootconfig.sh |
| 14 | + |
| 15 | +set -eu -o pipefail |
| 16 | + |
| 17 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 18 | +IMGHELPER="${SCRIPT_DIR}/../imghelper" |
| 19 | + |
| 20 | +if [[ ! -f "${IMGHELPER}" ]]; then |
| 21 | + echo "test_uki_bootconfig: imghelper not found at ${IMGHELPER}" >&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# imghelper's top-level `${VAR:?}` expansions require these to be set before |
| 26 | +# it can be sourced, even though this test only exercises |
| 27 | +# `uki_bootconfig_cmdline`. |
| 28 | +IMAGE_NAME=x VARIANT=x ARCH=x86_64 VERSION_ID=x BUILD_ID=x |
| 29 | +export IMAGE_NAME VARIANT ARCH VERSION_ID BUILD_ID |
| 30 | + |
| 31 | +# shellcheck source=../imghelper |
| 32 | +. "${IMGHELPER}" |
| 33 | + |
| 34 | +pass_count=0 |
| 35 | +fail_count=0 |
| 36 | + |
| 37 | +pass() { |
| 38 | + pass_count=$((pass_count + 1)) |
| 39 | + echo " ok: $1" |
| 40 | +} |
| 41 | + |
| 42 | +fail() { |
| 43 | + fail_count=$((fail_count + 1)) |
| 44 | + echo " FAIL: $1" >&2 |
| 45 | +} |
| 46 | + |
| 47 | +assert_eq() { |
| 48 | + local actual expected name |
| 49 | + actual="$1" |
| 50 | + expected="$2" |
| 51 | + name="$3" |
| 52 | + if [[ "${actual}" == "${expected}" ]]; then |
| 53 | + pass "${name}" |
| 54 | + else |
| 55 | + fail "${name}: expected '${expected}' got '${actual}'" |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# Assert that a space-separated token list contains every expected token. |
| 60 | +assert_contains_tokens() { |
| 61 | + local actual name token |
| 62 | + actual="$1" |
| 63 | + name="$2" |
| 64 | + shift 2 |
| 65 | + for token in "$@"; do |
| 66 | + if [[ " ${actual} " != *" ${token} "* ]]; then |
| 67 | + fail "${name}: expected token '${token}' missing from '${actual}'" |
| 68 | + return |
| 69 | + fi |
| 70 | + done |
| 71 | + pass "${name}" |
| 72 | +} |
| 73 | + |
| 74 | +echo "Test 1: UKI + FIPS variant includes systemd and FIPS tokens" |
| 75 | +out="$(uki_bootconfig_cmdline "aws-k8s-1.35-fips")" |
| 76 | +assert_contains_tokens "${out}" "fips variant: systemd tokens present" \ |
| 77 | + "SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1" "SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST=25" |
| 78 | +assert_contains_tokens "${out}" "fips variant: FIPS tokens present" \ |
| 79 | + "fips=1" "init.systemd.unit=fipscheck.target" |
| 80 | + |
| 81 | +echo |
| 82 | +echo "Test 1b: UKI + FIPS variant with an extra flavor segment (nvidia-fips)" |
| 83 | +out="$(uki_bootconfig_cmdline "aws-k8s-1.34-nvidia-fips")" |
| 84 | +assert_contains_tokens "${out}" "nvidia-fips variant: systemd tokens present" \ |
| 85 | + "SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1" "SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST=25" |
| 86 | +assert_contains_tokens "${out}" "nvidia-fips variant: FIPS tokens present" \ |
| 87 | + "fips=1" "init.systemd.unit=fipscheck.target" |
| 88 | + |
| 89 | +echo |
| 90 | +echo "Test 2: UKI + non-FIPS variant includes systemd tokens only" |
| 91 | +out="$(uki_bootconfig_cmdline "aws-k8s-1.35")" |
| 92 | +assert_contains_tokens "${out}" "non-fips variant: systemd tokens present" \ |
| 93 | + "SYSTEMD_CGROUP_ENABLE_LEGACY_FORCE=1" "SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST=25" |
| 94 | +if [[ " ${out} " == *" fips=1 "* ]] || [[ " ${out} " == *" init.systemd.unit=fipscheck.target "* ]]; then |
| 95 | + fail "non-fips variant: FIPS tokens must be absent, got '${out}'" |
| 96 | +else |
| 97 | + pass "non-fips variant: FIPS tokens absent" |
| 98 | +fi |
| 99 | + |
| 100 | +echo |
| 101 | +echo "Test 2b: a variant merely containing 'fips' mid-string is not FIPS" |
| 102 | +# Guards against a naive substring match: FIPS-ness is a trailing dash-token, |
| 103 | +# not merely the presence of the substring 'fips' anywhere in the name. |
| 104 | +out="$(uki_bootconfig_cmdline "aws-fips-k8s-1.35")" |
| 105 | +if [[ " ${out} " == *" fips=1 "* ]]; then |
| 106 | + fail "mid-string 'fips' incorrectly treated as a FIPS variant, got '${out}'" |
| 107 | +else |
| 108 | + pass "mid-string 'fips' correctly not treated as a FIPS variant" |
| 109 | +fi |
| 110 | + |
| 111 | +echo |
| 112 | +echo "Test 3: non-UKI images never call uki_bootconfig_cmdline (values absent)" |
| 113 | +# rpm2img only calls uki_bootconfig_cmdline inside the `UKI_IMAGE == yes` |
| 114 | +# branch; a non-UKI build never invokes it, so uki_bootconfig is never |
| 115 | +# populated there. We assert the source-level guard directly, since |
| 116 | +# rpm2img itself is not practical to run end-to-end in a unit test (it |
| 117 | +# performs real partitioning, RPM installs, and image assembly first). |
| 118 | +RPM2IMG="${SCRIPT_DIR}/../rpm2img" |
| 119 | +if [[ ! -f "${RPM2IMG}" ]]; then |
| 120 | + echo "test_uki_bootconfig: rpm2img not found at ${RPM2IMG}" >&2 |
| 121 | + exit 1 |
| 122 | +fi |
| 123 | +if grep -qE '^\s*if \[\[ "\$\{UKI_IMAGE\}" == "yes" \]\]; then\s*$' "${RPM2IMG}"; then |
| 124 | + # Extract the UKI-image conditional block and confirm the |
| 125 | + # uki_bootconfig_cmdline call lives inside it. |
| 126 | + uki_block="$(awk '/if \[\[ "\$\{UKI_IMAGE\}" == "yes" \]\]; then/,/^fi$/' "${RPM2IMG}")" |
| 127 | + if [[ "${uki_block}" == *"uki_bootconfig_cmdline"* ]]; then |
| 128 | + pass "uki_bootconfig_cmdline is only invoked inside the UKI_IMAGE=yes branch" |
| 129 | + else |
| 130 | + fail "uki_bootconfig_cmdline call not found inside the UKI_IMAGE=yes branch" |
| 131 | + fi |
| 132 | +else |
| 133 | + fail "could not locate the UKI_IMAGE=yes conditional in rpm2img" |
| 134 | +fi |
| 135 | + |
| 136 | +echo |
| 137 | +echo "Results: ${pass_count} passed, ${fail_count} failed" |
| 138 | +if [[ "${fail_count}" -gt 0 ]]; then |
| 139 | + exit 1 |
| 140 | +fi |
0 commit comments