testcontainers 8.11.0 → 8.13.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.
Files changed (29) hide show
  1. package/README.md +11 -0
  2. package/dist/docker/docker-client.d.ts +1 -1
  3. package/dist/docker/docker-client.js +8 -1
  4. package/dist/docker/functions/container/create-container.js +1 -1
  5. package/dist/docker/functions/container/get-container.js +2 -2
  6. package/dist/docker/functions/container/list-containers.js +1 -1
  7. package/dist/docker/functions/container/restart-container.d.ts +5 -0
  8. package/dist/docker/functions/container/restart-container.js +23 -0
  9. package/dist/docker/functions/demux-stream.js +1 -1
  10. package/dist/docker/functions/get-info.js +1 -1
  11. package/dist/docker/functions/image/build-image.js +1 -1
  12. package/dist/docker/functions/image/list-images.js +1 -1
  13. package/dist/docker/functions/image/pull-image.js +1 -1
  14. package/dist/docker/functions/network/connect-network.js +1 -1
  15. package/dist/docker/functions/network/create-network.js +1 -1
  16. package/dist/docker/functions/network/remove-network.js +1 -1
  17. package/dist/docker-compose-environment/docker-compose-environment.js +2 -2
  18. package/dist/generic-container/generic-container.js +4 -4
  19. package/dist/generic-container/started-generic-container.d.ts +8 -4
  20. package/dist/generic-container/started-generic-container.js +35 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.js +4 -1
  23. package/dist/modules/abstract-started-container.d.ts +2 -1
  24. package/dist/modules/abstract-started-container.js +14 -0
  25. package/dist/modules/mongodb/mongodb-container.d.ts +19 -0
  26. package/dist/modules/mongodb/mongodb-container.js +85 -0
  27. package/dist/port-forwarder.js +1 -1
  28. package/dist/test-container.d.ts +4 -0
  29. package/package.json +4 -3
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
@@ -5,5 +5,5 @@ declare type DockerClient = {
5
5
  host: Host;
6
6
  dockerode: Dockerode;
7
7
  };
8
- export declare const dockerClient: Promise<DockerClient>;
8
+ export declare const dockerClient: () => Promise<DockerClient>;
9
9
  export {};
@@ -153,4 +153,11 @@ const findGateway = (dockerode) => __awaiter(void 0, void 0, void 0, function* (
153
153
  });
154
154
  const findDefaultGateway = (dockerode) => __awaiter(void 0, void 0, void 0, function* () { return (0, run_in_container_1.runInContainer)(dockerode, "alpine:3.14", ["sh", "-c", "ip route|awk '/default/ { print $3 }'"]); });
155
155
  const isInContainer = () => (0, fs_1.existsSync)("/.dockerenv");
156
- exports.dockerClient = getDockerClient();
156
+ let _dockerClient;
157
+ const dockerClient = () => {
158
+ if (!_dockerClient) {
159
+ _dockerClient = getDockerClient();
160
+ }
161
+ return _dockerClient;
162
+ };
163
+ exports.dockerClient = dockerClient;
@@ -17,7 +17,7 @@ const create_labels_1 = require("../create-labels");
17
17
  const createContainer = (options) => __awaiter(void 0, void 0, void 0, function* () {
18
18
  try {
19
19
  logger_1.log.info(`Creating container for image: ${options.imageName}`);
20
- const { dockerode } = yield docker_client_1.dockerClient;
20
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
21
21
  return yield dockerode.createContainer({
22
22
  name: options.name,
23
23
  User: options.user,
@@ -15,7 +15,7 @@ const logger_1 = require("../../../logger");
15
15
  const labels_1 = require("../../../labels");
16
16
  const getContainerById = (id) => __awaiter(void 0, void 0, void 0, function* () {
17
17
  try {
18
- const { dockerode } = yield docker_client_1.dockerClient;
18
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
19
19
  return dockerode.getContainer(id);
20
20
  }
21
21
  catch (err) {
@@ -26,7 +26,7 @@ const getContainerById = (id) => __awaiter(void 0, void 0, void 0, function* ()
26
26
  exports.getContainerById = getContainerById;
27
27
  const getContainerByHash = (hash) => __awaiter(void 0, void 0, void 0, function* () {
28
28
  try {
29
- const { dockerode } = yield docker_client_1.dockerClient;
29
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
30
30
  const containers = yield dockerode.listContainers({
31
31
  limit: 1,
32
32
  filters: {
@@ -14,7 +14,7 @@ const docker_client_1 = require("../../docker-client");
14
14
  const logger_1 = require("../../../logger");
15
15
  const listContainers = () => __awaiter(void 0, void 0, void 0, function* () {
16
16
  try {
17
- const { dockerode } = yield docker_client_1.dockerClient;
17
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
18
18
  return yield dockerode.listContainers();
19
19
  }
20
20
  catch (err) {
@@ -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;
@@ -16,7 +16,7 @@ const docker_client_1 = require("../docker-client");
16
16
  const demuxStream = (stream) => __awaiter(void 0, void 0, void 0, function* () {
17
17
  try {
18
18
  const demuxedStream = new stream_1.PassThrough({ autoDestroy: true, encoding: "utf-8" });
19
- const { dockerode } = yield docker_client_1.dockerClient;
19
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
20
20
  dockerode.modem.demuxStream(stream, demuxedStream, demuxedStream);
21
21
  stream.on("end", () => demuxedStream.end());
22
22
  demuxedStream.on("close", () => {
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getDockerInfo = void 0;
13
13
  const docker_client_1 = require("../docker-client");
14
14
  const getDockerInfo = () => __awaiter(void 0, void 0, void 0, function* () {
15
- const { dockerode } = yield docker_client_1.dockerClient;
15
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
16
16
  const info = yield dockerode.info();
17
17
  return {
18
18
  serverVersion: info.ServerVersion,
@@ -36,7 +36,7 @@ const buildImage = (options) => __awaiter(void 0, void 0, void 0, function* () {
36
36
  }
37
37
  },
38
38
  });
39
- const { dockerode } = yield docker_client_1.dockerClient;
39
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
40
40
  return new Promise((resolve) => dockerode
41
41
  .buildImage(tarStream, {
42
42
  dockerfile: options.dockerfileName,
@@ -15,7 +15,7 @@ const docker_client_1 = require("../../docker-client");
15
15
  const logger_1 = require("../../../logger");
16
16
  const listImages = () => __awaiter(void 0, void 0, void 0, function* () {
17
17
  try {
18
- const { dockerode } = yield docker_client_1.dockerClient;
18
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
19
19
  const images = yield dockerode.listImages();
20
20
  return images.reduce((dockerImageNames, image) => {
21
21
  var _a;
@@ -21,7 +21,7 @@ const pullImage = (options) => __awaiter(void 0, void 0, void 0, function* () {
21
21
  return;
22
22
  }
23
23
  logger_1.log.info(`Pulling image: ${options.imageName}`);
24
- const { dockerode } = yield docker_client_1.dockerClient;
24
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
25
25
  const stream = yield dockerode.pull(options.imageName.toString(), { authconfig: options.authConfig });
26
26
  yield new pull_stream_parser_1.PullStreamParser(options.imageName, logger_1.log).consume(stream);
27
27
  }
@@ -15,7 +15,7 @@ const docker_client_1 = require("../../docker-client");
15
15
  const connectNetwork = (options) => __awaiter(void 0, void 0, void 0, function* () {
16
16
  try {
17
17
  logger_1.log.info(`Connecting container ${options.containerId} to network ${options.networkId}`);
18
- const { dockerode } = yield docker_client_1.dockerClient;
18
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
19
19
  const network = dockerode.getNetwork(options.networkId);
20
20
  yield network.connect({ Container: options.containerId, EndpointConfig: { Aliases: options.networkAliases } });
21
21
  }
@@ -16,7 +16,7 @@ const create_labels_1 = require("../create-labels");
16
16
  const createNetwork = (options) => __awaiter(void 0, void 0, void 0, function* () {
17
17
  try {
18
18
  logger_1.log.info(`Creating network ${options.name}`);
19
- const { dockerode } = yield docker_client_1.dockerClient;
19
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
20
20
  const network = yield dockerode.createNetwork({
21
21
  Name: options.name,
22
22
  CheckDuplicate: options.checkDuplicate,
@@ -15,7 +15,7 @@ const docker_client_1 = require("../../docker-client");
15
15
  const removeNetwork = (id) => __awaiter(void 0, void 0, void 0, function* () {
16
16
  try {
17
17
  logger_1.log.info(`Removing network ${id}`);
18
- const { dockerode } = yield docker_client_1.dockerClient;
18
+ const { dockerode } = yield (0, docker_client_1.dockerClient)();
19
19
  const network = dockerode.getNetwork(id);
20
20
  const { message } = yield network.remove();
21
21
  if (message) {
@@ -119,7 +119,7 @@ class DockerComposeEnvironment {
119
119
  }
120
120
  throw err;
121
121
  }
122
- return new started_generic_container_1.StartedGenericContainer(container, (yield 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 });
@@ -130,7 +130,7 @@ class DockerComposeEnvironment {
130
130
  }
131
131
  waitForContainer(container, containerName, boundPorts) {
132
132
  return __awaiter(this, void 0, void 0, function* () {
133
- const waitStrategy = this.getWaitStrategy((yield docker_client_1.dockerClient).host, container, containerName);
133
+ const waitStrategy = this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, container, containerName);
134
134
  yield waitStrategy.withStartupTimeout(this.startupTimeout).waitUntilReady(container, boundPorts);
135
135
  });
136
136
  }
@@ -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 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 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
  }
@@ -263,7 +263,7 @@ class GenericContainer {
263
263
  waitForContainer(container, boundPorts) {
264
264
  return __awaiter(this, void 0, void 0, function* () {
265
265
  logger_1.log.debug(`Waiting for container to be ready: ${container.id}`);
266
- const waitStrategy = this.getWaitStrategy((yield docker_client_1.dockerClient).host, container);
266
+ const waitStrategy = this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, container);
267
267
  try {
268
268
  yield waitStrategy.withStartupTimeout(this.startupTimeout).waitUntilReady(container, boundPorts);
269
269
  logger_1.log.info("Container is ready");
@@ -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;
@@ -69,7 +69,7 @@ class PortForwarderInstance {
69
69
  `echo "${username}:$PASSWORD" | chpasswd && /usr/sbin/sshd -D -o PermitRootLogin=yes -o AddressFamily=inet -o GatewayPorts=yes`,
70
70
  ])
71
71
  .start();
72
- const host = (yield docker_client_1.dockerClient).host;
72
+ const host = (yield (0, docker_client_1.dockerClient)()).host;
73
73
  const port = container.getMappedPort(22);
74
74
  logger_1.log.debug(`Connecting to Port Forwarder on ${host}:${port}`);
75
75
  const connection = yield (0, ssh_remote_port_forward_1.createSshConnection)({ host, port, username, password });
@@ -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.0",
4
+ "version": "8.13.0",
5
5
  "main": "dist/index",
6
6
  "types": "dist/index",
7
7
  "keywords": [
@@ -41,6 +41,7 @@
41
41
  "docker-compose": "^0.23.17",
42
42
  "dockerode": "^3.3.1",
43
43
  "get-port": "^5.1.1",
44
+ "mongoose": "^5.13.15",
44
45
  "properties-reader": "^2.2.0",
45
46
  "ssh-remote-port-forward": "^1.0.4",
46
47
  "tar-fs": "^2.1.1"
@@ -66,6 +67,7 @@
66
67
  "kafkajs": "^1.16.0",
67
68
  "lint-staged": "^12.3.8",
68
69
  "mysql2": "^2.3.3",
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": [