This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsk.test.js
103 lines (89 loc) · 3.17 KB
/
msk.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
'use strict';
const { expect } = require('chai');
const log = require('log').get('serverless:test');
const fixtures = require('../../../fixtures/programmatic');
const { confirmCloudWatchLogs } = require('../../../utils/misc');
const {
isDependencyStackAvailable,
getDependencyStackOutputMap,
} = require('../../../utils/cloudformation');
const awsRequest = require('@serverless/test/aws-request');
const LambdaService = require('aws-sdk').Lambda;
const KafkaService = require('aws-sdk').Kafka;
const crypto = require('crypto');
const { deployService, removeService } = require('../../../utils/integration');
describe('AWS - MSK Integration Test', function () {
this.timeout(1000 * 60 * 100); // Involves time-taking deploys
let stackName;
let serviceDir;
const stage = 'dev';
const topicName = `msk-topic-${crypto.randomBytes(8).toString('hex')}`;
before(async () => {
const isDepsStackAvailable = await isDependencyStackAvailable();
if (!isDepsStackAvailable) {
throw new Error('CloudFormation stack with integration test dependencies not found.');
}
const outputMap = await getDependencyStackOutputMap();
log.notice('Getting MSK Boostrap Brokers URLs...');
const getBootstrapBrokersResponse = await awsRequest(KafkaService, 'getBootstrapBrokers', {
ClusterArn: outputMap.get('MSKCluster'),
});
const brokerUrls = getBootstrapBrokersResponse.BootstrapBrokerStringTls;
const serviceData = await fixtures.setup('function-msk', {
configExt: {
functions: {
producer: {
vpc: {
subnetIds: [outputMap.get('PrivateSubnetA')],
securityGroupIds: [outputMap.get('SecurityGroup')],
},
environment: {
TOPIC_NAME: topicName,
BROKER_URLS: brokerUrls,
},
},
consumer: {
events: [
{
msk: {
arn: outputMap.get('MSKCluster'),
topic: topicName,
batchSize: 100,
maximumBatchingWindow: 3,
},
},
],
},
},
},
});
({ servicePath: serviceDir } = serviceData);
const serviceName = serviceData.serviceConfig.service;
stackName = `${serviceName}-${stage}`;
await deployService(serviceDir);
});
after(async () => {
if (serviceDir) {
await removeService(serviceDir);
}
});
it('correctly processes messages from MSK topic', async () => {
const functionName = 'consumer';
const message = 'Hello from MSK Integration test!';
const events = await confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
async () =>
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-producer`,
InvocationType: 'RequestResponse',
}),
{
checkIsComplete: (soFarEvents) =>
soFarEvents.reduce((data, event) => data + event.message, '').includes(message),
}
);
const logs = events.reduce((data, event) => data + event.message, '');
expect(logs).to.include(functionName);
expect(logs).to.include(message);
});
});