Files
zag/test/e2e.sh
Daniel SamsonandClaude Sonnet 4.6 6325a87670
CI / test (pull_request) Successful in 3m20s
review fixes: safe reload warnings, idiomatic @field, range-check guard, e2e FIFO assert
- reloadConfig: use a local ArrayList per reload instead of the shared
  ctx.config_deprecations — the previous approach could reallocate the
  backing array into the short-lived reload arena, causing use-after-free
  on successive reloads when warning counts grew between reloads
- fromDoc: replace unsafe @ptrFromInt/@offsetOf arithmetic with &@field,
  which is idiomatic Zig and carries no extra risk; remove the incorrect
  comment claiming @field materialises a copy
- problems(): fix the out.items.len == 0 range-check guard to count only
  non-warning (error) items so a deprecated setting no longer suppresses
  the range check for the rest of the document; add regression test
- deprecations now typed as []Problem throughout (env-var warnings carry
  key/message/is_warning like file-level ones) so callers have full context
- e2e: assert b1 never leaves QUEUED while a1 is running (true FIFO check)
- sections table shared by fromDoc and checkDoc eliminates drift risk
  between the two section lists; problemNodes refactored to ArrayList
  single-pass; isDeprecatedKey inlined; isDeprecatedEnvName comment added

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-12 14:38:30 +00:00

123 lines
7.1 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 -----------------------------------------------------------------------------
defaults_out=$(Z config show --defaults)
contains "config show --defaults" "retry_backoff_multiplier = 2.0" "$defaults_out"
case $defaults_out in *max_high_jobs*) fail "config show --defaults is free of load levels" ;; *) pass "config show --defaults is free of load levels" ;; esac
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"
# The checks above would still pass if the scheduler ran both jobs concurrently
# (sleep 3 always finishes before sleep 1 started after it could catch up). Assert the
# defining FIFO invariant directly: b1 never leaves QUEUED while a1 is running.
exclusive=1
last=""
for i in 1 2 3 4 5 6 7 8 9 10 11 12; do
st_a=$(json_field state "$(Z describe -o json "$a1" --offline)")
[[ $st_a == "running" ]] || break
last=$(json_field state "$(Z describe -o json "$b1" --offline)")
[[ $last == "queued" ]] || exclusive=0
sleep 0.3
done
[[ $exclusive -eq 1 ]] && pass "serial: second job never runs while the first runs" || fail "serial: second job never runs while the first runs (state: $last)"
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