Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve diagnostic logging for HostPort wait strategy (#397)
* Improve diagnostic logging for HostPort wait strategy * Cleanup * Improve test names
- Loading branch information
1 parent
7aca953
commit 3dfc799
Showing
3 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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", | ||
], | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters