forked from testcontainers/testcontainers-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-container-reuse.test.ts
194 lines (167 loc) · 6.59 KB
/
generic-container-reuse.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { randomUuid } from "../common/uuid";
import { checkContainerIsHealthy } from "../utils/test-helper";
import { GenericContainer } from "./generic-container";
describe("GenericContainer reuse", { timeout: 180_000 }, () => {
it("should not reuse the container by default", async () => {
const name = `there_can_only_be_one_${randomUuid()}`;
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.start();
await checkContainerIsHealthy(container);
try {
await expect(() =>
new GenericContainer("cristianrgreco/testcontainer:1.1.14").withName(name).withExposedPorts(8080).start()
).rejects.toThrowError();
} finally {
await container.stop();
}
});
it("should not reuse the container even when there is a candidate 1", async () => {
const name = `there_can_only_be_one_${randomUuid()}`;
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container);
try {
await expect(() =>
new GenericContainer("cristianrgreco/testcontainer:1.1.14").withName(name).withExposedPorts(8080).start()
).rejects.toThrowError();
} finally {
await container.stop();
}
});
it("should not reuse the container even when there is a candidate 2", async () => {
const name = `there_can_only_be_one_${randomUuid()}`;
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.start();
await checkContainerIsHealthy(container);
try {
await expect(() =>
new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start()
).rejects.toThrowError();
} finally {
await container.stop();
}
});
it("should not reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to false", async () => {
vi.stubEnv("TESTCONTAINERS_REUSE_ENABLE", "false");
const name = `there_can_only_be_one_${randomUuid()}`;
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container);
try {
await expect(() =>
new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start()
).rejects.toThrowError();
} finally {
await container.stop();
}
});
it.each(["true", undefined])(
"should reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to %s",
async (reuseEnable: string | undefined) => {
vi.stubEnv("TESTCONTAINERS_REUSE_ENABLE", reuseEnable);
const name = `there_can_only_be_one_${randomUuid()}`;
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container1);
const container2 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container2);
expect(container1.getId()).toBe(container2.getId());
await container1.stop();
}
);
it("should create a new container when an existing reusable container has stopped and is removed", async () => {
const name = `there_can_only_be_one_${randomUuid()}`;
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await container1.stop();
const container2 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container2);
expect(container1.getId()).not.toBe(container2.getId());
await container2.stop();
});
it("should reuse stopped container, if configured withAutoRemove(false)", async () => {
const name = `will_stop_and_reuse_again_${randomUuid()}`;
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.withAutoRemove(false)
.start();
await container1.stop({ timeout: 10000 });
const container2 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container2);
expect(container1.getId()).toBe(container2.getId());
await container2.stop({ remove: true });
});
it("should reuse container when an existing reusable container has stopped but not removed", async () => {
const name = `there_can_only_be_one_${randomUuid()}`;
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await container1.stop({ remove: false, timeout: 10000 });
const container2 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(name)
.withExposedPorts(8080)
.withReuse()
.start();
await checkContainerIsHealthy(container2);
expect(container1.getId()).toBe(container2.getId());
await container2.stop();
});
it("should keep the labels passed in when a new reusable container is created", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`there_can_only_be_one_${randomUuid()}`)
.withExposedPorts(8080)
.withLabels({ test: "foo", bar: "baz" })
.withReuse()
.start();
expect(container.getLabels()).toEqual(expect.objectContaining({ test: "foo" }));
await container.stop();
});
it("should not create multiple reusable containers if called in parallel", async () => {
const [container1, container2] = await Promise.all([
new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).withReuse().start(),
new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).withReuse().start(),
]);
expect(container1.getId()).toBe(container2.getId());
await container2.stop();
});
});