/
index.test.js
661 lines (607 loc) · 21.6 KB
/
index.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
'use strict';
/* eslint-disable no-unused-expressions */
const chai = require('chai');
const runServerless = require('../../../../../../../../utils/run-serverless');
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
const expect = chai.expect;
const NAME_OVER_64_CHARS = 'oneVeryLongAndVeryStrangeAndVeryComplicatedFunctionNameOver64Chars';
const serverlessConfigurationExtension = {
functions: {
default: {
handler: 'index.handler',
events: [
{
eventBridge: {
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/default',
schedule: 'rate(10 minutes)',
},
},
],
},
[NAME_OVER_64_CHARS]: {
handler: 'index.handler',
name: 'one-very-long-and-very-strange-and-very-complicated-function-name-over-64-chars',
events: [
{
eventBridge: {
schedule: 'rate(10 minutes)',
pattern: {
'source': ['aws.cloudformation'],
'detail-type': ['AWS API Call via CloudTrail'],
'detail': {
eventSource: ['cloudformation.amazonaws.com'],
},
},
},
},
],
},
configureInput: {
handler: 'index.handler',
events: [
{
eventBridge: {
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/some-event-bus',
schedule: 'rate(10 minutes)',
pattern: {
'source': ['aws.cloudformation'],
'detail-type': ['AWS API Call via CloudTrail'],
'detail': {
eventSource: ['cloudformation.amazonaws.com'],
},
},
input: {
key1: 'value1',
key2: {
nested: 'value2',
},
},
},
},
],
},
inputPathConfiguration: {
handler: 'index.handler',
events: [
{
eventBridge: {
pattern: {
'source': ['aws.cloudformation'],
'detail-type': ['AWS API Call via CloudTrail'],
'detail': {
eventSource: ['cloudformation.amazonaws.com'],
},
},
inputPath: '$.stageVariables',
},
},
],
},
inputTransformer: {
handler: 'index.handler',
events: [
{
eventBridge: {
pattern: {
'source': ['aws.cloudformation'],
'detail-type': ['AWS API Call via CloudTrail'],
'detail': {
eventSource: ['cloudformation.amazonaws.com'],
},
},
inputTransformer: {
inputTemplate: '{"time": <eventTime>, "key1": "value1"}',
inputPathsMap: {
eventTime: '$.time',
},
},
},
},
],
},
customSaas: {
handler: 'index.handler',
events: [
{
eventBridge: {
eventBus: 'custom-saas-events',
pattern: {
detail: {
eventSource: ['saas.external'],
},
},
inputTransformer: {
inputTemplate: '{"time": <eventTime>, "key1": "value1"}',
inputPathsMap: {
eventTime: '$.time',
},
},
},
},
],
},
disabled: {
handler: 'index.handler',
events: [
{
eventBridge: {
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/default',
schedule: 'rate(10 minutes)',
enabled: false,
},
},
],
},
enabled: {
handler: 'index.handler',
events: [
{
eventBridge: {
eventBus: 'arn:aws:events:us-east-1:12345:event-bus/default',
schedule: 'rate(10 minutes)',
enabled: true,
},
},
],
},
},
};
describe('EventBridgeEvents', () => {
describe('using custom resources deployment pattern', () => {
let cfResources;
let naming;
before(async () => {
const { cfTemplate, awsNaming } = await runServerless({
fixture: 'function',
configExt: {
disabledDeprecations: ['AWS_EVENT_BRIDGE_CUSTOM_RESOURCE'],
...serverlessConfigurationExtension,
},
command: 'package',
});
cfResources = cfTemplate.Resources;
naming = awsNaming;
});
function getEventBridgeConfigById(resourceLogicalId) {
const eventBridgeId = naming.getCustomResourceEventBridgeResourceLogicalId(
resourceLogicalId,
1
);
return cfResources[eventBridgeId].Properties.EventBridgeConfig;
}
it('should create the correct policy Statement', () => {
const roleId = naming.getCustomResourcesRoleLogicalId('default', '12345');
const [firstStatement, secondStatement, thirdStatment] =
cfResources[roleId].Properties.Policies[0].PolicyDocument.Statement;
expect(firstStatement.Effect).to.be.eq('Allow');
expect(firstStatement.Resource['Fn::Join'][1]).to.deep.include('arn');
expect(firstStatement.Resource['Fn::Join'][1]).to.deep.include('events');
expect(firstStatement.Resource['Fn::Join'][1]).to.deep.include('event-bus/*');
expect(firstStatement.Action).to.be.deep.eq([
'events:CreateEventBus',
'events:DeleteEventBus',
]);
expect(secondStatement.Effect).to.be.eq('Allow');
expect(secondStatement.Resource['Fn::Join'][1]).to.deep.include('events');
expect(secondStatement.Resource['Fn::Join'][1]).to.deep.include('rule/*');
expect(secondStatement.Action).to.be.deep.eq([
'events:PutRule',
'events:RemoveTargets',
'events:PutTargets',
'events:DeleteRule',
]);
expect(thirdStatment.Effect).to.be.eq('Allow');
expect(thirdStatment.Resource['Fn::Join'][1]).to.deep.include('function');
expect(thirdStatment.Resource['Fn::Join'][1]).to.deep.include('lambda');
expect(thirdStatment.Action).to.be.deep.eq([
'lambda:AddPermission',
'lambda:RemovePermission',
]);
});
it('should create the necessary resource', () => {
const eventBridgeConfig = getEventBridgeConfigById('default');
expect(eventBridgeConfig.RuleName).to.include('dev-default-rule-1');
});
it('should ensure state is enabled by default', () => {
const eventBridgeConfig = getEventBridgeConfigById('default');
expect(eventBridgeConfig.State).to.be.eq('ENABLED');
});
it('should ensure state is enabled when explicity set', () => {
const eventBridgeConfig = getEventBridgeConfigById('enabled');
expect(eventBridgeConfig.State).to.be.eq('ENABLED');
});
it('should ensure state is disabled when explicity set', () => {
const eventBridgeConfig = getEventBridgeConfigById('disabled');
expect(eventBridgeConfig.State).to.be.eq('DISABLED');
});
it("should ensure rule name doesn't exceed 64 chars", () => {
const eventBridgeConfig = getEventBridgeConfigById(NAME_OVER_64_CHARS);
expect(eventBridgeConfig.RuleName.endsWith('rule-1')).to.be.true;
expect(eventBridgeConfig.RuleName).lengthOf.lte(64);
});
it('should support input configuration', () => {
const eventBridgeConfig = getEventBridgeConfigById('configureInput');
expect(eventBridgeConfig.Input.key1).be.eq('value1');
expect(eventBridgeConfig.Input.key2).be.deep.eq({
nested: 'value2',
});
});
it('should support arn at eventBus', () => {
const eventBridgeConfig = getEventBridgeConfigById('configureInput');
expect(eventBridgeConfig.EventBus).be.eq(
'arn:aws:events:us-east-1:12345:event-bus/some-event-bus'
);
});
it('should support inputPath configuration', () => {
const eventBridgeConfig = getEventBridgeConfigById('inputPathConfiguration');
expect(eventBridgeConfig.InputPath).be.eq('$.stageVariables');
});
it('should support inputTransformer configuration', () => {
const eventBridgeConfig = getEventBridgeConfigById('inputTransformer');
const {
InputTemplate,
InputPathsMap: { eventTime },
} = eventBridgeConfig.InputTransformer;
expect(InputTemplate).be.eq('{"time": <eventTime>, "key1": "value1"}');
expect(eventTime).be.eq('$.time');
});
it('should register created and delete event bus permissions for non default event bus', () => {
const roleId = naming.getCustomResourcesRoleLogicalId('customSaas', '12345');
const [firstStatement] = cfResources[roleId].Properties.Policies[0].PolicyDocument.Statement;
expect(firstStatement.Action[0]).to.be.eq('events:CreateEventBus');
expect(firstStatement.Action[1]).to.be.eq('events:DeleteEventBus');
expect(firstStatement.Effect).to.be.eq('Allow');
});
it('should fail when trying to set RetryPolicy', async () => {
await expect(
runServerless({
fixture: 'function',
configExt: {
disabledDeprecations: ['AWS_EVENT_BRIDGE_CUSTOM_RESOURCE'],
functions: {
foo: {
events: [
{
eventBridge: {
retryPolicy: {
maximumEventAge: 4200,
maximumRetryAttempts: 180,
},
pattern: {
source: ['aws.something'],
},
},
},
],
},
},
},
command: 'package',
})
).to.be.eventually.rejected.and.have.property(
'code',
'ERROR_INVALID_RETRY_POLICY_TO_EVENT_BUS_CUSTOM_RESOURCE'
);
});
it('should fail when trying to set DeadLetterQueueArn', async () => {
await expect(
runServerless({
fixture: 'function',
configExt: {
disabledDeprecations: ['AWS_EVENT_BRIDGE_CUSTOM_RESOURCE'],
functions: {
foo: {
events: [
{
eventBridge: {
deadLetterQueueArn: {
'Fn::GetAtt': ['not-supported', 'Arn'],
},
pattern: {
source: ['aws.something'],
},
},
},
],
},
},
},
command: 'package',
})
).to.be.eventually.rejected.and.have.property(
'code',
'ERROR_INVALID_DEAD_LETTER_CONFIG_TO_EVENT_BUS_CUSTOM_RESOURCE'
);
});
it('should fail when trying to reference event bus via CF intrinsic function', async () => {
await expect(
runServerless({
fixture: 'function',
configExt: {
disabledDeprecations: ['AWS_EVENT_BRIDGE_CUSTOM_RESOURCE'],
functions: {
foo: {
events: [
{
eventBridge: {
eventBus: { Ref: 'ImportedEventBus' },
schedule: 'rate(10 minutes)',
},
},
],
},
},
},
command: 'package',
})
).to.be.eventually.rejected.and.have.property(
'code',
'ERROR_INVALID_REFERENCE_TO_EVENT_BUS_CUSTOM_RESOURCE'
);
});
});
describe('using native CloudFormation', () => {
describe('when event bus is created as a part of the stack', () => {
let cfResources;
let naming;
let eventBusLogicalId;
let ruleResource;
let ruleTarget;
const schedule = 'rate(10 minutes)';
const eventBusName = 'nondefault';
const pattern = {
source: ['aws.cloudformation'],
};
const input = {
key1: 'value1',
key2: {
nested: 'value2',
},
};
const inputPath = '$.stageVariables';
const inputTransformer = {
inputTemplate: '{"time": <eventTime>, "key1": "value1"}',
inputPathsMap: {
eventTime: '$.time',
},
};
const retryPolicy = {
maximumEventAge: 7200,
maximumRetryAttempts: 9,
};
const deadLetterQueueArn = {
'Fn::GetAtt': ['test', 'Arn'],
};
const getRuleResourceEndingWith = (resources, ending) =>
Object.values(resources).find(
(resource) =>
resource.Type === 'AWS::Events::Rule' && resource.Properties.Name.endsWith(ending)
);
before(async () => {
const { cfTemplate, awsNaming } = await runServerless({
fixture: 'function',
configExt: {
provider: {
eventBridge: {
useCloudFormation: true,
},
},
functions: {
foo: {
events: [
{
eventBridge: {
eventBus: eventBusName,
schedule,
pattern,
input,
},
},
{
eventBridge: {
eventBus: eventBusName,
schedule,
pattern,
inputPath,
},
},
{
eventBridge: {
eventBus: eventBusName,
schedule,
pattern,
inputTransformer,
},
},
{
eventBridge: {
eventBus: eventBusName,
schedule,
enabled: false,
pattern,
},
},
{
eventBridge: {
eventBus: eventBusName,
schedule,
enabled: true,
pattern,
},
},
{
eventBridge: {
eventBus: eventBusName,
schedule,
pattern,
retryPolicy,
},
},
{
eventBridge: {
eventBus: eventBusName,
schedule,
pattern,
deadLetterQueueArn,
},
},
],
},
},
},
command: 'package',
});
cfResources = cfTemplate.Resources;
naming = awsNaming;
eventBusLogicalId = naming.getEventBridgeEventBusLogicalId(eventBusName);
ruleResource = getRuleResourceEndingWith(cfResources, '1');
ruleTarget = ruleResource.Properties.Targets[0];
});
it('should create an EventBus resource', () => {
expect(cfResources[eventBusLogicalId].Properties).to.deep.equal({ Name: eventBusName });
});
it('should correctly set ScheduleExpression on a created rule', () => {
expect(ruleResource.Properties.ScheduleExpression).to.equal('rate(10 minutes)');
});
it('should correctly set State by default on a created rule', () => {
expect(ruleResource.Properties.State).to.equal('ENABLED');
});
it('should correctly set State when disabled on a created rule', () => {
const disabledRuleResource = getRuleResourceEndingWith(cfResources, '4');
expect(disabledRuleResource.Properties.State).to.equal('DISABLED');
});
it('should correctly set State when enabled on a created rule', () => {
const enabledRuleResource = getRuleResourceEndingWith(cfResources, '5');
expect(enabledRuleResource.Properties.State).to.equal('ENABLED');
});
it('should correctly set EventPattern on a created rule', () => {
expect(ruleResource.Properties.EventPattern).to.deep.equal(JSON.stringify(pattern));
});
it('should correctly set Input on the target for the created rule', () => {
expect(ruleTarget.Input).to.deep.equal(JSON.stringify(input));
});
it('should correctly set InputPath on the target for the created rule', () => {
const inputPathRuleResource = getRuleResourceEndingWith(cfResources, '2');
const inputPathRuleTarget = inputPathRuleResource.Properties.Targets[0];
expect(inputPathRuleTarget.InputPath).to.deep.equal(inputPath);
});
it('should correctly set InputTransformer on the target for the created rule', () => {
const inputTransformerRuleResource = getRuleResourceEndingWith(cfResources, '3');
const inputTransformerRuleTarget = inputTransformerRuleResource.Properties.Targets[0];
expect(inputTransformerRuleTarget.InputTransformer.InputPathsMap).to.deep.equal(
inputTransformer.inputPathsMap
);
expect(inputTransformerRuleTarget.InputTransformer.InputTemplate).to.deep.equal(
inputTransformer.inputTemplate
);
});
it('should support retryPolicy configuration', () => {
const retryPolicyRuleTarget = getRuleResourceEndingWith(cfResources, '6').Properties
.Targets[0];
expect(retryPolicyRuleTarget.RetryPolicy).to.deep.equal({
MaximumEventAgeInSeconds: 7200,
MaximumRetryAttempts: 9,
});
});
it('should support deadLetterQueueArn configuration', () => {
const deadLetterConfigRuleTarget = getRuleResourceEndingWith(cfResources, '7').Properties
.Targets[0];
expect(deadLetterConfigRuleTarget.DeadLetterConfig).to.have.property('Arn');
});
it('should create a rule that depends on created EventBus', () => {
expect(ruleResource.DependsOn).to.equal(eventBusLogicalId);
});
it('should create a rule that references correct function in target', () => {
expect(ruleTarget.Arn['Fn::GetAtt'][0]).to.equal(naming.getLambdaLogicalId('foo'));
});
it('should create a lambda permission resource that correctly references event bus in SourceArn', () => {
const lambdaPermissionResource =
cfResources[naming.getEventBridgeLambdaPermissionLogicalId('foo', 1)];
expect(
lambdaPermissionResource.Properties.SourceArn['Fn::Join'][1][5]['Fn::Join'][1][1]
).to.deep.equal(eventBusName);
});
});
describe('when it references already existing EventBus or uses default one', () => {
let cfResources;
let naming;
before(async () => {
const { cfTemplate, awsNaming } = await runServerless({
fixture: 'function',
command: 'package',
configExt: {
provider: {
eventBridge: {
useCloudFormation: true,
},
},
functions: {
foo: {
events: [
{
eventBridge: {
schedule: 'rate(10 minutes)',
eventBus: 'arn:xxxxx',
},
},
{
eventBridge: {
schedule: 'rate(10 minutes)',
eventBus: { Ref: 'ImportedEventBus' },
},
},
{
eventBridge: {
schedule: 'rate(10 minutes)',
eventBus: 'default',
},
},
{
eventBridge: {
schedule: 'rate(10 minutes)',
},
},
],
},
},
},
});
cfResources = cfTemplate.Resources;
naming = awsNaming;
});
it('should not create an EventBus if it is provided or default', async () => {
expect(Object.values(cfResources).some((value) => value.Type === 'AWS::Events::EventBus'))
.to.be.false;
});
it('should create a lambda permission resource that correctly references arn event bus in SourceArn', () => {
const lambdaPermissionResource =
cfResources[naming.getEventBridgeLambdaPermissionLogicalId('foo', 1)];
expect(
lambdaPermissionResource.Properties.SourceArn['Fn::Join'][1][5]['Fn::Join'][1][1]
).to.deep.equal('arn:xxxxx');
});
it('should create a lambda permission resource that correctly references CF event bus in SourceArn', () => {
const lambdaPermissionResource =
cfResources[naming.getEventBridgeLambdaPermissionLogicalId('foo', 2)];
expect(
lambdaPermissionResource.Properties.SourceArn['Fn::Join'][1][5]['Fn::Join'][1][1]
).to.deep.equal({ Ref: 'ImportedEventBus' });
});
it('should create a lambda permission resource that correctly references explicit default event bus in SourceArn', () => {
const lambdaPermissionResource =
cfResources[naming.getEventBridgeLambdaPermissionLogicalId('foo', 3)];
expect(
lambdaPermissionResource.Properties.SourceArn['Fn::Join'][1][5]['Fn::Join'][1][1]
).to.equal('default');
});
it('should create a lambda permission resource that correctly references implicit default event bus in SourceArn', () => {
const lambdaPermissionResource =
cfResources[naming.getEventBridgeLambdaPermissionLogicalId('foo', 4)];
expect(
lambdaPermissionResource.Properties.SourceArn['Fn::Join'][1][5]['Fn::Join'][1]
).not.to.include('default');
});
});
});
});