#!/bin/sh # Replacing an installed version (ROADMAP 2.1): plain -l refuses, -lO # replaces, KEEP_OLD (glacier.conf, no default) governs compressed # backups of the replaced version. Runs INSIDE the Dockerfile.alpine_root # image: # # ./run_docker.sh Dockerfile.alpine_root # builds glacier-alpine_root # docker run --rm -v "$PWD/tests/upgrade_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh apk add --no-cache tar >/dev/null 2>&1 # GNU tar, for the hostile 3.0.0 below FAILS=0 check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; } nocolor() { sed 's/\x1b\[[0-9;]*m//g'; } has() { echo "$OUT" | grep -q "$1"; } CONF=/etc/glacier/glacier.conf # A package "upg" at VERSION, with a file, a soname symlink and a manifest # description that only the real (saved) manifest carries. mkpkg() { # mkpkg VERSION -> /srv/upg-VERSION.gpkg (+ sidecar) w=$(mktemp -d); d=$w/upg-$1 mkdir -p $d/files/usr/bin $d/files/usr/lib printf '#!/bin/sh\necho upg %s\n' "$1" > $d/files/usr/bin/upg; chmod 755 $d/files/usr/bin/upg echo "lib $1" > $d/files/usr/lib/libupg.so.1.0; ln -s libupg.so.1.0 $d/files/usr/lib/libupg.so.1 printf 'PKG_NAME = "upg";\nPKG_VER = "%s";\nPKG_DESC = "real manifest %s";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "extra";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' "$1" "$1" > $d/manifest.gpm.cfg tar czf /srv/upg-$1.gpkg -C $w upg-$1 printf 'sha256:%s\n' "$(sha256sum /srv/upg-$1.gpkg | cut -d' ' -f1)" > /srv/upg-$1.gpkg.sha256 rm -rf $w } mkdir -p /srv; mkpkg 1.0.0; mkpkg 2.0.0 # 3.0.0: valid manifest and hash, but an entry escaping files/ via "..": # refused at extraction, i.e. AFTER the backup has been written and the # old version removed from the stage. Exercises the abort path for real. w=$(mktemp -d); mkdir -p $w/upg-3.0.0/files/usr/bin printf 'PKG_NAME = "upg";\nPKG_VER = "3.0.0";\nPKG_DESC = "x";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "extra";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' > $w/upg-3.0.0/manifest.gpm.cfg echo evil > $w/evil tar cf $w/p.tar -C $w upg-3.0.0 tar rf $w/p.tar -C $w --transform 's|^evil|upg-3.0.0/files/../../evil|' evil gzip -c $w/p.tar > /srv/upg-3.0.0.gpkg printf 'sha256:%s\n' "$(sha256sum /srv/upg-3.0.0.gpkg | cut -d' ' -f1)" > /srv/upg-3.0.0.gpkg.sha256 rm -rf $w S=/glacier/sys/store/extra/upg; B=/glacier/sys/backup/extra/upg cd /app && ./syspkg -n >/dev/null echo y | ./syspkg -l /srv/upg-1.0.0.gpkg >/dev/null 2>&1 check "1.0.0 installed" test -d $S/upg-1.0.0 check "manifest saved beside the installed files" test -f $S/upg-1.0.0/.glacier-manifest echo "=== plain -l of another version: refused, no hint, no prompt" OUT=$(echo y | ./syspkg -l /srv/upg-2.0.0.gpkg 2>&1 | nocolor); echo "$OUT" check "refused with the installed version named" has "extra::upg 1.0.0 is already installed." check "nothing else said (no prompt, no -lO hint)" test "$(echo "$OUT" | wc -l)" -eq 1 check "still 1.0.0" test -d $S/upg-1.0.0 -a ! -d $S/upg-2.0.0 echo "=== KEEP_OLD problems are refused before the prompt" cp $CONF /tmp/conf.bak rm $CONF OUT=$(echo y | ./syspkg -lO /srv/upg-2.0.0.gpkg 2>&1 | nocolor); echo "$OUT" check "no glacier.conf" has "Cannot read /etc/glacier/glacier.conf." echo '// nothing here' > $CONF OUT=$(echo y | ./syspkg -lO /srv/upg-2.0.0.gpkg 2>&1 | nocolor); echo "$OUT" check "KEEP_OLD absent" has "KEEP_OLD is not set" echo 'KEEP_OLD = -1;' > $CONF OUT=$(echo y | ./syspkg -lO /srv/upg-2.0.0.gpkg 2>&1 | nocolor); echo "$OUT" check "KEEP_OLD negative" has "must be a whole number" check "none of those reached the prompt or changed anything" sh -c "! echo '$OUT' | grep -q Proceed && test -d $S/upg-1.0.0" OUT=$(echo y | ./syspkg -lO /srv/upg-1.0.0.gpkg 2>&1 | nocolor) check "same-version reinstall doesn't need KEEP_OLD" has "Completed with no errors" cp /tmp/conf.bak $CONF echo "=== -lO replaces, KEEP_OLD = 1 backs up" OUT=$(echo y | ./syspkg -lO /srv/upg-2.0.0.gpkg 2>&1 | nocolor); echo "$OUT" check "summary shows 1.0.0 -> 2.0.0" has "1.0.0 -> 2.0.0" check "only 2.0.0 in the store" test -d $S/upg-2.0.0 -a ! -d $S/upg-1.0.0 check "links point at 2.0.0" sh -c "readlink /glacier/sys/links/bin/upg | grep -q upg-2.0.0" check "backup + sidecar written" test -f $B/upg-1.0.0.gpkg -a -f $B/upg-1.0.0.gpkg.sha256 check "sidecar matches" sh -c "test -f $B/upg-1.0.0.gpkg && test \"\$(cut -c8- $B/upg-1.0.0.gpkg.sha256)\" = \"\$(sha256sum $B/upg-1.0.0.gpkg | cut -d' ' -f1)\"" check "backup carries the REAL manifest" sh -c "tar xzOf $B/upg-1.0.0.gpkg upg-1.0.0/manifest.gpm.cfg | grep -q 'real manifest 1.0.0'" check "backup keeps the symlink" sh -c "tar tvzf $B/upg-1.0.0.gpkg | grep -q 'libupg.so.1 -> libupg.so.1.0'" check "backup has no glacier markers" sh -c "tar tzf $B/upg-1.0.0.gpkg >/tmp/l && ! grep -q glacier- /tmp/l" check "no .part left" sh -c "test -d $B && ! ls $B | grep -q part" echo "=== a backup is an installable package (restore path)" OUT=$(echo y | ./syspkg -lO $B/upg-1.0.0.gpkg 2>&1 | nocolor); echo "$OUT" | tail -2 check "restored 1.0.0 from its backup" test -d $S/upg-1.0.0 -a ! -d $S/upg-2.0.0 check "KEEP_OLD = 1: only the newest backup kept (2.0.0)" test -f $B/upg-2.0.0.gpkg -a ! -f $B/upg-1.0.0.gpkg echo "=== a failed replacement leaves no backup and changes nothing" rm -f $B/* OUT=$(echo y | ./syspkg -lO /srv/upg-3.0.0.gpkg 2>&1 | nocolor); echo "$OUT" | tail -2 check "it got past the backup step (extraction refused)" has "Refusing unsafe archive entry" check "still 1.0.0" test -d $S/upg-1.0.0 -a ! -d $S/upg-3.0.0 check "no backup of 1.0.0 created by the failed attempt" test ! -f $B/upg-1.0.0.gpkg check "no .part left" sh -c "test -d $B && ! ls $B | grep -q part" echo "=== KEEP_OLD = 0: replace without a backup" rm -f $B/* echo 'KEEP_OLD = 0;' > $CONF echo y | ./syspkg -lO /srv/upg-2.0.0.gpkg >/dev/null 2>&1 check "replaced" test -d $S/upg-2.0.0 check "no backup of 1.0.0" test ! -f $B/upg-1.0.0.gpkg cp /tmp/conf.bak $CONF echo "=== gpkg (user scope) does the same" su glacier -c "doas /app/gstore -n" >/dev/null 2>&1 chmod 644 /srv/*.gpkg /srv/*.sha256 su glacier -c "cd /app && echo y | ./gpkg -l /srv/upg-1.0.0.gpkg" >/dev/null 2>&1 OUT=$(su glacier -c "cd /app && echo y | ./gpkg -lO /srv/upg-2.0.0.gpkg" 2>&1 | nocolor); echo "$OUT" | tail -2 check "user backup in the user's own dir" test -f /glacier/usr/1000/backup/extra/upg/upg-1.0.0.gpkg check "owned by the user" test "$(stat -c %u /glacier/usr/1000/backup/extra/upg/upg-1.0.0.gpkg)" = 1000 echo; echo "FAILURES: $FAILS"; [ $FAILS -eq 0 ]