forked from testcontainers/testcontainers-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric-container.test.ts
More file actions
592 lines (443 loc) · 20.9 KB
/
generic-container.test.ts
File metadata and controls
592 lines (443 loc) · 20.9 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import getPort from "get-port";
import path from "path";
import { RandomUuid } from "../common";
import { getContainerRuntimeClient } from "../container-runtime";
import { PullPolicy } from "../utils/pull-policy";
import {
checkContainerIsHealthy,
getDockerEventStream,
getRunningContainerNames,
getStoppedContainerNames,
waitForDockerEvent,
} from "../utils/test-helper";
import { GenericContainer } from "./generic-container";
describe("GenericContainer", { timeout: 180_000 }, () => {
const fixtures = path.resolve(__dirname, "..", "..", "fixtures", "docker");
it("should return first mapped port", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
expect(container.getFirstMappedPort()).toBe(container.getMappedPort(8080));
await container.stop();
});
it("should bind to specified host port", async () => {
const hostPort = await getPort();
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withExposedPorts({
container: 8080,
host: hostPort,
})
.start();
await checkContainerIsHealthy(container);
expect(container.getMappedPort(8080)).toBe(hostPort);
await container.stop();
});
it("should execute a command on a running container", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
const { output, stdout, stderr, exitCode } = await container.exec(["echo", "hello", "world"]);
expect(exitCode).toBe(0);
expect(stdout).toEqual(expect.stringContaining("hello world"));
expect(stderr).toBe("");
expect(output).toEqual(stdout);
await container.stop();
});
it("should execute a command in a different working directory", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
const { output, stdout, stderr, exitCode } = await container.exec(["pwd"], { workingDir: "/var/log" });
expect(exitCode).toBe(0);
expect(stdout).toEqual(expect.stringContaining("/var/log"));
expect(stderr).toBe("");
expect(output).toEqual(stdout);
await container.stop();
});
it("should execute a command with custom environment variables", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
const { output, stdout, stderr, exitCode } = await container.exec(["env"], { env: { TEST_ENV: "test" } });
expect(exitCode).toBe(0);
expect(stdout).toEqual(expect.stringContaining("TEST_ENV=test"));
expect(stderr).toBe("");
expect(output).toEqual(stdout);
await container.stop();
});
it("should execute a command with a different user", async () => {
// By default, node:alpine runs as root
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
const { output, stdout, stderr, exitCode } = await container.exec(["whoami"], { user: "node" });
expect(exitCode).toBe(0);
expect(stdout).toEqual(expect.stringContaining("node"));
expect(stderr).toBe("");
expect(output).toEqual(stdout);
await container.stop();
});
it("should capture stderr when a command fails", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
const { output, stdout, stderr, exitCode } = await container.exec(["ls", "/nonexistent/path"]);
expect(exitCode).not.toBe(0);
expect(stdout).toBe("");
expect(stderr).toEqual(expect.stringContaining("No such file or directory"));
expect(output).toEqual(stderr);
await container.stop();
});
it("should capture stdout and stderr in the correct order", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
// The command first writes to stdout and then tries to access a nonexistent file (stderr)
const { output, stdout, stderr, exitCode } = await container.exec([
"sh",
"-c",
"echo 'This is stdout'; ls /nonexistent/path",
]);
expect(exitCode).not.toBe(0); // The command should fail due to the ls error
expect(stdout).toEqual(expect.stringContaining("This is stdout"));
expect(stderr).toEqual(expect.stringContaining("No such file or directory"));
expect(output).toEqual(expect.stringContaining("This is stdout"));
expect(output).toEqual(expect.stringContaining("No such file or directory"));
await container.stop();
});
it("should set environment variables", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withEnvironment({ customKey: "customValue" })
.withExposedPorts(8080)
.start();
const url = `http://${container.getHost()}:${container.getMappedPort(8080)}`;
const response = await fetch(`${url}/env`);
const responseBody = (await response.json()) as { [key: string]: string };
expect(responseBody.customKey).toBe("customValue");
await container.stop();
});
it("should set command", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCommand(["node", "index.js", "one", "two", "three"])
.withExposedPorts(8080)
.start();
const url = `http://${container.getHost()}:${container.getMappedPort(8080)}`;
const response = await fetch(`${url}/cmd`);
const responseBody = await response.json();
expect(responseBody).toEqual(["/usr/local/bin/node", "/index.js", "one", "two", "three"]);
await container.stop();
});
it("should set working directory", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withWorkingDir("/tmp")
.withCommand(["node", "../index.js"])
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["pwd"]);
expect(output).toEqual(expect.stringContaining("/tmp"));
});
it("should set platform", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withPullPolicy(PullPolicy.alwaysPull())
.withCommand(["node", "../index.js"])
.withPlatform("linux/amd64")
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["arch"]);
expect(output).toEqual(expect.stringContaining("x86_64"));
});
it("should set entrypoint", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withEntrypoint(["node"])
.withCommand(["index.js"])
.withExposedPorts(8080)
.start();
await checkContainerIsHealthy(container);
await container.stop();
});
it("should set name", async () => {
const containerName = "special-test-container";
const expectedContainerName = "/special-test-container";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withName(containerName).start();
expect(container.getName()).toEqual(expectedContainerName);
await container.stop();
});
it("should set labels", async () => {
const labels = {
["label-1"]: "value-1",
["label-2"]: "value-2",
};
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withLabels(labels).start();
expect(container.getLabels()).toMatchObject(labels);
await container.stop();
});
it("should set bind mounts", async () => {
const filename = "test.txt";
const source = path.resolve(fixtures, "docker", filename);
const target = `/tmp/${filename}`;
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withBindMounts([{ source, target }])
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["cat", target]);
expect(output).toContain("hello world");
await container.stop();
});
it("should set tmpfs", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withTmpFs({ "/testtmpfs": "rw" })
.withExposedPorts(8080)
.start();
const tmpFsFile = "/testtmpfs/test.file";
const { exitCode: exitCode1 } = await container.exec(["ls", tmpFsFile]);
expect(exitCode1).toBe(1);
await container.exec(["touch", tmpFsFile]);
const { exitCode: exitCode2 } = await container.exec(["ls", tmpFsFile]);
expect(exitCode2).toBe(0);
await container.stop();
});
if (!process.env["CI_ROOTLESS"]) {
it("should set ulimits", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withUlimits({ memlock: { hard: -1, soft: -1 } })
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["sh", "-c", "ulimit -l"]);
expect(output.trim()).toBe("unlimited");
await container.stop();
});
}
it("should add capabilities", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withAddedCapabilities("IPC_LOCK")
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["sh", "-c", "getpcaps 1 2>&1"]);
expect(output).toContain("cap_ipc_lock");
await container.stop();
});
it("should drop capabilities", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withDroppedCapabilities("CHOWN")
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["sh", "-c", "getpcaps 1 2>&1"]);
expect(output).not.toContain("cap_chown");
await container.stop();
});
it("should set default log driver", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withDefaultLogDriver().start();
const client = await getContainerRuntimeClient();
const dockerContainer = client.container.getById(container.getId());
const containerInfo = await dockerContainer.inspect();
expect(containerInfo.HostConfig.LogConfig).toEqual(
expect.objectContaining({
Type: "json-file",
})
);
await container.stop();
});
it("should set privileged mode", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withPrivilegedMode()
.withExposedPorts(8080)
.start();
const client = await getContainerRuntimeClient();
const dockerContainer = client.container.getById(container.getId());
const containerInfo = await dockerContainer.inspect();
expect(containerInfo.HostConfig.Privileged).toBe(true);
await checkContainerIsHealthy(container);
await container.stop();
});
it("should use pull policy", async () => {
const container = new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080);
const startedContainer1 = await container.start();
const dockerEventStream = await getDockerEventStream();
const dockerPullEventPromise = waitForDockerEvent(dockerEventStream, "pull");
const startedContainer2 = await container.withPullPolicy(PullPolicy.alwaysPull()).start();
await dockerPullEventPromise;
dockerEventStream.destroy();
await startedContainer1.stop();
await startedContainer2.stop();
});
it("should set the IPC mode", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withIpcMode("host")
.withExposedPorts(8080)
.start();
await checkContainerIsHealthy(container);
await container.stop();
});
it("should set the user", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withUser("node")
.withExposedPorts(8080)
.start();
const { output } = await container.exec(["whoami"]);
expect(output).toEqual(expect.stringContaining("node"));
await container.stop();
});
it("should copy file to container", async () => {
const source = path.resolve(fixtures, "docker", "test.txt");
const target = "/tmp/test.txt";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyFilesToContainer([{ source, target }])
.withExposedPorts(8080)
.start();
expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining("hello world"));
await container.stop();
});
it("should copy file to container with permissions", async () => {
const source = path.resolve(fixtures, "docker", "test.txt");
const target = "/tmp/test.txt";
const mode = parseInt("0777", 8);
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyFilesToContainer([{ source, target, mode }])
.withExposedPorts(8080)
.start();
expect((await container.exec(`stat -c "%a %n" ${target}`)).output).toContain("777");
await container.stop();
});
it("should copy file to started container", async () => {
const source = path.resolve(fixtures, "docker", "test.txt");
const target = "/tmp/test.txt";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
await container.copyFilesToContainer([{ source, target }]);
expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining("hello world"));
await container.stop();
});
it("should copy directory to container", async () => {
const source = path.resolve(fixtures, "docker");
const target = "/tmp";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyDirectoriesToContainer([{ source, target }])
.withExposedPorts(8080)
.start();
expect((await container.exec("cat /tmp/test.txt")).output).toEqual(expect.stringContaining("hello world"));
await container.stop();
});
it("should copy directory to container with permissions", async () => {
const source = path.resolve(fixtures, "docker");
const target = "/tmp/newdir";
const mode = parseInt("0777", 8);
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyDirectoriesToContainer([{ source, target, mode }])
.withExposedPorts(8080)
.start();
expect((await container.exec(`stat -c "%a %n" /tmp/newdir/test.txt`)).output).toContain("777");
await container.stop();
});
it("should copy directory to started container", async () => {
const source = path.resolve(fixtures, "docker");
const target = "/tmp";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
await container.copyDirectoriesToContainer([{ source, target }]);
expect((await container.exec("cat /tmp/test.txt")).output).toEqual(expect.stringContaining("hello world"));
await container.stop();
});
it("should copy content to container", async () => {
const content = "hello world";
const target = "/tmp/test.txt";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyContentToContainer([{ content, target }])
.withExposedPorts(8080)
.start();
expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining(content));
await container.stop();
});
it("should copy content to container with permissions", async () => {
const content = "hello world";
const target = "/tmp/test.txt";
const mode = parseInt("0777", 8);
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withCopyContentToContainer([{ content, target, mode }])
.withExposedPorts(8080)
.start();
expect((await container.exec(`stat -c "%a %n" ${target}`)).output).toContain("777");
await container.stop();
});
it("should copy content to started container", async () => {
const content = "hello world";
const target = "/tmp/test.txt";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();
await container.copyContentToContainer([{ content, target }]);
expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining(content));
await container.stop();
});
it("should honour .dockerignore file", async () => {
const context = path.resolve(fixtures, "docker-with-dockerignore");
const container = await GenericContainer.fromDockerfile(context).build();
const startedContainer = await container.withExposedPorts(8080).start();
const { output } = await startedContainer.exec(["find"]);
expect(output).toContain("exist1.txt");
expect(output).toContain("exist2.txt");
expect(output).toContain("exist7.txt");
expect(output).not.toContain("example1.txt");
expect(output).not.toContain("example2.txt");
expect(output).not.toContain("example3.txt");
expect(output).not.toContain("example4.txt");
expect(output).not.toContain("example5.txt");
expect(output).not.toContain("example6.txt");
expect(output).not.toContain("example7.txt");
expect(output).not.toContain("Dockerfile");
await startedContainer.stop();
});
it("should stop the container", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`container-${new RandomUuid().nextUuid()}`)
.start();
await container.stop();
expect(await getRunningContainerNames()).not.toContain(container.getName());
});
it("should stop the container and NOT remove it if withRemove(false) set", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`container-${new RandomUuid().nextUuid()}`)
.withRemove(false)
.start();
await container.stop();
const containerName = container.getName()?.slice(1); // remove first '/'
expect(await getStoppedContainerNames()).toContain(containerName);
// ryuk will clean up this container after test anyway
// unless env var TESTCONTAINERS_RYUK_DISABLED=true is set
});
it("should stop the container and remove it if withRemove(true) set", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`container-${new RandomUuid().nextUuid()}`)
.withRemove(true)
.start();
await container.stop();
const containerName = container.getName()?.slice(1); // remove first '/'
expect(await getStoppedContainerNames()).not.toContain(containerName);
});
it("should stop the container and remove it when explicitly overriding withRemove(false)", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`container-${new RandomUuid().nextUuid()}`)
.withRemove(false)
.start();
await container.stop({ remove: true }); // remove: true here should override withRemove(false)
const containerName = container.getName()?.slice(1); // remove first '/'
expect(await getStoppedContainerNames()).not.toContain(containerName);
});
it("should stop the container idempotently", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withName(`container-${new RandomUuid().nextUuid()}`)
.start();
const stopContainerPromises = Promise.all(
Array(5)
.fill(0)
.map(() => container.stop())
);
await expect(stopContainerPromises).resolves.not.toThrow();
expect(await getRunningContainerNames()).not.toContain(container.getName());
});
it("should build a target stage", async () => {
const context = path.resolve(fixtures, "docker-multi-stage");
const firstContainer = await GenericContainer.fromDockerfile(context).withTarget("first").build();
const secondContainer = await GenericContainer.fromDockerfile(context).withTarget("second").build();
const firstStartedContainer = await firstContainer.start();
const secondStartedContainer = await secondContainer.start();
expect(firstStartedContainer.getLabels().stage).toEqual("first");
expect(secondStartedContainer.getLabels().stage).toEqual("second");
await firstStartedContainer.stop();
await secondStartedContainer.stop();
});
it("should set the hostname", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withHostname("hostname")
.start();
expect(container.getHostname()).toEqual("hostname");
await container.stop();
});
// failing to build an image hangs within the DockerImageClient.build method,
// that change might be larger so leave it out of this commit but skip the failing test
it.skip("should throw an error for a target stage that does not exist", async () => {
const context = path.resolve(fixtures, "docker-multi-stage");
await GenericContainer.fromDockerfile(context).withTarget("invalid").build();
});
});