Skip to content
Draft
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
Expand Up @@ -3,9 +3,11 @@ import { inspectContainerUntilPortsExposed } from "./inspect-container-util-port

function mockInspectResult(
portBindings: ContainerInspectInfo["HostConfig"]["PortBindings"],
ports: ContainerInspectInfo["NetworkSettings"]["Ports"]
ports: ContainerInspectInfo["NetworkSettings"]["Ports"],
state: Partial<ContainerInspectInfo["State"]> = { Status: "running" }
): ContainerInspectInfo {
return {
State: state,
HostConfig: {
PortBindings: portBindings,
},
Expand Down Expand Up @@ -74,4 +76,33 @@ describe("inspectContainerUntilPortsExposed", { concurrent: false }, () => {
"Timed out after 0ms while waiting for container ports to be bound to the host"
);
});

it("throws an error when the container exits before its ports are exposed", async () => {
const data = mockInspectResult({ "8080/tcp": [] }, {}, { Status: "exited", ExitCode: 1 });
const inspectFn = vi.fn().mockResolvedValue(data);

await expect(inspectContainerUntilPortsExposed(inspectFn, "container-id")).rejects.toThrow(
"Container exited with code 1 before its ports were bound to the host"
);
expect(inspectFn).toHaveBeenCalledTimes(1);
});

it("returns the inspect result when the container has exited and no ports are exposed", async () => {
const data = mockInspectResult({}, {}, { Status: "exited", ExitCode: 0 });
const inspectFn = vi.fn().mockResolvedValueOnce(data);

const result = await inspectContainerUntilPortsExposed(inspectFn, "container-id");

expect(result).toEqual(data);
});

it("retries the inspect while the container is restarting", async () => {
const data1 = mockInspectResult({ "8080/tcp": [] }, {}, { Status: "restarting" });
const data2 = mockInspectResult({ "8080/tcp": [] }, { "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] });
const inspectFn = vi.fn().mockResolvedValueOnce(data1).mockResolvedValueOnce(data2);

const result = await inspectContainerUntilPortsExposed(inspectFn, "container-id");

expect(result).toEqual(data2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ export async function inspectContainerUntilPortsExposed(
): Promise<ContainerInspectInfo> {
const result = await new IntervalRetry<ContainerInspectInfo, Error>(250).retryUntil(
() => inspectFn(),
(inspectResult) => {
const portBindings = inspectResult?.HostConfig?.PortBindings;
if (!portBindings) return true;
const expectedlyBoundPorts = Object.keys(portBindings);
return expectedlyBoundPorts.every((exposedPort) => inspectResult.NetworkSettings.Ports[exposedPort]?.length > 0);
},
(inspectResult) => arePortsBound(inspectResult) || hasExited(inspectResult),
() => {
const message = `Timed out after ${timeout}ms while waiting for container ports to be bound to the host`;
log.error(message, { containerId });
Expand All @@ -26,5 +21,22 @@ export async function inspectContainerUntilPortsExposed(
throw result;
}

if (!arePortsBound(result)) {
const message = `Container exited with code ${result.State.ExitCode} before its ports were bound to the host`;
log.error(message, { containerId });
throw new Error(message);
}

return result;
}

function arePortsBound(inspectResult: ContainerInspectInfo): boolean {
const portBindings = inspectResult?.HostConfig?.PortBindings;
if (!portBindings) return true;
const expectedlyBoundPorts = Object.keys(portBindings);
return expectedlyBoundPorts.every((exposedPort) => inspectResult.NetworkSettings.Ports[exposedPort]?.length > 0);
}

function hasExited(inspectResult: ContainerInspectInfo): boolean {
return inspectResult?.State?.Status === "exited";
}
Loading