|
| 1 | +#!/usr/bin/env bash |
| 2 | +# tests/test_rebuild_uki_flow.sh — Integration tests for bin/atomic-rebuild-uki |
| 3 | +# Covers --list, overwrite prompt, mount/build/sign/unmount flow, cleanup |
| 4 | +# Run: bash tests/test_rebuild_uki_flow.sh |
| 5 | + |
| 6 | +source "$(dirname "${BASH_SOURCE[0]}")/test_harness.sh" |
| 7 | + |
| 8 | +SCRIPT="${PROJECT_ROOT}/bin/atomic-rebuild-uki" |
| 9 | + |
| 10 | +# Mock verify-lib |
| 11 | +make_mock verify-lib "echo '${PROJECT_ROOT}/lib/atomic/common.sh'; exit 0" |
| 12 | + |
| 13 | +# ── Setup ────────────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +_FAKE_ESP="${TESTDIR}/fake_esp" |
| 16 | +_FAKE_BTRFS="${TESTDIR}/fake_btrfs" |
| 17 | +_FAKE_LOCK="${TESTDIR}/lock" |
| 18 | + |
| 19 | +mkdir -p "${_FAKE_ESP}/EFI/Linux" |
| 20 | +mkdir -p "${_FAKE_BTRFS}" |
| 21 | +mkdir -p "${_FAKE_LOCK}" |
| 22 | + |
| 23 | +# Create a test generation |
| 24 | +GEN_ID="20250615-120000" |
| 25 | +mkdir -p "${_FAKE_BTRFS}/root-${GEN_ID}" |
| 26 | +mkdir -p "${_FAKE_BTRFS}/root-${GEN_ID}/boot" |
| 27 | +mkdir -p "${_FAKE_BTRFS}/root-${GEN_ID}/usr/lib/modules/6.14.0-arch1-1" |
| 28 | +echo "linux" > "${_FAKE_BTRFS}/root-${GEN_ID}/usr/lib/modules/6.14.0-arch1-1/pkgbase" |
| 29 | +touch "${_FAKE_BTRFS}/root-${GEN_ID}/boot/vmlinuz-linux" |
| 30 | +touch "${_FAKE_BTRFS}/root-${GEN_ID}/boot/initramfs-linux.img" |
| 31 | +mkdir -p "${_FAKE_BTRFS}/root-${GEN_ID}/etc" |
| 32 | +echo "PRETTY_NAME=\"Arch Linux\"" > "${_FAKE_BTRFS}/root-${GEN_ID}/etc/os-release" |
| 33 | +mkdir -p "${_FAKE_BTRFS}/root-${GEN_ID}/efi" |
| 34 | + |
| 35 | +# Build mocks |
| 36 | +cat > "${TESTDIR}/rebuild-mocks.txt" << MOCKS |
| 37 | +
|
| 38 | +ESP="${_FAKE_ESP}" |
| 39 | +BTRFS_MOUNT="${_FAKE_BTRFS}" |
| 40 | +LOCK_DIR="${_FAKE_LOCK}" |
| 41 | +
|
| 42 | +acquire_lock() { echo "ACQUIRE_LOCK"; } |
| 43 | +validate_config() { return 0; } |
| 44 | +ensure_btrfs_mounted(){ echo "ENSURE_BTRFS"; return 0; } |
| 45 | +validate_subvolume() { |
| 46 | + local subvol="\$1" |
| 47 | + if [[ -d "\${BTRFS_MOUNT}/\${subvol}" ]]; then |
| 48 | + return 0 |
| 49 | + else |
| 50 | + echo "ERROR: Subvolume not found" >&2 |
| 51 | + return 1 |
| 52 | + fi |
| 53 | +} |
| 54 | +
|
| 55 | +get_root_device() { echo "/dev/sda2"; } |
| 56 | +
|
| 57 | +build_uki() { |
| 58 | + local gen_id="\$1" |
| 59 | + echo "BUILD_UKI gen=\$gen_id" |
| 60 | + touch "\${ESP}/EFI/Linux/arch-\${gen_id}.efi" |
| 61 | + echo "\${ESP}/EFI/Linux/arch-\${gen_id}.efi" |
| 62 | + return 0 |
| 63 | +} |
| 64 | +
|
| 65 | +sign_uki() { echo "SIGN_UKI"; return 0; } |
| 66 | +
|
| 67 | +mount() { echo "MOUNT: \$*"; return 0; } |
| 68 | +mountpoint() { return 1; } |
| 69 | +umount() { echo "UMOUNT: \$*"; return 0; } |
| 70 | +rmdir() { echo "RMDIR: \$*"; return 0; } |
| 71 | +mktemp() { echo "${_FAKE_LOCK}/rebuild-XXXXXX"; } |
| 72 | +
|
| 73 | +MOCKS |
| 74 | + |
| 75 | +# Create patched test script |
| 76 | +_TEST_SCRIPT="${TESTDIR}/rebuild-uki-flow-test" |
| 77 | + |
| 78 | +sed \ |
| 79 | + -e 's/^\(\[\[ \$EUID -eq 0 \]\]\)/# \1/' \ |
| 80 | + -e '/^validate_config || exit 1$/s/^/# /' \ |
| 81 | + -e '/^_src "\${LIBDIR}\/common.sh"$/r '"${TESTDIR}/rebuild-mocks.txt"'' \ |
| 82 | + "$SCRIPT" > "$_TEST_SCRIPT" |
| 83 | + |
| 84 | +chmod +x "$_TEST_SCRIPT" |
| 85 | + |
| 86 | +_run_rebuild() { |
| 87 | + run_cmd bash "$_TEST_SCRIPT" "$@" |
| 88 | +} |
| 89 | + |
| 90 | +# ── Successful rebuild ─────────────────────────────────────────────── |
| 91 | + |
| 92 | +section "Successful rebuild flow" |
| 93 | + |
| 94 | +# Remove UKI so no overwrite prompt |
| 95 | +rm -f "${_FAKE_ESP}/EFI/Linux/arch-${GEN_ID}.efi" |
| 96 | + |
| 97 | +_run_rebuild "$GEN_ID" |
| 98 | +assert_eq "rebuild → exit 0" "0" "$_rc" |
| 99 | +assert_contains "lock acquired" "ACQUIRE_LOCK" "$_out" |
| 100 | +assert_contains "btrfs mounted" "ENSURE_BTRFS" "$_out" |
| 101 | +assert_contains "subvolume checked" "Checking subvolume" "$_out" |
| 102 | +assert_contains "mounting" "Mounting" "$_out" |
| 103 | +assert_contains "UKI built" "Building UKI" "$_out" |
| 104 | +assert_contains "BUILD_UKI called" "BUILD_UKI" "$_out" |
| 105 | +assert_contains "UKI signed" "SIGN_UKI" "$_out" |
| 106 | +assert_contains "unmounting" "Unmounting" "$_out" |
| 107 | +assert_contains "done message" "Done:" "$_out" |
| 108 | + |
| 109 | +# ── Rebuild with existing UKI (overwrite) ──────────────────────────── |
| 110 | + |
| 111 | +section "Rebuild with existing UKI" |
| 112 | + |
| 113 | +# Create UKI file so the prompt triggers |
| 114 | +touch "${_FAKE_ESP}/EFI/Linux/arch-${GEN_ID}.efi" |
| 115 | + |
| 116 | +# The script asks for confirmation; we simulate "y" response |
| 117 | +run_cmd bash -c "echo 'y' | bash '${_TEST_SCRIPT}' '${GEN_ID}'" |
| 118 | +assert_eq "rebuild with overwrite → exit 0" "0" "$_rc" |
| 119 | +assert_contains "already exists" "already exists" "$_out" |
| 120 | + |
| 121 | +# ── Rebuild with existing UKI (abort) ──────────────────────────────── |
| 122 | + |
| 123 | +section "Rebuild: user aborts overwrite" |
| 124 | + |
| 125 | +touch "${_FAKE_ESP}/EFI/Linux/arch-${GEN_ID}.efi" |
| 126 | + |
| 127 | +run_cmd bash -c "echo 'n' | bash '${_TEST_SCRIPT}' '${GEN_ID}'" || true |
| 128 | +assert_contains "aborted message" "Aborted" "$_out" |
| 129 | + |
| 130 | +# ── --list: shows subvolumes ──────────────────────────────────────── |
| 131 | + |
| 132 | +section "List orphans" |
| 133 | + |
| 134 | +# Create multiple subvolumes |
| 135 | +mkdir -p "${_FAKE_BTRFS}/root-20250614-110000" |
| 136 | +mkdir -p "${_FAKE_BTRFS}/root-20250613-100000-old" |
| 137 | + |
| 138 | +# Create UKI for one of them |
| 139 | +touch "${_FAKE_ESP}/EFI/Linux/arch-20250615-120000.efi" |
| 140 | +touch "${_FAKE_ESP}/EFI/Linux/arch-20250614-110000.efi" |
| 141 | +# No UKI for 20250613-100000-old |
| 142 | + |
| 143 | +run_cmd bash "$_TEST_SCRIPT" --list |
| 144 | +assert_eq "list → exit 0" "0" "$_rc" |
| 145 | +assert_contains "shows subvolumes" "Subvolumes" "$_out" |
| 146 | +assert_contains "UKI exists" "UKI exists" "$_out" |
| 147 | +assert_contains "UKI missing" "UKI missing" "$_out" |
| 148 | + |
| 149 | +# ── Invalid GEN_ID ────────────────────────────────────────────────── |
| 150 | + |
| 151 | +section "Invalid GEN_ID" |
| 152 | + |
| 153 | +_run_rebuild "invalid" |
| 154 | +assert_eq "invalid → rc 1" "1" "$_rc" |
| 155 | +assert_contains "error mentions format" "Invalid GEN_ID" "$_out" |
| 156 | + |
| 157 | +_run_rebuild "20250615" |
| 158 | +assert_eq "partial timestamp → rc 1" "1" "$_rc" |
| 159 | + |
| 160 | +_run_rebuild "" |
| 161 | +assert_eq "empty → rc 1" "1" "$_rc" |
| 162 | + |
| 163 | +_run_rebuild "../../etc/passwd" |
| 164 | +assert_eq "path traversal → rc 1" "1" "$_rc" |
| 165 | + |
| 166 | +# ── Non-existent subvolume ────────────────────────────────────────── |
| 167 | + |
| 168 | +section "Non-existent subvolume" |
| 169 | + |
| 170 | +_run_rebuild "20250101-000000" |
| 171 | +assert_eq "missing subvol → rc 1" "1" "$_rc" |
| 172 | +assert_contains "error about subvolume" "Subvolume not found" "$_out" |
| 173 | +assert_contains "suggests --list" "atomic-rebuild-uki --list" "$_out" |
| 174 | + |
| 175 | +# ── Tagged generation rebuild ──────────────────────────────────────── |
| 176 | + |
| 177 | +section "Tagged generation rebuild" |
| 178 | + |
| 179 | +TAGGED_GEN="20250616-130000-kde" |
| 180 | +mkdir -p "${_FAKE_BTRFS}/root-${TAGGED_GEN}" |
| 181 | +mkdir -p "${_FAKE_BTRFS}/root-${TAGGED_GEN}/boot" |
| 182 | +mkdir -p "${_FAKE_BTRFS}/root-${TAGGED_GEN}/usr/lib/modules/6.14.0-arch1-1" |
| 183 | +echo "linux" > "${_FAKE_BTRFS}/root-${TAGGED_GEN}/usr/lib/modules/6.14.0-arch1-1/pkgbase" |
| 184 | +touch "${_FAKE_BTRFS}/root-${TAGGED_GEN}/boot/vmlinuz-linux" |
| 185 | +touch "${_FAKE_BTRFS}/root-${TAGGED_GEN}/boot/initramfs-linux.img" |
| 186 | +mkdir -p "${_FAKE_BTRFS}/root-${TAGGED_GEN}/etc" |
| 187 | +echo "PRETTY_NAME=\"Arch Linux\"" > "${_FAKE_BTRFS}/root-${TAGGED_GEN}/etc/os-release" |
| 188 | +mkdir -p "${_FAKE_BTRFS}/root-${TAGGED_GEN}/efi" |
| 189 | + |
| 190 | +rm -f "${_FAKE_ESP}/EFI/Linux/arch-${TAGGED_GEN}.efi" 2>/dev/null || true |
| 191 | + |
| 192 | +_run_rebuild "$TAGGED_GEN" |
| 193 | +assert_eq "tagged rebuild → exit 0" "0" "$_rc" |
| 194 | +assert_contains "tagged gen in output" "$TAGGED_GEN" "$_out" |
| 195 | + |
| 196 | +# ── Cleanup trap structure ────────────────────────────────────────── |
| 197 | + |
| 198 | +section "Cleanup trap: structure verification" |
| 199 | + |
| 200 | +_script_content=$(cat "$SCRIPT") |
| 201 | + |
| 202 | +assert_contains "cleanup function" "cleanup_rebuild()" "$_script_content" |
| 203 | +assert_contains "EXIT trap" "trap cleanup_rebuild EXIT" "$_script_content" |
| 204 | +assert_contains "unmounts MOUNT_DIR" 'MOUNT_DIR' "$_script_content" |
| 205 | +assert_contains "rmdir MOUNT_DIR" 'rmdir' "$_script_content" |
| 206 | +assert_contains "unmounts BTRFS" 'BTRFS_MOUNT' "$_script_content" |
| 207 | +assert_contains "closes lock FD" 'LOCK_FD' "$_script_content" |
| 208 | + |
| 209 | +# ── Version and help ──────────────────────────────────────────────── |
| 210 | + |
| 211 | +section "Version and help" |
| 212 | + |
| 213 | +run_cmd bash "$SCRIPT" --version |
| 214 | +assert_eq "version → exit 0" "0" "$_rc" |
| 215 | +assert_contains "version output" "atomic-rebuild-uki v" "$_out" |
| 216 | + |
| 217 | +run_cmd bash "$SCRIPT" --help |
| 218 | +assert_eq "help → exit 0" "0" "$_rc" |
| 219 | +assert_contains "usage" "Usage" "$_out" |
| 220 | +assert_contains "list option" "list" "$_out" |
| 221 | +assert_contains "GEN_ID mentioned" "GEN_ID" "$_out" |
| 222 | + |
| 223 | +# ── Unknown option (real script requires root, so GEN_ID validation |
| 224 | +# is tested in test_rebuild_uki.sh with patched script) ────────── |
| 225 | + |
| 226 | +section "Unknown option behavior" |
| 227 | + |
| 228 | +# --bogus is treated as GEN_ID — real script checks EUID first |
| 229 | +# so we verify the behavior with a quick patched test |
| 230 | +_TEST_UO="${TESTDIR}/rebuild-unknown-test" |
| 231 | +sed -e 's/^\(\[\[ \$EUID -eq 0 \]\]\)/# \1/' \ |
| 232 | + -e '/^validate_config || exit 1$/s/^/# /' \ |
| 233 | + "$SCRIPT" > "$_TEST_UO" |
| 234 | +chmod +x "$_TEST_UO" |
| 235 | + |
| 236 | +run_cmd bash "$_TEST_UO" --bogus |
| 237 | +assert_eq "unknown option → rc 1" "1" "$_rc" |
| 238 | +assert_contains "invalid GEN_ID error" "Invalid GEN_ID" "$_out" |
| 239 | + |
| 240 | +# ── list_orphans function structure ───────────────────────────────── |
| 241 | + |
| 242 | +section "list_orphans function structure" |
| 243 | + |
| 244 | +assert_contains "list_orphans defined" "list_orphans()" "$_script_content" |
| 245 | +assert_contains "subvolume glob" 'root-[0-9]' "$_script_content" |
| 246 | +assert_contains "sort -r for reverse" "sort -r" "$_script_content" |
| 247 | +assert_contains "empty message" "No generation subvolumes found" "$_script_content" |
| 248 | + |
| 249 | +summary |
0 commit comments