-
Notifications
You must be signed in to change notification settings - Fork 1.8k
test(NODE-4693): test lambda function #3569
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6ed3386
test(WRITING-11435): test lambda function
durran 5696c4e
test: adding readme and number printout
durran 1a2f37b
chore: update readme
durran 16d5253
feat: updating scripts and env
durran e932c1f
test: add task to evg to see failures
durran 199e284
test: continue on the journey
durran f77609b
test: update tests
durran e1f96d1
test: use drivers tools test
durran 8dfbd1c
test: add sam to box
durran d243b6f
test: set region
durran 4143d53
test: add region
durran 7037d56
test: export vars
durran b4b5fd5
test: update env
durran 2e762cf
chore: local readme
durran 0f144d6
chore: json stringify before reset
durran bfa5bad
chore: sam now installed on evg
durran 67836d8
test: point to tools master
durran 33c23b5
test: test with branch
durran 8858932
test: use master from tools
durran b38ff4b
test: increase role duratiom
durran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
set -o errexit # Exit the script with error if any of the commands fail | ||
|
||
. ${DRIVERS_TOOLS}/.evergreen/run-deployed-lambda-aws-tests.sh | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
AWS Lambda Testing | ||
------------------ | ||
|
||
Running locally | ||
=============== | ||
|
||
Prerequisites: | ||
|
||
- AWS SAM CLI | ||
- Docker daemon running | ||
|
||
Steps | ||
===== | ||
|
||
- `sam build` from the `test/lambda`. | ||
|
||
- `sam local invoke --parameter-overrides "MongoDbUri=mongodb://127.0.0.1:27017"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{ | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"body": "{\"message\": \"hello world\"}", | ||
"resource": "/{proxy+}", | ||
"path": "/path/to/resource", | ||
"httpMethod": "POST", | ||
"isBase64Encoded": false, | ||
"queryStringParameters": { | ||
"foo": "bar" | ||
}, | ||
"pathParameters": { | ||
"proxy": "/path/to/resource" | ||
}, | ||
"stageVariables": { | ||
"baz": "qux" | ||
}, | ||
"headers": { | ||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||
"Accept-Encoding": "gzip, deflate, sdch", | ||
"Accept-Language": "en-US,en;q=0.8", | ||
"Cache-Control": "max-age=0", | ||
"CloudFront-Forwarded-Proto": "https", | ||
"CloudFront-Is-Desktop-Viewer": "true", | ||
"CloudFront-Is-Mobile-Viewer": "false", | ||
"CloudFront-Is-SmartTV-Viewer": "false", | ||
"CloudFront-Is-Tablet-Viewer": "false", | ||
"CloudFront-Viewer-Country": "US", | ||
"Host": "1234567890.execute-api.us-east-1.amazonaws.com", | ||
"Upgrade-Insecure-Requests": "1", | ||
"User-Agent": "Custom User Agent String", | ||
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", | ||
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", | ||
"X-Forwarded-For": "127.0.0.1, 127.0.0.2", | ||
"X-Forwarded-Port": "443", | ||
"X-Forwarded-Proto": "https" | ||
}, | ||
"requestContext": { | ||
"accountId": "123456789012", | ||
"resourceId": "123456", | ||
"stage": "prod", | ||
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", | ||
"requestTime": "09/Apr/2015:12:34:56 +0000", | ||
"requestTimeEpoch": 1428582896000, | ||
"identity": { | ||
"cognitoIdentityPoolId": null, | ||
"accountId": null, | ||
"cognitoIdentityId": null, | ||
"caller": null, | ||
"accessKey": null, | ||
"sourceIp": "127.0.0.1", | ||
"cognitoAuthenticationType": null, | ||
"cognitoAuthenticationProvider": null, | ||
"userArn": null, | ||
"userAgent": "Custom User Agent String", | ||
"user": null | ||
}, | ||
"path": "/prod/path/to/resource", | ||
"resourcePath": "/{proxy+}", | ||
"httpMethod": "POST", | ||
"apiId": "1234567890", | ||
"protocol": "HTTP/1.1" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.aws-sam |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tests/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { MongoClient } from 'mongodb'; | ||
|
||
// Creates the client that is cached for all requests, subscribes to | ||
// relevant events, and forces the connection pool to get populated. | ||
const mongoClient = new MongoClient(process.env.MONGODB_URI, { | ||
monitorCommands: true | ||
}); | ||
|
||
let openConnections = 0; | ||
let heartbeatCount = 0; | ||
let totalHeartbeatDuration = 0; | ||
let totalCommands = 0; | ||
let totalCommandDuration = 0; | ||
|
||
mongoClient.on('commandStarted', (event) => { | ||
console.log('commandStarted', event); | ||
}); | ||
|
||
mongoClient.on('commandSucceeded', (event) => { | ||
totalCommands++; | ||
totalCommandDuration += event.duration; | ||
console.log('commandSucceeded', event); | ||
}); | ||
|
||
mongoClient.on('commandFailed', (event) => { | ||
totalCommands++; | ||
totalCommandDuration += event.duration; | ||
console.log('commandFailed', event); | ||
}); | ||
|
||
mongoClient.on('serverHeartbeatStarted', (event) => { | ||
console.log('serverHeartbeatStarted', event); | ||
}); | ||
|
||
mongoClient.on('serverHeartbeatSucceeded', (event) => { | ||
heartbeatCount++; | ||
totalHeartbeatDuration += event.duration; | ||
console.log('serverHeartbeatSucceeded', event); | ||
}); | ||
|
||
mongoClient.on('serverHeartbeatFailed', (event) => { | ||
heartbeatCount++; | ||
totalHeartbeatDuration += event.duration; | ||
console.log('serverHeartbeatFailed', event); | ||
}); | ||
|
||
mongoClient.on('connectionCreated', (event) => { | ||
openConnections++; | ||
console.log('connectionCreated', event); | ||
}); | ||
|
||
mongoClient.on('connectionClosed', (event) => { | ||
openConnections--; | ||
console.log('connectionClosed', event); | ||
}); | ||
|
||
// Populate the connection pool. | ||
await mongoClient.connect(); | ||
|
||
// Create the response to send back. | ||
function createResponse() { | ||
return { | ||
averageCommandDuration: totalCommandDuration / totalCommands, | ||
averageHeartbeatDuration: totalHeartbeatDuration / heartbeatCount, | ||
openConnections: openConnections, | ||
heartbeatCount: heartbeatCount | ||
}; | ||
} | ||
|
||
// Reset the numbers. | ||
function reset() { | ||
openConnections = 0; | ||
heartbeatCount = 0; | ||
totalHeartbeatDuration = 0; | ||
totalCommands = 0; | ||
totalCommandDuration = 0; | ||
} | ||
|
||
/** | ||
* The handler function itself performs an insert/delete and returns the | ||
* id of the document in play. | ||
* | ||
* @param {Object} event - API Gateway Lambda Proxy Input Format | ||
* @returns {Object} object - API Gateway Lambda Proxy Output Format | ||
*/ | ||
export const lambdaHandler = async (event) => { | ||
const db = mongoClient.db('lambdaTest'); | ||
const collection = db.collection('test'); | ||
const { insertedId } = await collection.insertOne({ n: 1 }); | ||
await collection.deleteOne({ _id: insertedId }); | ||
// Create the response and then reset the numbers. | ||
const response = JSON.stringify(createResponse()); | ||
reset(); | ||
|
||
return { | ||
statusCode: 200, | ||
body: response | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.