testcontainers 9.0.0 → 9.0.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.
@@ -22,6 +22,4 @@ export declare class DockerComposeEnvironment {
22
22
  withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this;
23
23
  withStartupTimeout(startupTimeout: number): this;
24
24
  up(services?: Array<string>): Promise<StartedDockerComposeEnvironment>;
25
- private waitForContainer;
26
- private getWaitStrategy;
27
25
  }
@@ -14,7 +14,6 @@ const bound_ports_1 = require("../bound-ports");
14
14
  const container_name_resolver_1 = require("../docker-compose/functions/container-name-resolver");
15
15
  const started_generic_container_1 = require("../generic-container/started-generic-container");
16
16
  const logger_1 = require("../logger");
17
- const port_check_1 = require("../port-check");
18
17
  const wait_strategy_1 = require("../wait-strategy");
19
18
  const reaper_1 = require("../reaper");
20
19
  const uuid_1 = require("../uuid");
@@ -26,6 +25,7 @@ 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 wait_for_container_1 = require("../wait-for-container");
29
29
  class DockerComposeEnvironment {
30
30
  constructor(composeFilePath, composeFiles, uuid = new uuid_1.RandomUuid()) {
31
31
  this.build = false;
@@ -104,9 +104,11 @@ class DockerComposeEnvironment {
104
104
  .on("err", (data) => logger_1.containerLog.error(`${containerName}: ${data}`));
105
105
  const inspectResult = yield (0, inspect_container_1.inspectContainer)(container);
106
106
  const boundPorts = bound_ports_1.BoundPorts.fromInspectResult(inspectResult);
107
+ const host = (yield (0, docker_client_1.dockerClient)()).host;
108
+ const waitStrategy = (this.waitStrategy[containerName] ? this.waitStrategy[containerName] : (0, wait_strategy_1.defaultWaitStrategy)(host, container)).withStartupTimeout(this.startupTimeout);
107
109
  try {
108
110
  logger_1.log.info(`Waiting for container ${containerName} to be ready`);
109
- yield this.waitForContainer(container, containerName, boundPorts);
111
+ yield (0, wait_for_container_1.waitForContainer)(container, waitStrategy, boundPorts);
110
112
  logger_1.log.info(`Container ${containerName} is ready`);
111
113
  }
112
114
  catch (err) {
@@ -119,7 +121,7 @@ class DockerComposeEnvironment {
119
121
  }
120
122
  throw err;
121
123
  }
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));
124
+ return new started_generic_container_1.StartedGenericContainer(container, host, inspectResult, boundPorts, containerName, waitStrategy);
123
125
  })))).reduce((map, startedGenericContainer) => {
124
126
  const containerName = startedGenericContainer.getName();
125
127
  return Object.assign(Object.assign({}, map), { [containerName]: startedGenericContainer });
@@ -128,21 +130,5 @@ class DockerComposeEnvironment {
128
130
  return new started_docker_compose_environment_1.StartedDockerComposeEnvironment(startedGenericContainers, Object.assign(Object.assign({}, options), { composeOptions, environment: this.environment }));
129
131
  });
130
132
  }
131
- waitForContainer(container, containerName, boundPorts) {
132
- return __awaiter(this, void 0, void 0, function* () {
133
- const waitStrategy = this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, container, containerName);
134
- yield waitStrategy.withStartupTimeout(this.startupTimeout).waitUntilReady(container, boundPorts);
135
- });
136
- }
137
- getWaitStrategy(host, container, containerName) {
138
- if (this.waitStrategy[containerName]) {
139
- return this.waitStrategy[containerName];
140
- }
141
- else {
142
- const hostPortCheck = new port_check_1.HostPortCheck(host);
143
- const internalPortCheck = new port_check_1.InternalPortCheck(container);
144
- return new wait_strategy_1.HostPortWaitStrategy(hostPortCheck, internalPortCheck);
145
- }
146
- }
147
133
  }
148
134
  exports.DockerComposeEnvironment = DockerComposeEnvironment;
@@ -70,6 +70,4 @@ export declare class GenericContainer implements TestContainer {
70
70
  withCopyFilesToContainer(filesToCopy: FileToCopy[]): this;
71
71
  withCopyContentToContainer(contentsToCopy: ContentToCopy[]): this;
72
72
  protected getTarToCopy(): archiver.Archiver;
73
- private waitForContainer;
74
- private getWaitStrategy;
75
73
  }
@@ -16,7 +16,6 @@ exports.GenericContainer = void 0;
16
16
  const archiver_1 = __importDefault(require("archiver"));
17
17
  const bound_ports_1 = require("../bound-ports");
18
18
  const logger_1 = require("../logger");
19
- const port_check_1 = require("../port-check");
20
19
  const pull_policy_1 = require("../pull-policy");
21
20
  const reaper_1 = require("../reaper");
22
21
  const docker_image_name_1 = require("../docker-image-name");
@@ -30,14 +29,13 @@ const docker_client_1 = require("../docker/docker-client");
30
29
  const inspect_container_1 = require("../docker/functions/container/inspect-container");
31
30
  const start_container_1 = require("../docker/functions/container/start-container");
32
31
  const container_logs_1 = require("../docker/functions/container/container-logs");
33
- const stop_container_1 = require("../docker/functions/container/stop-container");
34
- const remove_container_1 = require("../docker/functions/container/remove-container");
35
32
  const put_container_archive_1 = require("../docker/functions/container/put-container-archive");
36
33
  const generic_container_builder_1 = require("./generic-container-builder");
37
34
  const started_generic_container_1 = require("./started-generic-container");
38
35
  const hash_1 = require("../hash");
39
36
  const get_container_1 = require("../docker/functions/container/get-container");
40
37
  const labels_1 = require("../labels");
38
+ const wait_for_container_1 = require("../wait-for-container");
41
39
  class GenericContainer {
42
40
  constructor(image) {
43
41
  this.image = image;
@@ -119,14 +117,18 @@ class GenericContainer {
119
117
  });
120
118
  }
121
119
  reuseContainer(startedContainer) {
120
+ var _a;
122
121
  return __awaiter(this, void 0, void 0, function* () {
123
122
  const inspectResult = yield (0, inspect_container_1.inspectContainer)(startedContainer);
124
123
  const boundPorts = bound_ports_1.BoundPorts.fromInspectResult(inspectResult).filter(this.ports);
125
- yield this.waitForContainer(startedContainer, boundPorts);
126
- 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));
124
+ const host = (yield (0, docker_client_1.dockerClient)()).host;
125
+ const waitStrategy = ((_a = this.waitStrategy) !== null && _a !== void 0 ? _a : (0, wait_strategy_1.defaultWaitStrategy)(host, startedContainer)).withStartupTimeout(this.startupTimeout);
126
+ yield (0, wait_for_container_1.waitForContainer)(startedContainer, waitStrategy, boundPorts);
127
+ return new started_generic_container_1.StartedGenericContainer(startedContainer, host, inspectResult, boundPorts, inspectResult.name, waitStrategy);
127
128
  });
128
129
  }
129
130
  startContainer(createContainerOptions) {
131
+ var _a;
130
132
  return __awaiter(this, void 0, void 0, function* () {
131
133
  const container = yield (0, create_container_1.createContainer)(createContainerOptions);
132
134
  if (!this.imageName.isHelperContainer() && port_forwarder_1.PortForwarderInstance.isRunning()) {
@@ -159,8 +161,10 @@ class GenericContainer {
159
161
  .on("err", (data) => logger_1.containerLog.error(`${container.id}: ${data.trim()}`));
160
162
  const inspectResult = yield (0, inspect_container_1.inspectContainer)(container);
161
163
  const boundPorts = bound_ports_1.BoundPorts.fromInspectResult(inspectResult).filter(this.ports);
162
- yield this.waitForContainer(container, boundPorts);
163
- 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));
164
+ const host = (yield (0, docker_client_1.dockerClient)()).host;
165
+ const waitStrategy = ((_a = this.waitStrategy) !== null && _a !== void 0 ? _a : (0, wait_strategy_1.defaultWaitStrategy)(host, container)).withStartupTimeout(this.startupTimeout);
166
+ yield (0, wait_for_container_1.waitForContainer)(container, waitStrategy, boundPorts);
167
+ const startedContainer = new started_generic_container_1.StartedGenericContainer(container, host, inspectResult, boundPorts, inspectResult.name, waitStrategy);
164
168
  if (this.postStart) {
165
169
  yield this.postStart(startedContainer, inspectResult, boundPorts);
166
170
  }
@@ -284,34 +288,5 @@ class GenericContainer {
284
288
  }
285
289
  return this.tarToCopy;
286
290
  }
287
- waitForContainer(container, boundPorts) {
288
- return __awaiter(this, void 0, void 0, function* () {
289
- logger_1.log.debug(`Waiting for container to be ready: ${container.id}`);
290
- const waitStrategy = this.getWaitStrategy((yield (0, docker_client_1.dockerClient)()).host, container);
291
- try {
292
- yield waitStrategy.withStartupTimeout(this.startupTimeout).waitUntilReady(container, boundPorts);
293
- logger_1.log.info("Container is ready");
294
- }
295
- catch (err) {
296
- logger_1.log.error(`Container failed to be ready: ${err}`);
297
- try {
298
- yield (0, stop_container_1.stopContainer)(container, { timeout: 0 });
299
- yield (0, remove_container_1.removeContainer)(container, { removeVolumes: true });
300
- }
301
- catch (stopErr) {
302
- logger_1.log.error(`Failed to stop container after it failed to be ready: ${stopErr}`);
303
- }
304
- throw err;
305
- }
306
- });
307
- }
308
- getWaitStrategy(host, container) {
309
- if (this.waitStrategy) {
310
- return this.waitStrategy;
311
- }
312
- const hostPortCheck = new port_check_1.HostPortCheck(host);
313
- const internalPortCheck = new port_check_1.InternalPortCheck(container);
314
- return new wait_strategy_1.HostPortWaitStrategy(hostPortCheck, internalPortCheck);
315
- }
316
291
  }
317
292
  exports.GenericContainer = GenericContainer;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { RestartOptions, StartedTestContainer, StopOptions, ExecOptions, StoppedTestContainer } from "../test-container";
2
+ import { ExecOptions, RestartOptions, StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
3
3
  import Dockerode from "dockerode";
4
4
  import { ExecResult, Labels } from "../docker/types";
5
5
  import { InspectResult } from "../docker/functions/container/inspect-container";
@@ -17,7 +17,6 @@ export declare class StartedGenericContainer implements StartedTestContainer {
17
17
  stop(options?: Partial<StopOptions>): Promise<StoppedTestContainer>;
18
18
  restart(options?: Partial<RestartOptions>): Promise<void>;
19
19
  private stopContainer;
20
- private waitForContainer;
21
20
  getHost(): string;
22
21
  getMappedPort(port: number): number;
23
22
  getId(): string;
@@ -19,6 +19,7 @@ const container_logs_1 = require("../docker/functions/container/container-logs")
19
19
  const stopped_generic_container_1 = require("./stopped-generic-container");
20
20
  const stop_container_1 = require("../docker/functions/container/stop-container");
21
21
  const restart_container_1 = require("../docker/functions/container/restart-container");
22
+ const wait_for_container_1 = require("../wait-for-container");
22
23
  class StartedGenericContainer {
23
24
  constructor(container, host, inspectResult, boundPorts, name, waitStrategy) {
24
25
  this.container = container;
@@ -39,7 +40,7 @@ class StartedGenericContainer {
39
40
  yield (0, restart_container_1.restartContainer)(this.container, resolvedOptions);
40
41
  this.inspectResult = yield (0, inspect_container_1.inspectContainer)(this.container);
41
42
  this.boundPorts = bound_ports_1.BoundPorts.fromInspectResult(this.inspectResult).filter(Array.from(this.boundPorts.iterator()).map((port) => port[0]));
42
- yield this.waitForContainer(this.container, this.boundPorts);
43
+ yield (0, wait_for_container_1.waitForContainer)(this.container, this.waitStrategy, this.boundPorts);
43
44
  });
44
45
  }
45
46
  stopContainer(options = {}) {
@@ -51,26 +52,6 @@ class StartedGenericContainer {
51
52
  return new stopped_generic_container_1.StoppedGenericContainer();
52
53
  });
53
54
  }
54
- waitForContainer(container, boundPorts) {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- logger_1.log.debug(`Waiting for container to be ready: ${container.id}`);
57
- try {
58
- yield this.waitStrategy.waitUntilReady(container, boundPorts);
59
- logger_1.log.info("Container is ready");
60
- }
61
- catch (err) {
62
- logger_1.log.error(`Container failed to be ready: ${err}`);
63
- try {
64
- yield (0, stop_container_1.stopContainer)(container, { timeout: 0 });
65
- yield (0, remove_container_1.removeContainer)(container, { removeVolumes: true });
66
- }
67
- catch (stopErr) {
68
- logger_1.log.error(`Failed to stop container after it failed to be ready: ${stopErr}`);
69
- }
70
- throw err;
71
- }
72
- });
73
- }
74
55
  getHost() {
75
56
  return this.host;
76
57
  }
package/dist/reaper.js CHANGED
@@ -18,6 +18,7 @@ const images_1 = require("./images");
18
18
  const port_1 = require("./port");
19
19
  const labels_1 = require("./labels");
20
20
  const wait_1 = require("./wait");
21
+ const retry_strategy_1 = require("./retry-strategy");
21
22
  class RealReaper {
22
23
  constructor(container, socket) {
23
24
  this.container = container;
@@ -97,28 +98,38 @@ class ReaperInstance {
97
98
  const containerId = startedContainer.getId();
98
99
  const host = startedContainer.getHost();
99
100
  const port = startedContainer.getMappedPort(8080);
100
- logger_1.log.debug(`Connecting to Reaper ${containerId} on ${host}:${port}`);
101
- const socket = new net_1.Socket();
102
- socket.unref();
103
- socket
104
- .on("timeout", () => logger_1.log.error(`Reaper ${containerId} socket timed out`))
105
- .on("error", (err) => logger_1.log.error(`Reaper ${containerId} socket error: ${err}`))
106
- .on("close", (hadError) => {
107
- if (hadError) {
108
- logger_1.log.error(`Connection to Reaper ${containerId} closed with error`);
109
- }
110
- else {
111
- logger_1.log.warn(`Connection to Reaper ${containerId} closed`);
112
- }
113
- });
114
- return new Promise((resolve) => {
115
- socket.connect((0, port_1.getContainerPort)(port), host, () => {
116
- logger_1.log.debug(`Connected to Reaper ${containerId}`);
117
- socket.write(`label=${labels_1.LABEL_SESSION_ID}=${session_id_1.sessionId}\r\n`);
118
- const reaper = new RealReaper(startedContainer, socket);
119
- resolve(reaper);
101
+ const retryStrategy = new retry_strategy_1.IntervalRetryStrategy(1000);
102
+ const retryResult = yield retryStrategy.retryUntil((attempt) => {
103
+ return new Promise((resolve) => {
104
+ logger_1.log.debug(`Connecting to Reaper (attempt ${attempt + 1}) ${containerId} on ${host}:${port}`);
105
+ const socket = new net_1.Socket();
106
+ socket
107
+ .unref()
108
+ .on("timeout", () => logger_1.log.error(`Reaper ${containerId} socket timed out`))
109
+ .on("error", (err) => logger_1.log.error(`Reaper ${containerId} socket error: ${err}`))
110
+ .on("close", (hadError) => {
111
+ if (hadError) {
112
+ logger_1.log.error(`Connection to Reaper ${containerId} closed with error`);
113
+ }
114
+ else {
115
+ logger_1.log.warn(`Connection to Reaper ${containerId} closed`);
116
+ }
117
+ resolve(undefined);
118
+ })
119
+ .connect((0, port_1.getContainerPort)(port), host, () => {
120
+ logger_1.log.debug(`Connected to Reaper ${containerId}`);
121
+ socket.write(`label=${labels_1.LABEL_SESSION_ID}=${session_id_1.sessionId}\r\n`);
122
+ const reaper = new RealReaper(startedContainer, socket);
123
+ resolve(reaper);
124
+ });
120
125
  });
121
- });
126
+ }, (result) => result !== undefined, () => new Error(`Failed to connect to Reaper ${containerId}`), 4000);
127
+ if (retryResult instanceof RealReaper) {
128
+ return retryResult;
129
+ }
130
+ else {
131
+ throw retryResult;
132
+ }
122
133
  });
123
134
  }
124
135
  }
@@ -12,6 +12,6 @@ declare abstract class AbstractRetryStrategy<T, U> implements RetryStrategy<T, U
12
12
  export declare class IntervalRetryStrategy<T, U> extends AbstractRetryStrategy<T, U> {
13
13
  private readonly interval;
14
14
  constructor(interval: number);
15
- retryUntil(fn: () => Promise<T>, predicate: (result: T) => boolean, onTimeout: () => U, timeout: number): Promise<T | U>;
15
+ retryUntil(fn: (attempt: number) => Promise<T>, predicate: (result: T) => boolean, onTimeout: () => U, timeout: number): Promise<T | U>;
16
16
  }
17
17
  export {};
@@ -30,13 +30,14 @@ class IntervalRetryStrategy extends AbstractRetryStrategy {
30
30
  retryUntil(fn, predicate, onTimeout, timeout) {
31
31
  return __awaiter(this, void 0, void 0, function* () {
32
32
  const startTime = this.clock.getTime();
33
- let result = yield fn();
33
+ let attemptNumber = 0;
34
+ let result = yield fn(attemptNumber++);
34
35
  while (!predicate(result)) {
35
36
  if (this.hasTimedOut(timeout, startTime)) {
36
37
  return onTimeout();
37
38
  }
38
39
  yield this.wait(this.interval);
39
- result = yield fn();
40
+ result = yield fn(attemptNumber++);
40
41
  }
41
42
  return result;
42
43
  });
@@ -0,0 +1,4 @@
1
+ import Dockerode from "dockerode";
2
+ import { BoundPorts } from "./bound-ports";
3
+ import { WaitStrategy } from "./wait-strategy";
4
+ export declare const waitForContainer: (container: Dockerode.Container, waitStrategy: WaitStrategy, boundPorts: BoundPorts) => Promise<void>;
@@ -0,0 +1,34 @@
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.waitForContainer = void 0;
13
+ const logger_1 = require("./logger");
14
+ const stop_container_1 = require("./docker/functions/container/stop-container");
15
+ const remove_container_1 = require("./docker/functions/container/remove-container");
16
+ const waitForContainer = (container, waitStrategy, boundPorts) => __awaiter(void 0, void 0, void 0, function* () {
17
+ logger_1.log.debug(`Waiting for container to be ready: ${container.id}`);
18
+ try {
19
+ yield waitStrategy.waitUntilReady(container, boundPorts);
20
+ logger_1.log.info("Container is ready");
21
+ }
22
+ catch (err) {
23
+ logger_1.log.error(`Container failed to be ready: ${err}`);
24
+ try {
25
+ yield (0, stop_container_1.stopContainer)(container, { timeout: 0 });
26
+ yield (0, remove_container_1.removeContainer)(container, { removeVolumes: true });
27
+ }
28
+ catch (stopErr) {
29
+ logger_1.log.error(`Failed to stop container after it failed to be ready: ${stopErr}`);
30
+ }
31
+ throw err;
32
+ }
33
+ });
34
+ exports.waitForContainer = waitForContainer;
@@ -28,4 +28,5 @@ export declare class LogWaitStrategy extends AbstractWaitStrategy {
28
28
  export declare class HealthCheckWaitStrategy extends AbstractWaitStrategy {
29
29
  waitUntilReady(container: Dockerode.Container): Promise<void>;
30
30
  }
31
+ export declare const defaultWaitStrategy: (host: string, container: Dockerode.Container) => HostPortWaitStrategy;
31
32
  export {};
@@ -12,9 +12,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.HealthCheckWaitStrategy = exports.LogWaitStrategy = exports.HostPortWaitStrategy = void 0;
15
+ exports.defaultWaitStrategy = exports.HealthCheckWaitStrategy = exports.LogWaitStrategy = exports.HostPortWaitStrategy = void 0;
16
16
  const byline_1 = __importDefault(require("byline"));
17
17
  const logger_1 = require("./logger");
18
+ const port_check_1 = require("./port-check");
18
19
  const retry_strategy_1 = require("./retry-strategy");
19
20
  const container_logs_1 = require("./docker/functions/container/container-logs");
20
21
  const inspect_container_1 = require("./docker/functions/container/inspect-container");
@@ -127,3 +128,5 @@ class HealthCheckWaitStrategy extends AbstractWaitStrategy {
127
128
  }
128
129
  }
129
130
  exports.HealthCheckWaitStrategy = HealthCheckWaitStrategy;
131
+ const defaultWaitStrategy = (host, container) => new HostPortWaitStrategy(new port_check_1.HostPortCheck(host), new port_check_1.InternalPortCheck(container));
132
+ exports.defaultWaitStrategy = defaultWaitStrategy;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "testcontainers",
3
3
  "author": "Cristian Greco",
4
- "version": "9.0.0",
4
+ "version": "9.0.1",
5
5
  "main": "dist/index",
6
6
  "types": "dist/index",
7
7
  "keywords": [