-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtrigger.spec.ts
224 lines (215 loc) · 7.38 KB
/
trigger.spec.ts
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
import { parseEventSource, parseEventSourceARN, extractTriggerTags, extractHTTPStatusCodeTag } from "./trigger";
import { readFileSync } from "fs";
import { Context } from "aws-lambda";
const mockARN = "arn:aws:lambda:us-east-1:123456789012:function:test-nodejs-lambda";
const mockContext = {
invokedFunctionArn: mockARN,
} as any as Context;
describe("parseEventSource", () => {
const events = [
{
result: {
"function_trigger.event_source": "api-gateway",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/id/stages/$default",
"http.url": "id.execute-api.us-east-1.amazonaws.com",
"http.url_details.path": "/my/path",
"http.method": "GET",
"http.route": "/my/path",
},
file: "api-gateway-v1.json",
},
{
result: {
"function_trigger.event_source": "api-gateway",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/r3pmxmplak/stages/default",
"http.url": "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
"http.url_details.path": "/default/nodejs-apig-function-1G3XMPLZXVXYI",
"http.method": "GET",
"http.route": "/nodejs-apig-function-1G3XMPLZXVXYI",
},
file: "api-gateway-v2.json",
},
{
result: {
"function_trigger.event_source": "lambda-function-url",
"http.url": "a8hyhsshac.lambda-url.eu-south-1.amazonaws.com",
"http.url_details.path": "/",
"http.method": "GET",
},
file: "lambda-function-urls.json",
},
{
result: {
"function_trigger.event_source": "application-load-balancer",
"function_trigger.event_source_arn":
"arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-xyz/123abc",
"http.url_details.path": "/lambda",
"http.method": "GET",
},
file: "application-load-balancer.json",
},
{
result: {
"function_trigger.event_source": "cloudfront",
"function_trigger.event_source_arn": "arn:aws:cloudfront::123456789012:distribution/EXAMPLE",
},
file: "cloudfront.json",
},
{
result: {
"function_trigger.event_source": "cloudwatch-events",
"function_trigger.event_source_arn": "arn:aws:events:us-east-1:123456789012:rule/ExampleRule",
},
file: "cloudwatch-events.json",
},
{
result: {
"function_trigger.event_source": "cloudwatch-logs",
"function_trigger.event_source_arn": "arn:aws:logs:us-east-1:123456789012:log-group:testLogGroup",
},
file: "cloudwatch-logs.json",
},
{ result: {}, file: "custom.json" },
{
result: {
"function_trigger.event_source": "dynamodb",
"function_trigger.event_source_arn":
"arn:aws:dynamodb:us-east-1:123456789012:table/ExampleTableWithStream/stream/2015-06-27T00:48:05.899",
},
file: "dynamodb.json",
},
{
result: {
"function_trigger.event_source": "kinesis",
"function_trigger.event_source_arn": "arn:aws:kinesis:EXAMPLE",
},
file: "kinesis.json",
},
{
result: {
"function_trigger.event_source": "s3",
"function_trigger.event_source_arn": "arn:aws:s3:::example-bucket",
},
file: "s3.json",
},
{
result: {
"function_trigger.event_source": "sns",
"function_trigger.event_source_arn": "arn:aws:sns:us-east-1:123456789012:ExampleTopic",
},
file: "sns.json",
},
{
result: {
"function_trigger.event_source": "sqs",
"function_trigger.event_source_arn": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
},
file: "sqs.json",
},
];
const bufferedResponses = [
{
responseBody: {
statusCode: 200,
body: '"Hello from Lambda!"',
},
expectedStatusCode: "200",
},
{
responseBody: {
statusCode: 404,
body: '"NOT FOUND"',
},
expectedStatusCode: "404",
},
{
responseBody: undefined,
expectedStatusCode: "502",
},
];
const streamingResponses = [
{
responseBody: {
statusCode: 200,
body: '"Hello from Lambda!"',
},
expectedStatusCode: "200",
},
{
responseBody: {
statusCode: 404,
body: '"NOT FOUND"',
},
expectedStatusCode: "404",
},
{
responseBody: undefined,
expectedStatusCode: "200",
},
];
it("returns the correct event source and ARN", () => {
for (let event of events) {
const eventData = JSON.parse(readFileSync(`./event_samples/${event.file}`, "utf8"));
const eventSource = parseEventSource(eventData);
const eventSourceARN = parseEventSourceARN(eventSource, eventData, mockContext);
expect(eventSource).toEqual(event.result["function_trigger.event_source"]);
expect(eventSourceARN).toEqual(event.result["function_trigger.event_source_arn"]);
}
});
it("extracts all trigger tags", () => {
for (let event of events) {
const eventData = JSON.parse(readFileSync(`./event_samples/${event.file}`, "utf8"));
const eventSource = parseEventSource(event);
const triggerTags = extractTriggerTags(eventData, mockContext, eventSource);
expect(triggerTags).toEqual(event.result);
}
});
it("extracts the status code if API Gateway, ALB, or Function URL, otherwise do nothing, for buffered functions", () => {
for (const event of events) {
const eventData = JSON.parse(readFileSync(`./event_samples/${event.file}`, "utf8"));
const eventSource = parseEventSource(event);
const triggerTags = extractTriggerTags(eventData, mockContext, eventSource);
const isResponseStreamingFunction = false;
for (const response of bufferedResponses) {
const statusCode = extractHTTPStatusCodeTag(triggerTags, response.responseBody, isResponseStreamingFunction);
// We should always return a status code for API Gateway, ALB, and Lambda Function URL
if (
[
"api-gateway-v1.json",
"api-gateway-v2.json",
"application-load-balancer.json",
"lambda-function-urls.json",
].includes(event.file)
) {
expect(statusCode).toEqual(response.expectedStatusCode);
} else {
expect(statusCode).toBeUndefined();
}
}
}
});
it("extracts the status code if API Gateway, ALB, or Function URL, otherwise do nothing, for streaming functions", () => {
for (let event of events) {
const eventData = JSON.parse(readFileSync(`./event_samples/${event.file}`, "utf8"));
const eventSource = parseEventSource(event);
const triggerTags = extractTriggerTags(eventData, mockContext, eventSource);
const isResponseStreamingFunction = true;
for (const response of streamingResponses) {
const statusCode = extractHTTPStatusCodeTag(triggerTags, response.responseBody, isResponseStreamingFunction);
// We should always return a status code for API Gateway, ALB, and Lambda Function URL
if (
[
"api-gateway-v1.json",
"api-gateway-v2.json",
"application-load-balancer.json",
"lambda-function-urls.json",
].includes(event.file)
) {
expect(statusCode).toEqual(response.expectedStatusCode);
} else {
expect(statusCode).toBeUndefined();
}
}
}
});
});