Skip to content

Commit 77055b8

Browse files
committed
feat!: implement perser function with test
1 parent 5ac9de7 commit 77055b8

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/funcs/parser.lambda.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { gunzipSync } from 'zlib';
2+
import { CloudWatchLogsDecodedData, CloudWatchLogsEvent, Context } from 'aws-lambda';
3+
4+
export const handler = async (event: CloudWatchLogsEvent, context: Context): Promise<CloudWatchLogsDecodedData | Error> => {
5+
console.log({ event: event });
6+
console.log({ context: context });
7+
8+
// decode base64
9+
const payload = Buffer.from(event.awslogs.data, 'base64');
10+
11+
// gunzip
12+
const result = gunzipSync(payload);
13+
14+
// parse json
15+
return JSON.parse(result.toString('utf8'));
16+
};
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { CloudWatchLogsEvent, Context } from 'aws-lambda';
2+
import { handler } from '../../src/funcs/parser.lambda';
3+
4+
describe('Lambda Function Handler Succeed testing', () => {
5+
6+
// const ceClientMock = mockClient(CostExplorerClient);
7+
8+
beforeEach(() => {
9+
// ceClientMock.reset();
10+
// Date.now = jest.fn(() => new Date(2023, 1, 23, 2, 2, 2).getTime());
11+
});
12+
13+
afterEach(() => {
14+
jest.clearAllMocks();
15+
});
16+
17+
describe('Lambda Function handler', () => {
18+
19+
const context = {
20+
callbackWaitsForEmptyEventLoop: true,
21+
functionVersion: '$LATEST',
22+
functionName: 'lambda-xxxx-func',
23+
memoryLimitInMB: '128',
24+
logGroupName: '/aws/lambda/lambda-xxxx-func',
25+
logStreamName: '2024/10/10/llambda-xxxx-func[$LATEST]aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
26+
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:999999999999:function:lambda-xxxx-func',
27+
awsRequestId: '00000000-0000-0000-0000-000000000000',
28+
};
29+
30+
const event = {
31+
awslogs: {
32+
data: 'H4sIAAAAAAAAE51TYWvbMBD9K+LYhxac2HKabtG3QL0yWBkk/rQoBMW+pGps2ZXkNCbkvxfJThtGYGMPYcO7d8fp7ukIRbVN9qisAbY4gsyBwSj6L1AIoERjxBaBwZGDlSUaK8qaA+MQR/HdgEYDGqU0Yv4MKaW/OQQcCtxj4WXJbPZr5jmNrw0a+yP3/LrH4MrnDJ/Wt8CBHTmg1pVO2xp9jQdhxVoYTBzrxT7+9JHBwbTKigPxPBGWVJooFJpw7vScg08zVmS7VIvMJS36Moz8WzIhxAXDvdChFWYXSpXjYfhiGB1HUcxieimrdZWhMcP+n8psZ6Yqn+ELZlZWypAbVeXIpLKolSjCXuhLr14bbNCwyZiNby+rCtOqjMwa5XY0fBYqL1CTm2s9Te5GMRvdclj6PamtfebAJjTgoETZTQ0/Bmpwj1ra9o9dZlXeKe/i+6i7X10Z6S7g6dF44smNLPo9ZEINi84bUnUcpaOoc0bV2DPphAr1qm37Jk4nCD6tB4x+jb+N4/ieRpTS0zJwjn/UVVMDg1C8mbAQ5ToXIR5EWRc4OPQYbBqVgZfPrUZRAvMeDmnkzlX54svPaZrM06X4Cz6fijMnMHiYptPVUzKfTx8TCKB6U6iBweQCEIBp1ibTsnZz+y4Li9o9Wzj3chkebHwclqd3GRuijuQDAAA=',
33+
},
34+
};
35+
36+
it('Should client succeed', async () => {
37+
38+
const result = await handler(event as CloudWatchLogsEvent, context as Context);
39+
40+
expect(result).toEqual({
41+
logEvents: [
42+
{
43+
id: '30000000000000000000000000000000000000000000000000000001',
44+
message: JSON.stringify({
45+
timestamp: '2024-10-10T10:10:10.111Z',
46+
level: 'ERROR',
47+
requestId: 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
48+
message: {
49+
errorType: 'DatabaseError',
50+
errorMessage: 'syntax error at or near ","',
51+
stackTrace: [
52+
'error: syntax error at or near ","',
53+
' at /var/task/index.js:15002:21',
54+
' at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
55+
' at async Runtime.handler (/var/task/index.js:19432:3)',
56+
],
57+
length: 91,
58+
name: 'error',
59+
severity: 'ERROR',
60+
code: '42601',
61+
position: '359',
62+
file: 'scan.l',
63+
line: '1130',
64+
routine: 'scanner_yyerror',
65+
},
66+
}),
67+
timestamp: 1728522610111,
68+
},
69+
],
70+
logGroup: '/aws/lambda/example-xxxxxxxx-func',
71+
logStream: '2024/10/10/example-xxxxxxxx-func[$LATEST]aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
72+
messageType: 'DATA_MESSAGE',
73+
owner: '999999999999',
74+
subscriptionFilters: [
75+
'example-subscription-filter',
76+
],
77+
});
78+
});
79+
80+
});
81+
82+
// describe('Error handling', () => {
83+
// describe('Should Environment Variable Error handling', () => {
84+
// it('Should error cause MissingEnvironmentVariableError(SLACK_WEBHOOK_URL)', async () => {
85+
// process.env = {
86+
// SLACK_CHANNEL: 'example-channel',
87+
// };
88+
// // await expect(handler({ Type: EventInputType.SERVICES }, {} as Context)).rejects.toThrow(MissingEnvironmentVariableError);
89+
// });
90+
// it('Should error cause MissingEnvironmentVariableError(SLACK_POST_CHANNEL)', async () => {
91+
// process.env = {
92+
// SLACK_TOKEN: 'xxxx-xxxxxxxxx-xxxx',
93+
// };
94+
// // await expect(handler({ Type: EventInputType.SERVICES }, {} as Context)).rejects.toThrow(MissingEnvironmentVariableError);
95+
// });
96+
// });
97+
// describe('Should Event Input Variable Error handling', () => {
98+
// it('Should error cause MissingInputVariableError(Type)', async () => {
99+
// process.env = {
100+
// SLACK_TOKEN: 'xxxx-xxxxxxxxx-xxxx',
101+
// SLACK_CHANNEL: 'example-channel',
102+
// };
103+
// // await expect(handler({ Type: '' as EventInputType }, {} as Context)).rejects.toThrow(MissingInputVariableError);
104+
// });
105+
// it('Should error cause InvalidInputVariableError(Type)', async () => {
106+
// process.env = {
107+
// SLACK_TOKEN: 'xxxx-xxxxxxxxx-xxxx',
108+
// SLACK_CHANNEL: 'example-channel',
109+
// };
110+
// // await expect(handler({ Type: 'Miss' as EventInputType }, {} as Context)).rejects.toThrow(InvalidInputVariableFormatError);
111+
// });
112+
// });
113+
// });
114+
115+
});

0 commit comments

Comments
 (0)