Files
watermaps/deploy/scripts/tests/update-route-data.test.sh
BuTzZ 593dbd5f85
Test and publish container images / test (push) Successful in 2m26s
Test and publish container images / publish (push) Failing after 3s
optimized events
2026-07-28 14:36:47 +02:00

196 lines
6.0 KiB
Bash
Executable File

#!/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" <<EOF
WATERMAPS_DATA_DIR=$DATA_DIR
WATERMAPS_RUNTIME_DIR=$RUNTIME_DIR
WATERMAPS_POSTGRES_PASSWORD=0123456789abcdef0123456789abcdef
EOF
cat >"$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 postgres "* ]]; then
printf 'postgres-test-container\n'
exit 0
fi
if [[ "$arguments" == *" ps --quiet watermaps "* ]]; then
printf 'watermaps-test-container\n'
exit 0
fi
if [[ "$arguments" == *" run "*" route-data "* ||
"$arguments" == *" run "*" route-data" ]]; then
mkdir -p "$WATERMAPS_DATA_DIR/geofabrik"
printf '%s germany-latest.osm.pbf\n' \
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \
>"$WATERMAPS_DATA_DIR/geofabrik/germany-latest.osm.pbf.md5"
printf '%s netherlands-latest.osm.pbf\n' \
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' \
>"$WATERMAPS_DATA_DIR/geofabrik/netherlands-latest.osm.pbf.md5"
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" <<JSON
{
"version": 1,
"generatorVersion": 2,
"source": "germany+netherlands",
"revision": ${FAKE_ROUTE_REVISION:-1},
"sources": [
{
"region": "germany",
"file": "germany-latest.osm.pbf",
"checksumMd5": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"sizeBytes": 1
},
{
"region": "netherlands",
"file": "netherlands-latest.osm.pbf",
"checksumMd5": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"sizeBytes": 1
}
],
"ways": [{"id": "1"}]
}
JSON
# shellcheck source=../common.sh
source "$WM_TEST_ROOT/deploy/scripts/common.sh"
wm_write_route_marker "$route_file" "$marker_file"
exit 0
fi
if [[ "$arguments" == *" run "*" marine-data "* ||
"$arguments" == *" run "*" marine-data" ]]; then
cat >"$WATERMAPS_DATA_DIR/local/.marine-features.ready" <<'MARKER'
format_version=1
generated_at=2026-07-28T00:00:00Z
source=germany+netherlands
germany_md5=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
netherlands_md5=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
total_osm_features=100
harbour_count=10
lock_count=5
bridge_count=20
MARKER
exit 0
fi
if [[ "$arguments" == *" exec --no-TTY postgres "* ]]; then
printf '100|10|5|20\n'
exit 0
fi
if [[ "$arguments" == *" exec --no-TTY watermaps "* ]]; then
active_file="$WATERMAPS_DATA_DIR/local/germany-netherlands-fairways.json"
active_revision="$(jq --raw-output '.revision // 0' "$active_file" 2>/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'