testcontainers 8.11.1 → 8.13.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 CHANGED
@@ -162,6 +162,17 @@ const container2 = await new GenericContainer("alpine")
162
162
  assert(container1.getId() === container2.getId());
163
163
  ```
164
164
 
165
+ Restarting a container:
166
+
167
+ ```javascript
168
+ const { GenericContainer } = require("testcontainers");
169
+
170
+ const container = await new GenericContainer("alpine")
171
+ .start();
172
+
173
+ await container.restart();
174
+ ```
175
+
165
176
  Creating a container with a specified name:
166
177
 
167
178
  ```javascript
@@ -0,0 +1,5 @@
1
+ import Dockerode from "dockerode";
2
+ export declare type RestartContainerOptions = {
3
+ timeout: number;
4
+ };
5
+ export declare const restartContainer: (container: Dockerode.Container, options: RestartContainerOptions) => Promise<void>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.restartContainer = void 0;
13
+ const logger_1 = require("../../../logger");
14
+ const restartContainer = (container, options) => __awaiter(void 0, void 0, void 0, function* () {
15
+ try {
16
+ yield container.restart({ t: options.timeout / 1000 });
17
+ }
18
+ catch (err) {
19
+ logger_1.log.error(`Failed to restart container ${container.id}: ${err}`);
20
+ throw err;
21
+ }
22
+ });
23
+ exports.restartContainer = restartContainer;
@@ -119,7 +119,7 @@ class DockerComposeEnvironment {
119
119
  }
120
120
  throw err;
121
121
  }
122
- return new started_generic_container_1.StartedGenericContainer(container, (yield (0, docker_client_1.dockerClient)()).host, inspectResult, boundPorts, containerName);
122
+ return new started_generic_container_1.StartedGenericContainer(container, (yield (0, docker_client_1.dockerClient)()).host, inspectResult, boundPorts, containerName, this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, container, containerName).withStartupTimeout(this.startupTimeout));
123
123
  })))).reduce((map, startedGenericContainer) => {
124
124
  const containerName = startedGenericContainer.getName();
125
125
  return Object.assign(Object.assign({}, map), { [containerName]: startedGenericContainer });
@@ -119,7 +119,7 @@ class GenericContainer {
119
119
  const inspectResult = yield (0, inspect_container_1.inspectContainer)(startedContainer);
120
120
  const boundPorts = bound_ports_1.BoundPorts.fromInspectResult(inspectResult).filter(this.ports);
121
121
  yield this.waitForContainer(startedContainer, boundPorts);
122
- return new started_generic_container_1.StartedGenericContainer(startedContainer, (yield (0, docker_client_1.dockerClient)()).host, inspectResult, boundPorts, inspectResult.name);
122
+ return new started_generic_container_1.StartedGenericContainer(startedContainer, (yield (0, docker_client_1.dockerClient)()).host, inspectResult, boundPorts, inspectResult.name, this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, startedContainer).withStartupTimeout(this.startupTimeout));
123
123
  });
124
124
  }
125
125
  startContainer(createContainerOptions) {
@@ -145,7 +145,7 @@ class GenericContainer {
145
145
  });
146
146
  }
147
147
  if (this.tarToCopy) {
148
- yield this.tarToCopy.finalize();
148
+ this.tarToCopy.finalize();
149
149
  yield (0, put_container_archive_1.putContainerArchive)({ container, stream: this.tarToCopy, containerPath: "/" });
150
150
  }
151
151
  logger_1.log.info(`Starting container ${this.imageName} with ID: ${container.id}`);
@@ -156,7 +156,7 @@ class GenericContainer {
156
156
  const inspectResult = yield (0, inspect_container_1.inspectContainer)(container);
157
157
  const boundPorts = bound_ports_1.BoundPorts.fromInspectResult(inspectResult).filter(this.ports);
158
158
  yield this.waitForContainer(container, boundPorts);
159
- const startedContainer = new started_generic_container_1.StartedGenericContainer(container, (yield (0, docker_client_1.dockerClient)()).host, inspectResult, boundPorts, inspectResult.name);
159
+ const startedContainer = new started_generic_container_1.StartedGenericContainer(container, (yield (0, docker_client_1.dockerClient)()).host, inspectResult, boundPorts, inspectResult.name, this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, container).withStartupTimeout(this.startupTimeout));
160
160
  if (this.postStart) {
161
161
  yield this.postStart(startedContainer, inspectResult, boundPorts);
162
162
  }
@@ -1,20 +1,24 @@
1
1
  /// <reference types="node" />
2
- import { StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
2
+ import { RestartOptions, StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
3
3
  import Dockerode from "dockerode";
4
4
  import { Command, ContainerName, ExecResult, Host, Id as ContainerId, Labels } from "../docker/types";
5
5
  import { InspectResult } from "../docker/functions/container/inspect-container";
6
6
  import { BoundPorts } from "../bound-ports";
7
7
  import { Port } from "../port";
8
8
  import { Readable } from "stream";
9
+ import { WaitStrategy } from "../wait-strategy";
9
10
  export declare class StartedGenericContainer implements StartedTestContainer {
10
11
  private readonly container;
11
12
  private readonly host;
12
- private readonly inspectResult;
13
- private readonly boundPorts;
13
+ private inspectResult;
14
+ private boundPorts;
14
15
  private readonly name;
15
- constructor(container: Dockerode.Container, host: Host, inspectResult: InspectResult, boundPorts: BoundPorts, name: ContainerName);
16
+ private readonly waitStrategy;
17
+ constructor(container: Dockerode.Container, host: Host, inspectResult: InspectResult, boundPorts: BoundPorts, name: ContainerName, waitStrategy: WaitStrategy);
16
18
  stop(options?: Partial<StopOptions>): Promise<StoppedTestContainer>;
19
+ restart(options?: Partial<RestartOptions>): Promise<void>;
17
20
  private stopContainer;
21
+ private waitForContainer;
18
22
  getHost(): Host;
19
23
  getMappedPort(port: Port): Port;
20
24
  getId(): ContainerId;
@@ -10,25 +10,39 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.StartedGenericContainer = void 0;
13
+ const inspect_container_1 = require("../docker/functions/container/inspect-container");
14
+ const bound_ports_1 = require("../bound-ports");
13
15
  const logger_1 = require("../logger");
14
16
  const remove_container_1 = require("../docker/functions/container/remove-container");
15
17
  const exec_container_1 = require("../docker/functions/container/exec-container");
16
18
  const container_logs_1 = require("../docker/functions/container/container-logs");
17
19
  const stopped_generic_container_1 = require("./stopped-generic-container");
18
20
  const stop_container_1 = require("../docker/functions/container/stop-container");
21
+ const restart_container_1 = require("../docker/functions/container/restart-container");
19
22
  class StartedGenericContainer {
20
- constructor(container, host, inspectResult, boundPorts, name) {
23
+ constructor(container, host, inspectResult, boundPorts, name, waitStrategy) {
21
24
  this.container = container;
22
25
  this.host = host;
23
26
  this.inspectResult = inspectResult;
24
27
  this.boundPorts = boundPorts;
25
28
  this.name = name;
29
+ this.waitStrategy = waitStrategy;
26
30
  }
27
31
  stop(options = {}) {
28
32
  return __awaiter(this, void 0, void 0, function* () {
29
33
  return this.stopContainer(options);
30
34
  });
31
35
  }
36
+ restart(options = {}) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const resolvedOptions = Object.assign({ timeout: 0 }, options);
39
+ yield (0, restart_container_1.restartContainer)(this.container, resolvedOptions);
40
+ // Inspect the restarted container and update the bound ports
41
+ this.inspectResult = yield (0, inspect_container_1.inspectContainer)(this.container);
42
+ this.boundPorts = yield bound_ports_1.BoundPorts.fromInspectResult(this.inspectResult).filter(Array.from(this.boundPorts.iterator()).map((port) => port[0]));
43
+ yield this.waitForContainer(this.container, this.boundPorts);
44
+ });
45
+ }
32
46
  stopContainer(options = {}) {
33
47
  return __awaiter(this, void 0, void 0, function* () {
34
48
  logger_1.log.info(`Stopping container with ID: ${this.container.id}`);
@@ -38,6 +52,26 @@ class StartedGenericContainer {
38
52
  return new stopped_generic_container_1.StoppedGenericContainer();
39
53
  });
40
54
  }
55
+ waitForContainer(container, boundPorts) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ logger_1.log.debug(`Waiting for container to be ready: ${container.id}`);
58
+ try {
59
+ yield this.waitStrategy.waitUntilReady(container, boundPorts);
60
+ logger_1.log.info("Container is ready");
61
+ }
62
+ catch (err) {
63
+ logger_1.log.error(`Container failed to be ready: ${err}`);
64
+ try {
65
+ yield (0, stop_container_1.stopContainer)(container, { timeout: 0 });
66
+ yield (0, remove_container_1.removeContainer)(container, { removeVolumes: true });
67
+ }
68
+ catch (stopErr) {
69
+ logger_1.log.error(`Failed to stop container after it failed to be ready: ${stopErr}`);
70
+ }
71
+ throw err;
72
+ }
73
+ });
74
+ }
41
75
  getHost() {
42
76
  return this.host;
43
77
  }
package/dist/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export { ArangoDBContainer, StartedArangoContainer } from "./modules/arangodb/ar
16
16
  export { ElasticsearchContainer, StartedElasticsearchContainer } from "./modules/elasticsearch/elasticsearch-container";
17
17
  export { MySqlContainer, StartedMySqlContainer } from "./modules/mysql/mysql-container";
18
18
  export { NatsContainer, StartedNatsContainer, NatsConnectionOptions, NatsTlsOptions, } from "./modules/nats/nats-container";
19
+ export { MongoDBContainer, StartedMongoDBContainer } from "./modules/mongodb/mongodb-container";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StartedNatsContainer = exports.NatsContainer = exports.StartedMySqlContainer = exports.MySqlContainer = exports.StartedElasticsearchContainer = exports.ElasticsearchContainer = exports.StartedArangoContainer = exports.ArangoDBContainer = exports.StartedNeo4jContainer = exports.Neo4jContainer = exports.StartedKafkaContainer = exports.KafkaContainer = exports.StartedPostgreSqlContainer = exports.PostgreSqlContainer = exports.AlwaysPullPolicy = exports.DefaultPullPolicy = exports.Wait = exports.StoppedNetwork = exports.StartedNetwork = exports.Network = exports.DownedDockerComposeEnvironment = exports.StoppedDockerComposeEnvironment = exports.StartedDockerComposeEnvironment = exports.DockerComposeEnvironment = exports.TestContainers = exports.GenericContainerBuilder = exports.GenericContainer = void 0;
3
+ exports.StartedMongoDBContainer = exports.MongoDBContainer = exports.StartedNatsContainer = exports.NatsContainer = exports.StartedMySqlContainer = exports.MySqlContainer = exports.StartedElasticsearchContainer = exports.ElasticsearchContainer = exports.StartedArangoContainer = exports.ArangoDBContainer = exports.StartedNeo4jContainer = exports.Neo4jContainer = exports.StartedKafkaContainer = exports.KafkaContainer = exports.StartedPostgreSqlContainer = exports.PostgreSqlContainer = exports.AlwaysPullPolicy = exports.DefaultPullPolicy = exports.Wait = exports.StoppedNetwork = exports.StartedNetwork = exports.Network = exports.DownedDockerComposeEnvironment = exports.StoppedDockerComposeEnvironment = exports.StartedDockerComposeEnvironment = exports.DockerComposeEnvironment = exports.TestContainers = exports.GenericContainerBuilder = exports.GenericContainer = void 0;
4
4
  var generic_container_1 = require("./generic-container/generic-container");
5
5
  Object.defineProperty(exports, "GenericContainer", { enumerable: true, get: function () { return generic_container_1.GenericContainer; } });
6
6
  var generic_container_builder_1 = require("./generic-container/generic-container-builder");
@@ -45,3 +45,6 @@ Object.defineProperty(exports, "StartedMySqlContainer", { enumerable: true, get:
45
45
  var nats_container_1 = require("./modules/nats/nats-container");
46
46
  Object.defineProperty(exports, "NatsContainer", { enumerable: true, get: function () { return nats_container_1.NatsContainer; } });
47
47
  Object.defineProperty(exports, "StartedNatsContainer", { enumerable: true, get: function () { return nats_container_1.StartedNatsContainer; } });
48
+ var mongodb_container_1 = require("./modules/mongodb/mongodb-container");
49
+ Object.defineProperty(exports, "MongoDBContainer", { enumerable: true, get: function () { return mongodb_container_1.MongoDBContainer; } });
50
+ Object.defineProperty(exports, "StartedMongoDBContainer", { enumerable: true, get: function () { return mongodb_container_1.StartedMongoDBContainer; } });
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
2
+ import { RestartOptions, StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
3
3
  import { Port } from "../port";
4
4
  import { ContainerName, Host, Id, Command, ExecResult, Labels } from "../docker/types";
5
5
  import { Readable } from "stream";
@@ -7,6 +7,7 @@ export declare class AbstractStartedContainer {
7
7
  protected readonly startedTestContainer: StartedTestContainer;
8
8
  constructor(startedTestContainer: StartedTestContainer);
9
9
  stop(options?: Partial<StopOptions>): Promise<StoppedTestContainer>;
10
+ restart(options?: Partial<RestartOptions>): Promise<void>;
10
11
  getHost(): Host;
11
12
  getMappedPort(port: Port): Port;
12
13
  getName(): ContainerName;
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.AbstractStartedContainer = void 0;
4
13
  class AbstractStartedContainer {
@@ -8,6 +17,11 @@ class AbstractStartedContainer {
8
17
  stop(options) {
9
18
  return this.startedTestContainer.stop(options);
10
19
  }
20
+ restart(options) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ return this.startedTestContainer.restart(options);
23
+ });
24
+ }
11
25
  getHost() {
12
26
  return this.startedTestContainer.getHost();
13
27
  }
@@ -0,0 +1,19 @@
1
+ import { GenericContainer } from "../../generic-container/generic-container";
2
+ import { AbstractStartedContainer } from "../abstract-started-container";
3
+ import { StartedTestContainer } from "../../test-container";
4
+ export declare class MongoDBContainer extends GenericContainer {
5
+ private readonly dockerImageName;
6
+ constructor(image?: string);
7
+ start(): Promise<StartedMongoDBContainer>;
8
+ protected postStart(startedTestContainer: StartedTestContainer): Promise<void>;
9
+ private initReplicaSet;
10
+ private executeMongoEvalCommand;
11
+ private buildMongoEvalCommand;
12
+ private getMongoCmdBasedOnImageTag;
13
+ private checkMongoNodeExitCode;
14
+ private buildMongoWaitCommand;
15
+ }
16
+ export declare class StartedMongoDBContainer extends AbstractStartedContainer {
17
+ constructor(startedTestContainer: StartedTestContainer);
18
+ getConnectionString(): string;
19
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.StartedMongoDBContainer = exports.MongoDBContainer = void 0;
13
+ const generic_container_1 = require("../../generic-container/generic-container");
14
+ const abstract_started_container_1 = require("../abstract-started-container");
15
+ const wait_1 = require("../../wait");
16
+ const docker_image_name_1 = require("../../docker-image-name");
17
+ const MONGODB_PORT = 27017;
18
+ class MongoDBContainer extends generic_container_1.GenericContainer {
19
+ constructor(image = "mongo:4.0.1") {
20
+ super(image);
21
+ this.dockerImageName = docker_image_name_1.DockerImageName.fromString(this.image);
22
+ }
23
+ start() {
24
+ const _super = Object.create(null, {
25
+ start: { get: () => super.start }
26
+ });
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ this.withExposedPorts(MONGODB_PORT)
29
+ .withCmd(["--replSet", "rs0"])
30
+ .withWaitStrategy(wait_1.Wait.forLogMessage(new RegExp(".*waiting for connections.*", "i")))
31
+ .withStartupTimeout(120000);
32
+ return new StartedMongoDBContainer(yield _super.start.call(this));
33
+ });
34
+ }
35
+ postStart(startedTestContainer) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ yield this.initReplicaSet(startedTestContainer);
38
+ });
39
+ }
40
+ initReplicaSet(startedTestContainer) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ yield this.executeMongoEvalCommand(startedTestContainer, "rs.initiate();");
43
+ yield this.executeMongoEvalCommand(startedTestContainer, this.buildMongoWaitCommand());
44
+ });
45
+ }
46
+ executeMongoEvalCommand(startedTestContainer, command) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const execResult = yield startedTestContainer.exec(this.buildMongoEvalCommand(command));
49
+ this.checkMongoNodeExitCode(execResult);
50
+ });
51
+ }
52
+ buildMongoEvalCommand(command) {
53
+ return [this.getMongoCmdBasedOnImageTag(), "--eval", command];
54
+ }
55
+ getMongoCmdBasedOnImageTag() {
56
+ return parseInt(this.dockerImageName.tag[0]) >= 5 ? "mongosh" : "mongo";
57
+ }
58
+ checkMongoNodeExitCode(execResult) {
59
+ const { exitCode, output } = execResult;
60
+ if (execResult.exitCode !== 0) {
61
+ throw new Error(`Error running mongo command. Exit code ${exitCode}: ${output}`);
62
+ }
63
+ }
64
+ buildMongoWaitCommand() {
65
+ return `
66
+ var attempt = 0;
67
+ while(db.runCommand({isMaster: 1}).ismaster==false) {
68
+ if (attempt > 60) {
69
+ quit(1);
70
+ }
71
+ print(attempt); sleep(100); attempt++;
72
+ }
73
+ `;
74
+ }
75
+ }
76
+ exports.MongoDBContainer = MongoDBContainer;
77
+ class StartedMongoDBContainer extends abstract_started_container_1.AbstractStartedContainer {
78
+ constructor(startedTestContainer) {
79
+ super(startedTestContainer);
80
+ }
81
+ getConnectionString() {
82
+ return `mongodb://${this.getHost()}:${this.getMappedPort(MONGODB_PORT)}`;
83
+ }
84
+ }
85
+ exports.StartedMongoDBContainer = StartedMongoDBContainer;
@@ -23,12 +23,16 @@ export interface TestContainer {
23
23
  withCopyFileToContainer(sourcePath: string, containerPath: string): this;
24
24
  withCopyContentToContainer(content: string | Buffer | Readable, containerPath: string): this;
25
25
  }
26
+ export interface RestartOptions {
27
+ timeout: number;
28
+ }
26
29
  export interface StopOptions {
27
30
  timeout: number;
28
31
  removeVolumes: boolean;
29
32
  }
30
33
  export interface StartedTestContainer {
31
34
  stop(options?: Partial<StopOptions>): Promise<StoppedTestContainer>;
35
+ restart(options?: Partial<RestartOptions>): Promise<void>;
32
36
  getHost(): Host;
33
37
  getMappedPort(port: Port): Port;
34
38
  getName(): ContainerName;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "testcontainers",
3
3
  "author": "Cristian Greco",
4
- "version": "8.11.1",
4
+ "version": "8.13.1",
5
5
  "main": "dist/index",
6
6
  "types": "dist/index",
7
7
  "keywords": [
@@ -66,6 +66,8 @@
66
66
  "kafkajs": "^1.16.0",
67
67
  "lint-staged": "^12.3.8",
68
68
  "mysql2": "^2.3.3",
69
+ "mongoose": "^5.13.15",
70
+ "nats": "^2.7.1",
69
71
  "neo4j-driver": "^4.4.5",
70
72
  "node-fetch": "^2.6.7",
71
73
  "pg": "^8.7.3",
@@ -74,8 +76,7 @@
74
76
  "ts-jest": "^27.1.4",
75
77
  "ts-node": "^10.7.0",
76
78
  "typescript": "^4.6.3",
77
- "wait-for-expect": "^3.0.2",
78
- "nats": "^2.7.1"
79
+ "wait-for-expect": "^3.0.2"
79
80
  },
80
81
  "lint-staged": {
81
82
  "src/**/*.ts": [