#!/usr/bin/env bash set -Eeuo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" TEST_DIR="$(mktemp -d)" trap 'rm -rf "$TEST_DIR"' EXIT FAKE_BIN="$TEST_DIR/bin" DATA_DIR="$TEST_DIR/data" RUNTIME_DIR="$TEST_DIR/runtime" ENV_FILE="$TEST_DIR/production.env" IMAGES_ENV_FILE="$TEST_DIR/images.env" mkdir -p "$FAKE_BIN" "$DATA_DIR/local" "$RUNTIME_DIR" cat >"$ENV_FILE" <"$IMAGES_ENV_FILE" <<'EOF' WATERMAPS_DEPLOY_REVISION=0123456789abcdef0123456789abcdef01234567 WATERMAPS_APP_IMAGE=registry.example/watermaps:0123456789abcdef0123456789abcdef01234567 WATERMAPS_ROUTE_DATA_IMAGE=registry.example/watermaps-route-data:0123456789abcdef0123456789abcdef01234567 EOF cat >"$FAKE_BIN/id" <<'SH' #!/usr/bin/env bash if [[ "${1:-}" == "-u" ]]; then printf '0\n' else exec /usr/bin/id "$@" fi SH cat >"$FAKE_BIN/mountpoint" <<'SH' #!/usr/bin/env bash exit 0 SH cat >"$FAKE_BIN/docker" <<'SH' #!/usr/bin/env bash set -Eeuo pipefail if [[ "${1:-}" == "inspect" ]]; then printf 'healthy\n' exit 0 fi arguments=" $* " if [[ "$arguments" == *" ps --quiet watermaps "* ]]; then printf 'watermaps-test-container\n' exit 0 fi if [[ "$arguments" == *" run "*" route-data "* || "$arguments" == *" run "*" route-data" ]]; then route_file="$WATERMAPS_DATA_DIR/local/.germany-netherlands-fairways.candidate.json" marker_file="$WATERMAPS_DATA_DIR/local/.germany-netherlands-fairways.candidate.ready" cat >"$route_file" </dev/null || true)" if [[ -n "${FAKE_FAIL_REVISION:-}" && "$active_revision" == "$FAKE_FAIL_REVISION" ]]; then exit 1 fi exit 0 fi exit 0 SH chmod +x "$FAKE_BIN/id" "$FAKE_BIN/mountpoint" "$FAKE_BIN/docker" export PATH="$FAKE_BIN:$PATH" export WATERMAPS_ENV_FILE="$ENV_FILE" export WATERMAPS_IMAGES_ENV_FILE="$IMAGES_ENV_FILE" export WM_TEST_ROOT="$ROOT_DIR" # A first successful activation creates an active index and a validated marker. FAKE_ROUTE_REVISION=1 "$ROOT_DIR/deploy/scripts/update-route-data.sh" active_file="$DATA_DIR/local/germany-netherlands-fairways.json" active_marker="$DATA_DIR/local/.germany-netherlands-fairways.ready" [[ "$(jq --raw-output '.revision' "$active_file")" == "1" ]] export WATERMAPS_DATA_DIR="$DATA_DIR" export WATERMAPS_RUNTIME_DIR="$RUNTIME_DIR" # shellcheck source=../common.sh source "$ROOT_DIR/deploy/scripts/common.sh" wm_route_data_files_ready "$active_file" "$active_marker" # A candidate that fails its route smoke test must restore and retest revision 1. set +e FAKE_ROUTE_REVISION=2 FAKE_FAIL_REVISION=2 \ "$ROOT_DIR/deploy/scripts/update-route-data.sh" update_status=$? set -e [[ "$update_status" -ne 0 ]] [[ "$(jq --raw-output '.revision' "$active_file")" == "1" ]] wm_route_data_files_ready "$active_file" "$active_marker" [[ ! -e "$RUNTIME_DIR/locks/route-update.transaction" ]] # A hard-crash marker restores the durable previous index before any new build. backup_file="$DATA_DIR/local/.germany-netherlands-fairways.previous.json" backup_marker="$DATA_DIR/local/.germany-netherlands-fairways.previous.ready" cp "$active_file" "$backup_file" wm_write_route_marker "$backup_file" "$backup_marker" jq '.revision = 9' "$active_file" >"$active_file.tmp" mv "$active_file.tmp" "$active_file" chmod 0644 "$active_file" wm_write_route_marker "$active_file" "$active_marker" printf 'had_previous=true\n' >"$RUNTIME_DIR/locks/route-update.transaction" FAKE_ROUTE_REVISION=1 "$ROOT_DIR/deploy/scripts/update-route-data.sh" [[ "$(jq --raw-output '.revision' "$active_file")" == "1" ]] wm_route_data_files_ready "$active_file" "$active_marker" [[ ! -e "$RUNTIME_DIR/locks/route-update.transaction" ]] # Without a previous valid index, a failed first candidate must not stay ready. rm -f "$active_file" "$active_marker" \ "$DATA_DIR/local/.germany-netherlands-fairways.previous.json" \ "$DATA_DIR/local/.germany-netherlands-fairways.previous.ready" set +e FAKE_ROUTE_REVISION=3 FAKE_FAIL_REVISION=3 \ "$ROOT_DIR/deploy/scripts/update-route-data.sh" first_update_status=$? set -e [[ "$first_update_status" -ne 0 ]] [[ ! -e "$active_file" ]] [[ ! -e "$active_marker" ]] [[ ! -e "$RUNTIME_DIR/locks/route-update.transaction" ]] printf 'Staging-, Rollback- und First-Update-Prüfungen: OK\n'