mirror of
https://github.com/gadgethd/ukmesh.git
synced 2026-12-06 03:14:09 +00:00
44 lines
2.3 KiB
Bash
44 lines
2.3 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
script_dir="$(cd -- "$(dirname -- "$0")" && pwd)"
|
|
source "$script_dir/lib/analytics-snapshot.sh"
|
|
sql="$script_dir/lib/analytics-baseline.sql"
|
|
tmp_dir="$(mktemp -d)"
|
|
db="$(docker run -d --network none -e POSTGRES_HOST_AUTH_METHOD=trust postgres:16-bookworm)"
|
|
trap 'docker rm -fv "$db" >/dev/null; rm -rf -- "$tmp_dir"' EXIT
|
|
for ((attempt=0; attempt<60; attempt++)); do
|
|
docker exec "$db" pg_isready -h 127.0.0.1 -U postgres >/dev/null 2>&1 && break
|
|
sleep 1
|
|
done
|
|
psql_fixture() { command docker exec "$db" psql -Xq -v ON_ERROR_STOP=1 -U postgres -d "$1" -c "$2" >/dev/null; }
|
|
psql_fixture postgres 'CREATE TABLE nodes(node_id text, name text); CREATE TABLE packets(packet_hash text, time timestamptz, network text);'
|
|
for kind in empty populated; do
|
|
if [[ "$kind" == populated ]]; then
|
|
psql_fixture postgres "INSERT INTO nodes VALUES ('fixture-a', 'first'); INSERT INTO packets VALUES ('a', now(), 'fixture');"
|
|
fi
|
|
# Commit a row after export but before pg_dump opens its transaction.
|
|
docker() {
|
|
if [[ ${3:-} == pg_dump ]]; then
|
|
psql_fixture postgres "INSERT INTO nodes VALUES ('later', 'later');"
|
|
fi
|
|
command docker "$@"
|
|
}
|
|
dump_analytics_snapshot "$db" postgres postgres "$sql" "$tmp_dir/data.dump" "$tmp_dir/baseline.json"
|
|
unset -f docker
|
|
command docker exec "$db" createdb -U postgres "restored_$kind"
|
|
command docker exec -i "$db" pg_restore --exit-on-error -U postgres -d "restored_$kind" < "$tmp_dir/data.dump"
|
|
actual="$(read_analytics_baseline "$db" postgres "restored_$kind" "$sql")"
|
|
jq -e --argjson actual "$actual" '. == $actual' "$tmp_dir/baseline.json" >/dev/null
|
|
if [[ "$kind" == empty ]]; then
|
|
jq -e '.nodes == 0 and .packets == 0' "$tmp_dir/baseline.json" >/dev/null
|
|
else
|
|
psql_fixture "restored_$kind" "UPDATE nodes SET name = 'changed' WHERE node_id = 'fixture-a';"
|
|
actual="$(read_analytics_baseline "$db" postgres "restored_$kind" "$sql")"
|
|
jq -e --argjson actual "$actual" '.nodes == $actual.nodes and .node_sample_sha256 != $actual.node_sample_sha256' "$tmp_dir/baseline.json" >/dev/null
|
|
psql_fixture "restored_$kind" 'TRUNCATE packets'
|
|
actual="$(read_analytics_baseline "$db" postgres "restored_$kind" "$sql")"
|
|
jq -e --argjson actual "$actual" '.packets != $actual.packets' "$tmp_dir/baseline.json" >/dev/null
|
|
fi
|
|
done
|
|
echo 'Logical snapshot checks passed'
|