Mongo is a one-member replica set
Owner decision 2026-10-07: production's mongod becomes a one-member replica set (rs0), so that a backup can read every collection at one instant (a snapshot read session), which a standalone mongod can't. The unit runs mongod with --replSet rs0 and a 990 MB oplog; setup.sh converts it once, idempotently (PrivaPub stopped, mongod restarted, rs.initiate, the primary awaited, PrivaPub started); every connection string says directConnection=true, which works against the standalone too, so this code can deploy before the conversion. The CI's throwaway mongod and the pasture's are replica sets as well, so the snapshot path is what the tests exercise; MongoTopology tells which a mongod is, and TopologyTests holds the test mongod to it. The suite passes on it (906). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
1 parent
830385ea2b
commit
37b12c5fee
10 files changed
+90
-11
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs a command (normally `dotnet test`) with a throwaway mongod: a fresh data directory on a random localhost port,
|
||||
# stopped and deleted when the command ends. The box's own mongods (27017 shared, 27022 PrivaPub's) are never touched.
|
||||
# Like production's, it is a one-member replica set (rs0), so the backup's snapshot reads are what the tests exercise.
|
||||
# usage: tools/ci/with-test-mongod.sh dotnet test PrivaPub.sln -c Release
|
||||
set -euo pipefail
|
||||
|
||||
@@ -39,11 +40,11 @@ trap cleanup EXIT INT TERM
|
||||
if command -v mongod >/dev/null 2>&1; then
|
||||
mkdir -p "$dir/db"
|
||||
timeout 7200 mongod --dbpath "$dir/db" --port "$port" --bind_ip 127.0.0.1 --noauth \
|
||||
--wiredTigerCacheSizeGB 0.25 --quiet --logpath "$dir/mongod.log" &
|
||||
--wiredTigerCacheSizeGB 0.25 --replSet rs0 --quiet --logpath "$dir/mongod.log" &
|
||||
echo $! > "$dir/pid"
|
||||
how="mongod $(mongod --version | head -1)"
|
||||
elif command -v podman >/dev/null 2>&1; then
|
||||
podman run -d --rm -p "127.0.0.1:$port:27017" docker.io/library/mongo:8 --quiet > "$dir/container"
|
||||
podman run -d --rm -p "127.0.0.1:$port:27017" docker.io/library/mongo:8 --quiet --replSet rs0 > "$dir/container"
|
||||
how="podman mongo:8"
|
||||
else
|
||||
echo "::error::neither mongod nor podman is available for the test mongod"; exit 1
|
||||
@@ -51,7 +52,17 @@ fi
|
||||
|
||||
for _ in $(seq 1 60); do listening "$port" && break; sleep 0.5; done
|
||||
listening "$port" || { echo "::error::the test mongod did not start"; cat "$dir/mongod.log" 2>/dev/null | tail -20; exit 1; }
|
||||
echo "test mongod: $how on 127.0.0.1:$port"
|
||||
|
||||
export PRIVAPUB_TEST_MONGOD=1 PRIVAPUB_TEST_REQUIRE_MONGOD=1 PRIVAPUB_TEST_MONGO="mongodb://127.0.0.1:$port"
|
||||
# the one-member replica set, then wait until it is its primary
|
||||
shell() {
|
||||
if [ -f "$dir/container" ]; then podman exec "$(cat "$dir/container")" mongosh --quiet --eval "$1"
|
||||
else mongosh --quiet --port "$port" --eval "$1"; fi
|
||||
}
|
||||
member=$([ -f "$dir/container" ] && echo "127.0.0.1:27017" || echo "127.0.0.1:$port")
|
||||
shell "rs.initiate({_id: 'rs0', members: [{_id: 0, host: '$member'}]})" >/dev/null
|
||||
for _ in $(seq 1 60); do [ "$(shell 'db.hello().isWritablePrimary' 2>/dev/null)" = "true" ] && break; sleep 0.5; done
|
||||
[ "$(shell 'db.hello().isWritablePrimary' 2>/dev/null)" = "true" ] || { echo "::error::the test mongod never became its replica set's primary"; exit 1; }
|
||||
echo "test mongod: $how on 127.0.0.1:$port, replica set rs0"
|
||||
|
||||
export PRIVAPUB_TEST_MONGOD=1 PRIVAPUB_TEST_REQUIRE_MONGOD=1 PRIVAPUB_TEST_MONGO="mongodb://127.0.0.1:$port/?directConnection=true"
|
||||
"$@"
|
||||
Reference in new issue
Block a user