#!/usr/bin/env bash ## pkgcache-host.test.sh — fixture test for scripts/host/pkgcache-host-repos.sh (opt-in ## setting, apply/revert/sync/status) and pkgcache-host-health.sh (fallback/restore). ## Everything runs against a temp repo dir; curl/logger are fakes; no systemd, no root. set -euo pipefail H=$(cd "$(dirname "$0")/../host" && pwd) R="$H/pkgcache-host-repos.sh"; HL="$H/pkgcache-host-health.sh" T=$(mktemp -d); trap 'rm -rf "$T"' EXIT fail=0 ok() { echo "ok - $1"; } bad() { echo "FAIL - $1"; fail=1; } check() { if "${@:2}"; then ok "$1"; else bad "$1"; fi; } mkdir -p "$T/bin" "$T/repos" "$T/state" "$T/units" "$T/bk" printf '#!/bin/sh\nexit 0\n' >"$T/bin/logger" # fake curl: FAKE_CACHE=ok|empty|noasc|refused ; FAKE_ORIGIN=ok|down ; asks recorded cat >"$T/bin/curl" <<'EOS' #!/bin/sh for a; do u=$a; done echo "$u" >>"$T/curl.log" host=${u#https://}; host=${host%%/*} if [ "$host" = cache.test ]; then mode=$FAKE_CACHE; else [ "$FAKE_ORIGIN" = ok ] && mode=ok || mode=refused; fi case "$mode" in refused) exit 7;; empty) exit 52;; noasc) case "$u" in *.asc) printf 'garbage\n200'; exit 0;; esac; printf '\n200';; ok) case "$u" in *.asc) printf -- '-----BEGIN PGP SIGNATURE-----\nx\n200';; *) printf '\n200';; esac;; esac EOS chmod +x "$T/bin/"* sed -i "s|\$T/curl.log|$T/curl.log|" "$T/bin/curl" export PATH="$T/bin:$PATH" PKGCACHE_REPO_DIR="$T/repos" PKGCACHE_STATE_DIR="$T/state" PKGCACHE_UNIT_DIR="$T/units" \ PKGCACHE_BACKUP_ROOT="$T/bk" PKGCACHE_NO_SYSTEMD=1 PKGCACHE_NO_CATRUST=1 PKGCACHE_ANCHOR="$T/anchor.crt" \ PKGCACHE_HOST_CONF="$T/pkgcache-host.conf" PKGCACHE_RELEASEVER=9 FAKE_CACHE=ok FAKE_ORIGIN=ok cp "$H/../../keys/pkg-cache/ca.crt" "$T/ca.crt" cat >"$T/repos/almalinux-baseos.repo" <<'EOS' [baseos] name=AlmaLinux $releasever - BaseOS mirrorlist=https://mirrors.almalinux.org/mirrorlist/$releasever/baseos # baseurl=https://repo.almalinux.org/almalinux/$releasever/BaseOS/$basearch/os/ enabled=1 gpgcheck=1 EOS cat >"$T/repos/custom.repo" <<'EOS' [custom] name=custom baseurl=https://example.org/custom/ enabled=1 EOS cp "$T/repos/almalinux-baseos.repo" "$T/orig-alma"; cp "$T/repos/custom.repo" "$T/orig-custom" st() { "$R" status >"$T/st"; grep -q "$1" "$T/st"; } same() { cmp -s "$1" "$2"; } echo "PKGCACHE_CA=$T/ca.crt" >"$T/pkgcache-host.conf" # no ENABLED => OFF # ---- default OFF ---- st '^setting: OFF' && st '^mode: off' && ok "default: setting OFF, mode off" || bad "default status" if "$R" apply >/dev/null 2>&1; then bad "apply without URL must refuse while OFF"; else ok "apply without URL refuses while OFF"; fi "$R" sync >/dev/null; same "$T/repos/almalinux-baseos.repo" "$T/orig-alma" && ok "sync while OFF leaves stock repos untouched" || bad "sync OFF touched repos" [ ! -e "$T/units/pkgcache-host-health.timer" ] && ok "sync while OFF: no health timer" || bad "timer present while OFF" # ---- ON via setting ---- printf 'PKGCACHE_ENABLED=1\nPKGCACHE_URL=https://cache.test\nPKGCACHE_CA=%s\n' "$T/ca.crt" >"$T/pkgcache-host.conf" "$R" sync >/dev/null grep -q '^baseurl=https://cache.test/almalinux/\$releasever/BaseOS/\$basearch/os/ https://repo.almalinux.org/almalinux/' "$T/repos/almalinux-baseos.repo" && ok "sync ON: cache-first baseurl with origin as 2nd URL" || bad "sync ON rewrite" same "$T/repos/custom.repo" "$T/orig-custom" && ok "unmanaged repo untouched" || bad "unmanaged repo changed" [ -e "$T/units/pkgcache-host-health.timer" ] && grep -q 'OnUnitActiveSec=5min' "$T/units/pkgcache-host-health.timer" && ok "health timer installed (5 min)" || bad "timer missing" st '^mode: cache-first' && ok "status: mode cache-first" || bad "status cache-first" "$R" sync >/dev/null; [ "$(ls -d "$T"/bk/pkgcache-repo-backup-* | wc -l)" = 1 ] && ok "second sync does not take a 2nd (poisoned) backup" || bad "double backup" # ---- health: fallback after 2 failures, restore after 3 successes ---- FAKE_CACHE=empty "$HL" run 2>/dev/null; "$HL" mode | grep -q cache-first && ok "1 failure: still cache-first" || bad "1 failure" FAKE_CACHE=empty "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "2 failures in a row: origin-only" || bad "no fallback" grep -q '^baseurl=https://repo.almalinux.org/almalinux/\$releasever/BaseOS/\$basearch/os/$' "$T/repos/almalinux-baseos.repo" && grep -q '^#pkgcache-cachefirst baseurl=https://cache.test/' "$T/repos/almalinux-baseos.repo" && ok "origin-only rewrite keeps the marker line" || bad "origin-only content" same "$T/repos/custom.repo" "$T/orig-custom" && ok "fallback never touches unmanaged repos" || bad "fallback touched custom.repo" st '^mode: origin-only' && ok "status: mode origin-only" || bad "status origin-only" "$HL" run 2>/dev/null; "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "2 successes: still origin-only" || bad "restored too early" FAKE_CACHE=noasc "$HL" run 2>/dev/null; "$HL" run 2>/dev/null; "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "a bad .asc resets the success streak" || bad "streak not reset" "$HL" run 2>/dev/null; [ "$("$HL" mode)" = cache-first ] && ok "3 successes: cache-first again" || bad "no restore" ls "$T"/repos/.pkgcache-* >/dev/null 2>&1 && bad "temp file left behind" || ok "no temp files left (atomic rename)" grep -q 'repomd.xml.asc' "$T/curl.log" && ok "probe fetched repomd.xml.asc" || bad ".asc not probed" # both down => not counted FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null; FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null; FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null [ "$("$HL" mode)" = cache-first ] && ok "cache+origin both down: no fallback" || bad "network outage caused a fallback" FAKE_CACHE=refused "$HL" run 2>/dev/null; FAKE_CACHE=refused "$HL" run 2>/dev/null; [ "$("$HL" mode)" = origin-only ] && ok "connection refused counts as failure" || bad "refused not counted" # ---- OFF via setting reverts cleanly, even from origin-only ---- echo "PKGCACHE_ENABLED=0" >"$T/pkgcache-host.conf"; printf 'PKGCACHE_CA=%s\n' "$T/ca.crt" >>"$T/pkgcache-host.conf" "$R" sync >/dev/null same "$T/repos/almalinux-baseos.repo" "$T/orig-alma" && same "$T/repos/custom.repo" "$T/orig-custom" && ok "setting OFF: stock repos restored byte-identical (from origin-only)" || bad "revert not identical" [ ! -e "$T/units/pkgcache-host-health.timer" ] && [ ! -e "$T/state/state" ] && ok "setting OFF: timer + state removed" || bad "timer/state left" st '^mode: off' && ok "status: mode off after revert" || bad "status after revert" exit $fail