#!/bin/sh
# A journal that needs migrating while other pics have it open: a follower (pic attach) of an
# older pic lets go of it when asked and the migration goes ahead; one that does not is named in
# the error with its pid and the command that ends it; a holder that does not say who it is (a pic
# from before holders said so) is described with the lsof that lists it. pic doctor stops the
# older pics in the way and migrates; a newer pic's journal is reported in plain words. Needs sqlite3.
# Usage: test/pic/holders.sh [--no-faults]
set -eu
PIC=${PIC:?set PIC to the pic binary}
FAULTS=1
[ "${1:-}" = --no-faults ] && FAULTS=0
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-holders.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME" "$T/ws"
pids=
cleanup() {
for p in $pids; do kill "$p" 2>/dev/null || true; done
rm -rf -- "$T"
}
trap cleanup EXIT HUP INT TERM
fail() {
printf 'FAIL: %s\n' "$*" >&2
for f in "$T"/*.err; do [ -s "$f" ] && { printf -- '--- %s\n' "$f" >&2; tail -20 "$f" >&2; }; done
exit 1
}
fixture() { # DB: a journal at this pic's schema, with a conversation
sqlite3 "$1" < "$(dirname "$0")/journal-v3.sql"
"$PIC" --db "$1" --workspace "$T/ws" check > /dev/null || fail "fixture: $1"
CUR=$(version "$1")
}
older() { # DB: make it a schema-18 journal under whoever has it open
sqlite3 "$1" "DROP TABLE pic_verdict; UPDATE pic_meta SET value='18' WHERE key='schema_version'"
}
version() { sqlite3 "$1" "SELECT value FROM pic_meta WHERE key='schema_version'"; }
CUR= # this pic's schema: what a fixture is at once it has opened it
attach() { # DB NAME [ENV...]: a follower of the latest conversation, in the background; its pid in AP
db=$1 name=$2
shift 2
env "$@" "$PIC" --db "$db" --workspace "$T/ws" attach --format json < /dev/null > "$T/$name.out" 2> "$T/$name.err" &
AP=$!
pids="$pids $AP"
i=0
until ls "$db.pic-holders" 2>/dev/null | grep -q "^$AP\."; do
i=$((i + 1))
[ "$i" -lt 300 ] || fail "$name: the follower did not open the journal"
sleep 0.1
done
}
alive() { kill -0 "$1" 2>/dev/null; }
# A holder that says who it is, and does not let go (a follower of this same pic: nothing newer is
# asking): the migration is refused, naming its pid, what it is, and the command that ends it.
DB="$T/named.sqlite"
fixture "$DB"
attach "$DB" named
older "$DB"
if "$PIC" --db "$DB" --workspace "$T/ws" check > /dev/null 2> "$T/named-check.err"; then fail "named: migrated under another pic"; fi
grep -qF "this journal needs updating for this pic and another pic process has it open; pic doctor stops it and updates it" "$T/named-check.err" ||
fail "named: $(cat "$T/named-check.err")"
grep -qF "pid $AP, pic attach (started by pid $$), open since " "$T/named-check.err" || fail "named: no pid: $(cat "$T/named-check.err")"
grep -qF ": kill $AP" "$T/named-check.err" || fail "named: no command: $(cat "$T/named-check.err")"
grep -qF 'let it finish' "$T/named-check.err" && fail "named: told to wait: $(cat "$T/named-check.err")"
[ "$(version "$DB")" = 18 ] || fail "named: the journal was changed"
alive "$AP" || fail "named: the follower went"
kill "$AP"
wait "$AP" 2>/dev/null || :
sleep 3 # past the moment a holder's file is kept for while nobody holds it
"$PIC" --db "$DB" --workspace "$T/ws" check > /dev/null || fail "named: not migrated once the follower had gone"
[ "$(version "$DB")" = "$CUR" ] || fail "named: version not $CUR"
[ -z "$(ls "$DB.pic-holders")" ] || fail "named: the killed follower's file was left: $(ls "$DB.pic-holders")"
[ ! -e "$DB.pic-migrate" ] || fail "named: the request to let go was left"
printf 'PASS: a holder that does not let go is named with its pid and the command that ends it\n'
# A holder from before holders said who they are: described, with the command for pic remote and
# the lsof that lists the rest; nothing tells the user to wait.
DB="$T/old.sqlite"
fixture "$DB"
older "$DB"
perl -e 'use Fcntl qw(:flock); open(my $f, ">>", $ARGV[0]) or die; flock($f, LOCK_SH) or die; open(my $g, ">", $ARGV[1]); close $g; sleep 1 while !-e $ARGV[2]' \
"$DB.pic-lock" "$T/held" "$T/release" &
other=$!
pids="$pids $other"
i=0
while [ ! -e "$T/held" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "old: the other holder did not start"; sleep 0.1; done
if "$PIC" --db "$DB" --workspace "$T/ws" check > /dev/null 2> "$T/old-check.err"; then touch "$T/release"; fail "old: migrated under another pic"; fi
touch "$T/release"
wait "$other"
grep -qF 'It is a pic from before this one that does not say which: if pic remote runs, pic remote restart restarts it' "$T/old-check.err" ||
fail "old: $(cat "$T/old-check.err")"
grep -qF "(lsof " "$T/old-check.err" && grep -qF ".pic-lock lists them all)" "$T/old-check.err" || fail "old: no lsof: $(cat "$T/old-check.err")"
"$PIC" --db "$DB" --workspace "$T/ws" check > /dev/null || fail "old: not migrated once the other pic had gone"
printf 'PASS: a holder from an older pic is described with the commands that find and end it\n'
# pic doctor: a journal at this pic's schema needs nothing; a newer pic's is only reported, as it
# is to every command; no one sees a raw schema number.
DB="$T/doctor.sqlite"
fixture "$DB"
"$PIC" --db "$DB" --workspace "$T/ws" doctor > "$T/doc-current.out" 2>&1 || fail "doctor current: $(cat "$T/doc-current.out")"
grep -qF 'The journal is up to date; nothing to fix.' "$T/doc-current.out" || fail "doctor current: $(cat "$T/doc-current.out")"
"$PIC" --db "$DB" --workspace "$T/ws" doctor --json > "$T/doc-current.json" || fail "doctor current json"
grep -qF '{"type":"doctor","ok":true,"state":"current","message":"The journal is up to date; nothing to fix."}' "$T/doc-current.json" ||
fail "doctor json: $(cat "$T/doc-current.json")"
cp "$DB" "$T/newer.sqlite"
sqlite3 "$T/newer.sqlite" "UPDATE pic_meta SET value='999' WHERE key='schema_version'"
if "$PIC" --db "$T/newer.sqlite" --workspace "$T/ws" doctor > "$T/doc-newer.out" 2>&1; then fail "doctor newer: exit 0"; fi
grep -qF "This workspace's journal was written by a newer pic; update pic (in pic's source: make install-pic)." "$T/doc-newer.out" ||
fail "doctor newer: $(cat "$T/doc-newer.out")"
for cmd in check sessions "ask hi"; do
# shellcheck disable=SC2086
if "$PIC" --db "$T/newer.sqlite" --workspace "$T/ws" $cmd > /dev/null 2> "$T/newer-cmd.err"; then fail "newer: $cmd ran"; fi
grep -qF 'this journal was written by a newer pic; update pic' "$T/newer-cmd.err" || fail "newer: $cmd: $(cat "$T/newer-cmd.err")"
grep -q 'schema' "$T/newer-cmd.err" && fail "newer: $cmd names the schema: $(cat "$T/newer-cmd.err")"
done
"$PIC" --db "$T/newer.sqlite" --workspace "$T/ws" attach --no-follow > /dev/null 2> "$T/newer-attach.err" && fail "newer: attach ran"
grep -qF 'this journal was written by a newer pic; update pic' "$T/newer-attach.err" || fail "newer: attach: $(cat "$T/newer-attach.err")"
printf 'PASS: pic doctor leaves a current journal alone and reports a newer one in plain words, as every command does\n'
# pic doctor on an older journal two pics keep open: one that says what it is and does not let go,
# and one that does not say (its holder file gone, as for a pic from before holders said so).
# Both are stopped, politely, and the journal is migrated; what it did is said.
DB="$T/doctor-old.sqlite"
fixture "$DB"
attach "$DB" doc1
P1=$AP
attach "$DB" doc2
P2=$AP
rm -f "$DB.pic-holders/$P2".*
older "$DB"
"$PIC" --db "$DB" --workspace "$T/ws" doctor > "$T/doc-old.out" 2>&1 || fail "doctor old: $(cat "$T/doc-old.out")"
wait "$P1" 2>/dev/null || :
wait "$P2" 2>/dev/null || :
alive "$P1" && fail "doctor old: $P1 still runs"
alive "$P2" && fail "doctor old: $P2 still runs"
[ "$(version "$DB")" = "$CUR" ] || fail "doctor old: version $(version "$DB")"
grep -qF "This workspace's journal is from an older pic; updating it." "$T/doc-old.out" || fail "doctor old: $(cat "$T/doc-old.out")"
grep -qF "Stopped pid $P1 (pic attach), which had the journal open." "$T/doc-old.out" || fail "doctor old: $P1: $(cat "$T/doc-old.out")"
grep -qF "Stopped pid $P2 (an older pic that does not say what it is), which had the journal open." "$T/doc-old.out" ||
fail "doctor old: $P2: $(cat "$T/doc-old.out")"
grep -qF 'Updated the journal; pic works in this workspace again.' "$T/doc-old.out" || fail "doctor old: $(cat "$T/doc-old.out")"
printf 'PASS: pic doctor stops the pics that keep an older journal open, named or not, and migrates it\n'
if [ "$FAULTS" = 0 ]; then
exit 0
fi
# A follower of an older pic (PIC_TEST_HOLDER_SCHEMA, a dev build's seam) lets go when a newer pic
# asks: the migration goes ahead, and the follower says why it ended.
DB="$T/yield.sqlite"
fixture "$DB"
attach "$DB" yield PIC_TEST_HOLDER_SCHEMA=18
older "$DB"
"$PIC" --db "$DB" --workspace "$T/ws" check > /dev/null 2> "$T/yield-check.err" || fail "yield: not migrated: $(cat "$T/yield-check.err")"
[ "$(version "$DB")" = "$CUR" ] || fail "yield: version not $CUR"
rc=0
wait "$AP" || rc=$?
[ "$rc" = 1 ] || fail "yield: the follower's exit $rc"
grep -qF 'pic: attach: a newer pic is updating this journal; attach again with it' "$T/yield.err" || fail "yield: $(cat "$T/yield.err")"
[ ! -e "$DB.pic-migrate" ] || fail "yield: the request to let go was left"
printf 'PASS: a follower of an older pic lets go of the journal, and the newer pic migrates it\n'