-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheckConsumerLag.test.ts
108 lines (97 loc) · 2.99 KB
/
checkConsumerLag.test.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
import { describe, it, expect, vi } from "vitest";
import { handler } from "./checkConsumerLag";
import { Context } from "aws-lambda";
import {
TEST_FUNCTION_NAME,
TEST_TOPIC_NAME,
TEST_NONEXISTENT_TOPIC_NAME,
TEST_NONEXISTENT_FUNCTION_NAME,
TEST_MULTIPLE_TOPICS_FUNCTION_NAME,
TEST_MULTIPLE_TOPICS_TOPIC_NAME,
TEST_MISSING_CONSUMER_FUNCTION_NAME,
TEST_MISSING_CONSUMER_TOPIC_NAME,
mockedAdmin,
} from "mocks";
describe("Lambda Handler", () => {
const callback = vi.fn();
it("should handle successful execution with stable and current offsets", async () => {
const event = {
triggers: [
{
function: TEST_FUNCTION_NAME,
topics: [TEST_TOPIC_NAME],
},
],
brokerString: "broker1,broker2",
};
await handler(event, {} as Context, callback);
expect(callback).toHaveBeenCalledWith(null, {
statusCode: 200,
stable: true,
current: true,
ready: true,
});
});
it.each([
[
"should handle missing function",
TEST_NONEXISTENT_FUNCTION_NAME,
TEST_TOPIC_NAME,
`ERROR: No event source mapping found for function ${TEST_NONEXISTENT_FUNCTION_NAME} and topic ${TEST_TOPIC_NAME}`,
],
[
"should handle missing topic",
TEST_FUNCTION_NAME,
TEST_NONEXISTENT_TOPIC_NAME,
`ERROR: No event source mapping found for function ${TEST_FUNCTION_NAME} and topic ${TEST_NONEXISTENT_TOPIC_NAME}`,
],
[
"should handle multiple event source mappings",
TEST_MULTIPLE_TOPICS_FUNCTION_NAME,
TEST_MULTIPLE_TOPICS_TOPIC_NAME,
`ERROR: Multiple event source mappings found for function ${TEST_MULTIPLE_TOPICS_FUNCTION_NAME} and topic ${TEST_MULTIPLE_TOPICS_TOPIC_NAME}`,
],
[
"should handle missing ConsumerGroupId",
TEST_MISSING_CONSUMER_FUNCTION_NAME,
TEST_MISSING_CONSUMER_TOPIC_NAME,
`ERROR: No ConsumerGroupId found for function ${TEST_MISSING_CONSUMER_FUNCTION_NAME} and topic ${TEST_MISSING_CONSUMER_TOPIC_NAME}`,
],
])("%s", async (_, funcName, topicName, errorMessage) => {
const event = {
triggers: [
{
function: funcName,
topics: [topicName],
},
],
brokerString: "broker1,broker2",
};
await handler(event, {} as Context, callback);
expect(callback).toHaveBeenCalledWith(new Error(errorMessage), {
statusCode: 500,
stable: false,
current: false,
ready: false,
});
});
it("should handle kafka admin errors", async () => {
const event = {
triggers: [
{
function: TEST_FUNCTION_NAME,
topics: [TEST_TOPIC_NAME],
},
],
brokerString: "broker1,broker2",
};
mockedAdmin.describeGroups.mockRejectedValueOnce(new Error("Kafka admin error"));
await handler(event, {} as Context, callback);
expect(callback).toHaveBeenCalledWith(new Error(`Kafka admin error`), {
statusCode: 500,
stable: false,
current: false,
ready: false,
});
});
});