Files
zag/test/e2e.sh
T
Daniel Samson 3a4c4fa326
CI / test (pull_request) Successful in 3m18s
daemon: flush deprecation warnings into the log at startup
The re-exec'd daemon inherits the buffered stderr writer over
logs/zag.log from main.zig, so the deprecation warnings printed by
Context.loadConfigOnce stayed in the buffer until the process
exited and 'zag service logs' showed nothing right after starting
with an old config. Flush stderr right after printing the
warnings so they reach the log at startup, as the issue requires.

e2e now starts the daemon with a config that still carries the
deprecated load-level settings and asserts the warnings are in the
log while the daemon is running; the settings themselves are
ignored, so the effective config is unchanged for the rest of
the suite.
2026-11-12 10:40:41 +00:00

108 lines
6.3 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]
# ${2,,} would work on bash >= 4, but macOS ships bash 3.2
local lower i
lower=$(printf '%s' "$2" | tr '[:upper:]' '[:lower:]')
for ((i = 0; i < ${3:-100}; i++)); do
[[ $(json_field state "$(Z describe -o json "$1" --offline)") == "$lower" ]] && 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" "retry_backoff_multiplier = 2.0" "$(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")"
printf '[service]\nmax_concurrent_jobs = 4\n[load_levels]\nmax_high_jobs = 2\n' >"$DIR/deprecated.conf"
deprecated_out=$(Z config validate --file "$DIR/deprecated.conf" 2>&1); rc=$?
[[ $rc -eq 0 ]] && contains "validate warns on deprecated settings, exit 0" "deprecated" "$deprecated_out" || fail "validate warns on deprecated settings (rc=$rc)"
contains "install --show prints files" "zag.conf" "$(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"
# The daemon starts with the deprecated config: the removed settings must be ignored
# (effective config is the defaults) and its log must warn about them while it is running.
contains "service start" "Zag service started" "$("$ZAG" --barn-dir "$DIR" --config "$DIR/deprecated.conf" service start 2>&1)"
contains "second start refused" "already running" "$(Z service start 2>&1)"
contains "service status running" "running" "$(Z service status)"
contains "daemon log warns on deprecated settings at startup" "deprecated" "$(cat "$DIR/logs/zag.log" 2>/dev/null)"
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)"
# Strict single-job FIFO: while the first job runs, the second stays queued, and both
# finish in creation order.
out=$(Z run -o json sleep 3); a1=$(json_field id "$out")
wait_state "$a1" RUNNING && pass "serial: first job is running" || fail "serial: first job is running"
out=$(Z run -o json sleep 1); b1=$(json_field id "$out")
contains "serial: second job stays queued" '"state" : "QUEUED"' "$out"
wait_state "$a1" SUCCEEDED && pass "serial: first job finishes first" || fail "serial: first job finishes first"
wait_state "$b1" SUCCEEDED && pass "serial: second job finishes second" || fail "serial: second job finishes second"
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