forked from motdotla/node-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-lambda.js
More file actions
201 lines (177 loc) · 6.49 KB
/
Copy pathnode-lambda.js
File metadata and controls
201 lines (177 loc) · 6.49 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict'
const assert = require('chai').assert
const path = require('path')
const fs = require('fs-extra')
const spawn = require('child_process').spawn
const execSync = require('child_process').execSync
const nodeLambdaPath = path.join(__dirname, '..', 'bin', 'node-lambda')
/* global before, after, describe, it */
// The reason for specifying the node command in this test is to support Windows.
describe('bin/node-lambda', () => {
describe('node-lambda run', () => {
const _testMain = (expectedValues, done) => {
const run = spawn('node', [
nodeLambdaPath, 'run',
'--handler', '__test.handler',
'--eventFile', 'event.json'
])
let stdoutString = ''
let stderrString = ''
run.stdout.on('data', (data) => {
stdoutString += data.toString().replace(/\r|\n/g, '')
})
run.stderr.on('data', (data) => {
stderrString += data.toString().replace(/\r|\n/g, '')
})
run.on('exit', (code) => {
if (expectedValues.stdoutRegExp) {
assert.match(stdoutString, expectedValues.stdoutRegExp)
}
if (expectedValues.stderrRegExp) {
assert.match(stderrString, expectedValues.stderrRegExp)
}
assert.equal(code, expectedValues.exitCode)
done()
})
}
const _generateEventFile = (eventObj) => {
fs.writeFileSync('event.json', JSON.stringify(eventObj))
}
before(() => {
execSync(`node ${nodeLambdaPath} setup`)
fs.copy(path.join(__dirname, 'handler', 'index.js'), '__test.js')
})
after(() => {
[
'.env',
'context.json',
'event.json',
'deploy.env',
'event_sources.json',
'__test.js'
].forEach((file) => fs.unlinkSync(file))
})
describe('node-lambda run (Handler only sync processing)', () => {
const eventObj = {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true // True is the default value of Lambda
}
it('`node-lambda run` exitCode is `0` (callback(null))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null);'
}))
_testMain({ stdoutRegExp: /Success:$/, exitCode: 0 }, done)
})
it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null, "text");'
}))
_testMain({ stdoutRegExp: /Success:"text"$/, exitCode: 0 }, done)
})
it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(new Error("e"));'
}))
_testMain({ stdoutRegExp: /Error: Error: e$/, exitCode: 255 }, done)
})
})
describe('node-lambda run (Handler includes async processing)', () => {
describe('callbackWaitsForEmptyEventLoop = true', function () {
this.timeout(5000) // give it time to setTimeout
const eventObj = {
asyncTest: true,
callbackWaitsForEmptyEventLoop: true
}
it('`node-lambda run` exitCode is `0` (callback(null))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null);'
}))
_testMain({ stdoutRegExp: /Success:sleep 3500 msec$/, exitCode: 0 }, done)
})
it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null, "text");'
}))
_testMain({ stdoutRegExp: /Success:"text"sleep 3500 msec$/, exitCode: 0 }, done)
})
it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(new Error("e"));'
}))
_testMain({ stdoutRegExp: /Error: Error: esleep 3500 msec$/, exitCode: 255 }, done)
})
})
describe('callbackWaitsForEmptyEventLoop = false', () => {
const eventObj = {
asyncTest: true,
callbackWaitsForEmptyEventLoop: false
}
it('`node-lambda run` exitCode is `0` (callback(null))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null);'
}))
_testMain({ stdoutRegExp: /Success:$/, exitCode: 0 }, done)
})
it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null, "text");'
}))
_testMain({ stdoutRegExp: /Success:"text"$/, exitCode: 0 }, done)
})
it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(new Error("e"));'
}))
_testMain({ stdoutRegExp: /Error: Error: e$/, exitCode: 255 }, done)
})
})
})
describe('node-lambda run (Runtime is not supported)', () => {
const eventObj = {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true // True is the default value of Lambda
}
before(() => {
process.env.AWS_RUNTIME = 'test'
})
after(() => {
process.env.AWS_RUNTIME = 'nodejs6.10'
})
it('`node-lambda run` exitCode is `254` (callback(null))', (done) => {
_generateEventFile(Object.assign(eventObj, {
callbackCode: 'callback(null);'
}))
_testMain({
stderrRegExp: /^Runtime \[test\] is not supported\.$/,
exitCode: 254
}, done)
})
})
describe('node-lambda run (Multiple events))', () => {
const eventObj = [{
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 1
}, {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 2
}, {
asyncTest: false,
callbackWaitsForEmptyEventLoop: true,
callbackCode: 'callback(null);',
no: 3
}]
it('`node-lambda run` exitCode is `0`', function (done) {
this.timeout(10000) // give it time to multiple executions
_generateEventFile(eventObj)
_testMain({
stdoutRegExp: / no: 1 .+ no: 2 .+ no: 3 .+Success:/,
exitCode: 0
}, done)
})
})
})
})