AkurAI Build
Menu

Public repository / master

AkurAI-Monitor / scripts/test-deploy.sh

Latest change 672363de5c910b74bd75d069248a20f2c78036ac - release: v0.1.3 by Ólafur Búi Ólafsson

#!/usr/bin/env bash
# Tests deploy.sh against a scratch install root, a fake systemctl and a file:// health endpoint.
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
mkdir -p "${TEST_SCRATCH_DIR:-$here/target}"
scratch="$(mktemp -d "${TEST_SCRATCH_DIR:-$here/target}/deploy-test.XXXXXX")"
trap 'rm -rf "$scratch"' EXIT
fail() { echo "test-deploy: FAIL $*" >&2; exit 1; }

setup() {
  root="$scratch/$1"; mkdir -p "$root/bin" "$root/app/data"
  echo old-binary > "$root/bin/rust-monitor"; chmod +x "$root/bin/rust-monitor"
  echo old-ui > "$root/app/app.js"; echo keep > "$root/app/data/state"
  echo new-binary > "$scratch/rust-monitor.new"; chmod +x "$scratch/rust-monitor.new"
  printf '#!/bin/sh\necho "$@" >> "%s/systemctl.log"\n' "$root" > "$scratch/systemctl"; chmod +x "$scratch/systemctl"
}
run_deploy() {
  MONITOR_ROOT="$root" MONITOR_BIN="$scratch/rust-monitor.new" SYSTEMCTL="$scratch/systemctl" \
    HEALTH_URL="$1" HEALTH_TRIES=2 HEALTH_DELAY=0 SKIP_BUILD=1 SKIP_VALIDATE=1 "$here/deploy.sh" >/dev/null 2>&1
}

# A healthy deploy installs the new binary and UI, keeps the previous ones, and preserves runtime data.
setup ok
echo '{"app":"rust-monitor","status":"ok","version":"1","framework":"AkurAI-Framework","framework_version":"1"}' > "$scratch/health.json"
run_deploy "file://$scratch/health.json" || fail "healthy deploy exited non-zero"
grep -q new-binary "$root/bin/rust-monitor" || fail "new binary not installed"
grep -q old-binary "$root/bin/rust-monitor.prev" || fail "previous binary not kept"
cmp -s "$here/ui/app.js" "$root/app/app.js" || fail "ui not installed"
grep -q old-ui "$root/app.prev/app.js" || fail "previous ui not kept"
[ -f "$root/app/data/state" ] || fail "runtime data under app/ was removed"
grep -q 'restart rust-monitor-titan' "$root/systemctl.log" || fail "service not restarted"

# A deploy whose health check fails rolls back to the previous binary and UI.
setup bad
run_deploy "file://$scratch/missing.json" && fail "unhealthy deploy exited zero"
grep -q old-binary "$root/bin/rust-monitor" || fail "binary not rolled back"
grep -q old-ui "$root/app/app.js" || fail "ui not rolled back"
[ "$(grep -c 'restart rust-monitor-titan' "$root/systemctl.log")" = 2 ] || fail "rollback did not restart the service"

echo "test-deploy: ok"