#!/usr/bin/env bash ## pkgcache-selfcheck.test.sh — fixture test for pkg-cache/pkgcache-selfcheck.sh. ## Fakes findmnt/curl/systemctl/logger via PATH; one extra case uses a REAL ## read-only bind mount when `sudo -n` works (skipped otherwise). set -euo pipefail HERE=$(cd "$(dirname "$0")/../.." && pwd) S="$HERE/pkg-cache/pkgcache-selfcheck.sh" T=$(mktemp -d); trap 'rm -rf "$T"' EXIT fail=0 ok() { echo "ok - $1"; } bad() { echo "FAIL - $1"; fail=1; } mkdir -p "$T/bin" "$T/disk1" "$T/disk2" cat >"$T/bin/findmnt" <<'EOS' #!/bin/sh # last arg = path; $FAKE_RO = path that reports ro for a; do p=$a; done case "$p" in "$FAKE_RO") echo "ro,relatime" ;; *) echo "rw,relatime" ;; esac EOS cat >"$T/bin/curl" <<'EOS' #!/bin/sh # FAKE_PROBE=ok|http500|empty|refused ; FAKE_ORIGIN=ok|down for a; do last=$a; done case "$last" in *probe*) case "$FAKE_PROBE" in ok) printf -- '-----BEGIN PGP PUBLIC KEY BLOCK-----\nx\n200'; exit 0;; http500) printf 'err\n500'; exit 0;; empty) echo "curl: (52) Empty reply from server" >&2; exit 52;; refused) echo "curl: (7) refused" >&2; exit 7;; esac;; *origin*) [ "$FAKE_ORIGIN" = ok ] && exit 0 || exit 7;; esac EOS cat >"$T/bin/systemctl" <<'EOS' #!/bin/sh echo "$*" >>"$CALLS" case "$1" in is-active) [ -e "$ACTIVE" ]; exit $?;; start) touch "$ACTIVE";; stop) rm -f "$ACTIVE";; esac exit 0 EOS printf '#!/bin/sh\nexit 0\n' >"$T/bin/logger"; chmod +x "$T/bin/"* export PATH="$T/bin:$PATH" CALLS="$T/calls" ACTIVE="$T/active" export PKGCACHE_STATE_DIR="$T/state" PKGCACHE_CHECK_PATHS="$T/disk1 $T/disk2" export PKGCACHE_PROBE_URL=http://probe PKGCACHE_ORIGIN_URL=http://origin export PKGCACHE_SERVICES="nginx docker-registry" # explicit: never rely on the script default in a test export PKGCACHE_NGINX_TEST=true FAKE_RO=none FAKE_PROBE=ok FAKE_ORIGIN=ok run() { "$S" >"$T/out" 2>&1; } reset() { rm -rf "$T/state" "$T/calls"; touch "$T/calls" "$ACTIVE"; } stopped() { [ ! -e "$ACTIVE" ] && grep -q '^stop nginx' "$CALLS"; } reset; run && ! stopped && ok "healthy: nothing stopped" || bad "healthy" [ -z "$(ls -A "$T/disk1")" ] && ok "test file removed" || bad "test file left behind" reset; FAKE_RO="$T/disk2" run && bad "ro disk exit" || { stopped && grep -q '^stop docker-registry' "$CALLS" && ok "ro mount: nginx+registry stopped" || bad "ro mount not stopped"; } reset; chmod 555 "$T/disk1"; if [ "$(id -u)" != 0 ]; then run && bad "unwritable exit" || { stopped && ok "write failure: stopped" || bad "write failure not stopped"; }; fi; chmod 755 "$T/disk1" reset; FAKE_PROBE=empty run && bad "empty reply exit" || { stopped && ok "empty reply (curl 52), origin up: stopped" || bad "empty reply not stopped"; } reset; FAKE_PROBE=http500 run && bad "500 exit" || { stopped && ok "HTTP 500, origin up: stopped" || bad "500 not stopped"; } reset; FAKE_PROBE=empty FAKE_ORIGIN=down run && ! stopped && ok "origin down too: warn only, not stopped" || bad "upstream outage must not stop" # recovery: needs 3 consecutive good runs, only if WE stopped it reset; FAKE_PROBE=empty run || true export FAKE_PROBE=ok run; stopped && ok "recovery 1/3: still stopped" || bad "recovery 1" FAKE_RO="$T/disk1" run || true; run; stopped && ok "failure resets the streak (1/3 again)" || bad "streak reset" run; stopped && ok "recovery 2/3: still stopped" || bad "recovery 2" run && [ -e "$ACTIVE" ] && [ ! -e "$T/state/stopped" ] && grep -q '^start nginx' "$CALLS" && ok "3rd healthy run: services started, marker cleared" || bad "recovery 3" # nginx -t failure blocks the restart reset; FAKE_PROBE=empty run || true; export FAKE_PROBE=ok run; run; PKGCACHE_NGINX_TEST=false run && bad "nginx -t fail exit" || { [ ! -e "$ACTIVE" ] && ok "bad nginx config: stays stopped" || bad "started despite bad config"; } # restart whose fetch check fails is stopped again reset; FAKE_PROBE=empty run || true; run; run; FAKE_PROBE=empty run && bad "restart-fail exit" || { [ ! -e "$ACTIVE" ] && [ -e "$T/state/stopped" ] && ok "restart with failing fetch: stopped again" || bad "restart-fail"; } # never start something an admin stopped reset; rm -f "$ACTIVE"; run && ! grep -q '^start' "$CALLS" && ok "admin-stopped nginx is left alone" || bad "admin stop" # empty services list must mean "manage nothing", never the real defaults reset; PKGCACHE_SERVICES="" FAKE_PROBE=empty run || true ! grep -q '^stop' "$CALLS" && ok "empty PKGCACHE_SERVICES: nothing stopped" || bad "empty services list stopped something" # something else restarts nginx while our marker exists: re-checked, stopped again if still bad reset; FAKE_PROBE=empty run || true; touch "$ACTIVE" FAKE_RO="$T/disk1" run || true [ ! -e "$ACTIVE" ] && ok "nginx restarted by someone else while unhealthy: stopped again" || bad "fight: left running" reset; FAKE_PROBE=empty run || true; touch "$ACTIVE" run && [ -e "$ACTIVE" ] && [ ! -e "$T/state/stopped" ] && ok "nginx restarted by someone else and healthy: left running, marker cleared" || bad "fight: healthy case" # REAL read-only bind mount mkdir -p "$T/real" "$T/robind" if sudo -n mount --bind "$T/real" "$T/robind" 2>/dev/null && sudo -n mount -o remount,bind,ro "$T/robind"; then rm "$T/bin/findmnt"; reset; PKGCACHE_CHECK_PATHS="$T/robind" run && bad "real ro exit" || { stopped && ok "REAL read-only bind mount: stopped" || bad "real ro"; } sudo -n umount "$T/robind" else echo "skip - cannot bind-mount here, skipping the real ro bind mount case"; fi exit $fail