This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 428
/
test.js
124 lines (112 loc) · 2.85 KB
/
test.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
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
var should = require('should')
require('child_process').spawnSync = mockSpawnSync
var dockerLambda = require('.')
var captured = {}
var mockReturn
function mockSpawnSync(cmd, args, options) {
captured.cmd = cmd
captured.args = args
captured.options = options
return mockReturn
}
function resetMock(returnVal) {
mockReturn = returnVal || {status: 0, stdout: '{}'}
}
// Should return defaults if calling with no options
resetMock()
var result = dockerLambda()
captured.cmd.should.equal('docker')
captured.args.should.eql([
'run',
'-v',
__dirname + ':/var/task',
'--rm',
'lambci/lambda:nodejs4.3',
'index.handler',
'{}',
])
captured.options.should.eql({encoding: 'utf8'})
result.should.eql({})
// Should use env vars if asked to
resetMock()
result = dockerLambda({addEnvVars: true})
captured.cmd.should.equal('docker')
captured.args.should.eql([
'run',
'-v',
__dirname + ':/var/task',
'--rm',
'-e',
'AWS_REGION',
'-e',
'AWS_DEFAULT_REGION',
'-e',
'AWS_ACCOUNT_ID',
'-e',
'AWS_ACCESS_KEY_ID',
'-e',
'AWS_SECRET_ACCESS_KEY',
'-e',
'AWS_SESSION_TOKEN',
'-e',
'AWS_LAMBDA_FUNCTION_NAME',
'-e',
'AWS_LAMBDA_FUNCTION_VERSION',
'-e',
'AWS_LAMBDA_FUNCTION_MEMORY_SIZE',
'-e',
'AWS_LAMBDA_FUNCTION_TIMEOUT',
'-e',
'AWS_LAMBDA_FUNCTION_HANDLER',
'-e',
'AWS_LAMBDA_EVENT_BODY',
'-e',
'DOCKER_LAMBDA_USE_STDIN',
'lambci/lambda:nodejs4.3',
'index.handler',
'{}',
])
captured.options.should.eql({encoding: 'utf8'})
result.should.eql({})
// Should return spawn result if asked to
resetMock({status: 0, stdout: 'null'})
result = dockerLambda({returnSpawnResult: true})
result.should.eql({status: 0, stdout: 'null'})
// Should not fail if stdout contains logging
resetMock({status: 0, stdout: 'Test\nResult\n{"success":true}'})
result = dockerLambda()
result.should.eql({success: true})
// Should not fail if stdout contains extra newlines
resetMock({status: 0, stdout: 'Test\nResult\n\n{"success":true}\n\n'})
result = dockerLambda()
result.should.eql({success: true})
// Should return undefined if last stdout entry cannot be parsed
resetMock({status: 0, stdout: 'Test\nResult\nsuccess'})
result = dockerLambda()
should.not.exist(result)
// Should return undefined when function was successful but there is no stdout
resetMock({status: 0, stdout: ''})
result = dockerLambda()
should.not.exist(result)
// Should throw error if spawn returns error
resetMock({error: new Error('Something went wrong')})
var err
try {
result = dockerLambda()
} catch (e) {
err = e
}
err.should.eql(new Error('Something went wrong'))
// Should throw error if spawn process dies
resetMock({status: 1, stdout: 'wtf', stderr: 'ftw'})
try {
result = dockerLambda()
} catch (e) {
err = e
}
var expectedErr = new Error('wtf')
expectedErr.code = 1
expectedErr.stdout = 'wtf'
expectedErr.stderr = 'ftw'
err.should.eql(expectedErr)
console.log('All Passed!')