Skip to content

Commit 29f2220

Browse files
Add additional reaper tests
1 parent ea80400 commit 29f2220

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/testcontainers/src/reaper/reaper.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ContainerRuntimeClient, getContainerRuntimeClient } from "../container-runtime";
2+
import { RandomUniquePortGenerator } from "../utils/port-generator";
23

34
describe("Reaper", { timeout: 120_000 }, () => {
45
let client: ContainerRuntimeClient;
@@ -12,6 +13,42 @@ describe("Reaper", { timeout: 120_000 }, () => {
1213
client = await getContainerRuntimeClient();
1314
});
1415

16+
it("should create disabled reaper when TESTCONTAINERS_RYUK_DISABLED=true", async () => {
17+
vitest.stubEnv("TESTCONTAINERS_RYUK_DISABLED", "true");
18+
19+
vi.spyOn(client.container, "list").mockResolvedValue([]);
20+
const reaper = await getReaper();
21+
22+
// DisabledReaper has an empty containerId
23+
expect(reaper.containerId).toBe("");
24+
25+
// Should not throw exceptions when methods are called
26+
expect(() => reaper.addSession("test-session")).not.toThrow();
27+
expect(() => reaper.addComposeProject("test-project")).not.toThrow();
28+
});
29+
30+
it("should use custom port when TESTCONTAINERS_RYUK_PORT is set", async () => {
31+
const customPort = (await new RandomUniquePortGenerator().generatePort()).toString();
32+
vitest.stubEnv("TESTCONTAINERS_RYUK_PORT", customPort);
33+
34+
vi.spyOn(client.container, "list").mockResolvedValue([]);
35+
const reaper = await getReaper();
36+
37+
const reaperContainer = client.container.getById(reaper.containerId);
38+
const ports = (await reaperContainer.inspect()).HostConfig.PortBindings;
39+
expect(ports["8080"][0].HostPort).toBe(customPort);
40+
});
41+
42+
it("should reuse existing reaper container if one is already running", async () => {
43+
const reaper = await getReaper();
44+
const reaperContainerInfo = (await client.container.list()).filter((c) => c.Id === reaper.containerId)[0];
45+
vi.spyOn(client.container, "list").mockResolvedValue([reaperContainerInfo]);
46+
47+
const reaper2 = await getReaper();
48+
49+
expect(reaper2.containerId).toBe(reaper.containerId);
50+
});
51+
1552
it("should create Reaper container without RYUK_VERBOSE env var by default", async () => {
1653
vi.spyOn(client.container, "list").mockResolvedValue([]);
1754
const reaper = await getReaper();

0 commit comments

Comments
 (0)