Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ContainerInspectInfo } from "dockerode";
import { InspectResult } from "../types";
import { mapInspectResult } from "../utils/map-inspect-result";
import { inspectContainerUntilPortsExposed } from "./inspect-container-util-ports-exposed";

function mockExposed(): { inspectResult: ContainerInspectInfo; mappedInspectResult: InspectResult } {
function mockInspectResult(ports: ContainerInspectInfo["NetworkSettings"]["Ports"]) {
const date = new Date();

const inspectResult: ContainerInspectInfo = {
Expand All @@ -22,34 +21,7 @@ function mockExposed(): { inspectResult: ContainerInspectInfo; mappedInspectResu
FinishedAt: date.toISOString(),
},
NetworkSettings: {
Ports: { "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] },
Networks: {},
},
} as unknown as ContainerInspectInfo;

return { inspectResult, mappedInspectResult: mapInspectResult(inspectResult) };
}

function mockNotExposed(): { inspectResult: ContainerInspectInfo; mappedInspectResult: InspectResult } {
const date = new Date();

const inspectResult: ContainerInspectInfo = {
Name: "container-id",
Config: {
Hostname: "hostname",
Labels: {},
},
State: {
Health: {
Status: "healthy",
},
Status: "running",
Running: true,
StartedAt: date.toISOString(),
FinishedAt: date.toISOString(),
},
NetworkSettings: {
Ports: { "8080/tcp": [] },
Ports: ports,
Networks: {},
},
} as unknown as ContainerInspectInfo;
Expand All @@ -58,7 +30,7 @@ function mockNotExposed(): { inspectResult: ContainerInspectInfo; mappedInspectR
}

test("returns the inspect results when all ports are exposed", async () => {
const data = mockExposed();
const data = mockInspectResult({ "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] });
const inspectFn = vi.fn().mockResolvedValueOnce(data.inspectResult);

const result = await inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id");
Expand All @@ -67,8 +39,8 @@ test("returns the inspect results when all ports are exposed", async () => {
});

test("retries the inspect if ports are not yet exposed", async () => {
const data1 = mockNotExposed();
const data2 = mockExposed();
const data1 = mockInspectResult({ "8080/tcp": [] });
const data2 = mockInspectResult({ "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] });
const inspectFn = vi
.fn()
.mockResolvedValueOnce(data1.inspectResult)
Expand All @@ -81,8 +53,17 @@ test("retries the inspect if ports are not yet exposed", async () => {
expect(inspectFn).toHaveBeenCalledTimes(3);
});

test("throws an error when ports are not exposed within timeout", async () => {
const data = mockNotExposed();
test("throws an error when host ports are not exposed within timeout", async () => {
const data = mockInspectResult({ "8080/tcp": [] });
const inspectFn = vi.fn().mockResolvedValue(data.inspectResult);

await expect(inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id", 0)).rejects.toThrow(
"Container did not expose all ports after starting"
);
});

test("throws an error when container ports not exposed within timeout", async () => {
const data = mockInspectResult({});
const inspectFn = vi.fn().mockResolvedValue(data.inspectResult);

await expect(inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id", 0)).rejects.toThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function inspectContainerUntilPortsExposed(
({ mappedInspectResult }) =>
ports
.map((exposedPort) => getContainerPort(exposedPort))
.every((exposedPort) => mappedInspectResult.ports[exposedPort].length > 0),
.every((exposedPort) => mappedInspectResult.ports[exposedPort]?.length > 0),
() => {
const message = `Container did not expose all ports after starting`;
log.error(message, { containerId });
Expand Down