testcontainers 7.21.0 → 7.22.0

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 CHANGED
@@ -523,6 +523,16 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF
523
523
  .up();
524
524
  ```
525
525
 
526
+ Specify not to re-create containers that are already running:
527
+
528
+ ```javascript
529
+ const { DockerComposeEnvironment } = require("testcontainers");
530
+
531
+ const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
532
+ .withNoRecreate()
533
+ .up();
534
+ ```
535
+
526
536
  Bind environment variables to the docker-compose file. For example if we have the following docker-compose file:
527
537
 
528
538
  ```yaml
@@ -12,6 +12,7 @@ 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");
15
16
  const defaultDockerComposeOptions = (_a) => {
16
17
  var { env = {} } = _a, options = __rest(_a, ["env"]);
17
18
  return {
@@ -19,7 +20,7 @@ const defaultDockerComposeOptions = (_a) => {
19
20
  cwd: options.filePath,
20
21
  config: options.files,
21
22
  commandOptions: options.commandOptions,
22
- env: Object.assign(Object.assign(Object.assign({}, process.env), { COMPOSE_PROJECT_NAME: options.projectName }), env),
23
+ env: Object.assign(Object.assign(Object.assign({}, process.env), { COMPOSE_PROJECT_NAME: session_id_1.sessionId }), env),
23
24
  };
24
25
  };
25
26
  exports.defaultDockerComposeOptions = defaultDockerComposeOptions;
@@ -2,7 +2,6 @@ import { Env } from "../docker/types";
2
2
  export declare type DockerComposeOptions = {
3
3
  filePath: string;
4
4
  files: string | string[];
5
- projectName: string;
6
5
  commandOptions?: string[];
7
6
  env?: Env;
8
7
  };
@@ -1 +1 @@
1
- export declare const resolveContainerName: (projectName: string, containerName: string) => string;
1
+ export declare const resolveContainerName: (containerName: string) => string;
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.resolveContainerName = void 0;
4
- const resolveContainerName = (projectName, containerName) => {
5
- if (containerName.includes(projectName)) {
6
- return containerName.substring(`/${projectName}_`.length);
4
+ const session_id_1 = require("../../docker/session-id");
5
+ const resolveContainerName = (containerName) => {
6
+ if (containerName.includes(session_id_1.sessionId)) {
7
+ return containerName.substring(`/${session_id_1.sessionId}_`.length);
7
8
  }
8
9
  else if (containerName.startsWith("/")) {
9
10
  return containerName.substring(1);
10
11
  }
11
12
  else {
12
- throw new Error(`Unable to resolve container name for container name: "${containerName}", project name: "${projectName}"`);
13
+ throw new Error(`Unable to resolve container name for container name: "${containerName}", session ID: "${session_id_1.sessionId}"`);
13
14
  }
14
15
  };
15
16
  exports.resolveContainerName = resolveContainerName;
@@ -1,17 +1,17 @@
1
1
  import { WaitStrategy } from "../wait-strategy";
2
- import { Uuid } from "../uuid";
3
2
  import { EnvKey, EnvValue } from "../docker/types";
4
3
  import { StartedDockerComposeEnvironment } from "./started-docker-compose-environment";
5
4
  export declare class DockerComposeEnvironment {
6
- private readonly projectName;
7
5
  private readonly options;
8
6
  private build;
7
+ private recreate;
9
8
  private env;
10
9
  private waitStrategy;
11
10
  private startupTimeout;
12
- constructor(composeFilePath: string, composeFiles: string | string[], uuid?: Uuid);
11
+ constructor(composeFilePath: string, composeFiles: string | string[]);
13
12
  withBuild(): this;
14
13
  withEnv(key: EnvKey, value: EnvValue): this;
14
+ withNoRecreate(): this;
15
15
  withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this;
16
16
  withStartupTimeout(startupTimeout: number): this;
17
17
  up(services?: Array<string>): Promise<StartedDockerComposeEnvironment>;
@@ -17,7 +17,6 @@ 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");
21
20
  const list_containers_1 = require("../docker/functions/container/list-containers");
22
21
  const get_container_1 = require("../docker/functions/container/get-container");
23
22
  const docker_host_1 = require("../docker/docker-host");
@@ -26,14 +25,15 @@ const container_logs_1 = require("../docker/functions/container/container-logs")
26
25
  const started_docker_compose_environment_1 = require("./started-docker-compose-environment");
27
26
  const docker_compose_down_1 = require("../docker-compose/functions/docker-compose-down");
28
27
  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, uuid = new uuid_1.RandomUuid()) {
30
+ constructor(composeFilePath, composeFiles) {
31
31
  this.build = false;
32
+ this.recreate = true;
32
33
  this.env = {};
33
34
  this.waitStrategy = {};
34
35
  this.startupTimeout = 60000;
35
- this.projectName = `testcontainers-${uuid.nextUuid()}`;
36
- this.options = { filePath: composeFilePath, files: composeFiles, projectName: this.projectName };
36
+ this.options = { filePath: composeFilePath, files: composeFiles };
37
37
  }
38
38
  withBuild() {
39
39
  this.build = true;
@@ -43,6 +43,10 @@ class DockerComposeEnvironment {
43
43
  this.env[key] = value;
44
44
  return this;
45
45
  }
46
+ withNoRecreate() {
47
+ this.recreate = false;
48
+ return this;
49
+ }
46
50
  withWaitStrategy(containerName, waitStrategy) {
47
51
  this.waitStrategy[containerName] = waitStrategy;
48
52
  return this;
@@ -53,14 +57,17 @@ class DockerComposeEnvironment {
53
57
  }
54
58
  up(services) {
55
59
  return __awaiter(this, void 0, void 0, function* () {
56
- logger_1.log.info(`Starting DockerCompose environment ${this.projectName}`);
57
- (yield reaper_1.ReaperInstance.getInstance()).addProject(this.projectName);
60
+ logger_1.log.info(`Starting DockerCompose environment`);
61
+ yield reaper_1.ReaperInstance.getInstance();
58
62
  const commandOptions = [];
59
63
  if (this.build) {
60
64
  commandOptions.push("--build");
61
65
  }
66
+ if (!this.recreate) {
67
+ commandOptions.push("--no-recreate");
68
+ }
62
69
  yield docker_compose_up_1.dockerComposeUp(Object.assign(Object.assign({}, this.options), { commandOptions, env: this.env }), services);
63
- const startedContainers = (yield list_containers_1.listContainers()).filter((container) => container.Labels["com.docker.compose.project"] === this.projectName);
70
+ const startedContainers = (yield list_containers_1.listContainers()).filter((container) => container.Labels["com.docker.compose.project"] === session_id_1.sessionId);
64
71
  const startedContainerNames = startedContainers.reduce((containerNames, startedContainer) => [
65
72
  ...containerNames,
66
73
  startedContainer.Names.join(", "),
@@ -68,7 +75,7 @@ class DockerComposeEnvironment {
68
75
  logger_1.log.info(`Started the following containers: ${startedContainerNames.join(", ")}`);
69
76
  const startedGenericContainers = (yield Promise.all(startedContainers.map((startedContainer) => __awaiter(this, void 0, void 0, function* () {
70
77
  const container = get_container_1.getContainerById(startedContainer.Id);
71
- const containerName = container_name_resolver_1.resolveContainerName(this.projectName, startedContainer.Names[0]);
78
+ const containerName = container_name_resolver_1.resolveContainerName(startedContainer.Names[0]);
72
79
  (yield container_logs_1.containerLogs(container))
73
80
  .on("data", (data) => logger_1.containerLog.trace(`${containerName}: ${data}`))
74
81
  .on("err", (data) => logger_1.containerLog.error(`${containerName}: ${data}`));
package/dist/reaper.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export interface Reaper {
2
- addProject(projectName: string): void;
3
2
  getContainerId(): string;
4
3
  stop(): void;
5
4
  }
package/dist/reaper.js CHANGED
@@ -21,9 +21,6 @@ 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
- }
27
24
  getContainerId() {
28
25
  return this.container.getId();
29
26
  }
@@ -32,9 +29,6 @@ class RealReaper {
32
29
  }
33
30
  }
34
31
  class DisabledReaper {
35
- addProject() {
36
- // noop
37
- }
38
32
  stop() {
39
33
  // noop
40
34
  }
@@ -103,6 +97,7 @@ class ReaperInstance {
103
97
  socket.connect(port, host, () => {
104
98
  logger_1.log.debug(`Connected to Reaper ${container.getId()}`);
105
99
  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`);
106
101
  const reaper = new RealReaper(container, socket);
107
102
  resolve(reaper);
108
103
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "testcontainers",
3
3
  "author": "Cristian Greco",
4
- "version": "7.21.0",
4
+ "version": "7.22.0",
5
5
  "main": "dist/index",
6
6
  "types": "dist/index",
7
7
  "keywords": [