testcontainers 7.22.0 → 7.23.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/dist/docker-compose/default-docker-compose-options.js +2 -2
- package/dist/docker-compose/docker-compose-options.d.ts +2 -0
- package/dist/docker-compose/functions/container-name-resolver.d.ts +1 -1
- package/dist/docker-compose/functions/container-name-resolver.js +4 -5
- package/dist/docker-compose-environment/docker-compose-environment.d.ts +7 -2
- package/dist/docker-compose-environment/docker-compose-environment.js +28 -10
- package/dist/reaper.d.ts +1 -0
- package/dist/reaper.js +6 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -523,6 +523,16 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF
|
|
|
523
523
|
.up();
|
|
524
524
|
```
|
|
525
525
|
|
|
526
|
+
Specify [profiles](https://docs.docker.com/compose/profiles/):
|
|
527
|
+
|
|
528
|
+
```javascript
|
|
529
|
+
const { DockerComposeEnvironment } = require("testcontainers");
|
|
530
|
+
|
|
531
|
+
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
|
|
532
|
+
.withProfiles("profile1", "profile2")
|
|
533
|
+
.up();
|
|
534
|
+
```
|
|
535
|
+
|
|
526
536
|
Specify not to re-create containers that are already running:
|
|
527
537
|
|
|
528
538
|
```javascript
|
|
@@ -12,15 +12,15 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
exports.defaultDockerComposeOptions = void 0;
|
|
15
|
-
const session_id_1 = require("../docker/session-id");
|
|
16
15
|
const defaultDockerComposeOptions = (_a) => {
|
|
17
16
|
var { env = {} } = _a, options = __rest(_a, ["env"]);
|
|
18
17
|
return {
|
|
19
18
|
log: false,
|
|
20
19
|
cwd: options.filePath,
|
|
21
20
|
config: options.files,
|
|
21
|
+
composeOptions: options.composeOptions,
|
|
22
22
|
commandOptions: options.commandOptions,
|
|
23
|
-
env: Object.assign(Object.assign(Object.assign({}, process.env), { COMPOSE_PROJECT_NAME:
|
|
23
|
+
env: Object.assign(Object.assign(Object.assign({}, process.env), { COMPOSE_PROJECT_NAME: options.projectName }), env),
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
26
|
exports.defaultDockerComposeOptions = defaultDockerComposeOptions;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const resolveContainerName: (containerName: string) => string;
|
|
1
|
+
export declare const resolveContainerName: (projectName: string, containerName: string) => string;
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.resolveContainerName = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return containerName.substring(`/${session_id_1.sessionId}_`.length);
|
|
4
|
+
const resolveContainerName = (projectName, containerName) => {
|
|
5
|
+
if (containerName.includes(projectName)) {
|
|
6
|
+
return containerName.substring(`/${projectName}_`.length);
|
|
8
7
|
}
|
|
9
8
|
else if (containerName.startsWith("/")) {
|
|
10
9
|
return containerName.substring(1);
|
|
11
10
|
}
|
|
12
11
|
else {
|
|
13
|
-
throw new Error(`Unable to resolve container name for container name: "${containerName}",
|
|
12
|
+
throw new Error(`Unable to resolve container name for container name: "${containerName}", project name: "${projectName}"`);
|
|
14
13
|
}
|
|
15
14
|
};
|
|
16
15
|
exports.resolveContainerName = resolveContainerName;
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import { WaitStrategy } from "../wait-strategy";
|
|
2
|
+
import { Uuid } from "../uuid";
|
|
2
3
|
import { EnvKey, EnvValue } from "../docker/types";
|
|
3
4
|
import { StartedDockerComposeEnvironment } from "./started-docker-compose-environment";
|
|
4
5
|
export declare class DockerComposeEnvironment {
|
|
5
|
-
private readonly
|
|
6
|
+
private readonly composeFilePath;
|
|
7
|
+
private readonly composeFiles;
|
|
8
|
+
private projectName;
|
|
6
9
|
private build;
|
|
7
10
|
private recreate;
|
|
11
|
+
private profiles;
|
|
8
12
|
private env;
|
|
9
13
|
private waitStrategy;
|
|
10
14
|
private startupTimeout;
|
|
11
|
-
constructor(composeFilePath: string, composeFiles: string | string[]);
|
|
15
|
+
constructor(composeFilePath: string, composeFiles: string | string[], uuid?: Uuid);
|
|
12
16
|
withBuild(): this;
|
|
13
17
|
withEnv(key: EnvKey, value: EnvValue): this;
|
|
18
|
+
withProfiles(...profiles: string[]): this;
|
|
14
19
|
withNoRecreate(): this;
|
|
15
20
|
withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this;
|
|
16
21
|
withStartupTimeout(startupTimeout: number): this;
|
|
@@ -17,6 +17,7 @@ const logger_1 = require("../logger");
|
|
|
17
17
|
const port_check_1 = require("../port-check");
|
|
18
18
|
const wait_strategy_1 = require("../wait-strategy");
|
|
19
19
|
const reaper_1 = require("../reaper");
|
|
20
|
+
const uuid_1 = require("../uuid");
|
|
20
21
|
const list_containers_1 = require("../docker/functions/container/list-containers");
|
|
21
22
|
const get_container_1 = require("../docker/functions/container/get-container");
|
|
22
23
|
const docker_host_1 = require("../docker/docker-host");
|
|
@@ -25,15 +26,17 @@ const container_logs_1 = require("../docker/functions/container/container-logs")
|
|
|
25
26
|
const started_docker_compose_environment_1 = require("./started-docker-compose-environment");
|
|
26
27
|
const docker_compose_down_1 = require("../docker-compose/functions/docker-compose-down");
|
|
27
28
|
const docker_compose_up_1 = require("../docker-compose/functions/docker-compose-up");
|
|
28
|
-
const session_id_1 = require("../docker/session-id");
|
|
29
29
|
class DockerComposeEnvironment {
|
|
30
|
-
constructor(composeFilePath, composeFiles) {
|
|
30
|
+
constructor(composeFilePath, composeFiles, uuid = new uuid_1.RandomUuid()) {
|
|
31
31
|
this.build = false;
|
|
32
32
|
this.recreate = true;
|
|
33
|
+
this.profiles = [];
|
|
33
34
|
this.env = {};
|
|
34
35
|
this.waitStrategy = {};
|
|
35
36
|
this.startupTimeout = 60000;
|
|
36
|
-
this.
|
|
37
|
+
this.composeFilePath = composeFilePath;
|
|
38
|
+
this.composeFiles = composeFiles;
|
|
39
|
+
this.projectName = `testcontainers-${uuid.nextUuid()}`;
|
|
37
40
|
}
|
|
38
41
|
withBuild() {
|
|
39
42
|
this.build = true;
|
|
@@ -43,8 +46,13 @@ class DockerComposeEnvironment {
|
|
|
43
46
|
this.env[key] = value;
|
|
44
47
|
return this;
|
|
45
48
|
}
|
|
49
|
+
withProfiles(...profiles) {
|
|
50
|
+
this.profiles = profiles;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
46
53
|
withNoRecreate() {
|
|
47
54
|
this.recreate = false;
|
|
55
|
+
this.projectName = "testcontainers-node";
|
|
48
56
|
return this;
|
|
49
57
|
}
|
|
50
58
|
withWaitStrategy(containerName, waitStrategy) {
|
|
@@ -57,8 +65,13 @@ class DockerComposeEnvironment {
|
|
|
57
65
|
}
|
|
58
66
|
up(services) {
|
|
59
67
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
logger_1.log.info(`Starting DockerCompose environment`);
|
|
61
|
-
yield reaper_1.ReaperInstance.getInstance();
|
|
68
|
+
logger_1.log.info(`Starting DockerCompose environment: ${this.projectName}`);
|
|
69
|
+
(yield reaper_1.ReaperInstance.getInstance()).addProject(this.projectName);
|
|
70
|
+
const options = {
|
|
71
|
+
filePath: this.composeFilePath,
|
|
72
|
+
files: this.composeFiles,
|
|
73
|
+
projectName: this.projectName,
|
|
74
|
+
};
|
|
62
75
|
const commandOptions = [];
|
|
63
76
|
if (this.build) {
|
|
64
77
|
commandOptions.push("--build");
|
|
@@ -66,8 +79,13 @@ class DockerComposeEnvironment {
|
|
|
66
79
|
if (!this.recreate) {
|
|
67
80
|
commandOptions.push("--no-recreate");
|
|
68
81
|
}
|
|
69
|
-
|
|
70
|
-
|
|
82
|
+
const composeOptions = [];
|
|
83
|
+
this.profiles.forEach((profile) => {
|
|
84
|
+
composeOptions.push("--profile");
|
|
85
|
+
composeOptions.push(profile);
|
|
86
|
+
});
|
|
87
|
+
yield docker_compose_up_1.dockerComposeUp(Object.assign(Object.assign({}, options), { commandOptions, composeOptions, env: this.env }), services);
|
|
88
|
+
const startedContainers = (yield list_containers_1.listContainers()).filter((container) => container.Labels["com.docker.compose.project"] === this.projectName);
|
|
71
89
|
const startedContainerNames = startedContainers.reduce((containerNames, startedContainer) => [
|
|
72
90
|
...containerNames,
|
|
73
91
|
startedContainer.Names.join(", "),
|
|
@@ -75,7 +93,7 @@ class DockerComposeEnvironment {
|
|
|
75
93
|
logger_1.log.info(`Started the following containers: ${startedContainerNames.join(", ")}`);
|
|
76
94
|
const startedGenericContainers = (yield Promise.all(startedContainers.map((startedContainer) => __awaiter(this, void 0, void 0, function* () {
|
|
77
95
|
const container = get_container_1.getContainerById(startedContainer.Id);
|
|
78
|
-
const containerName = container_name_resolver_1.resolveContainerName(startedContainer.Names[0]);
|
|
96
|
+
const containerName = container_name_resolver_1.resolveContainerName(this.projectName, startedContainer.Names[0]);
|
|
79
97
|
(yield container_logs_1.containerLogs(container))
|
|
80
98
|
.on("data", (data) => logger_1.containerLog.trace(`${containerName}: ${data}`))
|
|
81
99
|
.on("err", (data) => logger_1.containerLog.error(`${containerName}: ${data}`));
|
|
@@ -89,7 +107,7 @@ class DockerComposeEnvironment {
|
|
|
89
107
|
catch (err) {
|
|
90
108
|
logger_1.log.error(`Container ${containerName} failed to be ready: ${err}`);
|
|
91
109
|
try {
|
|
92
|
-
yield docker_compose_down_1.dockerComposeDown(
|
|
110
|
+
yield docker_compose_down_1.dockerComposeDown(options);
|
|
93
111
|
}
|
|
94
112
|
catch (_a) {
|
|
95
113
|
logger_1.log.warn(`Failed to stop DockerCompose environment after failed up`);
|
|
@@ -102,7 +120,7 @@ class DockerComposeEnvironment {
|
|
|
102
120
|
return Object.assign(Object.assign({}, map), { [containerName]: startedGenericContainer });
|
|
103
121
|
}, {});
|
|
104
122
|
logger_1.log.info(`DockerCompose environment started: ${Object.keys(startedGenericContainers).join(", ")}`);
|
|
105
|
-
return new started_docker_compose_environment_1.StartedDockerComposeEnvironment(startedGenericContainers,
|
|
123
|
+
return new started_docker_compose_environment_1.StartedDockerComposeEnvironment(startedGenericContainers, Object.assign(Object.assign({}, options), { composeOptions, env: this.env }));
|
|
106
124
|
});
|
|
107
125
|
}
|
|
108
126
|
getBoundPorts(containerInfo) {
|
package/dist/reaper.d.ts
CHANGED
package/dist/reaper.js
CHANGED
|
@@ -21,6 +21,9 @@ class RealReaper {
|
|
|
21
21
|
this.container = container;
|
|
22
22
|
this.socket = socket;
|
|
23
23
|
}
|
|
24
|
+
addProject(projectName) {
|
|
25
|
+
this.socket.write(`label=com.docker.compose.project=${projectName}\r\n`);
|
|
26
|
+
}
|
|
24
27
|
getContainerId() {
|
|
25
28
|
return this.container.getId();
|
|
26
29
|
}
|
|
@@ -29,6 +32,9 @@ class RealReaper {
|
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
class DisabledReaper {
|
|
35
|
+
addProject() {
|
|
36
|
+
// noop
|
|
37
|
+
}
|
|
32
38
|
stop() {
|
|
33
39
|
// noop
|
|
34
40
|
}
|
|
@@ -97,7 +103,6 @@ class ReaperInstance {
|
|
|
97
103
|
socket.connect(port, host, () => {
|
|
98
104
|
logger_1.log.debug(`Connected to Reaper ${container.getId()}`);
|
|
99
105
|
socket.write(`label=org.testcontainers.session-id=${session_id_1.sessionId}\r\n`);
|
|
100
|
-
socket.write(`label=com.docker.compose.project=${session_id_1.sessionId}\r\n`);
|
|
101
106
|
const reaper = new RealReaper(container, socket);
|
|
102
107
|
resolve(reaper);
|
|
103
108
|
});
|