From e81d30c9a2374d3284a269ae8ceccee61fcfed9a Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 17 Aug 2022 15:26:44 -0400 Subject: [PATCH 01/12] test(NODE-4160): add aws lambda examples --- package.json | 1 + test/examples/handler.js | 17 +++++++++++++++++ test/examples/lambda.test.js | 26 ++++++++++++++++++++++++++ test/examples/mocha_lambda.json | 14 ++++++++++++++ test/examples/setup.js | 10 ++++++++++ 5 files changed, 68 insertions(+) create mode 100644 test/examples/handler.js create mode 100644 test/examples/lambda.test.js create mode 100644 test/examples/mocha_lambda.json create mode 100644 test/examples/setup.js diff --git a/package.json b/package.json index 554523b8463..7fb746caa3a 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "check:bench": "node test/benchmarks/driverBench", "check:coverage": "nyc npm run test:all", "check:integration-coverage": "nyc npm run check:test", + "check:lambda": "mocha --config test/examples/mocha_lambda.json test/examples/lambda.test.js", "check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd", "check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test", "check:tsd": "tsd --version && tsd", diff --git a/test/examples/handler.js b/test/examples/handler.js new file mode 100644 index 00000000000..a2f791f1fe6 --- /dev/null +++ b/test/examples/handler.js @@ -0,0 +1,17 @@ +// begin lambda connection +const { MongoClient } = require('mongodb'); + +// MongoClient now auto-connects so no need to store the connect() +// promise anywhere and reference it. +const client = new MongoClient(process.env.MONGODB_URI); + +module.exports.handler = async function () { + const databases = await client.db('admin').command({ listDatabases: 1 }); + return { + statusCode: 200, + databases: databases + }; +}; +// end lambda connection + +module.exports.client = client; diff --git a/test/examples/lambda.test.js b/test/examples/lambda.test.js new file mode 100644 index 00000000000..8f1f8a22147 --- /dev/null +++ b/test/examples/lambda.test.js @@ -0,0 +1,26 @@ +const { expect } = require('chai'); +const { client, handler } = require('./handler'); + +describe('AWS Lambda Examples', function () { + describe('#handler', function () { + context('when using standard authentication', function () { + let response; + + before(async function () { + response = await handler(); + }); + + after(async function () { + await client.close(); + }); + + it('returns the databases', async function () { + expect(response.databases).to.exist; + }); + + it('returns the status code', async function () { + expect(response.statusCode).to.equal(200); + }); + }); + }); +}); diff --git a/test/examples/mocha_lambda.json b/test/examples/mocha_lambda.json new file mode 100644 index 00000000000..f254a693d57 --- /dev/null +++ b/test/examples/mocha_lambda.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json", + "require": [ + "test/examples/setup.js" + ], + "extension": ["js"], + "ui": "test/tools/runner/metadata_ui.js", + "recursive": true, + "timeout": 6000, + "failZero": true, + "reporter": "test/tools/reporter/mongodb_reporter.js", + "sort": true, + "color": true +} diff --git a/test/examples/setup.js b/test/examples/setup.js new file mode 100644 index 00000000000..fb4870e7c21 --- /dev/null +++ b/test/examples/setup.js @@ -0,0 +1,10 @@ +const path = require('path'); +const Module = require('module'); +const loader = Module._load; + +Module._load = function(request, loc) { + if (request === 'mongodb') { + arguments[0] = path.join(__dirname, '..', '..', 'lib'); + } + return loader.apply(this, arguments); +}; From c2362de25859cf6d453e36da018d9660f600cfcb Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Thu, 18 Aug 2022 13:24:32 -0400 Subject: [PATCH 02/12] test(NODE-4160): add evg run for lambda example --- .evergreen/config.in.yml | 34 +++++++++ .evergreen/config.yml | 69 +++++++++++++++++++ .evergreen/generate_evergreen_tasks.js | 38 ++++++++++ .evergreen/run-lambda-aws-tests.sh | 24 +++++++ .evergreen/run-lambda-tests.sh | 13 ++++ package.json | 3 +- test/examples/aws_handler.js | 26 +++++++ test/examples/aws_handler.test.js | 26 +++++++ .../{lambda.test.js => handler.test.js} | 0 test/examples/setup.js | 6 +- 10 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 .evergreen/run-lambda-aws-tests.sh create mode 100644 .evergreen/run-lambda-tests.sh create mode 100644 test/examples/aws_handler.js create mode 100644 test/examples/aws_handler.test.js rename test/examples/{lambda.test.js => handler.test.js} (100%) diff --git a/.evergreen/config.in.yml b/.evergreen/config.in.yml index 79d0fb3000b..35f8a64655f 100644 --- a/.evergreen/config.in.yml +++ b/.evergreen/config.in.yml @@ -817,6 +817,40 @@ functions: args: - "${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh" + "run lambda tests": + - command: subprocess.exec + params: + working_dir: "src" + timeout_secs: 60 + env: + MONGODB_URI: ${MONGODB_URI} + PROJECT_DIRECTORY: ${PROJECT_DIRECTORY} + binary: bash + args: + - "${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh" + + "run lambda tests with aws auth": + - command: shell.exec + type: test + params: + working_dir: "src" + silent: true + script: | + cd ${DRIVERS_TOOLS}/.evergreen/auth_aws + ${MONGODB_BINARIES}/mongo --verbose aws_e2e_regular_aws.js + cd - + cat < "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh" + export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account} + export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key} + EOF + - command: shell.exec + type: test + params: + working_dir: "src" + script: | + ${PREPARE_SHELL} + ${PROJECT_DIRECTORY}/.evergreen/run-lambda-aws-tests.sh + "run bson-ext test": - command: subprocess.exec type: test diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 93bd152e64f..a12c1c8d2e1 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -781,6 +781,38 @@ functions: binary: bash args: - ${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh + run lambda tests: + - command: subprocess.exec + params: + working_dir: src + timeout_secs: 60 + env: + MONGODB_URI: ${MONGODB_URI} + PROJECT_DIRECTORY: ${PROJECT_DIRECTORY} + binary: bash + args: + - ${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh + run lambda tests with aws auth: + - command: shell.exec + type: test + params: + working_dir: src + silent: true + script: | + cd ${DRIVERS_TOOLS}/.evergreen/auth_aws + ${MONGODB_BINARIES}/mongo --verbose aws_e2e_regular_aws.js + cd - + cat < "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh" + export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account} + export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key} + EOF + - command: shell.exec + type: test + params: + working_dir: src + script: | + ${PREPARE_SHELL} + ${PROJECT_DIRECTORY}/.evergreen/run-lambda-aws-tests.sh run bson-ext test: - command: subprocess.exec type: test @@ -1291,6 +1323,32 @@ tasks: AUTH: auth COMPRESSOR: snappy - func: run-compression-tests + - name: test-lambda-example + tags: + - latest + - lambda + commands: + - func: install dependencies + - func: bootstrap mongo-orchestration + vars: + VERSION: latest + TOPOLOGY: server + - func: run lambda tests + - name: test-lambda-aws-auth-example + tags: + - latest + - lambda + commands: + - func: install dependencies + - func: bootstrap mongo-orchestration + vars: + VERSION: latest + AUTH: auth + ORCHESTRATION_FILE: auth-aws.json + TOPOLOGY: server + - func: add aws auth variables to file + - func: setup aws env + - func: run lambda tests with aws auth - name: test-tls-support-latest tags: - tls-support @@ -2245,6 +2303,8 @@ buildvariants: - test-socks5-tls - test-zstd-compression - test-snappy-compression + - test-lambda-example + - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 @@ -2295,6 +2355,13 @@ buildvariants: - test-auth-ldap - test-socks5 - test-socks5-tls +<<<<<<< HEAD +======= + - test-zstd-compression + - test-snappy-compression + - test-lambda-example + - test-lambda-aws-auth-example +>>>>>>> test(NODE-4160): add evg run for lambda example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 @@ -2334,6 +2401,8 @@ buildvariants: - test-socks5-tls - test-zstd-compression - test-snappy-compression + - test-lambda-example + - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 diff --git a/.evergreen/generate_evergreen_tasks.js b/.evergreen/generate_evergreen_tasks.js index ee4cb262f56..effdc60d419 100644 --- a/.evergreen/generate_evergreen_tasks.js +++ b/.evergreen/generate_evergreen_tasks.js @@ -242,6 +242,44 @@ for (const compressor of ['zstd', 'snappy']) { }); } +// Add task for testing lambda example without aws auth. +TASKS.push({ + name: 'test-lambda-example', + tags: ['latest', 'lambda'], + commands: [ + { func: 'install dependencies' }, + { + func: 'bootstrap mongo-orchestration', + vars: { + VERSION: 'latest', + TOPOLOGY: 'server' + } + }, + { func: 'run lambda tests' } + ] +}); + +// Add task for testing lambda example with aws auth. +TASKS.push({ + name: 'test-lambda-aws-auth-example', + tags: ['latest', 'lambda'], + commands: [ + { func: 'install dependencies' }, + { + func: 'bootstrap mongo-orchestration', + vars: { + VERSION: 'latest', + AUTH: 'auth', + ORCHESTRATION_FILE: 'auth-aws.json', + TOPOLOGY: 'server' + } + }, + { func: 'add aws auth variables to file' }, + { func: 'setup aws env' }, + { func: 'run lambda tests with aws auth' } + ] +}); + TLS_VERSIONS.forEach(VERSION => { TASKS.push({ name: `test-tls-support-${VERSION}`, diff --git a/.evergreen/run-lambda-aws-tests.sh b/.evergreen/run-lambda-aws-tests.sh new file mode 100644 index 00000000000..ec0a24c200f --- /dev/null +++ b/.evergreen/run-lambda-aws-tests.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# set -o xtrace # Write all commands first to stderr +set -o errexit # Exit the script with error if any of the commands fail + +MONGODB_URI=${MONGODB_URI:-} + +# ensure no secrets are printed in log files +set +x + +# load node.js environment +source "${PROJECT_DIRECTORY}/.evergreen/init-nvm.sh" + +# the default connection string, may be overridden by the environment script +export MONGODB_URI="mongodb://localhost:27017/aws" + +# load the script +shopt -s expand_aliases # needed for `urlencode` alias +[ -s "$PROJECT_DIRECTORY/prepare_mongodb_aws.sh" ] && source "$PROJECT_DIRECTORY/prepare_mongodb_aws.sh" + +# revert to show test output +set -x + +npm install aws4 +npm run check:lambda:aws diff --git a/.evergreen/run-lambda-tests.sh b/.evergreen/run-lambda-tests.sh new file mode 100644 index 00000000000..6b632984fb2 --- /dev/null +++ b/.evergreen/run-lambda-tests.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# set -o xtrace # Write all commands first to stderr +set -o errexit # Exit the script with error if any of the commands fail + +MONGODB_URI=${MONGODB_URI:-} + +# ensure no secrets are printed in log files +set +x + +# load node.js environment +source "${PROJECT_DIRECTORY}/.evergreen/init-nvm.sh" + +npm run check:lambda diff --git a/package.json b/package.json index 7fb746caa3a..3692434a0a7 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,8 @@ "check:bench": "node test/benchmarks/driverBench", "check:coverage": "nyc npm run test:all", "check:integration-coverage": "nyc npm run check:test", - "check:lambda": "mocha --config test/examples/mocha_lambda.json test/examples/lambda.test.js", + "check:lambda": "mocha --config test/examples/mocha_lambda.json test/examples/handler.test.js", + "check:lambda:aws": "mocha --config test/examples/mocha_lambda.json test/examples/aws_handler.test.js", "check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd", "check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test", "check:tsd": "tsd --version && tsd", diff --git a/test/examples/aws_handler.js b/test/examples/aws_handler.js new file mode 100644 index 00000000000..6a4ee0e1833 --- /dev/null +++ b/test/examples/aws_handler.js @@ -0,0 +1,26 @@ +// begin lambda connection +const { MongoClient } = require('mongodb'); + +// Get the URI for the cluster then set AWS_ACCESS_KEY_ID as the username in the +// URI and AWS_SECRET_ACCESS_KEY as the password, then set the appropriate +// options. +const connectionString = new URL(process.env.MONGODB_URI); +connectionString.username = process.env.AWS_ACCESS_KEY_ID; +connectionString.password = encodeURIComponent(process.env.AWS_SECRET_ACCESS_KEY); +connectionString.searchParams.set('authSource', '$external'); +connectionString.searchParams.set('authMechanism', 'MONGODB-AWS'); + +// MongoClient now auto-connects so no need to store the connect() +// promise anywhere and reference it. +const client = new MongoClient(connectionString.toString()); + +module.exports.handler = async function () { + const databases = await client.db('admin').command({ listDatabases: 1 }); + return { + statusCode: 200, + databases: databases + }; +}; +// end lambda connection + +module.exports.client = client; diff --git a/test/examples/aws_handler.test.js b/test/examples/aws_handler.test.js new file mode 100644 index 00000000000..532b84e324a --- /dev/null +++ b/test/examples/aws_handler.test.js @@ -0,0 +1,26 @@ +const { expect } = require('chai'); +const { client, handler } = require('./aws_handler'); + +describe('AWS Lambda Examples', function () { + describe('#handler', function () { + context('when using aws environment variable authentication', function () { + let response; + + before(async function () { + response = await handler(); + }); + + after(async function () { + await client.close(); + }); + + it('returns the databases', async function () { + expect(response.databases).to.exist; + }); + + it('returns the status code', async function () { + expect(response.statusCode).to.equal(200); + }); + }); + }); +}); diff --git a/test/examples/lambda.test.js b/test/examples/handler.test.js similarity index 100% rename from test/examples/lambda.test.js rename to test/examples/handler.test.js diff --git a/test/examples/setup.js b/test/examples/setup.js index fb4870e7c21..53678dd0745 100644 --- a/test/examples/setup.js +++ b/test/examples/setup.js @@ -1,8 +1,12 @@ const path = require('path'); const Module = require('module'); + const loader = Module._load; -Module._load = function(request, loc) { +// This little hack is to make require('mongodb') in our own project +// during this specific test run to resolve to /lib so we can do +// const { MongoClient } = require('mongodb'); +Module._load = function (request) { if (request === 'mongodb') { arguments[0] = path.join(__dirname, '..', '..', 'lib'); } From 71906f50b01389c1ba29aa8380509e9d4f0ef086 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Thu, 18 Aug 2022 14:22:21 -0400 Subject: [PATCH 03/12] test(NODE-4160): update test readme --- test/readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/readme.md b/test/readme.md index 09b47a158cc..3ceaed69486 100644 --- a/test/readme.md +++ b/test/readme.md @@ -26,11 +26,12 @@ Below is a summary of the types of test automation in this repo. | Type of Test | Test Location | About the Tests | How to Run Tests | | ----------------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Unit | `/test/unit` | The unit tests test individual pieces of code, typically functions. These tests do **not** interact with a real database, so mocks are used instead.

The unit test directory mirrors the `/src` directory structure with test file names matching the source file names of the code they test. | `npm run check:unit` | -| Integration | `/test/integration` | The integration tests test that a given feature or piece of a feature is working as expected. These tests do **not** use mocks; instead, they interact with a real database.

The integration test directory follows the `test/spec` directory structure representing the different functional areas of the driver.

**Note:** The `.gitkeep` files are intentionally left to ensure that this directory structure is preserved even as the actual test files are moved around. | +| Integration | `/test/integration` | The integration tests test that a given feature or piece of a feature is working as expected. These tests do **not** use mocks; instead, they interact with a real database.

The integration test directory follows the `test/spec` directory structure representing the different functional areas of the driver.

**Note:** The `.gitkeep` files are intentionally left to ensure that this directory structure is preserved even as the actual test files are moved around. | `npm run check:test` | | Benchmark | `/test/benchmarks` | The benchmark tests report how long a designated set of tests take to run. They are used to measure performance. | `npm run check:bench` | | Specialized Environment | `/test/manual` | The specalized environment tests are functional tests that require specialized environment setups in Evergreen.

**Note**: "manual" in the directory path does not refer to tests that should be run manually. These tests are automated. These tests have a special Evergreen configuration and run in isolation from the other tests. | There is no single script for running all of the specialized environment tests. Instead, you can run the appropriate script based on the specialized environment you want to use:
- `npm run check:atlas` to test Atlas
- `npm run check:adl` to test Atlas Data Lake
- `npm run check:ocsp` to test OSCP
- `npm run check:kerberos` to test Kerberos
- `npm run check:tls` to test TLS
- `npm run check:ldap` to test LDAP authorization | | TypeScript Definition | `/test/types` | The TypeScript definition tests verify the type definitions are correct. | `npm run check:tsd` | | Github Actions | `/test/action` | Tests that run as Github actions such as dependency checking. | Currently only `npm run check:dependencies` but could be expanded to more in the future. | +| Code Examples | `/test/examples` | Code examples that are also paired with tests that show they are working examples. | Currently `npm run check:lambda` to test the AWS Lambda example with default auth and `npm run check:lambda:aws` to test the AWS Lambda example with AWS auth. | ### Spec Tests From 1bcd70f9e5b67a348791e91078263487cdaccfd2 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Fri, 19 Aug 2022 14:45:53 -0400 Subject: [PATCH 04/12] Revert "test: adjust number of iterations of flakey test" This reverts commit ce71d5c8e0fda89c510393d722bb3347f7d51862. --- .../server_selection.prose.operation_count.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/integration/server-selection/server_selection.prose.operation_count.test.ts b/test/integration/server-selection/server_selection.prose.operation_count.test.ts index 48e8c23cb85..b767174a74c 100644 --- a/test/integration/server-selection/server_selection.prose.operation_count.test.ts +++ b/test/integration/server-selection/server_selection.prose.operation_count.test.ts @@ -154,14 +154,12 @@ describe('operationCount-based Selection Within Latency Window - Prose Test', fu // This test has proved flakey, not just for Node. The number of iterations for the test has been increased, // to prevent the test from failing. // Step 8: Start 10 concurrent threads / tasks that each run 100 findOne operations with empty filters using that client. - await Promise.all( - Array.from({ length: numberTaskGroups }, () => runTaskGroup(collection, numberOfTasks)) - ); + await Promise.all(Array.from({ length: 10 }, () => runTaskGroup(collection, 100))); // Step 9: Using command monitoring events, assert that each mongos was selected roughly 50% of the time (within +/- 10%). const [host1, host2] = seeds.map(seed => seed.split(':')[1]); - const percentageToHost1 = (counts[host1] / totalNumberOfTasks) * 100; - const percentageToHost2 = (counts[host2] / totalNumberOfTasks) * 100; + const percentageToHost1 = (counts[host1] / 1000) * 100; + const percentageToHost2 = (counts[host2] / 1000) * 100; expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60); expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60); }); From 866cecd0aeedd10d02453557129d9e02ae63c10c Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 23 Aug 2022 20:25:17 +0200 Subject: [PATCH 05/12] test(NODE-4160): address comments --- .evergreen/config.in.yml | 4 ++-- .evergreen/config.yml | 13 +++++-------- .evergreen/generate_evergreen_tasks.js | 6 +++--- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.evergreen/config.in.yml b/.evergreen/config.in.yml index 35f8a64655f..08c9c5cac94 100644 --- a/.evergreen/config.in.yml +++ b/.evergreen/config.in.yml @@ -817,7 +817,7 @@ functions: args: - "${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh" - "run lambda tests": + "run lambda handler example tests": - command: subprocess.exec params: working_dir: "src" @@ -829,7 +829,7 @@ functions: args: - "${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh" - "run lambda tests with aws auth": + "run lambda handler example tests with aws auth": - command: shell.exec type: test params: diff --git a/.evergreen/config.yml b/.evergreen/config.yml index a12c1c8d2e1..2fa451fcaac 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -781,7 +781,7 @@ functions: binary: bash args: - ${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh - run lambda tests: + run lambda handler example tests: - command: subprocess.exec params: working_dir: src @@ -792,7 +792,7 @@ functions: binary: bash args: - ${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh - run lambda tests with aws auth: + run lambda handler example tests with aws auth: - command: shell.exec type: test params: @@ -1331,9 +1331,9 @@ tasks: - func: install dependencies - func: bootstrap mongo-orchestration vars: - VERSION: latest + VERSION: rapid TOPOLOGY: server - - func: run lambda tests + - func: run lambda handler example tests - name: test-lambda-aws-auth-example tags: - latest @@ -1348,7 +1348,7 @@ tasks: TOPOLOGY: server - func: add aws auth variables to file - func: setup aws env - - func: run lambda tests with aws auth + - func: run lambda handler example tests with aws auth - name: test-tls-support-latest tags: - tls-support @@ -2303,7 +2303,6 @@ buildvariants: - test-socks5-tls - test-zstd-compression - test-snappy-compression - - test-lambda-example - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 @@ -2359,7 +2358,6 @@ buildvariants: ======= - test-zstd-compression - test-snappy-compression - - test-lambda-example - test-lambda-aws-auth-example >>>>>>> test(NODE-4160): add evg run for lambda example - test-tls-support-latest @@ -2401,7 +2399,6 @@ buildvariants: - test-socks5-tls - test-zstd-compression - test-snappy-compression - - test-lambda-example - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 diff --git a/.evergreen/generate_evergreen_tasks.js b/.evergreen/generate_evergreen_tasks.js index effdc60d419..acb6065daaf 100644 --- a/.evergreen/generate_evergreen_tasks.js +++ b/.evergreen/generate_evergreen_tasks.js @@ -251,11 +251,11 @@ TASKS.push({ { func: 'bootstrap mongo-orchestration', vars: { - VERSION: 'latest', + VERSION: 'rapid', TOPOLOGY: 'server' } }, - { func: 'run lambda tests' } + { func: 'run lambda handler example tests' } ] }); @@ -276,7 +276,7 @@ TASKS.push({ }, { func: 'add aws auth variables to file' }, { func: 'setup aws env' }, - { func: 'run lambda tests with aws auth' } + { func: 'run lambda handler example tests with aws auth' } ] }); From 02f0a8b2ae147ea427f393fb57b8e10ff6ce672e Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 24 Aug 2022 13:50:52 +0200 Subject: [PATCH 06/12] chore(NODE-4160): move to other examples --- package.json | 4 ++-- test/{ => integration/node-specific}/examples/aws_handler.js | 0 .../node-specific}/examples/aws_handler.test.js | 0 test/{ => integration/node-specific}/examples/handler.js | 0 test/{ => integration/node-specific}/examples/handler.test.js | 0 test/{ => integration/node-specific}/examples/setup.js | 2 +- test/{examples => }/mocha_lambda.json | 2 +- 7 files changed, 4 insertions(+), 4 deletions(-) rename test/{ => integration/node-specific}/examples/aws_handler.js (100%) rename test/{ => integration/node-specific}/examples/aws_handler.test.js (100%) rename test/{ => integration/node-specific}/examples/handler.js (100%) rename test/{ => integration/node-specific}/examples/handler.test.js (100%) rename test/{ => integration/node-specific}/examples/setup.js (84%) rename test/{examples => }/mocha_lambda.json (86%) diff --git a/package.json b/package.json index 3692434a0a7..f4521ac12f9 100644 --- a/package.json +++ b/package.json @@ -98,8 +98,8 @@ "check:bench": "node test/benchmarks/driverBench", "check:coverage": "nyc npm run test:all", "check:integration-coverage": "nyc npm run check:test", - "check:lambda": "mocha --config test/examples/mocha_lambda.json test/examples/handler.test.js", - "check:lambda:aws": "mocha --config test/examples/mocha_lambda.json test/examples/aws_handler.test.js", + "check:lambda": "mocha --config test/mocha_lambda.json test/integration/node-specific/examples/handler.test.js", + "check:lambda:aws": "mocha --config test/mocha_lambda.json test/integration/node-specific/examples/aws_handler.test.js", "check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd", "check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test", "check:tsd": "tsd --version && tsd", diff --git a/test/examples/aws_handler.js b/test/integration/node-specific/examples/aws_handler.js similarity index 100% rename from test/examples/aws_handler.js rename to test/integration/node-specific/examples/aws_handler.js diff --git a/test/examples/aws_handler.test.js b/test/integration/node-specific/examples/aws_handler.test.js similarity index 100% rename from test/examples/aws_handler.test.js rename to test/integration/node-specific/examples/aws_handler.test.js diff --git a/test/examples/handler.js b/test/integration/node-specific/examples/handler.js similarity index 100% rename from test/examples/handler.js rename to test/integration/node-specific/examples/handler.js diff --git a/test/examples/handler.test.js b/test/integration/node-specific/examples/handler.test.js similarity index 100% rename from test/examples/handler.test.js rename to test/integration/node-specific/examples/handler.test.js diff --git a/test/examples/setup.js b/test/integration/node-specific/examples/setup.js similarity index 84% rename from test/examples/setup.js rename to test/integration/node-specific/examples/setup.js index 53678dd0745..dcd4deed87b 100644 --- a/test/examples/setup.js +++ b/test/integration/node-specific/examples/setup.js @@ -8,7 +8,7 @@ const loader = Module._load; // const { MongoClient } = require('mongodb'); Module._load = function (request) { if (request === 'mongodb') { - arguments[0] = path.join(__dirname, '..', '..', 'lib'); + arguments[0] = path.join(__dirname, '..', '..', '..', '..', 'lib'); } return loader.apply(this, arguments); }; diff --git a/test/examples/mocha_lambda.json b/test/mocha_lambda.json similarity index 86% rename from test/examples/mocha_lambda.json rename to test/mocha_lambda.json index f254a693d57..53ec17874ee 100644 --- a/test/examples/mocha_lambda.json +++ b/test/mocha_lambda.json @@ -1,7 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json", "require": [ - "test/examples/setup.js" + "test/integration/node-specific/examples/setup.js" ], "extension": ["js"], "ui": "test/tools/runner/metadata_ui.js", From 3a7e6847b8eb77a871bf751bab74e9f80c5c2c07 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 29 Aug 2022 21:57:37 +0200 Subject: [PATCH 07/12] test(NODE-4160): grep out the aws lambda tests --- .evergreen/config.yml | 6 +----- package.json | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 2fa451fcaac..8e1d7b708b3 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -2354,12 +2354,7 @@ buildvariants: - test-auth-ldap - test-socks5 - test-socks5-tls -<<<<<<< HEAD -======= - - test-zstd-compression - - test-snappy-compression - test-lambda-aws-auth-example ->>>>>>> test(NODE-4160): add evg run for lambda example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 @@ -2444,6 +2439,7 @@ buildvariants: - test-atlas-data-lake - test-socks5 - test-socks5-tls + - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 diff --git a/package.json b/package.json index f4521ac12f9..34644fb168c 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ "check:tsd": "tsd --version && tsd", "check:dependencies": "mocha test/action/dependency.test.ts", "check:dts": "node ./node_modules/typescript/bin/tsc --noEmit mongodb.d.ts && tsd", - "check:test": "mocha --config test/mocha_mongodb.json test/integration", + "check:test": "mocha --config test/mocha_mongodb.json test/integration --grep 'AWS Lambda Examples' --invert", "check:unit": "mocha test/unit", "check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit", "check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js", From 034634165cf731498613d5d5474387125df16774 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 29 Aug 2022 22:17:58 +0200 Subject: [PATCH 08/12] test(NODE-4160): ignore aws tests in config --- package.json | 2 +- .../server_selection.prose.operation_count.test.ts | 8 +++++--- test/mocha_mongodb.json | 9 ++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 34644fb168c..f4521ac12f9 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ "check:tsd": "tsd --version && tsd", "check:dependencies": "mocha test/action/dependency.test.ts", "check:dts": "node ./node_modules/typescript/bin/tsc --noEmit mongodb.d.ts && tsd", - "check:test": "mocha --config test/mocha_mongodb.json test/integration --grep 'AWS Lambda Examples' --invert", + "check:test": "mocha --config test/mocha_mongodb.json test/integration", "check:unit": "mocha test/unit", "check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit", "check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js", diff --git a/test/integration/server-selection/server_selection.prose.operation_count.test.ts b/test/integration/server-selection/server_selection.prose.operation_count.test.ts index b767174a74c..48e8c23cb85 100644 --- a/test/integration/server-selection/server_selection.prose.operation_count.test.ts +++ b/test/integration/server-selection/server_selection.prose.operation_count.test.ts @@ -154,12 +154,14 @@ describe('operationCount-based Selection Within Latency Window - Prose Test', fu // This test has proved flakey, not just for Node. The number of iterations for the test has been increased, // to prevent the test from failing. // Step 8: Start 10 concurrent threads / tasks that each run 100 findOne operations with empty filters using that client. - await Promise.all(Array.from({ length: 10 }, () => runTaskGroup(collection, 100))); + await Promise.all( + Array.from({ length: numberTaskGroups }, () => runTaskGroup(collection, numberOfTasks)) + ); // Step 9: Using command monitoring events, assert that each mongos was selected roughly 50% of the time (within +/- 10%). const [host1, host2] = seeds.map(seed => seed.split(':')[1]); - const percentageToHost1 = (counts[host1] / 1000) * 100; - const percentageToHost2 = (counts[host2] / 1000) * 100; + const percentageToHost1 = (counts[host1] / totalNumberOfTasks) * 100; + const percentageToHost2 = (counts[host2] / totalNumberOfTasks) * 100; expect(percentageToHost1).to.be.greaterThan(40).and.lessThan(60); expect(percentageToHost2).to.be.greaterThan(40).and.lessThan(60); }); diff --git a/test/mocha_mongodb.json b/test/mocha_mongodb.json index 27e5a32e528..a553bec88f4 100644 --- a/test/mocha_mongodb.json +++ b/test/mocha_mongodb.json @@ -14,5 +14,12 @@ "failZero": true, "reporter": "test/tools/reporter/mongodb_reporter.js", "sort": true, - "color": true + "color": true, + "ignore": [ + "test/integration/node-specific/examples/handler.js", + "test/integration/node-specific/examples/handler.test.js", + "test/integration/node-specific/examples/aws_handler.js", + "test/integration/node-specific/examples/aws_handler.test.js", + "test/integration/node-specific/examples/setup.js" + ] } From 05ff7fd697b95678ccb65433e2c408aa6709126f Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 30 Aug 2022 16:01:15 +0200 Subject: [PATCH 09/12] Update test/readme.md Co-authored-by: Bailey Pearson --- test/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/readme.md b/test/readme.md index 3ceaed69486..a962235d4b5 100644 --- a/test/readme.md +++ b/test/readme.md @@ -31,7 +31,7 @@ Below is a summary of the types of test automation in this repo. | Specialized Environment | `/test/manual` | The specalized environment tests are functional tests that require specialized environment setups in Evergreen.

**Note**: "manual" in the directory path does not refer to tests that should be run manually. These tests are automated. These tests have a special Evergreen configuration and run in isolation from the other tests. | There is no single script for running all of the specialized environment tests. Instead, you can run the appropriate script based on the specialized environment you want to use:
- `npm run check:atlas` to test Atlas
- `npm run check:adl` to test Atlas Data Lake
- `npm run check:ocsp` to test OSCP
- `npm run check:kerberos` to test Kerberos
- `npm run check:tls` to test TLS
- `npm run check:ldap` to test LDAP authorization | | TypeScript Definition | `/test/types` | The TypeScript definition tests verify the type definitions are correct. | `npm run check:tsd` | | Github Actions | `/test/action` | Tests that run as Github actions such as dependency checking. | Currently only `npm run check:dependencies` but could be expanded to more in the future. | -| Code Examples | `/test/examples` | Code examples that are also paired with tests that show they are working examples. | Currently `npm run check:lambda` to test the AWS Lambda example with default auth and `npm run check:lambda:aws` to test the AWS Lambda example with AWS auth. | +| Code Examples | `/test/integration/node-specific/examples` | Code examples that are also paired with tests that show they are working examples. | Currently `npm run check:lambda` to test the AWS Lambda example with default auth and `npm run check:lambda:aws` to test the AWS Lambda example with AWS auth. | ### Spec Tests From 0e8a4441abdfaaa3146c29a5fe47cc8bab8ebb5b Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 30 Aug 2022 16:04:06 +0200 Subject: [PATCH 10/12] test(NODE-4160): change server to rapid --- .evergreen/config.yml | 6 +----- .evergreen/generate_evergreen_tasks.js | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 8e1d7b708b3..8ad59a7798c 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -1342,7 +1342,7 @@ tasks: - func: install dependencies - func: bootstrap mongo-orchestration vars: - VERSION: latest + VERSION: rapid AUTH: auth ORCHESTRATION_FILE: auth-aws.json TOPOLOGY: server @@ -2303,7 +2303,6 @@ buildvariants: - test-socks5-tls - test-zstd-compression - test-snappy-compression - - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 @@ -2354,7 +2353,6 @@ buildvariants: - test-auth-ldap - test-socks5 - test-socks5-tls - - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 @@ -2394,7 +2392,6 @@ buildvariants: - test-socks5-tls - test-zstd-compression - test-snappy-compression - - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 @@ -2439,7 +2436,6 @@ buildvariants: - test-atlas-data-lake - test-socks5 - test-socks5-tls - - test-lambda-aws-auth-example - test-tls-support-latest - test-tls-support-6.0 - test-tls-support-5.0 diff --git a/.evergreen/generate_evergreen_tasks.js b/.evergreen/generate_evergreen_tasks.js index acb6065daaf..22a331a94d6 100644 --- a/.evergreen/generate_evergreen_tasks.js +++ b/.evergreen/generate_evergreen_tasks.js @@ -268,7 +268,7 @@ TASKS.push({ { func: 'bootstrap mongo-orchestration', vars: { - VERSION: 'latest', + VERSION: 'rapid', AUTH: 'auth', ORCHESTRATION_FILE: 'auth-aws.json', TOPOLOGY: 'server' From 30ecb53d20a143d1d95878097d3ad525f9a8970e Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 30 Aug 2022 16:20:37 +0200 Subject: [PATCH 11/12] test(NODE-4160): add build variants --- .evergreen/config.yml | 6 ++++++ .evergreen/generate_evergreen_tasks.js | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 8ad59a7798c..55257f0c2f6 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -2543,3 +2543,9 @@ buildvariants: - test-3.6-server-noauth - test-3.6-replica_set-noauth - test-3.6-sharded_cluster-noauth + - name: ubuntu1804-test-lambda + display_name: AWS Lambda handler tests + run_on: ubuntu1804-test + tasks: + - test-lambda-example + - test-lambda-aws-auth-example diff --git a/.evergreen/generate_evergreen_tasks.js b/.evergreen/generate_evergreen_tasks.js index 22a331a94d6..39bab462094 100644 --- a/.evergreen/generate_evergreen_tasks.js +++ b/.evergreen/generate_evergreen_tasks.js @@ -664,6 +664,13 @@ BUILD_VARIANTS.push({ tasks: AUTH_DISABLED_TASKS.map(({ name }) => name) }); +BUILD_VARIANTS.push({ + name: 'ubuntu1804-test-lambda', + display_name: 'AWS Lambda handler tests', + run_on: 'ubuntu1804-test', + tasks: ['test-lambda-example', 'test-lambda-aws-auth-example'] +}); + // TODO(NODE-4575): unskip zstd and snappy on node 16 for (const variant of BUILD_VARIANTS.filter( variant => variant.expansions && variant.expansions.NODE_LTS_NAME === 'gallium' From b5f5d2a122113c254159594cd32a9cd248b7461f Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 31 Aug 2022 17:17:32 +0200 Subject: [PATCH 12/12] test(NODE-4160): pass options to client --- .../node-specific/examples/aws_handler.js | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/integration/node-specific/examples/aws_handler.js b/test/integration/node-specific/examples/aws_handler.js index 6a4ee0e1833..b22ce92bf5a 100644 --- a/test/integration/node-specific/examples/aws_handler.js +++ b/test/integration/node-specific/examples/aws_handler.js @@ -2,17 +2,17 @@ const { MongoClient } = require('mongodb'); // Get the URI for the cluster then set AWS_ACCESS_KEY_ID as the username in the -// URI and AWS_SECRET_ACCESS_KEY as the password, then set the appropriate -// options. -const connectionString = new URL(process.env.MONGODB_URI); -connectionString.username = process.env.AWS_ACCESS_KEY_ID; -connectionString.password = encodeURIComponent(process.env.AWS_SECRET_ACCESS_KEY); -connectionString.searchParams.set('authSource', '$external'); -connectionString.searchParams.set('authMechanism', 'MONGODB-AWS'); - -// MongoClient now auto-connects so no need to store the connect() +// URI and AWS_SECRET_ACCESS_KEY as the password, then set the appropriate auth +// options. Note that MongoClient now auto-connects so no need to store the connect() // promise anywhere and reference it. -const client = new MongoClient(connectionString.toString()); +const client = new MongoClient(process.env.MONGODB_URI, { + auth: { + username: process.env.AWS_ACCESS_KEY_ID, + password: process.env.AWS_SECRET_ACCESS_KEY + }, + authSource: '$external', + authMechanism: 'MONGODB-AWS' +}); module.exports.handler = async function () { const databases = await client.db('admin').command({ listDatabases: 1 });