Files
member-console/test/teardown-stack.sh

74 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tears down the test stack for the current worktree and returns it to a
# clean state: containers + volumes removed, test/.env, test/secrets/, and
# test/testdata/ deleted.
#
# Safe to run when nothing is up.
set -euo pipefail
REPO_ROOT="$(git rev-parse --show-toplevel)"
TEST_DIR="${REPO_ROOT}/test"
ENV_FILE="${TEST_DIR}/.env"
if [[ -f "$ENV_FILE" ]]; then
echo "Bringing down compose stack with volumes..."
docker compose \
-f "${TEST_DIR}/compose.yaml" \
--env-file "$ENV_FILE" \
down -v --remove-orphans || true
else
echo "No test/.env found; skipping docker compose down."
fi
remove_path() {
local path="$1"
[[ -e "$path" ]] || return 0
echo "Removing ${path}"
if rm -rf "$path" 2>/dev/null; then
return 0
fi
# Some testdata subdirs (postgres, temporal) are written by containers
# running as root and end up owned by root on the host. Fall back to a
# short-lived root container to delete them, avoiding a sudo prompt.
echo " (root-owned files; removing via docker)"
docker run --rm -v "${TEST_DIR}:/work" --workdir /work \
alpine:3 rm -rf "./$(basename "$path")"
}
# Determine whether this is the main worktree. Bootstrap copies test/secrets/
# from main into other worktrees, so deleting secrets/ on main wipes the
# source-of-truth. Refuse to do that.
MAIN_WT="$(git worktree list --porcelain | awk '/^worktree / { print $2; exit }')"
IS_MAIN=0
if [[ "$REPO_ROOT" == "$MAIN_WT" ]]; then
IS_MAIN=1
fi
for path in \
"${TEST_DIR}/.env" \
"${TEST_DIR}/secrets"; do
if [[ "$path" == "${TEST_DIR}/secrets" && "$IS_MAIN" -eq 1 ]]; then
echo "Keeping ${path} (main worktree — source-of-truth for other worktrees)"
continue
fi
remove_path "$path"
done
# Clean runtime data under testdata/ but preserve committed seed files
# (e.g. testdata/fedwiki/config.json, testdata/keycloak/master-realm.json).
# git clean -fdX removes only gitignored paths.
if [[ -d "${TEST_DIR}/testdata" ]]; then
echo "Cleaning gitignored runtime data under ${TEST_DIR}/testdata/"
if ! git -C "$REPO_ROOT" clean -fdX -- test/testdata/ 2>/dev/null; then
# Some subdirs (postgres, temporal) are root-owned by docker; retry via container.
echo " (root-owned files; cleaning via docker)"
docker run --rm -v "${TEST_DIR}:/work" --workdir /work alpine:3 \
sh -c 'rm -rf testdata/postgres testdata/temporal testdata/keycloak/h2 testdata/fedwiki/*/status' \
|| true
git -C "$REPO_ROOT" clean -fdX -- test/testdata/ 2>/dev/null || true
fi
fi
echo "Teardown complete."