Files
ARTEX-chixuweihu/scripts/deploy_test.sh
T
ji ruo 07309e4530 feat(deploy,gateway): add dual-edition deployment/migration/rollback and TUN route reconciliation
- Add standalone edition (`Dockerfile.standalone`, `docker-compose.standalone.yml`) alongside sandbox edition and publish both in `.github/workflows/release.yml`.
- Implement `scripts/deploy-lib.sh`, `install.sh`, `update.sh`, and `scripts/artex-sandbox-firewall.sh --cleanup` with edition detection, legacy standalone -> sandbox migration (symlink-safe workspace permissions), automatic snapshots with pinned `artex-rollback/*` images + PG dump, auto/manual cross-edition rollback, and post-rollback upstream unpinning.
- Update `ensureWritableDir` in `internal/sandbox/manager.go` to safely migrate nested root-owned workspace files without following symlinks.
- Expose `deploy_edition` in `/api/update/check` and update `UpdateCard` UI/i18n warnings for sandbox vs standalone in-app update/rollback.
- Reconcile `artex-gateway` TUN policy routing (`priority 218`, `raw PREROUTING`, `nat POSTROUTING`, `DOCKER-USER`) every 15s to self-heal after host network rule flushes or bridge changes.
- Add unit and shell tests (`scripts/deploy_test.sh`, `cmd/artex-gateway/tun_linux_test.go`, `internal/sandbox/sandbox_test.go`, `server/update_test.go`) and update `README.md` / `.env.example`.
2026-12-02 13:31:05 +08:00

283 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
# Automated regression tests for dual-edition deployment (standalone / sandbox),
# migration from legacy v0.3.32, snapshot creation, manual rollback, DB restore,
# and automatic rollback on failed health check.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
pass() { printf 'PASS: %s\n' "$*"; }
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
assert_eq() {
local got="$1" want="$2" msg="$3"
[ "$got" = "$want" ] || fail "${msg}: got='${got}' want='${want}'"
}
assert_contains() {
local file="$1" needle="$2" msg="$3"
grep -Fq -- "$needle" "$file" || fail "${msg}: '${needle}' not found in ${file}"
}
# 1. Syntax check all deployment scripts
bash -n "$REPO_ROOT/scripts/deploy-lib.sh"
bash -n "$REPO_ROOT/install.sh"
bash -n "$REPO_ROOT/update.sh"
sh -n "$REPO_ROOT/scripts/artex-sandbox-firewall.sh"
pass "shell syntax checks"
# 2. Unit-test tag normalization & env helpers
(
cd "$TMP_DIR"
# shellcheck source=scripts/deploy-lib.sh
. "$REPO_ROOT/scripts/deploy-lib.sh"
assert_eq "$(normalize_standalone_tag "")" "standalone" "empty standalone tag"
assert_eq "$(normalize_standalone_tag "latest")" "standalone" "latest -> standalone"
assert_eq "$(normalize_standalone_tag "standalone")" "standalone" "standalone -> standalone"
assert_eq "$(normalize_standalone_tag "v0.3.32")" "v0.3.32" "legacy pre-split tag preserved"
assert_eq "$(normalize_standalone_tag "v0.3.17")" "v0.3.17" "older pre-split tag preserved"
assert_eq "$(normalize_standalone_tag "v0.3.33")" "v0.3.33-standalone" "new tag mapped to -standalone"
assert_eq "$(normalize_standalone_tag "v0.4.0")" "v0.4.0-standalone" "minor bump mapped to -standalone"
assert_eq "$(normalize_standalone_tag "v0.3.33-standalone")" "v0.3.33-standalone" "already suffixed preserved"
assert_eq "$(normalize_sandbox_tag "standalone")" "latest" "sandbox standalone -> latest"
assert_eq "$(normalize_sandbox_tag "v0.3.33-standalone")" "v0.3.33" "sandbox strips -standalone"
assert_eq "$(normalize_sandbox_tag "v0.3.33")" "v0.3.33" "sandbox keeps semver"
env_set FOO "/a/b|c&d" "$TMP_DIR/t.env"
assert_eq "$(env_get FOO "$TMP_DIR/t.env")" "/a/b|c&d" "env_set/get preserves special chars"
env_set FOO "second" "$TMP_DIR/t.env"
assert_eq "$(env_get FOO "$TMP_DIR/t.env")" "second" "env_set updates idempotently"
assert_eq "$(grep -c '^FOO=' "$TMP_DIR/t.env")" "1" "env_set writes single key"
)
pass "tag normalization and env helpers"
# 3. End-to-end workflow test with stubbed docker & curl in isolated workspace
WORK="$TMP_DIR/work"
BIN_STUB="$TMP_DIR/bin"
STATE_STUB="$TMP_DIR/stub-state"
mkdir -p "$WORK/scripts" "$BIN_STUB" "$STATE_STUB/images"
cp "$REPO_ROOT/install.sh" "$REPO_ROOT/update.sh" "$REPO_ROOT/.env.example" \
"$REPO_ROOT/docker-compose.yml" "$REPO_ROOT/docker-compose.standalone.yml" "$WORK/"
cp "$REPO_ROOT/scripts/deploy-lib.sh" "$REPO_ROOT/scripts/artex-sandbox-firewall.sh" "$WORK/scripts/"
chmod +x "$WORK/install.sh" "$WORK/update.sh" "$WORK/scripts/deploy-lib.sh" "$WORK/scripts/artex-sandbox-firewall.sh"
# Seed mock docker image store (v0.3.32 standalone image)
printf 'sha256:old0332standalone\n' > "$STATE_STUB/images/sha256_old0332standalone"
printf 'sha256:old0332standalone\n' > "$STATE_STUB/images/ruoji6_artex_v0.3.32"
printf 'standalone\n' > "$STATE_STUB/running_edition"
printf 'ruoji6/artex:v0.3.32\n' > "$STATE_STUB/artex_image_ref"
printf 'sha256:old0332standalone\n' > "$STATE_STUB/artex_image_id"
printf 'ok\n' > "$STATE_STUB/health_mode"
cat > "$BIN_STUB/docker" <<'STUB'
#!/usr/bin/env bash
set -euo pipefail
STATE="$ARTEX_TEST_STUB_STATE"
LOG="$STATE/docker.log"
printf '%s\n' "$*" >> "$LOG"
norm_key() { printf '%s' "$1" | tr '/:' '__'; }
case "${1:-}" in
compose)
shift
cfile=""
if [ "${1:-}" = "-f" ]; then cfile="$2"; shift 2; fi
sub="${1:-}"; shift || true
case "$sub" in
version) echo "Docker Compose version v2.29.0"; exit 0 ;;
ps)
# ps -a -q <svc>
svc="${!#}"
ed="$(cat "$STATE/running_edition")"
case "$svc" in
artex) echo "cid-artex" ;;
postgres) echo "cid-pg" ;;
artex-gateway) [ "$ed" = "sandbox" ] && echo "cid-gw" || true ;;
docker-socket-proxy) [ "$ed" = "sandbox" ] && echo "cid-dsp" || true ;;
esac
exit 0 ;;
pull)
exit 0 ;;
stop)
exit 0 ;;
up)
if [ "$cfile" = "docker-compose.yml" ]; then
printf 'sandbox\n' > "$STATE/running_edition"
printf 'ruoji6/artex:latest\n' > "$STATE/artex_image_ref"
printf 'sha256:new0333sandbox\n' > "$STATE/artex_image_id"
printf 'sha256:new0333sandbox\n' > "$STATE/images/sha256_new0333sandbox"
printf 'sha256:new0333gw\n' > "$STATE/images/sha256_new0333gw"
printf 'sha256:new0333sbx\n' > "$STATE/images/ruoji6_artex-sandbox_latest"
elif [ "$cfile" = "docker-compose.standalone.yml" ]; then
printf 'standalone\n' > "$STATE/running_edition"
fi
exit 0 ;;
esac ;;
ps)
ed="$(cat "$STATE/running_edition")"
if [ "$ed" = "sandbox" ]; then echo "cid-sbx-1"; fi
exit 0 ;;
stop|pull|rmi)
exit 0 ;;
tag)
src="$2"; dst="$3"
sk="$(norm_key "$src")"; dk="$(norm_key "$dst")"
if [ -f "$STATE/images/$sk" ]; then
cp "$STATE/images/$sk" "$STATE/images/$dk"
exit 0
fi
exit 1 ;;
image)
if [ "${2:-}" = "inspect" ]; then
ref="${!#}"
k="$(norm_key "$ref")"
if [ -f "$STATE/images/$k" ]; then
cat "$STATE/images/$k"
exit 0
fi
exit 1
fi ;;
inspect)
fmt="$3"; cid="$4"
case "$cid:$fmt" in
cid-artex:*Config.Image*) cat "$STATE/artex_image_ref" ;;
cid-artex:*.Image*) cat "$STATE/artex_image_id" ;;
cid-artex:*State.Status*) echo "running" ;;
cid-artex:*Config.Env*)
[ "$(cat "$STATE/running_edition")" = "sandbox" ] && echo "ARTEX_SANDBOX_MODE=docker" || echo "ARTEX_SANDBOX_MODE=local"
;;
cid-gw:*Config.Image*) echo "ruoji6/artex-gateway:latest" ;;
cid-gw:*.Image*) echo "sha256:new0333gw" ;;
cid-pg:*State.Running*) echo "true" ;;
esac
exit 0 ;;
exec)
shift
[ "${1:-}" = "-i" ] && shift
shift # cid
cmd="${1:-}"
if [ "$cmd" = "pg_dump" ]; then
echo "-- mock pg_dump snapshot"
echo "CREATE TABLE test_ok(id int);"
elif [ "$cmd" = "psql" ]; then
cat >/dev/null || true
fi
exit 0 ;;
esac
exit 0
STUB
chmod +x "$BIN_STUB/docker"
cat > "$BIN_STUB/curl" <<'STUB'
#!/usr/bin/env bash
STATE="$ARTEX_TEST_STUB_STATE"
mode="$(cat "$STATE/health_mode")"
if [ "$mode" = "fail-once" ]; then
printf 'ok\n' > "$STATE/health_mode"
exit 22
fi
[ "$mode" = "ok" ] && { echo '{"ok":true}'; exit 0; }
exit 22
STUB
chmod +x "$BIN_STUB/curl"
# Create legacy v0.3.32 .env (no ARTEX_DEPLOY_EDITION, no ARTEX_HOST_DATA_DIR, no ARTEX_GATEWAY_TOKEN)
cat > "$WORK/.env" <<'ENV'
ARTEX_IMAGE=ruoji6/artex
ARTEX_TAG=v0.3.32
POSTGRES_USER=artex
POSTGRES_PASSWORD=legacy-secret-pw
POSTGRES_DB=artex
ENV
# Create simulated legacy workspace with read-only file + external symlink target
OUTSIDE_FILE="$TMP_DIR/outside-secret.txt"
echo "secret" > "$OUTSIDE_FILE"
chmod 0400 "$OUTSIDE_FILE"
mkdir -p "$WORK/data/tasks/5/findings"
echo "old finding" > "$WORK/data/tasks/5/findings/note.txt"
chmod 0555 "$WORK/data/tasks/5/findings"
chmod 0444 "$WORK/data/tasks/5/findings/note.txt"
ln -s "$OUTSIDE_FILE" "$WORK/data/tasks/5/escape-link"
export PATH="$BIN_STUB:$PATH"
export ARTEX_TEST_STUB_STATE="$STATE_STUB"
export ARTEX_HEALTH_TIMEOUT=2
# 3a. Detect legacy v0.3.32 standalone deployment
ed="$(cd "$WORK" && . ./scripts/deploy-lib.sh && detect_current_edition .env)"
assert_eq "$ed" "standalone" "detect legacy v0.3.32 as standalone"
pass "legacy standalone detection"
# 3b. Migrate legacy standalone -> sandbox
(
cd "$WORK"
./update.sh --migrate-sandbox --skip-git-pull --yes
)
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_DEPLOY_EDITION .env)" "sandbox" "edition migrated to sandbox"
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_HOST_DATA_DIR .env)" "$WORK/data" "ARTEX_HOST_DATA_DIR populated"
[ -n "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_GATEWAY_TOKEN .env)" ] || fail "ARTEX_GATEWAY_TOKEN not generated"
[ -w "$WORK/data/tasks/5/findings/note.txt" ] || fail "migrated task file not writable"
# Outside symlink target must still be 0400
outside_perm="$(stat -f '%Lp' "$OUTSIDE_FILE" 2>/dev/null || stat -c '%a' "$OUTSIDE_FILE")"
assert_eq "$outside_perm" "400" "symlink target permissions must not be modified"
snap_mig="$(cat "$WORK/.artex-deploy/latest")"
assert_contains "$WORK/.artex-deploy/snapshots/$snap_mig/meta.env" "SNAP_EDITION=standalone" "migration snapshot records standalone"
assert_contains "$WORK/.artex-deploy/snapshots/$snap_mig/meta.env" "ARTEX_IMAGE_REF=ruoji6/artex:v0.3.32" "migration snapshot records v0.3.32 image"
assert_contains "$WORK/.artex-deploy/snapshots/$snap_mig/meta.env" "HAS_PG_DUMP=1" "migration snapshot includes PG dump"
assert_contains "$STATE_STUB/docker.log" "compose -f docker-compose.yml up -d --remove-orphans" "started sandbox compose"
pass "legacy standalone -> sandbox migration"
# 3c. Rollback from sandbox back to legacy standalone (including --restore-db)
(
cd "$WORK"
./update.sh --rollback "$snap_mig" --restore-db --yes
)
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_DEPLOY_EDITION .env)" "standalone" "rolled back edition to standalone"
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_STANDALONE_IMAGE .env)" "artex-rollback/artex" "rollback pinned local image repo"
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_STANDALONE_TAG .env)" "$snap_mig" "rollback pinned local image tag"
assert_contains "$STATE_STUB/docker.log" "stop -t 10 cid-sbx-1" "stopped running sandbox container on rollback"
assert_contains "$STATE_STUB/docker.log" "compose -f docker-compose.yml stop -t 15 artex-gateway" "gracefully stopped artex-gateway on rollback"
assert_contains "$STATE_STUB/docker.log" "compose -f docker-compose.standalone.yml up -d --remove-orphans" "started standalone compose on rollback"
assert_contains "$STATE_STUB/docker.log" "exec -i cid-pg psql" "restored PG dump when --restore-db passed"
pass "sandbox -> standalone rollback + DB restore"
# 3d. Failed upgrade triggers automatic rollback
printf 'fail-once\n' > "$STATE_STUB/health_mode"
if ( cd "$WORK" && ./update.sh --upgrade --tag v0.3.34 --skip-git-pull --yes ); then
fail "upgrade with failing health check should exit non-zero after auto-rollback"
fi
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_DEPLOY_EDITION .env)" "standalone" "auto-rollback preserved standalone edition"
pass "auto-rollback on failed upgrade health check"
# 3e. Switch sandbox -> standalone pinning legacy v0.3.32 tag (also verifies unpinning artex-rollback/* after rollback)
printf 'sandbox\n' > "$STATE_STUB/running_edition"
printf 'ok\n' > "$STATE_STUB/health_mode"
(
cd "$WORK"
./update.sh --switch-standalone --tag v0.3.32 --skip-git-pull --yes
)
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_STANDALONE_IMAGE .env)" "ruoji6/artex" "post-rollback switch unpinned artex-rollback/* standalone image"
assert_eq "$(cd "$WORK" && . ./scripts/deploy-lib.sh && env_get ARTEX_STANDALONE_TAG .env)" "v0.3.32" "switch-standalone kept v0.3.32 tag"
pass "switch sandbox -> standalone with legacy tag and rollback unpin"
# 3f. Fresh install for both editions
for ed_opt in standalone sandbox; do
FRESH="$TMP_DIR/fresh-$ed_opt"
mkdir -p "$FRESH/scripts"
cp "$REPO_ROOT/install.sh" "$REPO_ROOT/.env.example" \
"$REPO_ROOT/docker-compose.yml" "$REPO_ROOT/docker-compose.standalone.yml" "$FRESH/"
cp "$REPO_ROOT/scripts/deploy-lib.sh" "$REPO_ROOT/scripts/artex-sandbox-firewall.sh" "$FRESH/scripts/"
chmod +x "$FRESH/install.sh" "$FRESH/scripts/deploy-lib.sh"
( cd "$FRESH" && ./install.sh --edition "$ed_opt" --yes )
assert_eq "$(cd "$FRESH" && . ./scripts/deploy-lib.sh && env_get ARTEX_DEPLOY_EDITION .env)" "$ed_opt" "fresh install edition=$ed_opt"
if [ "$ed_opt" = "sandbox" ]; then
assert_eq "$(cd "$FRESH" && . ./scripts/deploy-lib.sh && env_get ARTEX_HOST_DATA_DIR .env)" "$FRESH/data" "fresh sandbox sets ARTEX_HOST_DATA_DIR"
[ -n "$(cd "$FRESH" && . ./scripts/deploy-lib.sh && env_get ARTEX_GATEWAY_TOKEN .env)" ] || fail "fresh sandbox sets ARTEX_GATEWAY_TOKEN"
fi
done
pass "fresh dual-edition install.sh"
echo "ALL DEPLOY/MIGRATION/ROLLBACK TESTS PASSED"