#!/usr/bin/env bash # Regression tests for build/fetch-kernel-tarball.sh (#1588). # # Runs the real script, copied into a fixture dir so its own HERE-relative pin # lookup finds a pin we control, with a fake curl first on PATH: no network, # and the outcome of each fetch is exact. Covers: # - curl is bounded (connect timeout, stall abort, total cap), so a server # that answers and then stalls cannot hold the job until its timeout; # - a failed fetch leaves no partial tarball behind, so the next run fetches # again instead of taking the partial file as present; # - a good fetch is verified and kept; a present, matching tarball is reused. set -uo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" SCRIPT="$HERE/../../build/fetch-kernel-tarball.sh" [ -f "$SCRIPT" ] || { echo "FAIL: script not found at $SCRIPT"; exit 1; } KVER="$(sed -n 's/^KVER=//p' "$SCRIPT")" [ -n "$KVER" ] || { echo "FAIL: no KVER in $SCRIPT"; exit 1; } FAIL=0 ok() { printf '[PASS] %s\n' "$1"; } bad() { printf '[FAIL] %s\n' "$1"; FAIL=1; } TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT setup() { # setup -> prints the fixture dir local dir="$TMP/$1" mkdir -p "$dir/bin" cp "$SCRIPT" "$dir/fetch-kernel-tarball.sh" printf 'good-bytes' | sha256sum | awk '{print $1}' \ > "$dir/linux-$KVER.tar.xz.sha256" printf '%s' "$dir" } # fake_curl : records its argv in /argv and its call count in # /count. Modes: good (writes the pinned bytes), partial (writes a few # bytes, then fails the way curl does on a timeout), never (must not run). fake_curl() { local dir="$1" mode="$2" # These single-quoted lines are the literal source of the fake program. # shellcheck disable=SC2016 printf '%s\n' '#!/bin/sh' \ 'dir=$(dirname "$(dirname "$0")")' \ 'printf "%s\n" "$*" > "$dir/argv"' \ 'echo x >> "$dir/count"' \ 'out=' \ 'while [ "$#" -gt 0 ]; do' \ ' if [ "$1" = "-o" ]; then shift; out="$1"; fi' \ ' shift' \ 'done' \ '[ -n "$out" ] || exit 2' \ "case $mode in" \ ' good) printf good-bytes > "$out" ;;' \ ' partial) printf good > "$out"; exit 28 ;;' \ ' *) echo "curl should not have run" >&2; exit 1 ;;' \ 'esac' > "$dir/bin/curl" chmod +x "$dir/bin/curl" } run() { # run -> output in $OUT, status in $RC local dir="$1" dest="$2" OUT="$(PATH="$dir/bin:$PATH" bash "$dir/fetch-kernel-tarball.sh" "$dest" 2>&1)" RC=$? } # --- case A: a good fetch is bounded, verified and kept --------------------- dir="$(setup case-a)" fake_curl "$dir" good run "$dir" "$dir/linux.tar.xz" if [ "$RC" -eq 0 ] && [ -f "$dir/linux.tar.xz" ] \ && printf '%s' "$OUT" | grep -q 'tarball sha256 verified'; then ok "good fetch: verified and kept" else bad "good fetch: verified and kept (rc=$RC, out=$OUT)" fi for flag in --connect-timeout --speed-limit --speed-time --max-time; do if grep -q -- "$flag" "$dir/argv"; then ok "curl carries $flag" else bad "curl carries $flag (argv: $(cat "$dir/argv"))" fi done if ls "$dir"/linux.tar.xz.download.* >/dev/null 2>&1; then bad "good fetch: no download temp file left" else ok "good fetch: no download temp file left" fi # --- case B: a failed fetch leaves nothing, and the next run fetches again -- dir="$(setup case-b)" fake_curl "$dir" partial run "$dir" "$dir/linux.tar.xz" if [ "$RC" -ne 0 ] && [ ! -e "$dir/linux.tar.xz" ] \ && ! ls "$dir"/linux.tar.xz.download.* >/dev/null 2>&1; then ok "failed fetch: no partial tarball left" else bad "failed fetch: no partial tarball left (rc=$RC, files: $(ls "$dir"))" fi fake_curl "$dir" good run "$dir" "$dir/linux.tar.xz" if [ "$RC" -eq 0 ] && [ "$(wc -l < "$dir/count")" -eq 2 ]; then ok "run after a failed fetch downloads again and passes" else bad "run after a failed fetch downloads again and passes (rc=$RC, out=$OUT)" fi # --- case C: a present, matching tarball is reused without curl ------------- dir="$(setup case-c)" printf 'good-bytes' > "$dir/linux.tar.xz" fake_curl "$dir" never run "$dir" "$dir/linux.tar.xz" if [ "$RC" -eq 0 ] && [ ! -e "$dir/count" ]; then ok "present tarball: reused, curl never invoked" else bad "present tarball: reused, curl never invoked (rc=$RC, out=$OUT)" fi [ "$FAIL" -eq 0 ] && echo "fetch-kernel-tarball tests passed" exit "$FAIL"