Skip to content

Commit ed8a11d

Browse files
committed
ci: improve test coverage
1 parent 8b5293b commit ed8a11d

File tree

4 files changed

+255
-103
lines changed

4 files changed

+255
-103
lines changed

packages/core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const runRequest = async (
9090
onErrorMiddlewares,
9191
plugin
9292
) => {
93-
const { timeoutEarly } = plugin
93+
const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis // disable when AWS context missing (tests, containers)
9494
try {
9595
await runMiddlewares(request, beforeMiddlewares, plugin)
9696
// Check if before stack hasn't exit early

packages/event-normalizer/__tests__/index.js

Lines changed: 236 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,87 +28,31 @@ test('It should skip when unknown event', async (t) => {
2828
t.deepEqual(response, { Records: [{ eventSource: 'aws:new' }] })
2929
})
3030

31-
test('It should parse nested events', async (t) => {
32-
const handler = middy((event) => event)
33-
.use(eventNormalizer())
34-
35-
const s3Event = createEvent.default('aws:s3')
36-
const snsEvent = createEvent.default('aws:sns')
37-
snsEvent.Records[0].Sns.Message = JSON.stringify(s3Event)
38-
const sqsEvent = createEvent.default('aws:sqs')
39-
sqsEvent.Records[0].body = JSON.stringify(snsEvent)
40-
const event = sqsEvent
41-
const response = await handler(event, context)
42-
43-
t.deepEqual(response.Records[0].body.Records[0].Sns.Message, s3Event)
44-
})
45-
46-
// SNS
47-
test('It should parse SNS event message', async (t) => {
48-
const handler = middy((event) => event)
49-
.use(eventNormalizer())
50-
51-
const message = { hello: 'world' }
52-
const event = createEvent.default('aws:sns')
53-
event.Records[0].Sns.Message = JSON.stringify(message)
54-
const response = await handler(event, context)
55-
56-
t.deepEqual(response.Records[0].Sns.Message, message)
57-
})
31+
// Events
5832

59-
// SNS -> SQS
60-
test('It should parse SNS-> SQS event', async (t) => {
33+
// Config
34+
test('It should parse Config event', async (t) => {
6135
const handler = middy((event) => event)
6236
.use(eventNormalizer())
6337

38+
const invokingEvent = { configurationItem: { configurationItemCaptureTime: '2016-02-17T01:36:34.043Z', awsAccountId: '000000000000', configurationItemStatus: 'OK', resourceId: 'i-00000000', ARN: 'arn:aws:ec2:us-east-1:000000000000:instance/i-00000000', awsRegion: 'us-east-1', availabilityZone: 'us-east-1a', resourceType: 'AWS::EC2::Instance', tags: { Foo: 'Bar' }, relationships: [{ resourceId: 'eipalloc-00000000', resourceType: 'AWS::EC2::EIP', name: 'Is attached to ElasticIp' }], configuration: { foo: 'bar' } }, messageType: 'ConfigurationItemChangeNotification' }
39+
const ruleParameters = { myParameterKey: 'myParameterValue' }
6440
const event = {
65-
Records: [
66-
{
67-
messageId: '07dee686-bfa4-40d0-a85f-4194e204dbaa',
68-
receiptHandle: 'XNIOA7DA==',
69-
body: '{\n "Type" : "Notification",\n "MessageId" : "06e1a25f-b7c9-5cdf-a548-f838aec1e14b",\n "TopicArn" : "arn:aws:sns:ca-central-1:********:topic",\n "Subject" : "Amazon S3 Notification",\n "Message" : "{\\"Records\\":[{\\"eventVersion\\":\\"2.1\\",\\"eventSource\\":\\"aws:s3\\",\\"awsRegion\\":\\"ca-central-1\\",\\"eventTime\\":\\"2022-01-23T08:50:15.375Z\\",\\"eventName\\":\\"ObjectCreated:Put\\",\\"userIdentity\\":{\\"principalId\\":\\"AWS:********\\"},\\"requestParameters\\":{\\"sourceIPAddress\\":\\"0.0.0.0\\"},\\"responseElements\\":{\\"x-amz-request-id\\":\\"*******\\",\\"x-amz-id-2\\":\\"*****\\"},\\"s3\\":{\\"s3SchemaVersion\\":\\"1.0\\",\\"configurationId\\":\\"dataset\\",\\"bucket\\":{\\"name\\":\\"s3-upload\\",\\"ownerIdentity\\":{\\"principalId\\":\\"********\\"},\\"arn\\":\\"arn:aws:s3:::s3-upload\\"},\\"object\\":{\\"key\\":\\"path/to/file.csv\\",\\"size\\":9109,\\"eTag\\":\\"*****\\",\\"sequencer\\":\\"0061ED16C7445880F0\\"}}}]}",\n "Timestamp" : "2022-01-23T08:50:16.906Z",\n "SignatureVersion" : "1",\n "Signature" : "********",\n "SigningCertURL" : "https://sns.ca-central-1.amazonaws.com/SimpleNotificationService-*****.pem",\n "UnsubscribeURL" : "https://sns.ca-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ca-central-1:*********:topic:cfa3ee69-92f9-4db8-9784-f647f868952d"\n}',
70-
attributes: {
71-
ApproximateReceiveCount: '1',
72-
SentTimestamp: '1642927816967',
73-
SenderId: 'AIDAJKASJ4',
74-
ApproximateFirstReceiveTimestamp: '1642927816972'
75-
},
76-
messageAttributes: {},
77-
md5OfBody: '141dbdeb46110bc11a04210ac6b5efaf',
78-
eventSource: 'aws:sqs',
79-
eventSourceARN: 'arn:aws:sqs:ca-central-1:*********:queue',
80-
awsRegion: 'ca-central-1'
81-
}
82-
]
41+
invokingEvent: JSON.stringify(invokingEvent),
42+
ruleParameters: JSON.stringify(ruleParameters),
43+
resultToken: 'myResultToken',
44+
eventLeftScope: false,
45+
executionRoleArn: 'arn:aws:iam::012345678912:role/config-role',
46+
configRuleArn: 'arn:aws:config:us-east-1:012345678912:config-rule/config-rule-0123456',
47+
configRuleName: 'change-triggered-config-rule',
48+
configRuleId: 'config-rule-0123456',
49+
accountId: '012345678912',
50+
version: '1.0'
8351
}
8452
const response = await handler(event, context)
8553

86-
t.deepEqual(response.Records[0].body.Message.Records[0].eventSource, 'aws:s3')
87-
})
88-
89-
// SQS
90-
test('It should parse SQS event body', async (t) => {
91-
const handler = middy((event) => event)
92-
.use(eventNormalizer())
93-
94-
const body = { hello: 'world' }
95-
const event = createEvent.default('aws:sqs')
96-
event.Records[0].body = JSON.stringify(body)
97-
const response = await handler(event, context)
98-
99-
t.deepEqual(response.Records[0].body, body)
100-
})
101-
102-
// S3
103-
test('It should normalize S3 event key', async (t) => {
104-
const handler = middy((event) => event)
105-
.use(eventNormalizer())
106-
107-
const event = createEvent.default('aws:s3')
108-
event.Records[0].s3.object.key = 'This+is+a+picture.jpg'
109-
const response = await handler(event, context)
110-
111-
t.is(response.Records[0].s3.object.key, 'This is a picture.jpg')
54+
t.deepEqual(response.invokingEvent, invokingEvent)
55+
t.deepEqual(response.ruleParameters, ruleParameters)
11256
})
11357

11458
// DynamoDB
@@ -132,20 +76,47 @@ test('It should parse DynamoDB event keys/images', async (t) => {
13276
})
13377
})
13478

135-
// Kinesis Stream
136-
test('It should parse Kinesis Stream event data', async (t) => {
79+
// Apache Kafka
80+
test('It should parse Apache Kafka event', async (t) => {
13781
const handler = middy((event) => event)
13882
.use(eventNormalizer())
13983

140-
const data = { hello: 'world' }
141-
const event = createEvent.default('aws:kinesis')
142-
event.Records[0].kinesis.data = Buffer.from(
143-
JSON.stringify(data),
144-
'utf-8'
145-
).toString('base64')
84+
const event = {
85+
eventSource: 'aws:SelfManagedKafka',
86+
bootstrapServers: 'b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092,b-1.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092',
87+
records: {
88+
mytopic0: [
89+
{
90+
topic: 'mytopic',
91+
partition: '0',
92+
offset: 15,
93+
timestamp: 1545084650987,
94+
timestampType: 'CREATE_TIME',
95+
value: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==',
96+
headers: [
97+
{
98+
headerKey: [
99+
104,
100+
101,
101+
97,
102+
100,
103+
101,
104+
114,
105+
86,
106+
97,
107+
108,
108+
117,
109+
101
110+
]
111+
}
112+
]
113+
}
114+
]
115+
}
116+
}
146117
const response = await handler(event, context)
147118

148-
t.deepEqual(response.Records[0].kinesis.data, data)
119+
t.deepEqual(response.records.mytopic0[0].value, 'Hello, this is a test.')
149120
})
150121

151122
// Kinesis Firehose
@@ -168,7 +139,7 @@ test('It should parse Kinesis Firehose event data', async (t) => {
168139
partitionKey: '4d1ad2b9-24f8-4b9d-a088-76e9947c317a',
169140
approximateArrivalTimestamp: '2012-04-23T18:25:43.511Z',
170141
sequenceNumber:
171-
'49546986683135544286507457936321625675700192471156785154',
142+
'49546986683135544286507457936321625675700192471156785154',
172143
subsequenceNumber: ''
173144
}
174145
},
@@ -181,7 +152,7 @@ test('It should parse Kinesis Firehose event data', async (t) => {
181152
partitionKey: '4d1ad2b9-24f8-4b9d-a088-76e9947c318a',
182153
approximateArrivalTimestamp: '2012-04-23T19:25:43.511Z',
183154
sequenceNumber:
184-
'49546986683135544286507457936321625675700192471156785155',
155+
'49546986683135544286507457936321625675700192471156785155',
185156
subsequenceNumber: ''
186157
}
187158
}
@@ -192,3 +163,184 @@ test('It should parse Kinesis Firehose event data', async (t) => {
192163

193164
t.deepEqual(response.records[0].data, data)
194165
})
166+
167+
// Kinesis Stream
168+
test('It should parse Kinesis Stream event data', async (t) => {
169+
const handler = middy((event) => event)
170+
.use(eventNormalizer())
171+
172+
const data = { hello: 'world' }
173+
const event = createEvent.default('aws:kinesis')
174+
event.Records[0].kinesis.data = Buffer.from(
175+
JSON.stringify(data),
176+
'utf-8'
177+
).toString('base64')
178+
const response = await handler(event, context)
179+
180+
t.deepEqual(response.Records[0].kinesis.data, data)
181+
})
182+
183+
// MQ
184+
test('It should parse MQ event', async (t) => {
185+
const handler = middy((event) => event)
186+
.use(eventNormalizer())
187+
188+
const event = {
189+
eventSource: 'aws:amq',
190+
eventSourceArn: 'arn:aws:mq:us-west-2:112556298976:broker:test:b-9bcfa592-423a-4942-879d-eb284b418fc8',
191+
messages: [
192+
{
193+
messageID: 'ID:b-9bcfa592-423a-4942-879d-eb284b418fc8-1.mq.us-west-2.amazonaws.com-37557-1234520418293-4:1:1:1:1',
194+
messageType: 'jms/text-message',
195+
data: 'QUJDOkFBQUE=',
196+
connectionId: 'myJMSCoID',
197+
redelivered: false,
198+
destination: {
199+
physicalname: 'testQueue'
200+
},
201+
timestamp: 1598827811958,
202+
brokerInTime: 1598827811958,
203+
brokerOutTime: 1598827811959
204+
}
205+
]
206+
}
207+
const response = await handler(event, context)
208+
209+
t.deepEqual(response.messages[0].data, 'ABC:AAAA')
210+
})
211+
212+
// MSK
213+
test('It should parse MSK event', async (t) => {
214+
const handler = middy((event) => event)
215+
.use(eventNormalizer())
216+
217+
const event = {
218+
eventSource: 'aws:kafka',
219+
eventSourceArn: 'arn:aws:kafka:sa-east-1:123456789012:cluster/vpc-2priv-2pub/751d2973-a626-431c-9d4e-d7975eb44dd7-2',
220+
records: {
221+
mytopic0: [
222+
{
223+
topic: 'mytopic',
224+
partition: '0',
225+
offset: 15,
226+
timestamp: 1545084650987,
227+
timestampType: 'CREATE_TIME',
228+
value: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==',
229+
headers: [
230+
{
231+
headerKey: [
232+
104,
233+
101,
234+
97,
235+
100,
236+
101,
237+
114,
238+
86,
239+
97,
240+
108,
241+
117,
242+
101
243+
]
244+
}
245+
]
246+
}
247+
]
248+
}
249+
}
250+
const response = await handler(event, context)
251+
252+
t.deepEqual(response.records.mytopic0[0].value, 'Hello, this is a test.')
253+
})
254+
255+
// SNS
256+
test('It should parse SNS event message', async (t) => {
257+
const handler = middy((event) => event)
258+
.use(eventNormalizer())
259+
260+
const message = { hello: 'world' }
261+
const event = createEvent.default('aws:sns')
262+
event.Records[0].Sns.Message = JSON.stringify(message)
263+
const response = await handler(event, context)
264+
265+
t.deepEqual(response.Records[0].Sns.Message, message)
266+
})
267+
268+
// SQS
269+
test('It should parse SQS event body', async (t) => {
270+
const handler = middy((event) => event)
271+
.use(eventNormalizer())
272+
273+
const body = { hello: 'world' }
274+
const event = createEvent.default('aws:sqs')
275+
event.Records[0].body = JSON.stringify(body)
276+
const response = await handler(event, context)
277+
278+
t.deepEqual(response.Records[0].body, body)
279+
})
280+
281+
// S3
282+
test('It should normalize S3 event key', async (t) => {
283+
const handler = middy((event) => event)
284+
.use(eventNormalizer())
285+
286+
const event = createEvent.default('aws:s3')
287+
event.Records[0].s3.object.key = 'This+is+a+picture.jpg'
288+
const response = await handler(event, context)
289+
290+
t.is(response.Records[0].s3.object.key, 'This is a picture.jpg')
291+
})
292+
293+
// S3 Batch
294+
test('It should normalize S3 Batch event key', async (t) => {
295+
const handler = middy((event) => event)
296+
.use(eventNormalizer())
297+
298+
const event = {
299+
invocationSchemaVersion: '1.0',
300+
invocationId: 'YXNkbGZqYWRmaiBhc2RmdW9hZHNmZGpmaGFzbGtkaGZza2RmaAo',
301+
job: {
302+
id: 'f3cc4f60-61f6-4a2b-8a21-d07600c373ce'
303+
},
304+
tasks: [
305+
{
306+
taskId: 'dGFza2lkZ29lc2hlcmUK',
307+
s3Key: 'customer+Image+1.jpg',
308+
s3VersionId: '1',
309+
s3BucketArn: 'arn:aws:s3:us-east-1:0123456788:examplebucket'
310+
}
311+
]
312+
}
313+
const response = await handler(event, context)
314+
315+
t.is(response.tasks[0].s3Key, 'customer Image 1.jpg')
316+
})
317+
318+
// S3 -> SNS -> SQS
319+
test('It should parse S3 -> SNS -> SQS event', async (t) => {
320+
const handler = middy((event) => event)
321+
.use(eventNormalizer())
322+
323+
const event = {
324+
Records: [
325+
{
326+
messageId: '07dee686-bfa4-40d0-a85f-4194e204dbaa',
327+
receiptHandle: 'XNIOA7DA==',
328+
body: '{\n "Type" : "Notification",\n "MessageId" : "00e1a25f-b7c9-5cdf-a548-f838aec0e14b",\n "TopicArn" : "arn:aws:sns:ca-central-1:********:topic",\n "Subject" : "Amazon S3 Notification",\n "Message" : "{\\"Records\\":[{\\"eventVersion\\":\\"2.1\\",\\"eventSource\\":\\"aws:s3\\",\\"awsRegion\\":\\"ca-central-1\\",\\"eventTime\\":\\"2022-01-23T08:50:15.375Z\\",\\"eventName\\":\\"ObjectCreated:Put\\",\\"userIdentity\\":{\\"principalId\\":\\"AWS:********\\"},\\"requestParameters\\":{\\"sourceIPAddress\\":\\"0.0.0.0\\"},\\"responseElements\\":{\\"x-amz-request-id\\":\\"*******\\",\\"x-amz-id-2\\":\\"*****\\"},\\"s3\\":{\\"s3SchemaVersion\\":\\"1.0\\",\\"configurationId\\":\\"dataset\\",\\"bucket\\":{\\"name\\":\\"s3-upload\\",\\"ownerIdentity\\":{\\"principalId\\":\\"********\\"},\\"arn\\":\\"arn:aws:s3:::s3-upload\\"},\\"object\\":{\\"key\\":\\"path/to/file.csv\\",\\"size\\":9109,\\"eTag\\":\\"*****\\",\\"sequencer\\":\\"0061ED16C7445880F0\\"}}}]}",\n "Timestamp" : "2022-01-23T08:50:16.906Z",\n "SignatureVersion" : "1",\n "Signature" : "********",\n "SigningCertURL" : "https://sns.ca-central-1.amazonaws.com/SimpleNotificationService-*****.pem",\n "UnsubscribeURL" : "https://sns.ca-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ca-central-1:*********:topic:cfa3ee69-92f9-4db8-9784-f647f868952d"\n}',
329+
attributes: {
330+
ApproximateReceiveCount: '1',
331+
SentTimestamp: '1642927816967',
332+
SenderId: 'AIDAJKASJ4',
333+
ApproximateFirstReceiveTimestamp: '1642927816972'
334+
},
335+
messageAttributes: {},
336+
md5OfBody: '141dbdeb46110bc11a04210ac6b5efaf',
337+
eventSource: 'aws:sqs',
338+
eventSourceARN: 'arn:aws:sqs:ca-central-1:*********:queue',
339+
awsRegion: 'ca-central-1'
340+
}
341+
]
342+
}
343+
const response = await handler(event, context)
344+
345+
t.deepEqual(response.Records[0].body.Message.Records[0].eventSource, 'aws:s3')
346+
})

0 commit comments

Comments
 (0)