Skip to content

Commit d0814dc

Browse files
committed
Address PR comments
1 parent dfd4a7b commit d0814dc

File tree

4 files changed

+19
-38
lines changed

4 files changed

+19
-38
lines changed

packages/testcontainers/src/generic-container/started-generic-container.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { CommitOptions, ContentToCopy, DirectoryToCopy, ExecOptions, ExecResult,
1010
import { BoundPorts } from "../utils/bound-ports";
1111
import { LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels";
1212
import { mapInspectResult } from "../utils/map-inspect-result";
13+
import { PortWithOptionalBinding } from "../utils/port";
1314
import { waitForContainer } from "../wait-strategies/wait-for-container";
1415
import { WaitStrategy } from "../wait-strategies/wait-strategy";
1516
import { inspectContainerUntilPortsExposed } from "./inspect-container-util-ports-exposed";
@@ -95,7 +96,10 @@ export class StartedGenericContainer implements StartedTestContainer {
9596
}
9697

9798
this.boundPorts = BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, mappedInspectResult).filter(
98-
Array.from(this.boundPorts.iterator()).map((port) => port[0])
99+
Array.from(this.boundPorts.iterator()).map((port) => {
100+
const [portNumber, protocol] = port[0].split("/");
101+
return `${portNumber}/${protocol}` as PortWithOptionalBinding;
102+
})
99103
);
100104

101105
await waitForContainer(client, this.container, this.waitStrategy, this.boundPorts, startTime);

packages/testcontainers/src/utils/port.test.ts

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,50 @@ import { getContainerPort, getProtocol, hasHostBinding } from "./port";
33

44
describe("port utilities", () => {
55
describe("getContainerPort", () => {
6-
it("returns number as is", () => {
6+
it("should return the container port when defined as a number", () => {
77
expect(getContainerPort(8080)).toBe(8080);
88
});
99

10-
it("parses port from string", () => {
11-
expect(getContainerPort("8080")).toBe(8080);
12-
});
13-
14-
it("parses port from string with protocol", () => {
10+
it("should return the port when defined as a string with format `port/protocol`", () => {
1511
expect(getContainerPort("8080/tcp")).toBe(8080);
1612
expect(getContainerPort("8080/udp")).toBe(8080);
1713
});
1814

19-
it("returns container port from object", () => {
15+
it("should return the container port from when defined as `PortWithBinding`", () => {
2016
expect(getContainerPort({ container: 8080, host: 49000 })).toBe(8080);
2117
});
22-
23-
it("throws error for invalid port format", () => {
24-
expect(() => getContainerPort("invalid")).toThrow("Invalid port format: invalid");
25-
});
2618
});
2719

2820
describe("hasHostBinding", () => {
29-
it("returns true for object with host binding", () => {
21+
it("should return true for `PortWithBinding` with defined `host` parameter", () => {
3022
expect(hasHostBinding({ container: 8080, host: 49000 })).toBe(true);
3123
});
3224

33-
it("returns false for number", () => {
25+
it("should return false when querying for a number", () => {
3426
expect(hasHostBinding(8080)).toBe(false);
3527
});
3628

37-
it("returns false for string", () => {
38-
expect(hasHostBinding("8080")).toBe(false);
29+
it("should return false when querying for a string with format `port/protocol`", () => {
3930
expect(hasHostBinding("8080/tcp")).toBe(false);
4031
});
4132
});
4233

4334
describe("getProtocol", () => {
44-
it("returns tcp for number", () => {
35+
it("should return the default `tcp` for a number", () => {
4536
expect(getProtocol(8080)).toBe("tcp");
4637
});
4738

48-
it("returns tcp for string without protocol", () => {
49-
expect(getProtocol("8080")).toBe("tcp");
50-
});
51-
52-
it("returns protocol from string", () => {
39+
it("should return the protocol part of a string with format `port/protocol`", () => {
5340
expect(getProtocol("8080/tcp")).toBe("tcp");
5441
expect(getProtocol("8080/udp")).toBe("udp");
5542
});
5643

57-
it("returns protocol from object", () => {
44+
it("should maintain backwards compatibility when defined as `PortWithBinding` without protocol", () => {
5845
expect(getProtocol({ container: 8080, host: 49000 })).toBe("tcp");
59-
expect(getProtocol({ container: 8080, host: 49000, protocol: "udp" })).toBe("udp");
60-
});
61-
62-
it("handles protocol case-insensitively", () => {
63-
expect(getProtocol({ container: 8080, host: 49000, protocol: "TCP" })).toBe("tcp");
64-
expect(getProtocol({ container: 8080, host: 49000, protocol: "UDP" })).toBe("udp");
65-
expect(getProtocol("8080/TCP")).toBe("tcp");
66-
expect(getProtocol("8080/UDP")).toBe("udp");
6746
});
6847

69-
it("maintains backward compatibility", () => {
70-
expect(getProtocol(8080)).toBe("tcp");
71-
expect(getProtocol("8080")).toBe("tcp");
72-
expect(getProtocol({ container: 8080, host: 49000 })).toBe("tcp");
48+
it("should return the protocol parameter when defined as `PortWithBinding`", () => {
49+
expect(getProtocol({ container: 8080, host: 49000, protocol: "udp" })).toBe("udp");
7350
});
7451
});
7552
});

packages/testcontainers/src/wait-strategies/host-port-wait-strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class HostPortWaitStrategy extends AbstractWaitStrategy {
2323
boundPorts: BoundPorts
2424
): Promise<void> {
2525
for (const [portKey, hostPort] of boundPorts.iterator()) {
26-
if (portKey.toLowerCase().includes("/udp")) {
26+
if (portKey.toLowerCase().endsWith("/udp")) {
2727
log.debug(`Skipping wait for host port ${hostPort} (mapped from UDP port ${portKey})`, {
2828
containerId: container.id,
2929
});
@@ -42,7 +42,7 @@ export class HostPortWaitStrategy extends AbstractWaitStrategy {
4242
boundPorts: BoundPorts
4343
): Promise<void> {
4444
for (const [internalPort] of boundPorts.iterator()) {
45-
if (internalPort.toLowerCase().includes("/udp")) {
45+
if (internalPort.toLowerCase().endsWith("/udp")) {
4646
log.debug(`Skipping wait for internal UDP port ${internalPort}`, {
4747
containerId: container.id,
4848
});

packages/testcontainers/src/wait-strategies/utils/port-check.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class HostPortCheck implements PortCheck {
1111
constructor(private readonly client: ContainerRuntimeClient) {}
1212

1313
public isBound(port: number | string): Promise<boolean> {
14-
if (typeof port === "string" && port.toLowerCase().includes("/udp")) {
14+
if (typeof port === "string" && port.toLowerCase().endsWith("/udp")) {
1515
log.debug(`Skipping host port check for UDP port ${port} (UDP port checks not supported)`);
1616
return Promise.resolve(true);
1717
}

0 commit comments

Comments
 (0)