|
| 1 | +import { Container, ContainerCreateOptions, ContainerInspectInfo } from "dockerode"; |
| 2 | +import { Readable } from "stream"; |
| 3 | +import { ContainerRuntimeClient } from "../container-runtime"; |
| 4 | +import { LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels"; |
| 5 | +import { WaitStrategy } from "../wait-strategies/wait-strategy"; |
| 6 | +import { GenericContainer } from "./generic-container"; |
| 7 | + |
| 8 | +let mockClient: ContainerRuntimeClient; |
| 9 | +let mockGetReaper = vi.fn(); |
| 10 | + |
| 11 | +vi.mock("../container-runtime", async () => ({ |
| 12 | + ...(await vi.importActual("../container-runtime")), |
| 13 | + getContainerRuntimeClient: vi.fn(async () => mockClient), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("../reaper/reaper", async () => ({ |
| 17 | + ...(await vi.importActual("../reaper/reaper")), |
| 18 | + getReaper: vi.fn(async (client: ContainerRuntimeClient) => await mockGetReaper(client)), |
| 19 | +})); |
| 20 | + |
| 21 | +describe.sequential("GenericContainer auto cleanup", () => { |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + mockGetReaper = vi.fn(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should register the container with the reaper by default", async () => { |
| 28 | + const { client, inspectResult } = createMockContainerRuntimeClient(); |
| 29 | + mockClient = client; |
| 30 | + mockGetReaper.mockResolvedValue({ |
| 31 | + sessionId: "session-id", |
| 32 | + containerId: "reaper-container-id", |
| 33 | + addComposeProject: vi.fn(), |
| 34 | + addSession: vi.fn(), |
| 35 | + }); |
| 36 | + |
| 37 | + const container = await new GenericContainer("alpine").withWaitStrategy(createNoopWaitStrategy()).start(); |
| 38 | + |
| 39 | + expect(mockGetReaper).toHaveBeenCalledWith(client); |
| 40 | + expect(container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID]).toBe("session-id"); |
| 41 | + expect(inspectResult.Config.Labels[LABEL_TESTCONTAINERS_SESSION_ID]).toBe("session-id"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should not register the container with the reaper when auto cleanup is disabled", async () => { |
| 45 | + const { client, inspectResult } = createMockContainerRuntimeClient(); |
| 46 | + mockClient = client; |
| 47 | + |
| 48 | + const container = await new GenericContainer("alpine") |
| 49 | + .withAutoCleanup(false) |
| 50 | + .withWaitStrategy(createNoopWaitStrategy()) |
| 51 | + .start(); |
| 52 | + |
| 53 | + expect(mockGetReaper).not.toHaveBeenCalled(); |
| 54 | + expect(container.getLabels()[LABEL_TESTCONTAINERS_SESSION_ID]).toBeUndefined(); |
| 55 | + expect(inspectResult.Config.Labels[LABEL_TESTCONTAINERS_SESSION_ID]).toBeUndefined(); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +function createMockContainerRuntimeClient(): { |
| 60 | + client: ContainerRuntimeClient; |
| 61 | + inspectResult: ContainerInspectInfo; |
| 62 | +} { |
| 63 | + const inspectResult = { |
| 64 | + Config: { |
| 65 | + Hostname: "mock-hostname", |
| 66 | + Labels: {}, |
| 67 | + }, |
| 68 | + HostConfig: { |
| 69 | + PortBindings: {}, |
| 70 | + }, |
| 71 | + Name: "/mock-container", |
| 72 | + NetworkSettings: { |
| 73 | + Networks: {}, |
| 74 | + Ports: {}, |
| 75 | + }, |
| 76 | + State: { |
| 77 | + FinishedAt: "0001-01-01T00:00:00Z", |
| 78 | + Running: true, |
| 79 | + StartedAt: new Date().toISOString(), |
| 80 | + Status: "running", |
| 81 | + }, |
| 82 | + } as ContainerInspectInfo; |
| 83 | + |
| 84 | + const container = { |
| 85 | + id: "mock-container-id", |
| 86 | + } as Container; |
| 87 | + |
| 88 | + const client = { |
| 89 | + compose: {}, |
| 90 | + container: { |
| 91 | + attach: vi.fn(), |
| 92 | + commit: vi.fn(), |
| 93 | + connectToNetwork: vi.fn(), |
| 94 | + create: vi.fn(async (opts: ContainerCreateOptions) => { |
| 95 | + inspectResult.Config.Labels = opts.Labels ?? {}; |
| 96 | + return container; |
| 97 | + }), |
| 98 | + dockerode: {} as never, |
| 99 | + events: vi.fn(), |
| 100 | + exec: vi.fn(), |
| 101 | + fetchArchive: vi.fn(), |
| 102 | + fetchByLabel: vi.fn(), |
| 103 | + getById: vi.fn(), |
| 104 | + inspect: vi.fn(async () => inspectResult), |
| 105 | + list: vi.fn(), |
| 106 | + logs: vi.fn(async () => Readable.from([])), |
| 107 | + putArchive: vi.fn(), |
| 108 | + remove: vi.fn(), |
| 109 | + restart: vi.fn(), |
| 110 | + start: vi.fn(), |
| 111 | + stop: vi.fn(), |
| 112 | + }, |
| 113 | + image: { |
| 114 | + pull: vi.fn(), |
| 115 | + }, |
| 116 | + info: { |
| 117 | + containerRuntime: { |
| 118 | + host: "localhost", |
| 119 | + hostIps: [{ address: "127.0.0.1", family: 4 }], |
| 120 | + }, |
| 121 | + node: { |
| 122 | + architecture: "x64", |
| 123 | + platform: "linux", |
| 124 | + version: process.version, |
| 125 | + }, |
| 126 | + }, |
| 127 | + network: { |
| 128 | + getById: vi.fn(), |
| 129 | + }, |
| 130 | + } as unknown as ContainerRuntimeClient; |
| 131 | + |
| 132 | + return { client, inspectResult }; |
| 133 | +} |
| 134 | + |
| 135 | +function createNoopWaitStrategy(): WaitStrategy { |
| 136 | + return { |
| 137 | + waitUntilReady: vi.fn(async () => undefined), |
| 138 | + withStartupTimeout: vi.fn().mockReturnThis(), |
| 139 | + } as unknown as WaitStrategy; |
| 140 | +} |
0 commit comments