Skip to content

Commit

Permalink
run integration tests against several mongodb versions (#48)
Browse files Browse the repository at this point in the history
Runs integration tests against MongoDB versions 4, 6, and 7. (The oldest version supported by MongoDB is 4.4. The most recent stable release is 7.0) Version 5 is skipped for now because there is a problem with the `hello` native query example in that version.

We can't claim to support v3.6 because the `$replaceWith` stage wasn't implemented in that version.
  • Loading branch information
hallettj authored Apr 22, 2024
1 parent 4ea8cee commit 36e2bbc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
run: nix build .#checks.x86_64-linux.audit --print-build-logs

- name: run integration tests 📋
run: nix develop --command just test-integration
run: nix develop --command just test-mongodb-versions
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Requirements

* Rust `>= 1.57`
* MongoDB `>= 3.6`
* Rust via Rustup
* MongoDB `>= 4`
* OpenSSL development files

or Nix
or get dependencies automatically with Nix

Some of the build instructions require Nix. To set that up [install Nix][], and
configure it to [enable flakes][].
Expand Down Expand Up @@ -107,6 +107,18 @@ run from a fresh state. Note that you will have to remove any existing docker
volume to get to a fresh state. Using arion you can remove volumes by running
`arion down`.

### Running with a different MongoDB version

Override the MongoDB version that arion runs by assigning a Docker image name to
the environment variable `MONGODB_IMAGE`. For example,

$ arion down --volumes # delete potentially-incompatible MongoDB data
$ MONGODB_IMAGE=mongo:4 arion up -d

Or run integration tests against a specific MongoDB version,

$ MONGODB_IMAGE=mongo:4 just test-integration

## License

The Hasura MongoDB Connector is available under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0) (Apache-2.0).
20 changes: 14 additions & 6 deletions arion-compose/services/mongodb.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Provides an arion-compose service. Use in arion-compose.nix like this:
#
# services = {
# mongodb = import ./arion-compose/mongodb-service.nix {
# mongodb = import ./arion-compose/services/mongodb.nix {
# inherit pkgs;
# port = "27017";
# };
Expand All @@ -10,24 +10,32 @@
{ pkgs
, port ? "27017"
, hostPort ? null
, environment ? {}
, mongodb-image ? "mongo:7"
, environment ? { }
, volumes ? [
# By default load fixtures in the mongo-connector repo
(import ../fixtures/mongodb.nix).chinook
(import ../fixtures/mongodb.nix).allFixtures
]
}:

let
MONGO_INITDB_DATABASE = "test";

image-from-env = builtins.getEnv "MONGODB_IMAGE";
image = if image-from-env != "" then image-from-env else mongodb-image;

# Prior to v6 MongoDB provides an older client shell called "mongo". The new
# shell in v6 and later is called "mongosh"
mongosh = if builtins.lessThan major-version 6 then "mongo" else "mongosh";
major-version = pkgs.lib.toInt (builtins.head (builtins.match ".*:([0-9]).*" image));
in
{
service = {
image = "mongo:6-jammy";
inherit image volumes;
environment = { inherit MONGO_INITDB_DATABASE; } // environment;
inherit volumes;
ports = pkgs.lib.optionals (hostPort != null) [ "${hostPort}:${port}" ];
healthcheck = {
test = [ "CMD-SHELL" ''echo 'db.runCommand("ping").ok' | mongosh localhost:27017/${MONGO_INITDB_DATABASE} --quiet'' ];
test = [ "CMD-SHELL" ''echo 'db.runCommand("ping").ok' | ${mongosh} localhost:${port}/${MONGO_INITDB_DATABASE} --quiet'' ];
interval = "5s";
timeout = "10s";
retries = 5;
Expand Down
9 changes: 8 additions & 1 deletion fixtures/mongodb/chinook/chinook-import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ set -euo pipefail
FIXTURES=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
DATABASE_NAME=chinook

# In v6 and later the bundled MongoDB client shell is called "mongosh". In
# earlier versions it's called "mongo".
MONGO_SH=mongosh
if ! command -v mongosh &> /dev/null; then
MONGO_SH=mongo
fi

echo "📡 Importing Chinook into database $DATABASE_NAME..."

importCollection() {
local collection="$1"
local schema_file="$FIXTURES/$collection.schema.json"
local data_file="$FIXTURES/$collection.data.json"
echo "🔐 Applying validation for ${collection}..."
mongosh --eval "
$MONGO_SH --eval "
var schema = $(cat "${schema_file}");
db.createCollection('${collection}', { validator: schema });
" "$DATABASE_NAME"
Expand Down
11 changes: 9 additions & 2 deletions fixtures/mongodb/sample_import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ set -euo pipefail
# Get the directory of this script file
FIXTURES=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# In v6 and later the bundled MongoDB client shell is called "mongosh". In
# earlier versions it's called "mongo".
MONGO_SH=mongosh
if ! command -v mongosh &> /dev/null; then
MONGO_SH=mongo
fi

# Sample Claims Data
echo "📡 Importing claims sample data..."
mongoimport --db sample_claims --collection companies --type csv --headerline --file "$FIXTURES"/sample_claims/companies.csv
mongoimport --db sample_claims --collection carriers --type csv --headerline --file "$FIXTURES"/sample_claims/carriers.csv
mongoimport --db sample_claims --collection account_groups --type csv --headerline --file "$FIXTURES"/sample_claims/account_groups.csv
mongoimport --db sample_claims --collection claims --type csv --headerline --file "$FIXTURES"/sample_claims/claims.csv
mongosh sample_claims "$FIXTURES"/sample_claims/view_flat.js
mongosh sample_claims "$FIXTURES"/sample_claims/view_nested.js
$MONGO_SH sample_claims "$FIXTURES"/sample_claims/view_flat.js
$MONGO_SH sample_claims "$FIXTURES"/sample_claims/view_nested.js
echo "✅ Sample claims data imported..."

# mongo_flix
Expand Down
7 changes: 7 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ test-ndc: (_arion "arion-compose/ndc-test.nix" "test")

test-e2e: (_arion "arion-compose/e2e-testing.nix" "test")

# Run `just test-integration` on several MongoDB versions
test-mongodb-versions:
MONGODB_IMAGE=mongo:4 just test-integration
# MONGODB_IMAGE=mongo:5 just test-integration # there's a problem with the native query example in v5
MONGODB_IMAGE=mongo:6 just test-integration
MONGODB_IMAGE=mongo:7 just test-integration

# Runs a specified service in a specified project config using arion (a nix
# frontend for docker-compose). Propagates the exit status from that service.
_arion project service:
Expand Down

0 comments on commit 36e2bbc

Please sign in to comment.