-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
tests.js
147 lines (132 loc) · 5.51 KB
/
tests.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
'use strict';
const path = require('path');
const { expect } = require('chai');
const { getTmpDirPath, readYamlFile, writeYamlFile } = require('../../utils/fs');
const { confirmCloudWatchLogs } = require('../../utils/misc');
const {
createEventBus,
putEvents,
deleteEventBus,
describeEventBus,
} = require('../../utils/eventBridge');
const { createTestService, deployService, removeService } = require('../../utils/integration');
const { getMarkers } = require('../shared/utils');
describe('AWS - Event Bridge Integration Test', function() {
this.timeout(1000 * 60 * 10); // Involves time-taking deploys
let serviceName;
let stackName;
let tmpDirPath;
let namedEventBusName;
let arnEventBusName;
let arnEventBusArn;
const eventSource = 'serverless.test';
const stage = 'dev';
const putEventEntries = [
{
Source: eventSource,
DetailType: 'ServerlessDetailType',
Detail: '{"Key1":"Value1"}',
},
];
before(async () => {
tmpDirPath = getTmpDirPath();
console.info(`Temporary path: ${tmpDirPath}`);
// get default event bus ARN
const defaultEventBusArn = (await describeEventBus('default')).Arn;
const serverlessConfig = await createTestService(tmpDirPath, {
templateDir: path.join(__dirname, 'service'),
filesToAdd: [path.join(__dirname, '..', 'shared')],
serverlessConfigHook:
// Ensure unique event bus names for each test (to avoid collision among concurrent CI runs)
config => {
namedEventBusName = `${config.service}-named-event-bus`;
arnEventBusName = `${config.service}-arn-event-bus`;
config.functions.eventBusCustom.events[0].eventBridge.eventBus = namedEventBusName;
config.functions.eventBusDefaultArn.events[0].eventBridge.eventBus = defaultEventBusArn;
},
});
serviceName = serverlessConfig.service;
stackName = `${serviceName}-${stage}`;
// create an external Event Bus
// NOTE: deployment can only be done once the Event Bus is created
console.info(`Creating Event Bus "${arnEventBusName}"...`);
return createEventBus(arnEventBusName).then(data => {
arnEventBusArn = data.EventBusArn;
// update the YAML file with the arn
console.info(`Updating serverless.yml with Event Bus arn "${arnEventBusArn}"`);
const serverlessFilePath = path.join(tmpDirPath, 'serverless.yml');
const config = readYamlFile(serverlessFilePath);
config.functions.eventBusArn.events[0].eventBridge.eventBus = arnEventBusArn;
writeYamlFile(serverlessFilePath, config);
// deploy the service
console.info(`Deploying "${stackName}" service...`);
return deployService(tmpDirPath);
});
});
after(async () => {
console.info('Removing service...');
await removeService(tmpDirPath);
console.info(`Deleting Event Bus "${arnEventBusName}"...`);
return deleteEventBus(arnEventBusName);
});
describe('Default Event Bus', () => {
it('should invoke function when an event is sent to the event bus', () => {
const functionName = 'eventBusDefault';
const markers = getMarkers(functionName);
return confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
() => putEvents('default', putEventEntries),
{
checkIsComplete: events =>
events.find(event => event.message.includes(markers.start)) &&
events.find(event => event.message.includes(markers.end)),
}
).then(events => {
const logs = events.map(event => event.message).join('\n');
expect(logs).to.include(`"source":"${eventSource}"`);
expect(logs).to.include(`"detail-type":"${putEventEntries[0].DetailType}"`);
expect(logs).to.include(`"detail":${putEventEntries[0].Detail}`);
});
});
});
describe('Custom Event Bus', () => {
it('should invoke function when an event is sent to the event bus', () => {
const functionName = 'eventBusCustom';
const markers = getMarkers(functionName);
return confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
() => putEvents(namedEventBusName, putEventEntries),
{
checkIsComplete: events =>
events.find(event => event.message.includes(markers.start)) &&
events.find(event => event.message.includes(markers.end)),
}
).then(events => {
const logs = events.map(event => event.message).join('\n');
expect(logs).to.include(`"source":"${eventSource}"`);
expect(logs).to.include(`"detail-type":"${putEventEntries[0].DetailType}"`);
expect(logs).to.include(`"detail":${putEventEntries[0].Detail}`);
});
});
});
describe('Arn Event Bus', () => {
it('should invoke function when an event is sent to the event bus', () => {
const functionName = 'eventBusArn';
const markers = getMarkers(functionName);
return confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
() => putEvents(arnEventBusName, putEventEntries),
{
checkIsComplete: events =>
events.find(event => event.message.includes(markers.start)) &&
events.find(event => event.message.includes(markers.end)),
}
).then(events => {
const logs = events.map(event => event.message).join('\n');
expect(logs).to.include(`"source":"${eventSource}"`);
expect(logs).to.include(`"detail-type":"${putEventEntries[0].DetailType}"`);
expect(logs).to.include(`"detail":${putEventEntries[0].Detail}`);
});
});
});
});