/
msk.test.js
101 lines (88 loc) · 2.97 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
'use strict';
const { expect } = require('chai');
const log = require('log').get('serverless:test');
const fixtures = require('../fixtures');
const { confirmCloudWatchLogs } = require('../utils/misc');
const {
isDependencyStackAvailable,
getDependencyStackOutputMap,
} = require('../utils/cludformation');
const awsRequest = require('@serverless/test/aws-request');
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 servicePath;
const stage = 'dev';
const topicName = `msk-topic-${crypto.randomBytes(8).toString('hex')}`;
before(async function beforeHook() {
const isDepsStackAvailable = await isDependencyStackAvailable();
if (!isDepsStackAvailable) {
log.notice(
'CloudFormation stack with integration test dependencies not found. Skipping test.'
);
this.skip();
}
const outputMap = await getDependencyStackOutputMap();
log.notice('Getting MSK Boostrap Brokers URLs...');
const getBootstrapBrokersResponse = await awsRequest('Kafka', 'getBootstrapBrokers', {
ClusterArn: outputMap.MSKCluster,
});
const brokerUrls = getBootstrapBrokersResponse.BootstrapBrokerStringTls;
const serviceData = await fixtures.setup('functionMsk', {
configExt: {
functions: {
producer: {
vpc: {
subnetIds: [outputMap.PrivateSubnetA],
securityGroupIds: [outputMap.SecurityGroup],
},
environment: {
TOPIC_NAME: topicName,
BROKER_URLS: brokerUrls,
},
},
consumer: {
events: [
{
msk: {
arn: outputMap.MSKCluster,
topic: topicName,
},
},
],
},
},
},
});
({ servicePath } = serviceData);
const serviceName = serviceData.serviceConfig.service;
stackName = `${serviceName}-${stage}`;
log.notice(`Deploying "${stackName}" service...`);
await deployService(servicePath);
});
after(async () => {
if (servicePath) {
log.notice('Removing service...');
await removeService(servicePath);
}
});
it('correctly processes messages from MSK topic', async () => {
const functionName = 'consumer';
const message = 'Hello from MSK Integration test!';
return confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
async () =>
await awsRequest('Lambda', 'invoke', {
FunctionName: `${stackName}-producer`,
InvocationType: 'RequestResponse',
}),
{ timeout: 120 * 1000 }
).then(events => {
const logs = events.reduce((data, event) => data + event.message, '');
expect(logs).to.include(functionName);
expect(logs).to.include(message);
});
});
});