Skip to content

test(NODE-4160): add aws lambda examples #3369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .evergreen/config.in.yml
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,40 @@ functions:
args:
- "${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh"

"run lambda handler example 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 handler example 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 <<EOF > "${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
Expand Down
64 changes: 64 additions & 0 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,38 @@ functions:
binary: bash
args:
- ${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh
run lambda handler example 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 handler example 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 <<EOF > "${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
Expand Down Expand Up @@ -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: rapid
TOPOLOGY: server
- func: run lambda handler example tests
- name: test-lambda-aws-auth-example
tags:
- latest
- lambda
commands:
- func: install dependencies
- func: bootstrap mongo-orchestration
vars:
VERSION: rapid
AUTH: auth
ORCHESTRATION_FILE: auth-aws.json
TOPOLOGY: server
- func: add aws auth variables to file
- func: setup aws env
- func: run lambda handler example tests with aws auth
- name: test-tls-support-latest
tags:
- tls-support
Expand Down Expand Up @@ -2485,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
45 changes: 45 additions & 0 deletions .evergreen/generate_evergreen_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: 'rapid',
TOPOLOGY: 'server'
}
},
{ func: 'run lambda handler example 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: 'rapid',
AUTH: 'auth',
ORCHESTRATION_FILE: 'auth-aws.json',
TOPOLOGY: 'server'
}
},
{ func: 'add aws auth variables to file' },
{ func: 'setup aws env' },
{ func: 'run lambda handler example tests with aws auth' }
]
});

TLS_VERSIONS.forEach(VERSION => {
TASKS.push({
name: `test-tls-support-${VERSION}`,
Expand Down Expand Up @@ -626,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'
Expand Down
24 changes: 24 additions & 0 deletions .evergreen/run-lambda-aws-tests.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions .evergreen/run-lambda-tests.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +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/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",
Expand Down
26 changes: 26 additions & 0 deletions test/integration/node-specific/examples/aws_handler.js
Original file line number Diff line number Diff line change
@@ -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 auth
// options. Note that MongoClient now auto-connects so no need to store the connect()
// promise anywhere and reference it.
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 });
return {
statusCode: 200,
databases: databases
};
};
// end lambda connection

module.exports.client = client;
26 changes: 26 additions & 0 deletions test/integration/node-specific/examples/aws_handler.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
17 changes: 17 additions & 0 deletions test/integration/node-specific/examples/handler.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 26 additions & 0 deletions test/integration/node-specific/examples/handler.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
14 changes: 14 additions & 0 deletions test/integration/node-specific/examples/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path');
const Module = require('module');

const loader = Module._load;

// 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');
}
return loader.apply(this, arguments);
};
14 changes: 14 additions & 0 deletions test/mocha_lambda.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
"require": [
"test/integration/node-specific/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
}
9 changes: 8 additions & 1 deletion test/mocha_mongodb.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
Loading