Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
91 lines
4.8 KiB
Bash
Executable File
91 lines
4.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# End-to-end smoke test of the zag binary: offline mode, the daemon, IPC, kill/retry/clean.
|
|
# Usage: test/e2e.sh [path-to-zag] (default: zig-out/bin/zag)
|
|
set -u
|
|
ZAG=${1:-zig-out/bin/zag}
|
|
DIR=$(mktemp -d /tmp/zag-e2e.XXXXXX)
|
|
Z() { "$ZAG" --barn-dir "$DIR" "$@"; }
|
|
fails=0
|
|
pass() { printf 'ok %s\n' "$1"; }
|
|
fail() { printf 'FAIL %s\n' "$1"; fails=$((fails + 1)); }
|
|
check() { # check <name> <command...> (command must succeed)
|
|
local name=$1; shift
|
|
if "$@" >/dev/null 2>&1; then pass "$name"; else fail "$name"; fi
|
|
}
|
|
contains() { # contains <name> <needle> <haystack>
|
|
case $3 in *"$2"*) pass "$1" ;; *) fail "$1 (missing '$2' in: $3)" ;; esac
|
|
}
|
|
json_field() { printf '%s\n' "$2" | sed -n "s/.*\"$1\" : \"\{0,1\}\([^\",]*\)\"\{0,1\},\{0,1\}\$/\1/p" | head -1; }
|
|
wait_state() { # wait_state <id> <state> [seconds]
|
|
local i
|
|
for ((i = 0; i < ${3:-100}; i++)); do
|
|
[[ $(json_field state "$(Z describe -o json "$1" --offline)") == "${2,,}" ]] && return 0
|
|
sleep 0.1
|
|
done
|
|
return 1
|
|
}
|
|
cleanup() { Z service stop --force >/dev/null 2>&1; rm -rf "$DIR"; }
|
|
trap cleanup EXIT
|
|
|
|
# --- basics -----------------------------------------------------------------------------
|
|
contains "version" "zag " "$("$ZAG" --version)"
|
|
contains "help lists commands" "describe" "$("$ZAG" --help)"
|
|
"$ZAG" frobnicate >/dev/null 2>&1; [[ $? -eq 2 ]] && pass "unknown command exits 2" || fail "unknown command exits 2"
|
|
|
|
# --- offline mode -----------------------------------------------------------------------
|
|
out=$(Z run --offline -o json -t demo sh -c 'echo hello; exit 0')
|
|
id=$(json_field id "$out")
|
|
contains "offline run succeeds" '"state" : "SUCCEEDED"' "$out"
|
|
contains "offline run tag" '"tag" : "demo"' "$out"
|
|
contains "describe --logs shows stdout" "hello" "$(Z describe --offline --logs "$id")"
|
|
contains "status table" "succeeded" "$(Z status --offline)"
|
|
contains "status json summary" '"succeeded" : 1' "$(Z status --offline -o json)"
|
|
contains "status xml" "<job>" "$(Z status --offline -o xml)"
|
|
contains "usage has samples" "samples" "$(Z usage "$id")"
|
|
contains "clean without selector removes nothing" "Cleaned 0 jobs." "$(Z clean --offline)"
|
|
contains "clean --all --dry-run lists the job" "Would clean 1 job" "$(Z clean --offline --all --dry-run)"
|
|
contains "clean --all removes it" "Cleaned 1 job" "$(Z clean --offline --all)"
|
|
contains "status empty" "No jobs found." "$(Z status --offline)"
|
|
|
|
# --- config -----------------------------------------------------------------------------
|
|
contains "config show --defaults" "max_high_jobs = 2" "$(Z config show --defaults)"
|
|
printf '[jobs]\nmax_retries = 0\nbogus = 1\n' >"$DIR/bad.conf"
|
|
Z config validate --file "$DIR/bad.conf" >/dev/null 2>&1; [[ $? -eq 1 ]] && pass "validate flags bad config" || fail "validate flags bad config"
|
|
printf '[jobs]\nmax_retries = 0\n' >"$DIR/good.conf"
|
|
contains "validate accepts good config" "Configuration is valid" "$(Z config validate --file "$DIR/good.conf")"
|
|
contains "install --show prints files" "high.load" "$(Z config install --show)"
|
|
|
|
# --- daemon -----------------------------------------------------------------------------
|
|
Z run echo hi >/dev/null 2>&1; [[ $? -eq 1 ]] && pass "run without service exits 1" || fail "run without service exits 1"
|
|
contains "service start" "Zag service started" "$(Z service start)"
|
|
contains "second start refused" "already running" "$(Z service start 2>&1)"
|
|
contains "service status running" "running" "$(Z service status)"
|
|
|
|
out=$(Z run -o json -t svc sh -c 'echo from-daemon')
|
|
sid=$(json_field id "$out")
|
|
contains "run over IPC is queued" '"state" : "QUEUED"' "$out"
|
|
wait_state "$sid" SUCCEEDED && pass "daemon runs the job" || fail "daemon runs the job"
|
|
|
|
out=$(Z run -o json sleep 60); kid=$(json_field id "$out")
|
|
wait_state "$kid" RUNNING && pass "sleeper is running" || fail "sleeper is running"
|
|
contains "kill reports canceled" "New state: canceled" "$(Z kill "$kid")"
|
|
wait_state "$kid" CANCELED && pass "killed job is canceled" || fail "killed job is canceled"
|
|
contains "kill a finished job fails" "not running" "$(Z kill "$kid" 2>&1)"
|
|
|
|
out=$(Z run -o json --max-retries 1 --retry-delay 1 sh -c 'exit 5'); rid=$(json_field id "$out")
|
|
sleep 4
|
|
desc=$(Z describe -o json "$rid" --offline)
|
|
contains "failed job retried once" '"retryCount" : 1' "$desc"
|
|
contains "failed job ended failed" '"state" : "failed"' "$desc"
|
|
|
|
out=$(Z run -o json --timeout 1 sleep 30); tid=$(json_field id "$out")
|
|
sleep 3
|
|
contains "timeout fails the job" '"exitCode" : "timeout"' "$(Z describe -o json "$tid" --offline)"
|
|
|
|
contains "service reload" "Configuration reloaded" "$(Z service reload)"
|
|
contains "clean via IPC" "Cleaned" "$(Z clean --all --include-failed)"
|
|
contains "service stop" "Zag service stopped" "$(Z service stop)"
|
|
contains "service status stopped" "stopped" "$(Z service status)"
|
|
|
|
if [[ $fails -eq 0 ]]; then echo "all e2e checks passed"; else echo "$fails e2e check(s) failed"; exit 1; fi
|