Skip to content

assert

assert #37

Workflow file for this run

name: Test
on:
push:
branches:
- '*'
tags-ignore:
- '*'
pull_request:
workflow_dispatch:
jobs:
build:
name: Build image
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Build Docker image
run: docker buildx build . --platform linux/amd64 --tag mongo-provision
- name: Export image
run: docker save mongo-provision | gzip > mongo-provision.tar.gz
- name: Upload image artifact
uses: actions/upload-artifact@v4
with:
name: mongo-provision-image
path: mongo-provision.tar.gz
retention-days: 1
test-single:
name: "single / ${{ matrix.version }}"
needs: build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: ["4.0", "4.2", "4.4", "5.0", "6.0", "7.0", "8.0"]
steps:
- name: Download image artifact
uses: actions/download-artifact@v4
with:
name: mongo-provision-image
- name: Load Docker image
run: docker load < mongo-provision.tar.gz
- name: Install mongosh
run: |
if [ "${{ matrix.version }}" = "4.0" ]; then
npm install -g mongosh@2.5.6
else
npm install -g mongosh
fi
- name: Test
env:
EXPECTED_VERSION: ${{ matrix.version }}
run: |
set -euo pipefail
cleanup() { docker rm -f mongo-test 2>/dev/null || true; }
trap cleanup EXIT
echo "Starting container …"
docker run -d --name mongo-test \
-p 27017:27017 \
mongo-provision "$EXPECTED_VERSION" --single
echo "Waiting for cluster to be ready …"
timeout 5m bash -c '
until docker exec mongo-test [ -e ready ] 2>/dev/null; do
if ! docker ps -q --filter name=mongo-test --filter status=running | grep -q .; then
echo "Container has exited:" >&2
docker logs mongo-test >&2
exit 1
fi
sleep 2
done
'
echo "Server is ready. Connecting …"
CONN=$(docker exec mongo-test cat ready | jq -r .connection_string)
mongosh --quiet "$CONN" --eval '
const expected = process.env.EXPECTED_VERSION;
const im = db.adminCommand({ isMaster: 1 });
const ver = db.version();
assert(
ver.startsWith(expected),
"Version mismatch: expected " + expected + ".x, got " + ver
);
assert(
!im.setName,
"Expected standalone (no replica set), got setName=" + im.setName
);
print("PASS: version=" + ver + ", standalone");
'
test-replset:
name: "replset-3 / ${{ matrix.version }}"
needs: build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: ["4.0", "4.2", "4.4", "5.0", "6.0", "7.0", "8.0"]
steps:
- name: Download image artifact
uses: actions/download-artifact@v4
with:
name: mongo-provision-image
- name: Load Docker image
run: docker load < mongo-provision.tar.gz
- name: Install mongosh
run: |
if [ "${{ matrix.version }}" = "4.0" ]; then
npm install -g mongosh@2.5.6
else
npm install -g mongosh
fi
- name: Test
env:
EXPECTED_VERSION: ${{ matrix.version }}
run: |
set -euo pipefail
cleanup() { docker rm -f mongo-test 2>/dev/null || true; }
trap cleanup EXIT
docker run -d --name mongo-test \
--network host \
mongo-provision "$EXPECTED_VERSION" --replicaset --nodes 3 --hostname localhost
echo "Waiting for cluster to be ready …"
timeout 5m bash -c '
until docker exec mongo-test [ -e ready ] 2>/dev/null; do
if ! docker ps -q --filter name=mongo-test --filter status=running | grep -q .; then
echo "Container has exited:" >&2
docker logs mongo-test >&2
exit 1
fi
sleep 2
done
'
CONN=$(docker exec mongo-test cat ready | jq -r ".connection_string")
echo "Server is ready. Connecting to $CONN …"
mongosh "$CONN" --eval '
const expected = process.env.EXPECTED_VERSION;
const im = db.adminCommand({ isMaster: 1 });
const ver = db.version();
assert(
ver.startsWith(expected),
"Version mismatch: expected " + expected + ".x, got " + ver
);
assert(
im.setName,
"Expected a replica set, got standalone"
);
const status = db.adminCommand({ replSetGetStatus: 1 });
assert.eq(
status.members.length,
3,
"Expected 3 nodes, got " + status.members.length
);
print(
"PASS: version=" + ver +
", replica set \"" + im.setName + "\"" +
", " + status.members.length + " nodes"
);
'
test-sharded:
name: "sharded-2x3 / ${{ matrix.version }}"
needs: build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: ["4.0", "4.2", "4.4", "5.0", "6.0", "7.0", "8.0"]
steps:
- name: Download image artifact
uses: actions/download-artifact@v4
with:
name: mongo-provision-image
- name: Load Docker image
run: docker load < mongo-provision.tar.gz
- name: Install mongosh
run: |
if [ "${{ matrix.version }}" = "4.0" ]; then
npm install -g mongosh@2.5.6
else
npm install -g mongosh
fi
- name: Test
env:
EXPECTED_VERSION: ${{ matrix.version }}
run: |
set -euo pipefail
cleanup() { docker rm -f mongo-test 2>/dev/null || true; }
trap cleanup EXIT
# --network host: container shares the host network namespace, so
# localhost:PORT works from host mongosh without port mapping, and
# RS members configured with --hostname localhost are always reachable.
docker run -d --name mongo-test \
--network host \
mongo-provision "$EXPECTED_VERSION" --replicaset --sharded 2 --nodes 3 --hostname localhost
echo "Waiting for cluster to be ready …"
timeout 10m bash -c '
until docker exec mongo-test [ -e ready ] 2>/dev/null; do
if ! docker ps -q --filter name=mongo-test --filter status=running | grep -q .; then
echo "Container has exited:" >&2
docker logs mongo-test >&2
exit 1
fi
sleep 2
done
'
echo "Server is ready. Connecting …"
CONN=$(docker exec mongo-test cat ready | jq -r .connection_string)
mongosh --quiet "$CONN" --eval '
const expected = process.env.EXPECTED_VERSION;
const im = db.adminCommand({ isMaster: 1 });
const ver = db.version();
assert(
ver.startsWith(expected),
"Version mismatch: expected " + expected + ".x, got " + ver
);
assert(
im.msg === "isdbgrid",
"Expected mongos (isdbgrid), got: " + JSON.stringify(im)
);
const listShards = db.adminCommand({ listShards: 1 });
assert(
listShards.shards.length === 2,
"Expected 2 shards, got " + listShards.shards.length
);
// Shard host strings look like "rsName/host:port,host:port,host:port".
// Count the members in the first shard to verify nodes-per-shard.
const firstShardHost = listShards.shards[0].host;
const hostsStr = firstShardHost.includes("/")
? firstShardHost.split("/")[1]
: firstShardHost;
const nodeCount = hostsStr.split(",").length;
assert(
nodeCount === 3,
"Expected 3 nodes/shard, got " + nodeCount + " (host: " + firstShardHost + ")"
);
print(
"PASS: version=" + ver +
", " + listShards.shards.length + " shards" +
", " + nodeCount + " nodes/shard"
);
'