Skip to content

Commit 9dead6e

Browse files
Enable/disable container reuse with an environment variable (#908)
1 parent 1207f5d commit 9dead6e

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Configuration of Testcontainers and its behaviours:
4343
| TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX | mycompany.com/registry | Set default image registry |
4444
| RYUK_CONTAINER_IMAGE | testcontainers/ryuk:0.5.1 | Custom image for ryuk |
4545
| SSHD_CONTAINER_IMAGE | testcontainers/sshd:1.1.0 | Custom image for SSHd |
46+
| TESTCONTAINERS_REUSE_ENABLE | true | Enable reusable containers |

docs/features/containers.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ const container2 = await new GenericContainer("alpine")
377377
expect(container1.getId()).toBe(container2.getId());
378378
```
379379

380+
Container re-use can be enabled or disabled globally by setting the `TESTCONTAINERS_REUSE_ENABLE` environment variable to `true` or `false`.
381+
If this environment variable is not declared, the feature is enabled by default.
382+
380383
## Creating a custom container
381384

382385
You can create your own Generic Container as follows:

packages/testcontainers/src/generic-container/generic-container-reuse.test.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { checkContainerIsHealthy } from "../utils/test-helper";
44
describe("GenericContainer reuse", () => {
55
jest.setTimeout(180_000);
66

7+
afterEach(() => {
8+
process.env.TESTCONTAINERS_REUSE_ENABLE = undefined;
9+
});
10+
711
it("should not reuse the container by default", async () => {
812
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
913
.withName("there_can_only_be_one")
@@ -63,26 +67,54 @@ describe("GenericContainer reuse", () => {
6367
}
6468
});
6569

66-
it("should reuse the container", async () => {
67-
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
68-
.withName("there_can_only_be_one")
69-
.withExposedPorts(8080)
70-
.withReuse()
71-
.start();
72-
await checkContainerIsHealthy(container1);
70+
it("should not reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to false", async () => {
71+
process.env.TESTCONTAINERS_REUSE_ENABLE = "false";
7372

74-
const container2 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
73+
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
7574
.withName("there_can_only_be_one")
7675
.withExposedPorts(8080)
7776
.withReuse()
7877
.start();
79-
await checkContainerIsHealthy(container2);
80-
81-
expect(container1.getId()).toBe(container2.getId());
78+
await checkContainerIsHealthy(container);
8279

83-
await container1.stop();
80+
try {
81+
await expect(() =>
82+
new GenericContainer("cristianrgreco/testcontainer:1.1.14")
83+
.withName("there_can_only_be_one")
84+
.withExposedPorts(8080)
85+
.withReuse()
86+
.start()
87+
).rejects.toThrowError();
88+
} finally {
89+
await container.stop();
90+
}
8491
});
8592

93+
it.each(["true", undefined])(
94+
"should reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to %s",
95+
async (reuseEnable: string | undefined) => {
96+
process.env.TESTCONTAINERS_REUSE_ENABLE = reuseEnable;
97+
98+
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
99+
.withName("there_can_only_be_one")
100+
.withExposedPorts(8080)
101+
.withReuse()
102+
.start();
103+
await checkContainerIsHealthy(container1);
104+
105+
const container2 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
106+
.withName("there_can_only_be_one")
107+
.withExposedPorts(8080)
108+
.withReuse()
109+
.start();
110+
await checkContainerIsHealthy(container2);
111+
112+
expect(container1.getId()).toBe(container2.getId());
113+
114+
await container1.stop();
115+
}
116+
);
117+
86118
it("should create a new container when an existing reusable container has stopped and is removed", async () => {
87119
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
88120
.withName("there_can_only_be_one")

packages/testcontainers/src/generic-container/generic-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class GenericContainer implements TestContainer {
100100

101101
this.createOpts.Labels = { ...createLabels(), ...this.createOpts.Labels };
102102

103-
if (this.reuse) {
103+
if (process.env.TESTCONTAINERS_REUSE_ENABLE !== "false" && this.reuse) {
104104
return this.reuseOrStartContainer(client);
105105
}
106106

0 commit comments

Comments
 (0)