Skip to content

Commit d328536

Browse files
committed
Add SocatContainer
1 parent b92f669 commit d328536

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { fetch } from "undici";
2+
import { GenericContainer } from "../generic-container/generic-container";
3+
import { Network } from "../network/network";
4+
import { SocatContainer } from "./socat-container";
5+
6+
describe("Socat", { timeout: 120_000 }, () => {
7+
it("should forward requests to helloworld container", async () => {
8+
const network = await new Network().start();
9+
10+
const helloworld = await new GenericContainer("testcontainers/helloworld:1.2.0")
11+
.withExposedPorts(8080)
12+
.withNetwork(network)
13+
.withNetworkAliases("helloworld")
14+
.start();
15+
16+
const socat = await new SocatContainer().withNetwork(network).withTarget(8080, "helloworld").start();
17+
18+
const socatUrl = `http://${socat.getHost()}:${socat.getMappedPort(8080)}`;
19+
20+
const response = await fetch(`${socatUrl}/ping`);
21+
22+
expect(response.status).toBe(200);
23+
expect(await response.text()).toBe("PONG");
24+
25+
await socat.stop();
26+
await helloworld.stop();
27+
await network.stop();
28+
});
29+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { RandomUuid } from "../common";
2+
import { AbstractStartedContainer } from "../generic-container/abstract-started-container";
3+
import { GenericContainer } from "../generic-container/generic-container";
4+
import { StartedTestContainer } from "../test-container";
5+
6+
export class SocatContainer extends GenericContainer {
7+
private targets: { [key in number]: string } = {};
8+
9+
constructor(image = "alpine/socat:1.7.4.3-r0") {
10+
super(image);
11+
this.withEntrypoint(["/bin/sh"]);
12+
this.withName(`testcontainers-socat-${new RandomUuid().nextUuid()}`);
13+
}
14+
15+
public withTarget(exposePort: number, host: string): this;
16+
public withTarget(exposePort: number, host: string, internalPort: number): this;
17+
public withTarget(exposePort: number, host: string, internalPort?: number): this {
18+
this.withExposedPorts(exposePort);
19+
if (internalPort == null) {
20+
internalPort = exposePort;
21+
}
22+
this.targets[exposePort] = `${host}:${internalPort}`;
23+
return this;
24+
}
25+
26+
public override async start(): Promise<StartedSocatContainer> {
27+
const command = Object.entries(this.targets)
28+
.map(([exposePort, target]) => `socat TCP-LISTEN:${exposePort},fork,reuseaddr TCP:${target}`)
29+
.join(" & ");
30+
31+
this.withCommand(["-c", command]);
32+
return new StartedSocatContainer(await super.start());
33+
}
34+
}
35+
36+
export class StartedSocatContainer extends AbstractStartedContainer {
37+
constructor(startedTestcontainers: StartedTestContainer) {
38+
super(startedTestcontainers);
39+
}
40+
}

0 commit comments

Comments
 (0)