This repository has been archived by the owner on Oct 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the lambda to start the run by writing to s3
and mocha/chai testing w/ lambda-tester & aws-sdk-mock closes #4
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,25 @@ | ||
"use strict" | ||
const AWS = require('aws-sdk'); | ||
const path = require('path'); | ||
const util = require('util'); | ||
|
||
const START_FN = 'RUN.TXT' | ||
|
||
exports.handler = (event, context, callback) => { | ||
let s3 = new AWS.S3({apiVersion: '2006-03-01'}); | ||
let key = path.posix.join(event.plan_uuid, event.run_uuid, START_FN); | ||
let now = Math.round(new Date().getTime() / 1000); | ||
let s3_url = util.format("s3://%s/%s", event.bucket, key); | ||
|
||
s3.upload({ | ||
Bucket: event.bucket, | ||
Key: key, | ||
Body: new Buffer(now.toString(), 'binary'), | ||
ACL: 'public-read' | ||
}).promise().then((data) => { | ||
console.log("Uploaded: ", s3_url); | ||
callback(); | ||
}).catch((err) => { | ||
callback(err, "Error while uploading to: ", s3_url); | ||
}); | ||
} |
This file contains 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,20 @@ | ||
{ | ||
"name": "ardere-lambda-functions", | ||
"version": "0.0.1", | ||
"description": "Ardere AWS Lambda functions", | ||
"engines": { | ||
"node": "^4.3.x", | ||
"npm": "^3.5.x" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"author": "Mozilla Loads Project", | ||
"license": "MPL", | ||
"devDependencies": { | ||
"aws-sdk-mock": "^1.6.1", | ||
"chai": "^3.5.0", | ||
"lambda-tester": "^2.7.0", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
This file contains 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,34 @@ | ||
"use strict" | ||
const AWS = require('aws-sdk-mock'); | ||
const LambdaTester = require('lambda-tester'); | ||
|
||
const assert = require('chai').assert; | ||
const handler = require('../lib/start-test-plan.js').handler; | ||
|
||
describe('start test plan', () => { | ||
let upload; | ||
|
||
before(() => { | ||
upload = AWS.mock('S3', 'upload', (params, callback) => { | ||
callback(null, "Uploaded"); | ||
}); | ||
}); | ||
|
||
after(() => { | ||
AWS.restore('S3'); | ||
}); | ||
|
||
it('uploaded start file to s3', () => { | ||
return LambdaTester(handler) | ||
.event({bucket: 'foo', plan_uuid: 'abcd', run_uuid: 'efgh'}) | ||
.expectResult((result) => { | ||
assert(upload.stub.calledOnce); | ||
let params = upload.stub.args[0][0]; | ||
assert(params.Bucket == 'foo'); | ||
assert(params.Key == 'abcd/efgh/RUN.TXT'); | ||
assert(params.ACL.includes('public')); | ||
let start_time = parseInt(params.Body.toString()); | ||
assert(start_time <= Math.ceil(new Date().getTime() / 1000)); | ||
}); | ||
}); | ||
}); |