-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli.test.js
316 lines (287 loc) · 10.2 KB
/
cli.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const tempWrite = require('temp-write')
const api = require('./index')
const cli = require('./bin/update-lambda-edge')
jest.mock('./index')
describe('update-lambda-edge CLI', () => {
beforeEach(() => {
// silence console output
jest.spyOn(console, 'log').mockImplementation(() => {})
})
describe('create config', () => {
it('should set the dry run flag on', () => {
cli.parse('push --dry-run')
expect(api.pushNewCodeBundles).toHaveBeenCalledWith(
expect.objectContaining({
dryRun: true,
}),
)
})
it('should leave the dry run flag off', () => {
cli.parse('push')
expect(api.pushNewCodeBundles).toHaveBeenCalledWith(
expect.objectContaining({
dryRun: false,
}),
)
})
it('should set the auto-increment flag on (default state)', () => {
cli.parse('push --auto-increment')
expect(api.pushNewCodeBundles).toHaveBeenCalledWith(
expect.objectContaining({
autoIncrementVersion: true,
}),
)
})
it('should set the auto-increment flag off when version is set explicitly', () => {
cli.parse('push --auto-increment --lambda-version 5')
expect(api.pushNewCodeBundles).toHaveBeenCalledWith(
expect.objectContaining({
autoIncrementVersion: false,
}),
)
})
describe('with config file', () => {
let configFilePath
beforeEach(() => {
configFilePath = tempWrite.sync(
JSON.stringify(
{
awsRegion: 'fake',
s3Region: 'fake-s3-region',
lambdaRegion: 'fake-lambda-region',
cfDistributionID: 'fake',
autoIncrementVersion: true,
lambdaCodeS3Bucket: 'fake',
cfTriggers: [
{
cfTriggerName: 'viewer-request',
lambdaFunctionName: 'viewer-request-fake',
lambdaCodeS3Key: 'root/fake/path/to/vreq-lambda.zip',
lambdaCodeFilePath: '/absolute/path/to/vreq-lambda-local.zip',
},
{
cfTriggerName: 'origin-request',
lambdaFunctionName: 'origin-request-fake',
lambdaCodeS3Key: 'root/fake/path/to/oreq-lambda.zip',
lambdaCodeFilePath: 'relative/path/to/oreq-lambda-local.zip',
},
{
cfTriggerName: 'origin-response',
lambdaFunctionName: 'origin-response-fake',
lambdaCodeS3Key: 'root/fake/path/to/ores-lambda.zip',
lambdaCodeFilePath: '/dist/ores-lambda-local.zip',
},
{
cfTriggerName: 'viewer-response',
lambdaFunctionName: 'viewer-response-fake',
lambdaCodeS3Key: 'root/fake/path/to/vres-lambda.zip',
lambdaCodeFilePath: '/dist/vres-lambda-local.zip',
},
],
},
null,
2,
),
'config.json',
)
})
it('should load the data from the config file and properly parse paths', () => {
cli.parse(`push --config ${configFilePath}`)
expect(api.pushNewCodeBundles).toHaveBeenCalledWith({
dryRun: false,
awsRegion: 'fake',
s3Region: 'fake-s3-region',
lambdaRegion: 'fake-lambda-region',
cfDistributionID: 'fake',
cacheBehaviorPath: 'default',
autoIncrementVersion: true,
lambdaCodeS3Bucket: 'fake',
cfTriggers: [
{
cfTriggerName: 'viewer-request',
lambdaFunctionName: 'viewer-request-fake',
lambdaCodeS3Key: 'root/fake/path/to/vreq-lambda.zip',
lambdaCodeFilePath: '/absolute/path/to/vreq-lambda-local.zip',
lambdaFunctionVersion: undefined,
},
{
cfTriggerName: 'origin-request',
lambdaFunctionName: 'origin-request-fake',
lambdaCodeS3Key: 'root/fake/path/to/oreq-lambda.zip',
lambdaCodeFilePath: __dirname + '/relative/path/to/oreq-lambda-local.zip',
lambdaFunctionVersion: undefined,
},
{
cfTriggerName: 'origin-response',
lambdaFunctionName: 'origin-response-fake',
lambdaCodeS3Key: 'root/fake/path/to/ores-lambda.zip',
lambdaCodeFilePath: '/dist/ores-lambda-local.zip',
lambdaFunctionVersion: undefined,
},
{
cfTriggerName: 'viewer-response',
lambdaFunctionName: 'viewer-response-fake',
lambdaCodeS3Key: 'root/fake/path/to/vres-lambda.zip',
lambdaCodeFilePath: '/dist/vres-lambda-local.zip',
lambdaFunctionVersion: undefined,
},
],
})
})
it('should properly parse paths when pwd is set', () => {
cli.parse(`push --config ${configFilePath} --pwd /some/random/folder`)
expect(api.pushNewCodeBundles).toHaveBeenCalledWith(
expect.objectContaining({
cfTriggers: [
expect.objectContaining({
lambdaCodeFilePath: '/absolute/path/to/vreq-lambda-local.zip',
}),
expect.objectContaining({
lambdaCodeFilePath: '/some/random/folder/relative/path/to/oreq-lambda-local.zip',
}),
expect.any(Object),
expect.any(Object),
],
}),
)
})
it('should override config options from the command line', () => {
cli.parse(
`push --config ${configFilePath} --s3-region somewhere --lambda-region somewhere-else --distribution-id MYCLOUDFRONT --bucket S3 --lambda-version 5`,
)
expect(api.pushNewCodeBundles).toHaveBeenCalledWith({
dryRun: false,
awsRegion: 'fake',
s3Region: 'somewhere',
lambdaRegion: 'somewhere-else',
cfDistributionID: 'MYCLOUDFRONT',
cacheBehaviorPath: 'default',
autoIncrementVersion: false,
lambdaCodeS3Bucket: 'S3',
cfTriggers: [
expect.objectContaining({
lambdaFunctionVersion: '5',
}),
expect.objectContaining({
lambdaFunctionVersion: '5',
}),
expect.objectContaining({
lambdaFunctionVersion: '5',
}),
expect.objectContaining({
lambdaFunctionVersion: '5',
}),
],
})
})
it('should only update the specified triggers', () => {
cli.parse(`push --config ${configFilePath} --vreq --ores --vres`)
expect(api.pushNewCodeBundles).toHaveBeenCalledWith(
expect.objectContaining({
cfTriggers: [
expect.objectContaining({
cfTriggerName: 'viewer-request',
}),
expect.objectContaining({
cfTriggerName: 'origin-response',
}),
expect.objectContaining({
cfTriggerName: 'viewer-response',
}),
],
}),
)
})
})
})
describe('push', () => {
it('should push a single Lambda bundle', () => {
cli.parse('push --bucket test-bucket --key test-key --function-name test-function --file-path test/file/path.zip')
expect(api.pushNewCodeBundles).toHaveBeenCalledWith({
dryRun: false,
cfDistributionID: undefined,
cacheBehaviorPath: 'default',
autoIncrementVersion: true,
lambdaCodeS3Bucket: 'test-bucket',
awsRegion: undefined,
cfTriggers: [
{
cfTriggerName: undefined,
lambdaFunctionName: 'test-function',
lambdaFunctionVersion: undefined,
lambdaCodeS3Key: 'test-key',
lambdaCodeFilePath: 'test/file/path.zip',
},
],
})
})
})
describe('deploy', () => {
it('should deploy a single Lambda bundle', () => {
cli.parse('deploy --bucket test-bucket --key test-key --function-name test-function')
expect(api.deployLambdas).toHaveBeenCalledWith({
dryRun: false,
cfDistributionID: undefined,
cacheBehaviorPath: 'default',
autoIncrementVersion: true,
lambdaCodeS3Bucket: 'test-bucket',
awsRegion: undefined,
cfTriggers: [
{
cfTriggerName: undefined,
lambdaFunctionName: 'test-function',
lambdaFunctionVersion: undefined,
lambdaCodeS3Key: 'test-key',
lambdaCodeFilePath: undefined,
},
],
})
})
})
describe('publish', () => {
it('should publish a single Lambda bundle', () => {
cli.parse('publish --function-name test-function')
expect(api.publishLambdas).toHaveBeenCalledWith({
dryRun: false,
cfDistributionID: undefined,
cacheBehaviorPath: 'default',
autoIncrementVersion: false,
lambdaCodeS3Bucket: undefined,
awsRegion: undefined,
cfTriggers: [
{
cfTriggerName: undefined,
lambdaFunctionName: 'test-function',
lambdaFunctionVersion: undefined,
lambdaCodeS3Key: undefined,
lambdaCodeFilePath: undefined,
},
],
})
})
})
describe('activate', () => {
it('should activate a single Lambda bundle', () => {
cli.parse(
'activate --distribution-id cloudfront --cache-behavior-path "/*" --trigger-name viewer-request --function-name test-function --lambda-version 5',
)
expect(api.activateLambdas).toHaveBeenCalledWith({
dryRun: false,
cfDistributionID: 'cloudfront',
cacheBehaviorPath: '/*',
autoIncrementVersion: false,
lambdaCodeS3Bucket: undefined,
awsRegion: undefined,
cfTriggers: [
{
cfTriggerName: 'viewer-request',
lambdaFunctionName: 'test-function',
lambdaFunctionVersion: '5',
lambdaCodeS3Key: undefined,
lambdaCodeFilePath: undefined,
},
],
})
})
})
})