Skip to content

Commit 165e02a

Browse files
taplinclaude
andcommitted
Add Scripts/test-linux.sh: local Linux test runs with Apple container
Runs the suite in swift:6.4-noble against a mongo:8 container, like CI. libmongoc 2.5.5 is built once into a cached volume; Linux build products live in their own volume. Uses plain `container run`, not `container build`, whose builder VM hung while compiling libmongoc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 149f9a9 commit 165e02a

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,13 @@ The tests expect a MongoDB server on `mongodb://localhost`:
110110
mongod --dbpath /tmp/mongo-test
111111
swift test
112112
```
113+
114+
To run the suite on Linux locally the way CI does (Swift 6.4, libmongoc 2.5.5, MongoDB 8), use Apple's
115+
[`container`](https://github.com/apple/container) tool:
116+
117+
```sh
118+
container system start # once
119+
Scripts/test-linux.sh # extra arguments go to swift test, e.g. --filter Phase4Tests
120+
```
121+
122+
The first run compiles libmongoc into a cached volume (under a minute); later runs take seconds.

‎Scripts/test-linux.sh‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
# Runs the test suite on Linux locally with Apple's `container` tool, matching CI:
3+
# Swift 6.4 on Ubuntu 24.04, libmongoc 2.5.5 built from source, MongoDB 8.
4+
#
5+
# Scripts/test-linux.sh # full suite
6+
# Scripts/test-linux.sh --filter Phase4 # extra arguments go to `swift test`
7+
#
8+
# Needs `container system start` to have been run once. The first run compiles
9+
# libmongoc into the `perfect-mongodb-mongoc` volume (a few minutes); later runs reuse
10+
# it. Linux build products live in the `perfect-mongodb-build` volume, separate from
11+
# the macOS .build directory. No image build (`container build`) is involved.
12+
13+
set -euo pipefail
14+
15+
SWIFT_IMAGE="docker.io/library/swift:6.4-noble"
16+
MONGO_IMAGE="docker.io/library/mongo:8"
17+
MONGOC_VERSION="2.5.5"
18+
MONGOC_VOLUME="perfect-mongodb-mongoc"
19+
BUILD_VOLUME="perfect-mongodb-build"
20+
MONGO="perfect-mongodb-test-mongo"
21+
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
22+
23+
for volume in "$MONGOC_VOLUME" "$BUILD_VOLUME"; do
24+
container volume inspect "$volume" >/dev/null 2>&1 || container volume create "$volume" >/dev/null
25+
done
26+
27+
# One-time: build libmongoc into the volume. The marker file records the version.
28+
if ! container run --rm --volume "$MONGOC_VOLUME:/opt/mongoc" "$SWIFT_IMAGE" \
29+
test -f "/opt/mongoc/.version-$MONGOC_VERSION" >/dev/null 2>&1; then
30+
echo "Building libmongoc $MONGOC_VERSION into the $MONGOC_VOLUME volume (first run only)..."
31+
container run --rm --cpus 4 --memory 4G --volume "$MONGOC_VOLUME:/opt/mongoc" "$SWIFT_IMAGE" bash -euc "
32+
apt-get update -qq
33+
apt-get install -y -qq --no-install-recommends build-essential cmake curl libssl-dev libsasl2-dev libzstd-dev >/dev/null
34+
rm -rf /opt/mongoc/*
35+
curl -fsSL https://github.com/mongodb/mongo-c-driver/releases/download/$MONGOC_VERSION/mongo-c-driver-$MONGOC_VERSION.tar.gz | tar xz -C /tmp
36+
cmake -S /tmp/mongo-c-driver-$MONGOC_VERSION -B /tmp/mongoc-build -DCMAKE_BUILD_TYPE=Release \
37+
-DCMAKE_INSTALL_PREFIX=/opt/mongoc -DENABLE_TESTS=OFF -DENABLE_EXAMPLES=OFF >/dev/null
38+
cmake --build /tmp/mongoc-build --parallel 4 >/dev/null
39+
cmake --install /tmp/mongoc-build >/dev/null
40+
touch /opt/mongoc/.version-$MONGOC_VERSION
41+
"
42+
fi
43+
44+
cleanup() {
45+
container stop "$MONGO" >/dev/null 2>&1 || true
46+
}
47+
trap cleanup EXIT
48+
cleanup
49+
container run --detach --rm --name "$MONGO" "$MONGO_IMAGE" >/dev/null
50+
MONGO_IP="$(container inspect "$MONGO" | python3 -c 'import json, sys; print(json.load(sys.stdin)[0]["status"]["networks"][0]["ipv4Address"].split("/")[0])')"
51+
echo "MongoDB at $MONGO_IP"
52+
53+
container run --rm \
54+
--cpus 4 --memory 6G \
55+
--volume "$ROOT:/work" \
56+
--volume "$BUILD_VOLUME:/build" \
57+
--volume "$MONGOC_VOLUME:/opt/mongoc" \
58+
--workdir /work \
59+
--env "MONGODB_URI=mongodb://$MONGO_IP:27017" \
60+
"$SWIFT_IMAGE" \
61+
bash -c '
62+
export PKG_CONFIG_PATH=$(dirname $(find /opt/mongoc -name mongoc2.pc))
63+
export LD_LIBRARY_PATH=$(dirname $(find /opt/mongoc -name "libmongoc2.so" | head -1))
64+
for i in $(seq 1 30); do (echo > /dev/tcp/'"$MONGO_IP"'/27017) 2>/dev/null && break; sleep 1; done
65+
swift test --scratch-path /build "$@"
66+
' bash "$@"

0 commit comments

Comments
 (0)