interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / tests / user.sh

74 lines · 6259 bytes Raw Blame History
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
6set -u
7here=$(cd "$(dirname "$0")/.." && pwd)
8B=$here/out/aien-supervise
9[ -x "$B" ] || { echo "user.sh: build first (tools/build.sh)" >&2; exit 2; }
10T=$(mktemp -d /tmp/aien-supervise-test.XXXXXX)
11export AIEN_SUPERVISE_STATE=$T/state AIEN_SUPERVISE_USERDIR=$T/user AIEN_SUPERVISE_ROOTDIR=$T/root
12mkdir -p "$T/user" "$T/root"
13pass=0; fail=0
14ok() { pass=$((pass+1)); echo "PASS $*"; }
15bad() { fail=$((fail+1)); echo "FAIL $*"; }
16st() { "$B" status "$1" | tail -1; }
17cleanup() { "$B" shutdown >/dev/null 2>&1; sleep 3; rm -rf "$T"; }
18trap cleanup EXIT
19
20printf 'i=0; while :; do echo "tick $i HOME=${HOME:-unset} umask=$(umask) pwd=$(pwd)"; i=$((i+1)); sleep 1; done\n' > "$T/tick.sh"
21printf 'echo crashing; sleep 1; exit 3\n' > "$T/crash.sh"
22printf "trap 'echo ignoring TERM' TERM\nwhile :; do sleep 0.2; done\n" > "$T/stubborn.sh"
23printf 'i=0; while [ $i -lt 400 ]; do echo line-$i-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; i=$((i+1)); done; sleep 100\n' > "$T/chatty.sh"
24printf 'exec /usr/bin/sh %s/tick.sh\nrestart always\nenv HOME=/tmp\numask 0027\ndir /tmp\n' "$T" > "$T/user/ticker.svc"
25printf 'exec /usr/bin/sh %s/crash.sh\nrestart on-failure\nbackoff 1 4\n' "$T" > "$T/user/crash.svc"
26printf 'exec /usr/bin/true\ntype oneshot\nevery 2\n' > "$T/user/once.svc"
27printf 'exec /usr/bin/sleep 100\ntype oneshot\ntimeout 2\n' > "$T/user/slow.svc"
28printf 'exec /usr/bin/sh %s/stubborn.sh\nrestart always\nstoptime 2\n' "$T" > "$T/user/stubborn.svc"
29printf 'exec /usr/bin/sh %s/chatty.sh\nlog-max 4000\nlog-keep 2\n' "$T" > "$T/user/chatty.svc"
30printf 'exec /usr/bin/true\nuser root\n' > "$T/user/sneaky.svc"
31printf 'exec /usr/bin/sleep 100\nautostart no\n' > "$T/user/held.svc"
32printf 'exec /usr/bin/sleep 100\nautostart yes\n' > "$T/user/eager.svc"
33printf 'exec /usr/bin/sleep 100\nautostart 0\n' > "$T/user/numauto.svc"
34printf 'exec /usr/bin/sleep 100\nautostart\n' > "$T/user/bareauto.svc"
35printf 'exec /usr/bin/sleep 100\nautostart yes extra\n' > "$T/user/longauto.svc"
36printf '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"
41grep -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")"
42c=$(grep -c 'crash exited status=3' "$T/state/daemon.log")
43grep -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' ';')"
44r=$(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"
45grep -q 'slow timeout, stopping' "$T/state/daemon.log" && st slow | grep -q 'state=failed' && ok "oneshot timeout -> SIGTERM, state failed" || bad "slow: $(st slow)"
46st sneaky | grep -q 'state=error' && ok "sandbox key in a user service refused" || bad "sneaky: $(st sneaky)"
47st held | grep -q 'state=stopped' && ok "autostart no: loaded, not started" || bad "held: $(st held)"
48st eager | grep -q 'state=running' && ok "autostart yes: started" || bad "eager: $(st eager)"
49st numauto | grep -q 'state=error' && ok "autostart with a number refused" || bad "numauto: $(st numauto)"
50st bareauto | grep -q 'state=error' && ok "autostart without a value refused" || bad "bareauto: $(st bareauto)"
51st longauto | grep -q 'state=error' && ok "autostart with two values refused" || bad "longauto: $(st longauto)"
52"$B" start held >/dev/null; sleep 2
53st held | grep -q 'state=running' && ok "autostart no: starts on request" || bad "held after start: $(st held)"
54st 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)"
57p1=$(st ticker | sed 's/.* pid=\([0-9]*\).*/\1/'); "$B" restart ticker >/dev/null; sleep 1
58p2=$(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
60st 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)"
65t0=$(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)"
72if [ $fail = 0 ]; then v=PASS; else v=FAIL; fi
73echo "USER $v cases=$((pass+fail)) fail=$fail"
74[ $fail = 0 ]