Skip to content

Commit fc6d1d7

Browse files
durranbaileympearson
authored andcommitted
test(NODE-4160): add evg run for lambda example
1 parent 679a1ec commit fc6d1d7

File tree

10 files changed

+232
-2
lines changed

10 files changed

+232
-2
lines changed

.evergreen/config.in.yml

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

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

.evergreen/config.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,38 @@ functions:
782782
binary: bash
783783
args:
784784
- ${PROJECT_DIRECTORY}/.evergreen/run-snappy-version-test.sh
785+
run lambda tests:
786+
- command: subprocess.exec
787+
params:
788+
working_dir: src
789+
timeout_secs: 60
790+
env:
791+
MONGODB_URI: ${MONGODB_URI}
792+
PROJECT_DIRECTORY: ${PROJECT_DIRECTORY}
793+
binary: bash
794+
args:
795+
- ${PROJECT_DIRECTORY}/.evergreen/run-lambda-tests.sh
796+
run lambda tests with aws auth:
797+
- command: shell.exec
798+
type: test
799+
params:
800+
working_dir: src
801+
silent: true
802+
script: |
803+
cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
804+
${MONGODB_BINARIES}/mongo --verbose aws_e2e_regular_aws.js
805+
cd -
806+
cat <<EOF > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
807+
export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account}
808+
export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key}
809+
EOF
810+
- command: shell.exec
811+
type: test
812+
params:
813+
working_dir: src
814+
script: |
815+
${PREPARE_SHELL}
816+
${PROJECT_DIRECTORY}/.evergreen/run-lambda-aws-tests.sh
785817
run bson-ext test:
786818
- command: subprocess.exec
787819
type: test
@@ -1253,6 +1285,32 @@ tasks:
12531285
AUTH: auth
12541286
COMPRESSOR: snappy
12551287
- func: run-compression-tests
1288+
- name: test-lambda-example
1289+
tags:
1290+
- latest
1291+
- lambda
1292+
commands:
1293+
- func: install dependencies
1294+
- func: bootstrap mongo-orchestration
1295+
vars:
1296+
VERSION: latest
1297+
TOPOLOGY: server
1298+
- func: run lambda tests
1299+
- name: test-lambda-aws-auth-example
1300+
tags:
1301+
- latest
1302+
- lambda
1303+
commands:
1304+
- func: install dependencies
1305+
- func: bootstrap mongo-orchestration
1306+
vars:
1307+
VERSION: latest
1308+
AUTH: auth
1309+
ORCHESTRATION_FILE: auth-aws.json
1310+
TOPOLOGY: server
1311+
- func: add aws auth variables to file
1312+
- func: setup aws env
1313+
- func: run lambda tests with aws auth
12561314
- name: test-tls-support-latest
12571315
tags:
12581316
- tls-support
@@ -2162,6 +2220,8 @@ buildvariants:
21622220
- test-socks5-tls
21632221
- test-zstd-compression
21642222
- test-snappy-compression
2223+
- test-lambda-example
2224+
- test-lambda-aws-auth-example
21652225
- test-tls-support-latest
21662226
- test-tls-support-6.0
21672227
- test-tls-support-5.0
@@ -2221,6 +2281,8 @@ buildvariants:
22212281
- test-socks5-tls
22222282
- test-zstd-compression
22232283
- test-snappy-compression
2284+
- test-lambda-example
2285+
- test-lambda-aws-auth-example
22242286
- test-tls-support-latest
22252287
- test-tls-support-6.0
22262288
- test-tls-support-5.0
@@ -2274,6 +2336,8 @@ buildvariants:
22742336
- test-socks5-tls
22752337
- test-zstd-compression
22762338
- test-snappy-compression
2339+
- test-lambda-example
2340+
- test-lambda-aws-auth-example
22772341
- test-tls-support-latest
22782342
- test-tls-support-6.0
22792343
- test-tls-support-5.0

.evergreen/generate_evergreen_tasks.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,44 @@ TASKS.push(
247247
});
248248
});
249249

250+
// Add task for testing lambda example without aws auth.
251+
TASKS.push({
252+
name: 'test-lambda-example',
253+
tags: ['latest', 'lambda'],
254+
commands: [
255+
{ func: 'install dependencies' },
256+
{
257+
func: 'bootstrap mongo-orchestration',
258+
vars: {
259+
VERSION: 'latest',
260+
TOPOLOGY: 'server'
261+
}
262+
},
263+
{ func: 'run lambda tests' }
264+
]
265+
});
266+
267+
// Add task for testing lambda example with aws auth.
268+
TASKS.push({
269+
name: 'test-lambda-aws-auth-example',
270+
tags: ['latest', 'lambda'],
271+
commands: [
272+
{ func: 'install dependencies' },
273+
{
274+
func: 'bootstrap mongo-orchestration',
275+
vars: {
276+
VERSION: 'latest',
277+
AUTH: 'auth',
278+
ORCHESTRATION_FILE: 'auth-aws.json',
279+
TOPOLOGY: 'server'
280+
}
281+
},
282+
{ func: 'add aws auth variables to file' },
283+
{ func: 'setup aws env' },
284+
{ func: 'run lambda tests with aws auth' }
285+
]
286+
});
287+
250288
TLS_VERSIONS.forEach(VERSION => {
251289
TASKS.push({
252290
name: `test-tls-support-${VERSION}`,

.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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +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/examples/mocha_lambda.json test/examples/lambda.test.js",
101+
"check:lambda": "mocha --config test/examples/mocha_lambda.json test/examples/handler.test.js",
102+
"check:lambda:aws": "mocha --config test/examples/mocha_lambda.json test/examples/aws_handler.test.js",
102103
"check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd",
103104
"check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
104105
"check:tsd": "tsd --version && tsd",

test/examples/aws_handler.js

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
6+
// options.
7+
const connectionString = new URL(process.env.MONGODB_URI);
8+
connectionString.username = process.env.AWS_ACCESS_KEY_ID;
9+
connectionString.password = encodeURIComponent(process.env.AWS_SECRET_ACCESS_KEY);
10+
connectionString.searchParams.set('authSource', '$external');
11+
connectionString.searchParams.set('authMechanism', 'MONGODB-AWS');
12+
13+
// MongoClient now auto-connects so no need to store the connect()
14+
// promise anywhere and reference it.
15+
const client = new MongoClient(connectionString.toString());
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;

test/examples/aws_handler.test.js

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+
});
File renamed without changes.

test/examples/setup.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const path = require('path');
22
const Module = require('module');
3+
34
const loader = Module._load;
45

5-
Module._load = function(request, loc) {
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) {
610
if (request === 'mongodb') {
711
arguments[0] = path.join(__dirname, '..', '..', 'lib');
812
}

0 commit comments

Comments
 (0)