|
1 | | -import { createServer, Server } from "http"; |
| 1 | +import { Server } from "http"; |
2 | 2 | import { GenericContainer } from "../generic-container/generic-container"; |
3 | 3 | import { Network } from "../network/network"; |
4 | 4 | import { TestContainers } from "../test-containers"; |
5 | 5 | import { RandomUniquePortGenerator } from "../utils/port-generator"; |
| 6 | +import { createTestServer } from "../utils/test-helper"; |
6 | 7 |
|
7 | 8 | describe("PortForwarder", { timeout: 180_000 }, () => { |
8 | 9 | let randomPort: number; |
9 | 10 | let server: Server; |
10 | 11 |
|
11 | | - afterEach(() => { |
12 | | - server.close(); |
| 12 | + beforeEach(async () => { |
| 13 | + randomPort = await new RandomUniquePortGenerator().generatePort(); |
| 14 | + server = await createTestServer(randomPort); |
13 | 15 | }); |
14 | 16 |
|
15 | | - describe("Behaviour", () => { |
16 | | - beforeEach(async () => { |
17 | | - randomPort = await new RandomUniquePortGenerator().generatePort(); |
18 | | - server = await createTestServer(randomPort); |
19 | | - }); |
20 | | - |
21 | | - it("should expose host ports to the container", async () => { |
22 | | - await TestContainers.exposeHostPorts(randomPort); |
23 | | - |
24 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").start(); |
25 | | - |
26 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
27 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
28 | | - |
29 | | - await container.stop(); |
30 | | - }); |
31 | | - |
32 | | - it("should expose host ports to the container with custom network", async () => { |
33 | | - await TestContainers.exposeHostPorts(randomPort); |
34 | | - |
35 | | - const network = await new Network().start(); |
36 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withNetwork(network).start(); |
37 | | - |
38 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
39 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
40 | | - |
41 | | - await container.stop(); |
42 | | - await network.stop(); |
43 | | - }); |
44 | | - |
45 | | - it("should expose host ports to the container with custom network and network alias", async () => { |
46 | | - await TestContainers.exposeHostPorts(randomPort); |
47 | | - |
48 | | - const network = await new Network().start(); |
49 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") |
50 | | - .withNetwork(network) |
51 | | - .withNetworkAliases("foo") |
52 | | - .start(); |
53 | | - |
54 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
55 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
56 | | - |
57 | | - await container.stop(); |
58 | | - await network.stop(); |
59 | | - }); |
| 17 | + afterEach(async () => { |
| 18 | + await new Promise((resolve) => server.close(resolve)); |
60 | 19 | }); |
61 | 20 |
|
62 | | - describe("Reuse", () => { |
63 | | - afterEach(() => { |
64 | | - vi.resetModules(); |
65 | | - }); |
| 21 | + it("should expose host ports to the container", async () => { |
| 22 | + await TestContainers.exposeHostPorts(randomPort); |
66 | 23 |
|
67 | | - describe("Different host ports", () => { |
68 | | - beforeEach(async () => { |
69 | | - randomPort = await new RandomUniquePortGenerator().generatePort(); |
70 | | - server = await createTestServer(randomPort); |
71 | | - }); |
| 24 | + const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").start(); |
72 | 25 |
|
73 | | - it("1", async () => { |
74 | | - const { TestContainers } = await import("../test-containers"); |
75 | | - await TestContainers.exposeHostPorts(randomPort); |
| 26 | + const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
| 27 | + expect(output).toEqual(expect.stringContaining("hello world")); |
76 | 28 |
|
77 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").start(); |
78 | | - |
79 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
80 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
81 | | - |
82 | | - await container.stop(); |
83 | | - }); |
84 | | - |
85 | | - it("2", async () => { |
86 | | - const { TestContainers } = await import("../test-containers"); |
87 | | - await TestContainers.exposeHostPorts(randomPort); |
88 | | - |
89 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").start(); |
90 | | - |
91 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
92 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
93 | | - |
94 | | - await container.stop(); |
95 | | - }); |
96 | | - }); |
97 | | - |
98 | | - describe("Same host ports", () => { |
99 | | - beforeAll(async () => { |
100 | | - randomPort = await new RandomUniquePortGenerator().generatePort(); |
101 | | - }); |
102 | | - |
103 | | - beforeEach(async () => { |
104 | | - server = await createTestServer(randomPort); |
105 | | - }); |
| 29 | + await container.stop(); |
| 30 | + }); |
106 | 31 |
|
107 | | - it("1", async () => { |
108 | | - const { TestContainers } = await import("../test-containers"); |
109 | | - await TestContainers.exposeHostPorts(randomPort); |
| 32 | + it("should expose host ports to the container with custom network", async () => { |
| 33 | + await TestContainers.exposeHostPorts(randomPort); |
110 | 34 |
|
111 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").start(); |
| 35 | + const network = await new Network().start(); |
| 36 | + const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withNetwork(network).start(); |
112 | 37 |
|
113 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
114 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
| 38 | + const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
| 39 | + expect(output).toEqual(expect.stringContaining("hello world")); |
115 | 40 |
|
116 | | - await container.stop(); |
117 | | - }); |
| 41 | + await container.stop(); |
| 42 | + await network.stop(); |
| 43 | + }); |
118 | 44 |
|
119 | | - it("2", async () => { |
120 | | - const { TestContainers } = await import("../test-containers"); |
121 | | - await TestContainers.exposeHostPorts(randomPort); |
| 45 | + it("should expose host ports to the container with custom network and network alias", async () => { |
| 46 | + await TestContainers.exposeHostPorts(randomPort); |
122 | 47 |
|
123 | | - const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").start(); |
| 48 | + const network = await new Network().start(); |
| 49 | + const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") |
| 50 | + .withNetwork(network) |
| 51 | + .withNetworkAliases("foo") |
| 52 | + .start(); |
124 | 53 |
|
125 | | - const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
126 | | - expect(output).toEqual(expect.stringContaining("hello world")); |
| 54 | + const { output } = await container.exec(["curl", "-s", `http://host.testcontainers.internal:${randomPort}`]); |
| 55 | + expect(output).toEqual(expect.stringContaining("hello world")); |
127 | 56 |
|
128 | | - await container.stop(); |
129 | | - }); |
130 | | - }); |
| 57 | + await container.stop(); |
| 58 | + await network.stop(); |
131 | 59 | }); |
132 | 60 | }); |
133 | | - |
134 | | -async function createTestServer(port: number): Promise<Server> { |
135 | | - const server = createServer((req, res) => { |
136 | | - res.writeHead(200); |
137 | | - res.end("hello world"); |
138 | | - }); |
139 | | - await new Promise<void>((resolve) => server.listen(port, resolve)); |
140 | | - return server; |
141 | | -} |
0 commit comments