spark-supervisor
Process supervisor and health monitor for sovereign daemons
Brought by drakestapleton
spark-supervisor / tests / user.sh
| 1 | #!/bin/sh |
| 2 | # user.sh: unprivileged behaviour of out/aien-supervise in a scratch state directory: restart with |
| 3 | # backoff, oneshot period, oneshot timeout, start/stop/restart requests, TERM-to-KILL escalation, |
| 4 | # environment/umask/dir, log rotation, refusal of a bad service file, shutdown. No root, no sudo. |
| 5 | # sh tests/user.sh prints one line per case and USER PASS|FAIL cases=N fail=N |
| 6 | set -u |
| 7 | here=$(cd "$(dirname "$0")/.." && pwd) |
| 8 | B=$here/out/aien-supervise |
| 9 | [ -x "$B" ] || { echo "user.sh: build first (tools/build.sh)" >&2; exit 2; } |
| 10 | T=$(mktemp -d /tmp/aien-supervise-test.XXXXXX) |
| 11 | export AIEN_SUPERVISE_STATE=$T/state AIEN_SUPERVISE_USERDIR=$T/user AIEN_SUPERVISE_ROOTDIR=$T/root |
| 12 | mkdir -p "$T/user" "$T/root" |
| 13 | pass=0; fail=0 |
| 14 | ok() { pass=$((pass+1)); echo "PASS $*"; } |
| 15 | bad() { fail=$((fail+1)); echo "FAIL $*"; } |
| 16 | st() { "$B" status "$1" | tail -1; } |
| 17 | cleanup() { "$B" shutdown >/dev/null 2>&1; sleep 3; rm -rf "$T"; } |
| 18 | trap cleanup EXIT |
| 19 | |
| 20 | printf 'i=0; while :; do echo "tick $i HOME=${HOME:-unset} umask=$(umask) pwd=$(pwd)"; i=$((i+1)); sleep 1; done\n' > "$T/tick.sh" |
| 21 | printf 'echo crashing; sleep 1; exit 3\n' > "$T/crash.sh" |
| 22 | printf "trap 'echo ignoring TERM' TERM\nwhile :; do sleep 0.2; done\n" > "$T/stubborn.sh" |
| 23 | printf 'i=0; while [ $i -lt 400 ]; do echo line-$i-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; i=$((i+1)); done; sleep 100\n' > "$T/chatty.sh" |
| 24 | printf 'exec /usr/bin/sh %s/tick.sh\nrestart always\nenv HOME=/tmp\numask 0027\ndir /tmp\n' "$T" > "$T/user/ticker.svc" |
| 25 | printf 'exec /usr/bin/sh %s/crash.sh\nrestart on-failure\nbackoff 1 4\n' "$T" > "$T/user/crash.svc" |
| 26 | printf 'exec /usr/bin/true\ntype oneshot\nevery 2\n' > "$T/user/once.svc" |
| 27 | printf 'exec /usr/bin/sleep 100\ntype oneshot\ntimeout 2\n' > "$T/user/slow.svc" |
| 28 | printf 'exec /usr/bin/sh %s/stubborn.sh\nrestart always\nstoptime 2\n' "$T" > "$T/user/stubborn.svc" |
| 29 | printf 'exec /usr/bin/sh %s/chatty.sh\nlog-max 4000\nlog-keep 2\n' "$T" > "$T/user/chatty.svc" |
| 30 | printf 'exec /usr/bin/true\nuser root\n' > "$T/user/sneaky.svc" |
| 31 | printf 'exec /usr/bin/sleep 100\nautostart no\n' > "$T/user/held.svc" |
| 32 | printf 'exec /usr/bin/sleep 100\nautostart yes\n' > "$T/user/eager.svc" |
| 33 | printf 'exec /usr/bin/sleep 100\nautostart 0\n' > "$T/user/numauto.svc" |
| 34 | printf 'exec /usr/bin/sleep 100\nautostart\n' > "$T/user/bareauto.svc" |
| 35 | printf 'exec /usr/bin/sleep 100\nautostart yes extra\n' > "$T/user/longauto.svc" |
| 36 | printf 'exec /usr/bin/true\nbogus 1\n' > "$T/user/bad.svc" |
| 37 | |
| 38 | "$B" ensure; sleep 8 |
| 39 | "$B" status | head -1 | grep -q '^daemon running' && ok "ensure started the daemon" || bad "daemon not running" |
| 40 | "$B" ensure; [ "$(pgrep -fc "$B run")" = 1 ] && ok "second ensure is a no-op" || bad "ensure started a second daemon" |
| 41 | grep -q 'tick 1 HOME=/tmp umask=0027 pwd=/tmp' "$T/state/log/ticker.log" && ok "env, umask and dir applied" || bad "ticker env/umask/dir: $(tail -1 "$T/state/log/ticker.log")" |
| 42 | c=$(grep -c 'crash exited status=3' "$T/state/daemon.log") |
| 43 | grep -q 'crash backoff next_in_s=4' "$T/state/daemon.log" && [ "$c" -ge 3 ] && ok "on-failure restart with backoff 1,2,4 (exits=$c)" || bad "crash backoff: $(grep crash "$T/state/daemon.log" | tr '\n' ';')" |
| 44 | r=$(grep -c 'once exited status=0' "$T/state/daemon.log"); [ "$r" -ge 3 ] && ok "oneshot every 2 s ran $r times" || bad "oneshot ran $r times" |
| 45 | grep -q 'slow timeout, stopping' "$T/state/daemon.log" && st slow | grep -q 'state=failed' && ok "oneshot timeout -> SIGTERM, state failed" || bad "slow: $(st slow)" |
| 46 | st sneaky | grep -q 'state=error' && ok "sandbox key in a user service refused" || bad "sneaky: $(st sneaky)" |
| 47 | st held | grep -q 'state=stopped' && ok "autostart no: loaded, not started" || bad "held: $(st held)" |
| 48 | st eager | grep -q 'state=running' && ok "autostart yes: started" || bad "eager: $(st eager)" |
| 49 | st numauto | grep -q 'state=error' && ok "autostart with a number refused" || bad "numauto: $(st numauto)" |
| 50 | st bareauto | grep -q 'state=error' && ok "autostart without a value refused" || bad "bareauto: $(st bareauto)" |
| 51 | st longauto | grep -q 'state=error' && ok "autostart with two values refused" || bad "longauto: $(st longauto)" |
| 52 | "$B" start held >/dev/null; sleep 2 |
| 53 | st held | grep -q 'state=running' && ok "autostart no: starts on request" || bad "held after start: $(st held)" |
| 54 | st bad | grep -q 'state=error' && ok "unknown key refused" || bad "bad: $(st bad)" |
| 55 | "$B" stop ticker | grep -q 'state=stopped' && ok "stop request" || bad "stop: $(st ticker)" |
| 56 | "$B" start ticker | grep -q 'state=running' && ok "start request" || bad "start: $(st ticker)" |
| 57 | p1=$(st ticker | sed 's/.* pid=\([0-9]*\).*/\1/'); "$B" restart ticker >/dev/null; sleep 1 |
| 58 | p2=$(st ticker | sed 's/.* pid=\([0-9]*\).*/\1/'); [ "$p1" != "$p2" ] && st ticker | grep -q state=running && ok "restart request (pid $p1 -> $p2)" || bad "restart: $p1 -> $p2" |
| 59 | "$B" stop stubborn >/dev/null; sleep 3 |
| 60 | st stubborn | grep -q 'last=signal:9' && grep -q 'stubborn stop timeout, killing' "$T/state/daemon.log" && ok "TERM ignored -> SIGKILL after stoptime" || bad "stubborn: $(st stubborn)" |
| 61 | [ -f "$T/state/log/chatty.log.1" ] && ok "log rotated at log-max (chatty.log.1 exists)" || bad "no rotation: $(ls "$T/state/log")" |
| 62 | "$B" stop nosuch >/dev/null 2>&1; [ $? != 0 ] && ok "request for unknown service fails" || bad "unknown service request succeeded" |
| 63 | "$B" shutdown; sleep 4 |
| 64 | "$B" status | head -1 | grep -q 'not running' && ! pgrep -f "$T/tick.sh" >/dev/null && ok "shutdown stops services and daemon" || bad "after shutdown: $("$B" status | head -1)" |
| 65 | t0=$(date +%s); "$B" shutdown >/dev/null 2>&1; rc=$?; t1=$(date +%s) |
| 66 | [ $rc != 0 ] && [ $((t1-t0)) -lt 5 ] && [ -z "$(ls -A "$T/state/ctl")" ] && ok "shutdown with no daemon: refused at once, no request left" || bad "shutdown with no daemon: rc=$rc $((t1-t0))s ctl=$(ls -A "$T/state/ctl" | tr '\n' ' ')" |
| 67 | : > "$T/state/ctl/all.exit" |
| 68 | "$B" ensure; sleep 4 |
| 69 | "$B" status | head -1 | grep -q '^daemon running' && [ ! -e "$T/state/ctl/all.exit" ] && ok "stale exit request does not stop a new daemon" || bad "stale exit: $("$B" status | head -1)" |
| 70 | "$B" shutdown >/dev/null; sleep 4 |
| 71 | "$B" status | head -1 | grep -q 'not running' && ok "shutdown after a stale request still works" || bad "second shutdown: $("$B" status | head -1)" |
| 72 | if [ $fail = 0 ]; then v=PASS; else v=FAIL; fi |
| 73 | echo "USER $v cases=$((pass+fail)) fail=$fail" |
| 74 | [ $fail = 0 ] |