-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
port-check.test.ts
126 lines (101 loc) · 5.21 KB
/
port-check.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { log } from "./logger";
import { execContainer } from "./docker/functions/container/exec-container";
import Dockerode from "dockerode";
import { InternalPortCheck } from "./port-check";
jest.mock("./logger");
jest.mock("./docker/functions/container/exec-container");
const mockLogger = jest.mocked(log, true);
const mockExecContainer = jest.mocked(execContainer, true);
describe("PortCheck", () => {
describe("InternalPortCheck", () => {
let mockContainer: Dockerode.Container;
let portCheck: InternalPortCheck;
beforeEach(() => {
jest.resetAllMocks();
mockContainer = { id: "containerId" } as Dockerode.Container;
portCheck = new InternalPortCheck(mockContainer);
});
it("should return true when at least one command returns exit code 0", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "SUCCESS", exitCode: 0 }));
const result = await portCheck.isBound(8080);
expect(result).toBe(true);
});
it("should trace log unique error messages", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }));
await portCheck.isBound(8080);
expect(mockLogger.trace.mock.calls).toEqual([
["Port check result for container containerId exit code 1: ERROR 1"],
["Port check result for container containerId exit code 1: ERROR 2"],
]);
});
it("should trace log unique error messages across multiple invocations", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 1 }));
await portCheck.isBound(8080);
await portCheck.isBound(8080);
expect(mockLogger.trace.mock.calls).toEqual([
["Port check result for container containerId exit code 1: ERROR 1"],
["Port check result for container containerId exit code 1: ERROR 2"],
]);
});
it("should not trace log error messages with empty output", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "", exitCode: 1 }));
await portCheck.isBound(8080);
expect(mockLogger.trace.mock.calls).toEqual([
["Port check result for container containerId exit code 1: ERROR 1"],
]);
});
it("should not trace log error messages where the shell is missing if another shell exists", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 1 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }));
await portCheck.isBound(8080);
expect(mockLogger.trace.mock.calls).toEqual([
["Port check result for container containerId exit code 1: ERROR 1"],
]);
});
it("should error log when the port-check will fail due to missing shells (distroless)", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }));
await portCheck.isBound(8080);
expect(mockLogger.error.mock.calls).toEqual([
[
"The HostPortWaitStrategy will not work on a distroless image, use an alternate wait strategy for container containerId",
],
]);
});
it("should error log the distroless image once", async () => {
mockExecContainer
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 1", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }))
.mockReturnValueOnce(Promise.resolve({ output: "ERROR 2", exitCode: 126 }));
await portCheck.isBound(8080);
await portCheck.isBound(8080);
expect(mockLogger.error.mock.calls).toEqual([
[
"The HostPortWaitStrategy will not work on a distroless image, use an alternate wait strategy for container containerId",
],
]);
});
});
});