Skip to content

Commit d2b3ce1

Browse files
test(NODE-4160): add aws lambda examples (#3369)
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
1 parent e53be7c commit d2b3ce1

14 files changed

+315
-2
lines changed

.evergreen/config.in.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,40 @@ functions:
817817
args:
818818
- "${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh"
819819

820+
"run lambda handler example tests":
821+
- command: subprocess.exec
822+
params:
823+
working_dir: "src"
824+
timeout_secs: 60
825+
env:
826+
MONGODB_URI: ${MONGODB_URI}
827+
PROJECT_DIRECTORY: ${PROJECT_DIRECTORY}
828+
binary: bash
829+
args:
830+
- "${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh"
831+
832+
"run lambda handler example tests with aws auth":
833+
- command: shell.exec
834+
type: test
835+
params:
836+
working_dir: "src"
837+
silent: true
838+
script: |
839+
cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
840+
${MONGODB_BINARIES}/mongo --verbose aws_e2e_regular_aws.js
841+
cd -
842+
cat <<EOF > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
843+
export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account}
844+
export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key}
845+
EOF
846+
- command: shell.exec
847+
type: test
848+
params:
849+
working_dir: "src"
850+
script: |
851+
${PREPARE_SHELL}
852+
${PROJECT_DIRECTORY}/.evergreen/run-lambda-aws-tests.sh
853+
820854
"run bson-ext test":
821855
- command: subprocess.exec
822856
type: test

.evergreen/config.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,38 @@ functions:
781781
binary: bash
782782
args:
783783
- ${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh
784+
run lambda handler example tests:
785+
- command: subprocess.exec
786+
params:
787+
working_dir: src
788+
timeout_secs: 60
789+
env:
790+
MONGODB_URI: ${MONGODB_URI}
791+
PROJECT_DIRECTORY: ${PROJECT_DIRECTORY}
792+
binary: bash
793+
args:
794+
- ${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh
795+
run lambda handler example tests with aws auth:
796+
- command: shell.exec
797+
type: test
798+
params:
799+
working_dir: src
800+
silent: true
801+
script: |
802+
cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
803+
${MONGODB_BINARIES}/mongo --verbose aws_e2e_regular_aws.js
804+
cd -
805+
cat <<EOF > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
806+
export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account}
807+
export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key}
808+
EOF
809+
- command: shell.exec
810+
type: test
811+
params:
812+
working_dir: src
813+
script: |
814+
${PREPARE_SHELL}
815+
${PROJECT_DIRECTORY}/.evergreen/run-lambda-aws-tests.sh
784816
run bson-ext test:
785817
- command: subprocess.exec
786818
type: test
@@ -1320,6 +1352,32 @@ tasks:
13201352
AUTH: auth
13211353
COMPRESSOR: snappy
13221354
- func: run-compression-tests
1355+
- name: test-lambda-example
1356+
tags:
1357+
- latest
1358+
- lambda
1359+
commands:
1360+
- func: install dependencies
1361+
- func: bootstrap mongo-orchestration
1362+
vars:
1363+
VERSION: rapid
1364+
TOPOLOGY: server
1365+
- func: run lambda handler example tests
1366+
- name: test-lambda-aws-auth-example
1367+
tags:
1368+
- latest
1369+
- lambda
1370+
commands:
1371+
- func: install dependencies
1372+
- func: bootstrap mongo-orchestration
1373+
vars:
1374+
VERSION: rapid
1375+
AUTH: auth
1376+
ORCHESTRATION_FILE: auth-aws.json
1377+
TOPOLOGY: server
1378+
- func: add aws auth variables to file
1379+
- func: setup aws env
1380+
- func: run lambda handler example tests with aws auth
13231381
- name: test-tls-support-latest
13241382
tags:
13251383
- tls-support
@@ -2519,3 +2577,9 @@ buildvariants:
25192577
- test-3.6-server-noauth
25202578
- test-3.6-replica_set-noauth
25212579
- test-3.6-sharded_cluster-noauth
2580+
- name: ubuntu1804-test-lambda
2581+
display_name: AWS Lambda handler tests
2582+
run_on: ubuntu1804-test
2583+
tasks:
2584+
- test-lambda-example
2585+
- test-lambda-aws-auth-example

.evergreen/generate_evergreen_tasks.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,44 @@ for (const compressor of ['zstd', 'snappy']) {
242242
});
243243
}
244244

245+
// Add task for testing lambda example without aws auth.
246+
TASKS.push({
247+
name: 'test-lambda-example',
248+
tags: ['latest', 'lambda'],
249+
commands: [
250+
{ func: 'install dependencies' },
251+
{
252+
func: 'bootstrap mongo-orchestration',
253+
vars: {
254+
VERSION: 'rapid',
255+
TOPOLOGY: 'server'
256+
}
257+
},
258+
{ func: 'run lambda handler example tests' }
259+
]
260+
});
261+
262+
// Add task for testing lambda example with aws auth.
263+
TASKS.push({
264+
name: 'test-lambda-aws-auth-example',
265+
tags: ['latest', 'lambda'],
266+
commands: [
267+
{ func: 'install dependencies' },
268+
{
269+
func: 'bootstrap mongo-orchestration',
270+
vars: {
271+
VERSION: 'rapid',
272+
AUTH: 'auth',
273+
ORCHESTRATION_FILE: 'auth-aws.json',
274+
TOPOLOGY: 'server'
275+
}
276+
},
277+
{ func: 'add aws auth variables to file' },
278+
{ func: 'setup aws env' },
279+
{ func: 'run lambda handler example tests with aws auth' }
280+
]
281+
});
282+
245283
TLS_VERSIONS.forEach(VERSION => {
246284
TASKS.push({
247285
name: `test-tls-support-${VERSION}`,
@@ -626,6 +664,13 @@ BUILD_VARIANTS.push({
626664
tasks: AUTH_DISABLED_TASKS.map(({ name }) => name)
627665
});
628666

667+
BUILD_VARIANTS.push({
668+
name: 'ubuntu1804-test-lambda',
669+
display_name: 'AWS Lambda handler tests',
670+
run_on: 'ubuntu1804-test',
671+
tasks: ['test-lambda-example', 'test-lambda-aws-auth-example']
672+
});
673+
629674
// TODO(NODE-4575): unskip zstd and snappy on node 16
630675
for (const variant of BUILD_VARIANTS.filter(
631676
variant => variant.expansions && variant.expansions.NODE_LTS_NAME === 'gallium'

.evergreen/run-lambda-aws-tests.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
# set -o xtrace # Write all commands first to stderr
3+
set -o errexit # Exit the script with error if any of the commands fail
4+
5+
MONGODB_URI=${MONGODB_URI:-}
6+
7+
# ensure no secrets are printed in log files
8+
set +x
9+
10+
# load node.js environment
11+
source "${PROJECT_DIRECTORY}/.evergreen/init-nvm.sh"
12+
13+
# the default connection string, may be overridden by the environment script
14+
export MONGODB_URI="mongodb://localhost:27017/aws"
15+
16+
# load the script
17+
shopt -s expand_aliases # needed for `urlencode` alias
18+
[ -s "$PROJECT_DIRECTORY/prepare_mongodb_aws.sh" ] && source "$PROJECT_DIRECTORY/prepare_mongodb_aws.sh"
19+
20+
# revert to show test output
21+
set -x
22+
23+
npm install aws4
24+
npm run check:lambda:aws

.evergreen/run-lambda-tests.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
# set -o xtrace # Write all commands first to stderr
3+
set -o errexit # Exit the script with error if any of the commands fail
4+
5+
MONGODB_URI=${MONGODB_URI:-}
6+
7+
# ensure no secrets are printed in log files
8+
set +x
9+
10+
# load node.js environment
11+
source "${PROJECT_DIRECTORY}/.evergreen/init-nvm.sh"
12+
13+
npm run check:lambda

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
"check:bench": "node test/benchmarks/driverBench",
9999
"check:coverage": "nyc npm run test:all",
100100
"check:integration-coverage": "nyc npm run check:test",
101+
"check:lambda": "mocha --config test/mocha_lambda.json test/integration/node-specific/examples/handler.test.js",
102+
"check:lambda:aws": "mocha --config test/mocha_lambda.json test/integration/node-specific/examples/aws_handler.test.js",
101103
"check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd",
102104
"check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
103105
"check:tsd": "tsd --version && tsd",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// begin lambda connection
2+
const { MongoClient } = require('mongodb');
3+
4+
// Get the URI for the cluster then set AWS_ACCESS_KEY_ID as the username in the
5+
// URI and AWS_SECRET_ACCESS_KEY as the password, then set the appropriate auth
6+
// options. Note that MongoClient now auto-connects so no need to store the connect()
7+
// promise anywhere and reference it.
8+
const client = new MongoClient(process.env.MONGODB_URI, {
9+
auth: {
10+
username: process.env.AWS_ACCESS_KEY_ID,
11+
password: process.env.AWS_SECRET_ACCESS_KEY
12+
},
13+
authSource: '$external',
14+
authMechanism: 'MONGODB-AWS'
15+
});
16+
17+
module.exports.handler = async function () {
18+
const databases = await client.db('admin').command({ listDatabases: 1 });
19+
return {
20+
statusCode: 200,
21+
databases: databases
22+
};
23+
};
24+
// end lambda connection
25+
26+
module.exports.client = client;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { expect } = require('chai');
2+
const { client, handler } = require('./aws_handler');
3+
4+
describe('AWS Lambda Examples', function () {
5+
describe('#handler', function () {
6+
context('when using aws environment variable authentication', function () {
7+
let response;
8+
9+
before(async function () {
10+
response = await handler();
11+
});
12+
13+
after(async function () {
14+
await client.close();
15+
});
16+
17+
it('returns the databases', async function () {
18+
expect(response.databases).to.exist;
19+
});
20+
21+
it('returns the status code', async function () {
22+
expect(response.statusCode).to.equal(200);
23+
});
24+
});
25+
});
26+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// begin lambda connection
2+
const { MongoClient } = require('mongodb');
3+
4+
// MongoClient now auto-connects so no need to store the connect()
5+
// promise anywhere and reference it.
6+
const client = new MongoClient(process.env.MONGODB_URI);
7+
8+
module.exports.handler = async function () {
9+
const databases = await client.db('admin').command({ listDatabases: 1 });
10+
return {
11+
statusCode: 200,
12+
databases: databases
13+
};
14+
};
15+
// end lambda connection
16+
17+
module.exports.client = client;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { expect } = require('chai');
2+
const { client, handler } = require('./handler');
3+
4+
describe('AWS Lambda Examples', function () {
5+
describe('#handler', function () {
6+
context('when using standard authentication', function () {
7+
let response;
8+
9+
before(async function () {
10+
response = await handler();
11+
});
12+
13+
after(async function () {
14+
await client.close();
15+
});
16+
17+
it('returns the databases', async function () {
18+
expect(response.databases).to.exist;
19+
});
20+
21+
it('returns the status code', async function () {
22+
expect(response.statusCode).to.equal(200);
23+
});
24+
});
25+
});
26+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path');
2+
const Module = require('module');
3+
4+
const loader = Module._load;
5+
6+
// This little hack is to make require('mongodb') in our own project
7+
// during this specific test run to resolve to /lib so we can do
8+
// const { MongoClient } = require('mongodb');
9+
Module._load = function (request) {
10+
if (request === 'mongodb') {
11+
arguments[0] = path.join(__dirname, '..', '..', '..', '..', 'lib');
12+
}
13+
return loader.apply(this, arguments);
14+
};

test/mocha_lambda.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
3+
"require": [
4+
"test/integration/node-specific/examples/setup.js"
5+
],
6+
"extension": ["js"],
7+
"ui": "test/tools/runner/metadata_ui.js",
8+
"recursive": true,
9+
"timeout": 6000,
10+
"failZero": true,
11+
"reporter": "test/tools/reporter/mongodb_reporter.js",
12+
"sort": true,
13+
"color": true
14+
}

test/mocha_mongodb.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@
1414
"failZero": true,
1515
"reporter": "test/tools/reporter/mongodb_reporter.js",
1616
"sort": true,
17-
"color": true
17+
"color": true,
18+
"ignore": [
19+
"test/integration/node-specific/examples/handler.js",
20+
"test/integration/node-specific/examples/handler.test.js",
21+
"test/integration/node-specific/examples/aws_handler.js",
22+
"test/integration/node-specific/examples/aws_handler.test.js",
23+
"test/integration/node-specific/examples/setup.js"
24+
]
1825
}

0 commit comments

Comments
 (0)