Files
glacier-ng/tests/overwrite_alpine.sh
T
2026-10-29 09:15:34 -04:00

127 lines
6.3 KiB
Bash

#!/bin/sh
# -O/--overwrite tests for syspkg and gpkg. Runs inside a THROWAWAY alpine
# container as root — it displaces Alpine's own /usr/include and /usr/lib
# files, so never run it on a real system.
#
# Run it twice: once as-is (/glacier on the same filesystem as /usr, so
# displacing is a rename()), once with --tmpfs /glacier (different
# filesystem, so it exercises the copy+unlink EXDEV fallback):
#
# docker run --rm \
# -v /home/lw/Projects/glacier-ng:/src:ro \
# -v /home/lw/Projects/libglacier-ng:/libsrc:ro \
# -v /home/lw/Projects/glacier-ng/tests/overwrite_alpine.sh:/t.sh:ro \
# [--tmpfs /glacier:exec] alpine:latest sh /t.sh
#
# No gpkg/user-scope case: a user's links tree is swapped out wholesale on
# every commit, so nothing non-glacier can be sitting at a link
# destination when the post-commit relink runs. Only system-scope base
# packages, linking into /usr, can ever hit a conflict.
set -u
apk add --no-cache build-base luajit-dev libarchive-dev libconfig-dev linux-headers >/dev/null 2>&1 \
|| { echo "apk failed"; exit 1; }
FAILS=0
hr() { echo; echo "=== $* ==="; }
check() { # check DESCRIPTION COMMAND...
desc=$1; shift
if "$@"; then echo "PASS: $desc"; else echo "FAIL: $desc"; FAILS=$((FAILS + 1)); fi
}
# Strip the per-path "Refusing to overwrite" flood; print how many there were.
quiet() { "$@" 2>&1 | sed 's/\x1b\[[0-9;]*m//g' | awk '/Refusing to overwrite non-glacier/ {n++; next} {print} END {if (n) print "(" n " x \"Refusing to overwrite non-glacier path\")"}'; }
# run CMD: like quiet, but keeps the output in $OUT and the exit status
# in $STATUS.
run() {
OUT=$(sh -c "$1; echo \"exit=\$?\"" 2>&1 | sed 's/\x1b\[[0-9;]*m//g')
STATUS=$(echo "$OUT" | sed -n 's/^exit=//p' | tail -1)
OUT=$(echo "$OUT" | grep -v '^exit=')
echo "$OUT" | awk '/Refusing to overwrite non-glacier/ {n++; next} {print} END {if (n) print "(" n " x \"Refusing to overwrite non-glacier path\")"}'
}
last_line() { echo "$OUT" | tail -1; }
sha() { sha256sum "$1" | cut -d' ' -f1; }
is_reg() { [ -f "$1" ] && [ ! -L "$1" ]; }
links_into_store() { case "$(readlink "$1")" in "$2"/*) return 0 ;; esac; return 1; }
hr "0. build libglacier-ng against musl"
cp -a /libsrc /build && cd /build && make clean >/dev/null && make >/tmp/make.log 2>&1 \
&& echo "build ok" || { tail -30 /tmp/make.log; exit 1; }
cp -a /src /opt/glacier
mkdir -p /opt/glacier/lib/glacier
cp /build/build/lib/shared/libglacier_*.so /opt/glacier/lib/glacier/
chown -R root:root /opt/glacier && chmod -R go-w /opt/glacier
# A throwaway key trusted for base (REPO_DESIGN.txt section 7), and the
# copies' sidecars signed with it.
mkdir -p /etc/glacier/keys
/build/build/bin/gsign -G -p /etc/glacier/keys/t.pub -s /root/t.sec -c t >/dev/null
printf 'repos = (\n\t{ name = "base"; keys = [ "t" ]; }\n);\n' > /etc/glacier/repos.conf
for s in /opt/glacier/*.gpkg.sha256 /opt/glacier/pkgs/*.gpkg.sha256; do
[ -e "$s" ] && /build/build/bin/gsign -S -s /root/t.sec -m "$s" >/dev/null
done
cd /opt/glacier
SP=/opt/glacier/syspkg
SYS_STORE=/glacier/sys/store
DISP=$SYS_STORE/.displaced
echo "/usr fs: $(stat -f -c %i /usr 2>/dev/null) /glacier fs: $(mkdir -p /glacier; stat -f -c %i /glacier)"
$SP -n >/dev/null
STDIO=/usr/include/stdio.h
LIBC=/usr/lib/libc.so # Alpine ships this as a symlink, not a file
ORIG_SHA=$(sha $STDIO)
ORIG_MODE=$(stat -c '%a %u %g' $STDIO)
ORIG_LIBC_TARGET=$(readlink $LIBC)
hr "1. install base package without -O: originals untouched"
run "echo y | $SP -l pkgs/musl-1.2.5.gpkg"
check "stdio.h still Alpine's regular file" is_reg $STDIO
NREF=$(echo "$OUT" | grep -c 'Refusing to overwrite non-glacier')
check "refusals end in an error, not \"Completed\"" test "$(last_line)" = "[x] $NREF paths were not linked."
check "and exit status 1" test "$STATUS" = 1
check "no displaced tree created" test ! -e $DISP
hr "2. reinstall with -O: originals displaced, glacier symlinks in place"
run "echo y | $SP -O -l pkgs/musl-1.2.5.gpkg"
check "nothing refused: Completed with no errors" test "$(last_line)" = "[✓] Completed with no errors."
check "and exit status 0" test "$STATUS" = 0
check "stdio.h is now a glacier symlink" links_into_store $STDIO $SYS_STORE
check "original stdio.h kept, content intact" test "$(sha $DISP$STDIO 2>/dev/null)" = "$ORIG_SHA"
check "original stdio.h mode/owner kept" test "$(stat -c '%a %u %g' $DISP$STDIO 2>/dev/null)" = "$ORIG_MODE"
check "libc.so (a foreign symlink) displaced as a symlink" test "$(readlink $DISP$LIBC 2>/dev/null)" = "$ORIG_LIBC_TARGET"
check "libc.so now a glacier symlink" links_into_store $LIBC $SYS_STORE
check "overwrite marker consumed" test -z "$(find $SYS_STORE -name .glacier-overwrite)"
hr "3. unrelated transaction: displaced originals survive stage seeding"
quiet sh -c "echo y | $SP -l kilo-0.0.1.gpkg"
check "displaced stdio.h still present after another commit" test "$(sha $DISP$STDIO 2>/dev/null)" = "$ORIG_SHA"
check "stdio.h still a glacier symlink" links_into_store $STDIO $SYS_STORE
hr "4. -O on a later install doesn't spread to other packages"
# Only /usr can hold a foreign file at relink time (the links trees are
# swapped out wholesale on every commit), so plant one at a path musl
# links to, then install a DIFFERENT package with -O: musl's path must
# stay refused, since musl itself wasn't installed with -O this time.
FOREIGN=/usr/include/libintl.h
rm -f $FOREIGN && echo foreign > $FOREIGN
run "echo y | $SP -O -l pkgs/sbase-0.0.1.gpkg"
check "foreign file at a musl path untouched" test "$(cat $FOREIGN)" = foreign
check "musl's standing conflict doesn't fail sbase's install" test "$STATUS" = 0
check "and not displaced" test ! -e $DISP$FOREIGN
rm -f $FOREIGN
hr "5. remove: originals restored"
quiet sh -c "echo y | $SP -x musl"
check "stdio.h restored as a regular file" is_reg $STDIO
check "restored stdio.h content matches" test "$(sha $STDIO)" = "$ORIG_SHA"
check "restored stdio.h mode/owner match" test "$(stat -c '%a %u %g' $STDIO)" = "$ORIG_MODE"
check "libc.so restored as Alpine's symlink" test "$(readlink $LIBC)" = "$ORIG_LIBC_TARGET"
check "no musl originals left in displaced tree" test -z "$(find $DISP/usr -type f -o -type l 2>/dev/null | grep -v sbase)"
check "Alpine toolchain still works" sh -c 'echo "int main(void){return 0;}" | cc -x c - -o /tmp/a.out && /tmp/a.out'
hr "6. help / usage"
$SP -h | grep -- --overwrite
./gpkg -h | grep -- --overwrite
echo
echo "FAILURES: $FAILS"
[ $FAILS -eq 0 ]