Something went wrong. Try again.
agent management prototype
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183#!/usr/bin/env bash# testing/run_testsset -euo pipefail
SNAPBOX_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/snapbox/snapbox.conf"[[ -f "$SNAPBOX_CONFIG" ]] && source "$SNAPBOX_CONFIG"SNAPBOX_ROOT="${SNAPBOX_ROOT:-/snapbox}"export SNAPBOX_ROOT
TEST_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"TEST_TMPFS="${SNAPBOX_TEST_TMPFS:-$SNAPBOX_ROOT/testing}"GEN_DIR="$TEST_TMPFS/generated-tests"SNAPBOX_PRIV="${PRIV:-/usr/local/sbin/snapbox-priv}"
VERBOSE=0while [[ $# -gt 0 ]]; do case "$1" in -v|--verbose) VERBOSE=1; shift ;; *) shift ;; esacdoneexport VERBOSE
if [[ -t 2 ]]; then C_RED=$'\033[0;31m'; C_GRN=$'\033[0;32m'; C_YLW=$'\033[1;33m' C_BLU=$'\033[0;34m'; C_BLD=$'\033[1m'; C_RST=$'\033[0m'else C_RED=''; C_GRN=''; C_YLW=''; C_BLU=''; C_BLD=''; C_RST=''fi
log() { printf '%s[*]%s %s\n' "$C_BLU" "$C_RST" "$*" >&2; }ok() { printf '%s[+]%s %s\n' "$C_GRN" "$C_RST" "$*" >&2; }err() { printf '%s[x]%s %s\n' "$C_RED" "$C_RST" "$*" >&2; }warn() { printf '%s[!]%s %s\n' "$C_YLW" "$C_RST" "$*" >&2; }die() { err "$*"; exit 1; }
[[ -x "$SNAPBOX_PRIV" ]] || die "snapbox-priv not found. Is snapbox installed?"for cmd in box at-box take rmbox; do command -v "$cmd" >/dev/null 2>&1 || die "snapbox command '$cmd' not in PATH."done
ADMIN="${SUDO_USER:-$USER}"AUID=$(id -u "$ADMIN")
mount_tmpfs() { sudo "$SNAPBOX_PRIV" mount_tmpfs "$TEST_TMPFS" "512M" "$AUID"}umount_tmpfs() { sudo "$SNAPBOX_PRIV" umount_path "$TEST_TMPFS"; }
wipe_tmpfs() { mountpoint -q "$TEST_TMPFS" || return 0 find "$TEST_TMPFS" -mindepth 1 -maxdepth 1 \ ! -name "$(basename "$GEN_DIR")" \ -exec rm -rf {} + 2>/dev/null || true}
# Record existing projects so we don't delete user dataINITIAL_PROJECTS=$(find "$SNAPBOX_ROOT/snaps" "$SNAPBOX_ROOT/boxes" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; 2>/dev/null | sort -u)
cleanup_test_artifacts() { for dir in "$SNAPBOX_ROOT/boxes"/* "$SNAPBOX_ROOT/snaps"/*; do [[ -d "$dir" ]] || continue proj_name=$(basename "$dir") if [[ -n "$INITIAL_PROJECTS" ]] && grep -qx "$proj_name" <<< "$INITIAL_PROJECTS"; then continue fi find "$dir" -depth -type d 2>/dev/null | while read -r mp; do mountpoint -q "$mp" 2>/dev/null && sudo "$SNAPBOX_PRIV" umount_path "$mp" 2>/dev/null || true done sudo rm -rf "$dir" 2>/dev/null || true done}
cleanup() { wipe_tmpfs umount_tmpfs cleanup_test_artifacts}trap cleanup EXIT
log "Mounting test tmpfs at $TEST_TMPFS"mount_tmpfs
mkdir -p "$GEN_DIR"if [[ -f "./testing/generate_tests" ]]; then log "Generating tests into $GEN_DIR..." bash "./testing/generate_tests" "$GEN_DIR" >&2else warn "generate_tests script not found. Falling back to physical tests only."fi
tests=()for d in "$TEST_ROOT"/*/ "$GEN_DIR"/*/; do [[ -d "$d" ]] || continue name=$(basename "$d") [[ -f "$d/mksource" && -f "$d/mkresult" && -f "$d/ops" ]] || continue tests+=("$d")done
if [[ ${#tests[@]} -eq 0 ]]; then die "No tests found in $TEST_ROOT or $GEN_DIR"fi
log "Discovered ${#tests[@]} test(s): ${tests[*]##*/}"echo
passed=0; failed=0; failed_list=()
for test_path in "${tests[@]}"; do test_name=$(basename "$test_path") log "${C_BLD}── Test: $test_name ──${C_RST}"
# Disable errexit for the entire test execution block set +e ( set -e cleanup_test_artifacts wipe_tmpfs mkdir -p "$TEST_TMPFS/source/.snapbox" "$TEST_TMPFS/result/.snapbox" ) rc=$? if [[ $rc -ne 0 ]]; then err "FAIL: $test_name — setup exited $rc" ((++failed)); failed_list+=("$test_name") set -e continue fi
( cd "$TEST_TMPFS/source" && bash "$test_path/mksource" ) rc=$? if [[ $rc -ne 0 ]]; then err "FAIL: $test_name — mksource exited $rc" ((++failed)); failed_list+=("$test_name") set -e continue fi
err_output=$(mktemp) (cd "$TEST_TMPFS/source" && bash "$test_path/ops" 2> "$err_output") rc=$? if (( rc != 0 )); then err "FAIL: $test_name — ops exited $rc" cat "$err_output" | sed 's/^/ /' >&2 ((++failed)); failed_list+=("$test_name") rm -f "$err_output" set -e continue fi rm -f "$err_output"
(cd "$TEST_TMPFS/result" && bash "$test_path/mkresult") rc=$? if [[ $rc -ne 0 ]]; then err "FAIL: $test_name — mkresult exited $rc" ((++failed)); failed_list+=("$test_name") set -e continue fi
diff_output=$(diff -r --no-dereference "$TEST_TMPFS/source" "$TEST_TMPFS/result" 2>&1) rc=$? if [[ $rc -eq 0 ]]; then ok "PASS: $test_name" ((++passed)) else err "FAIL: $test_name — source ≠ result" printf '%s\n' "$diff_output" | head -40 | sed 's/^/ /' ((++failed)); failed_list+=("$test_name") fi
# Re-enable errexit for the loop set -edone
echolog "Results: ${C_GRN}$passed passed${C_RST}, ${C_RED}$failed failed${C_RST}"if [[ ${#failed_list[@]} -gt 0 ]]; then err "Failed: ${failed_list[*]}"fi
[[ $failed -eq 0 ]]