|
1 | 1 | import { ContainerInspectInfo } from "dockerode"; |
2 | | -import { mapInspectResult } from "../utils/map-inspect-result"; |
3 | 2 | import { inspectContainerUntilPortsExposed } from "./inspect-container-util-ports-exposed"; |
4 | 3 |
|
5 | | -function mockInspectResult(ports: ContainerInspectInfo["NetworkSettings"]["Ports"]) { |
6 | | - const date = new Date(); |
7 | | - |
8 | | - const inspectResult: ContainerInspectInfo = { |
9 | | - Name: "container-id", |
10 | | - Config: { |
11 | | - Hostname: "hostname", |
12 | | - Labels: {}, |
13 | | - }, |
14 | | - State: { |
15 | | - Health: { |
16 | | - Status: "healthy", |
17 | | - }, |
18 | | - Status: "running", |
19 | | - Running: true, |
20 | | - StartedAt: date.toISOString(), |
21 | | - FinishedAt: date.toISOString(), |
| 4 | +function mockInspectResult( |
| 5 | + portBindings: ContainerInspectInfo["HostConfig"]["PortBindings"], |
| 6 | + ports: ContainerInspectInfo["NetworkSettings"]["Ports"] |
| 7 | +): ContainerInspectInfo { |
| 8 | + return { |
| 9 | + HostConfig: { |
| 10 | + PortBindings: portBindings, |
22 | 11 | }, |
23 | 12 | NetworkSettings: { |
24 | 13 | Ports: ports, |
25 | | - Networks: {}, |
26 | 14 | }, |
27 | | - } as unknown as ContainerInspectInfo; |
28 | | - |
29 | | - return { inspectResult, mappedInspectResult: mapInspectResult(inspectResult) }; |
| 15 | + } as ContainerInspectInfo; |
30 | 16 | } |
31 | 17 |
|
32 | | -test("returns the inspect results when all ports are exposed", async () => { |
33 | | - const data = mockInspectResult({ "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] }); |
34 | | - const inspectFn = vi.fn().mockResolvedValueOnce(data.inspectResult); |
| 18 | +describe.sequential("inspectContainerUntilPortsExposed", () => { |
| 19 | + it("returns the inspect result when all ports are exposed", async () => { |
| 20 | + const data = mockInspectResult({ "8080/tcp": [] }, { "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] }); |
| 21 | + const inspectFn = vi.fn().mockResolvedValueOnce(data); |
35 | 22 |
|
36 | | - const result = await inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id"); |
| 23 | + const result = await inspectContainerUntilPortsExposed(inspectFn, "container-id"); |
37 | 24 |
|
38 | | - expect(result).toEqual(data); |
39 | | -}); |
| 25 | + expect(result).toEqual(data); |
| 26 | + }); |
40 | 27 |
|
41 | | -test("retries the inspect if ports are not yet exposed", async () => { |
42 | | - const data1 = mockInspectResult({ "8080/tcp": [] }); |
43 | | - const data2 = mockInspectResult({ "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] }); |
44 | | - const inspectFn = vi |
45 | | - .fn() |
46 | | - .mockResolvedValueOnce(data1.inspectResult) |
47 | | - .mockResolvedValueOnce(data1.inspectResult) |
48 | | - .mockResolvedValueOnce(data2.inspectResult); |
| 28 | + it("returns the inspect result when no ports are exposed", async () => { |
| 29 | + const data = mockInspectResult({}, {}); |
| 30 | + const inspectFn = vi.fn().mockResolvedValueOnce(data); |
49 | 31 |
|
50 | | - const result = await inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id"); |
| 32 | + const result = await inspectContainerUntilPortsExposed(inspectFn, "container-id"); |
51 | 33 |
|
52 | | - expect(result).toEqual(data2); |
53 | | - expect(inspectFn).toHaveBeenCalledTimes(3); |
54 | | -}); |
| 34 | + expect(result).toEqual(data); |
| 35 | + }); |
55 | 36 |
|
56 | | -test("throws an error when host ports are not exposed within timeout", async () => { |
57 | | - const data = mockInspectResult({ "8080/tcp": [] }); |
58 | | - const inspectFn = vi.fn().mockResolvedValue(data.inspectResult); |
| 37 | + it("returns the inspect result if host config port bindings are null", async () => { |
| 38 | + const data = mockInspectResult(null, {}); |
| 39 | + const inspectFn = vi.fn().mockResolvedValueOnce(data); |
59 | 40 |
|
60 | | - await expect(inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id", 0)).rejects.toThrow( |
61 | | - "Container did not expose all ports after starting" |
62 | | - ); |
63 | | -}); |
| 41 | + const result = await inspectContainerUntilPortsExposed(inspectFn, "container-id"); |
| 42 | + |
| 43 | + expect(result).toEqual(data); |
| 44 | + }); |
| 45 | + |
| 46 | + it("retries the inspect if ports are not yet exposed", async () => { |
| 47 | + const data1 = mockInspectResult({ "8080/tcp": [] }, { "8080/tcp": [] }); |
| 48 | + const data2 = mockInspectResult({ "8080/tcp": [] }, { "8080/tcp": [{ HostIp: "0.0.0.0", HostPort: "45000" }] }); |
| 49 | + const inspectFn = vi.fn().mockResolvedValueOnce(data1).mockResolvedValueOnce(data1).mockResolvedValueOnce(data2); |
| 50 | + |
| 51 | + const result = await inspectContainerUntilPortsExposed(inspectFn, "container-id"); |
| 52 | + |
| 53 | + expect(result).toEqual(data2); |
| 54 | + expect(inspectFn).toHaveBeenCalledTimes(3); |
| 55 | + }); |
| 56 | + |
| 57 | + it("throws an error when host ports are not exposed within timeout", async () => { |
| 58 | + const data = mockInspectResult({ "8080/tcp": [] }, { "8080/tcp": [] }); |
| 59 | + const inspectFn = vi.fn().mockResolvedValue(data); |
| 60 | + |
| 61 | + await expect(inspectContainerUntilPortsExposed(inspectFn, "container-id", 0)).rejects.toThrow( |
| 62 | + "Timed out after 0ms while waiting for container ports to be bound to the host" |
| 63 | + ); |
| 64 | + }); |
64 | 65 |
|
65 | | -test("throws an error when container ports not exposed within timeout", async () => { |
66 | | - const data = mockInspectResult({}); |
67 | | - const inspectFn = vi.fn().mockResolvedValue(data.inspectResult); |
| 66 | + it("throws an error when container ports not exposed within timeout", async () => { |
| 67 | + const data = mockInspectResult({ "8080/tcp": [] }, {}); |
| 68 | + const inspectFn = vi.fn().mockResolvedValue(data); |
68 | 69 |
|
69 | | - await expect(inspectContainerUntilPortsExposed(inspectFn, [8080], "container-id", 0)).rejects.toThrow( |
70 | | - "Container did not expose all ports after starting" |
71 | | - ); |
| 70 | + await expect(inspectContainerUntilPortsExposed(inspectFn, "container-id", 0)).rejects.toThrow( |
| 71 | + "Timed out after 0ms while waiting for container ports to be bound to the host" |
| 72 | + ); |
| 73 | + }); |
72 | 74 | }); |
0 commit comments