forked from motdotla/node-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
160 lines (133 loc) · 4.4 KB
/
Copy pathmain.js
File metadata and controls
160 lines (133 loc) · 4.4 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var chai = require('chai');
var program = require('commander');
var fs = require('fs');
var Hoek = require('hoek');
var lambda = require('../lib/main');
var fs = require('fs');
var _ = require('lodash');
var zip = require('node-zip');
var assert = chai.assert;
var originalProgram = {
environment: 'development',
accessKey: 'key',
secretKey: 'secret',
functionName: 'node-lambda',
handler: 'index.handler',
mode: 'event',
role: 'some:arn:aws:iam::role',
memorySize: 128,
timeout: 3,
description: '',
runtime: 'nodejs',
region: 'us-east-1,us-west-2,eu-west-1'
};
var codeDirectory = lambda._codeDirectory(program);
describe('node-lambda', function () {
beforeEach(function () {
program = Hoek.clone(originalProgram);
});
it('version should be set', function () {
assert.equal(lambda.version, '0.6.0');
});
describe('_params', function () {
it('appends environment to original functionName', function () {
var params = lambda._params(program);
assert.equal(params.FunctionName, 'node-lambda-development');
});
it('appends environment to original functionName (production)', function () {
program.environment = 'production';
var params = lambda._params(program);
assert.equal(params.FunctionName, 'node-lambda-production');
});
it('appends version to original functionName', function () {
program.version = '2015-02-01';
var params = lambda._params(program);
assert.equal(params.FunctionName, 'node-lambda-development-2015-02-01');
});
});
describe('_zipfileTmpPath', function () {
it('has the correct path', function () {
var zipfileTmpPath = lambda._zipfileTmpPath(program);
var value = zipfileTmpPath.indexOf(program.functionName) > 0;
assert.equal(value, true);
});
});
describe('_rsync', function () {
it('rsync an index.js as well as other files', function (done) {
lambda._rsync(program, codeDirectory, function (err, result) {
var contents = fs.readdirSync(codeDirectory);
result = _.includes(contents, 'index.js');
assert.equal(result, true);
done();
});
});
});
describe('_npmInstall', function () {
beforeEach(function (done) {
lambda._rsync(program, codeDirectory, function (err) {
if (err) {
return done(err);
}
done();
});
});
it('_npm adds node_modules', function (done) {
this.timeout(60000); // give it time to build the node modules
lambda._npmInstall(program, codeDirectory, function (err, result) {
var contents = fs.readdirSync(codeDirectory);
result = _.includes(contents, 'index.js');
assert.equal(result, true);
done();
});
});
});
describe('_zip', function () {
beforeEach(function (done) {
this.timeout(30000); // give it time to build the node modules
lambda._rsync(program, codeDirectory, function (err) {
if (err) {
return done(err);
}
lambda._npmInstall(program, codeDirectory, function (err) {
if (err) {
return done(err);
}
done();
});
});
});
it('zips the file and has an index.js file', function (done) {
this.timeout(30000); // give it time to zip
lambda._zip(program, codeDirectory, function (err, data) {
var archive = new zip(data);
var contents = _.map(archive.files, function (f) {
return f.name.toString();
});
var result = _.includes(contents, 'index.js');
assert.equal(result, true);
done();
});
});
});
describe('environment variable injection', function () {
beforeEach(function () {
// Prep...
fs.writeFileSync('tmp.env', 'FOO=bar\nBAZ=bing\n');
fs.writeFileSync('test.js', '');
});
afterEach(function () {
fs.unlinkSync('tmp.env');
fs.unlinkSync('test.js');
});
it('should inject environment variables at the top of the entry point file', function () {
// Run it...
lambda._setEnvironmentVars({
configFile: 'tmp.env',
handler: 'test.handler',
}, process.cwd());
assert.equal(fs.readFileSync('test.js').toString(), '////////////////////////////////////\n' +
'// "Environment Variables"\nprocess.env["FOO"]="bar";\n' +
'process.env["BAZ"]="bing";\n////////////////////////////////////\n\n');
});
});
});