Merge pull request 'pkg-cache: host package cache opt-in (default OFF) + origin fallback health check' (#68) from feat/pkgcache-host-optin into trunk
Cloud Apache Container / plan (push) Successful in 4s
Cloud Apache Container / Shell-Checks (push) Failing after 1m47s
Cloud Apache Container / Build-Shared-OLS (push) Successful in 3m2s
Cloud Apache Container / Build-LiteSpeed-Images (82) (push) Successful in 5m50s
Cloud Apache Container / Build-LSPHP-Images (82) (push) Successful in 6m2s
Cloud Apache Container / Build-Shared-httpd (push) Successful in 6m0s
Cloud Apache Container / Build-LiteSpeed-Images (83) (push) Successful in 5m27s
Cloud Apache Container / Build-LiteSpeed-Images (84) (push) Successful in 5m14s
Cloud Apache Container / Build-LiteSpeed-Images (85) (push) Successful in 5m59s
Cloud Apache Container / Build-LSPHP-Images (83) (push) Successful in 6m29s
Cloud Apache Container / Build-LSPHP-Images (84) (push) Successful in 6m25s
Cloud Apache Container / Build-LSPHP-Images (85) (push) Successful in 6m31s
Cloud Apache Container / Build-FPM-Images (82) (push) Successful in 17m55s
Cloud Apache Container / Build-and-Push (82) (push) Successful in 19m15s
Cloud Apache Container / Build-and-Push (83) (push) Successful in 16m18s
Cloud Apache Container / Build-FPM-Images (83) (push) Successful in 16m7s
Cloud Apache Container / Build-FPM-Images (84) (push) Successful in 10m16s
Cloud Apache Container / Build-FPM-Images (85) (push) Successful in 9m45s
Cloud Apache Container / Build-and-Push (84) (push) Successful in 14m43s
Cloud Apache Container / Build-and-Push (85) (push) Successful in 14m34s
Cloud Apache Container / Build-FPM-Images (86) (push) Successful in 9m55s
Cloud Apache Container / Build-and-Push (86) (push) Successful in 11m57s

This commit was merged in pull request #68.
This commit is contained in:
jknapp committed 2026-11-29 15:32:31 +00:00
commit 4c988758cc
5 files changed
+397 -34

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#!/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 '<repomd/>\n200';;
ok) case "$u" in *.asc) printf -- '-----BEGIN PGP SIGNATURE-----\nx\n200';; *) printf '<repomd/>\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"
# mode must not wait for the health lock (a held lock once turned `mode` into an empty answer)
( flock 9; sleep 4 ) 9>"$T/state/lock" & sleep 0.5
m=$(timeout 3 "$HL" mode || true); [ "$m" = origin-only ] && ok "mode answers while the health lock is held" || bad "mode blocked on the lock (got '$m')"
wait
# ---- 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