-
Notifications
You must be signed in to change notification settings - Fork 3
/
lambda-mock.js
69 lines (61 loc) · 2.38 KB
/
lambda-mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Mock of AWS Lambda for testing
*/
var uuid = require('node-uuid');
var async = require('async');
var LambdaMock = function (params) {
params = params || {};
this.region = params.region || 'us-test-1';
this.memory_size = params.memory_size || "128";
this.timeout = params.lambda_timeout || 5000;
};
LambdaMock.prototype.invoke = function (module, functionName, request, callback) {
var self = this;
var startTime = Date.now();
// Serialize request so any writes to it aren't passed by reference back to the tests.
var requestCopy = JSON.parse(JSON.stringify(request));
var context = {
"awsRequestId": uuid.v4(),
"invokeid": uuid.v4(),
"logGroupName": "/aws/lambda/" + functionName,
"logStreamName": "2015/09/10/[HEAD]1234",
"functionName": functionName,
"memoryLimitInMB": self.memory_size,
"functionVersion": "HEAD",
"isDefaultFunctionVersion": true,
done: callback,
succeed: function (result) { callback(null, result); },
fail: function(err) { callback(err); },
getRemainingTimeInMillis: function () { return Date.now() - startTime; }
};
process.env.AWS_LAMBDA_LOG_GROUP_NAME = "/aws/lambda/" + functionName;
process.env.AWS_LAMBDA_FUNCTION_NAME = functionName;
module[functionName](requestCopy, context);
};
/*
* Invoke a set of lambda calls in series. calls is an array of
* objects with { module, functionName, request(), callback(err, request, result, done) } fields
*/
LambdaMock.prototype.invokeSeries = function (calls, callback) {
var self = this;
async.eachSeries(calls, function (call, done) {
var request = call.request();
self.invoke(call.module, call.functionName, request, function (err, result) {
call.callback(err, request, result, done);
});
}, callback);
};
/*
* Invoke a set of lambda calls in parallel. calls is an array of
* objects with { moduel, functionName, request(), callback(err, request, result, done) } fields
*/
LambdaMock.prototype.invokeParallel = function (calls, callback) {
var self = this;
async.each(calls, function (call, done) {
var request = call.request();
self.invoke(call.module, call.functionName, request, function (err, result) {
call.callback(err, request, result, done);
});
}, callback);
};
module.exports = LambdaMock;