Files
ThothII/scripts/test-local-vector-smoke-safety.sh
T

72 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
fake="$tmp/docker"
log="$tmp/docker.log"
state="$tmp/state"
cat >"$fake" <<'SH'
#!/bin/sh
set -eu
printf '%s\n' "$*" >>"$FAKE_DOCKER_LOG"
if [ "${FAKE_COLLISION:-0}" = 1 ] && [ "$1 $2" = "ps -aq" ]; then
printf '%s\n' collision-container
exit 0
fi
if [ "$1 $2" = "ps -aq" ] || [ "$1 $2" = "volume ls" ] || [ "$1 $2" = "network ls" ]; then
if [ "${FAKE_MISMATCH_ON_CLEANUP:-0}" = 1 ] && [ -f "$FAKE_DOCKER_STATE" ]; then
printf '%s\n' foreign-resource
fi
: >"$FAKE_DOCKER_STATE"
exit 0
fi
if [ "$1" = inspect ] || [ "$1 $2" = "volume inspect" ] || [ "$1 $2" = "network inspect" ]; then
printf '%s\n' foreign-owner
exit 0
fi
case "$*" in
*"config --services"*) printf '%s\n' vector-db vector-reconcile vector-migrate core frontend ;;
*"run --rm --no-deps vector-migrate"*) printf '%s\n' '{"applied":["001","002","003"],"drifted":[],"pending":[]}' ;;
esac
exit 0
SH
chmod 0755 "$fake"
if PATH="$tmp:$PATH" FAKE_DOCKER_LOG="$log" FAKE_DOCKER_STATE="$state" \
SMOKE_PROJECT=operator-owned ./scripts/local-vector-smoke.sh >"$tmp/out" 2>"$tmp/err"; then
echo "smoke accepted caller-controlled SMOKE_PROJECT" >&2
exit 1
fi
grep -q 'SMOKE_PROJECT is not accepted' "$tmp/err"
test ! -s "$log"
: >"$log"
rm -f "$state"
PATH="$tmp:$PATH" FAKE_DOCKER_LOG="$log" FAKE_DOCKER_STATE="$state" \
FAKE_COLLISION=1 ./scripts/local-vector-smoke.sh >"$tmp/out" 2>"$tmp/err" || true
grep -q 'refusing existing Compose project resources' "$tmp/err"
if grep -q 'compose.*up' "$log"; then
echo "smoke started after detecting a project collision" >&2
exit 1
fi
: >"$log"
rm -f "$state"
PATH="$tmp:$PATH" FAKE_DOCKER_LOG="$log" FAKE_DOCKER_STATE="$state" \
FAKE_MISMATCH_ON_CLEANUP=1 ./scripts/local-vector-smoke.sh >"$tmp/out" 2>"$tmp/err" || true
grep -q 'refusing cleanup of resource not owned by this smoke' "$tmp/err"
if grep -q 'down --volumes' "$log"; then
echo "smoke removed resources after ownership mismatch" >&2
exit 1
fi
echo "local-vector smoke collision and cleanup ownership contracts passed."