testcontainers 12.1.0 → 12.2.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.
@@ -169,6 +169,11 @@ class DockerContainerClient {
169
169
  const actualLogStream = stream;
170
170
  actualLogStream.socket?.unref();
171
171
  const demuxedStream = await this.demuxStream(container.id, actualLogStream);
172
+ if (proxyStream.destroyed) {
173
+ demuxedStream.destroy();
174
+ return;
175
+ }
176
+ proxyStream.once("close", () => demuxedStream.destroy());
172
177
  demuxedStream.pipe(proxyStream);
173
178
  demuxedStream.on("error", (err) => proxyStream.emit("error", err));
174
179
  demuxedStream.on("end", () => proxyStream.end());
@@ -2,9 +2,10 @@ export declare class ImageName {
2
2
  readonly registry: string | undefined;
3
3
  readonly image: string;
4
4
  readonly tag: string;
5
+ readonly digest?: string | undefined;
5
6
  readonly string: string;
6
7
  private static readonly hexRE;
7
- constructor(registry: string | undefined, image: string, tag: string);
8
+ constructor(registry: string | undefined, image: string, tag: string, digest?: string | undefined);
8
9
  equals(other: ImageName): boolean;
9
10
  static fromString(string: string): ImageName;
10
11
  private static getRegistry;
@@ -6,12 +6,18 @@ class ImageName {
6
6
  registry;
7
7
  image;
8
8
  tag;
9
+ digest;
9
10
  string;
10
11
  static hexRE = /^[0-9a-f]{64}$/i;
11
- constructor(registry, image, tag) {
12
+ constructor(registry, image, tag, digest) {
12
13
  this.registry = registry;
13
14
  this.image = image;
14
15
  this.tag = tag;
16
+ this.digest = digest;
17
+ if (!this.digest && this.tag.startsWith("sha256:")) {
18
+ // A digest-only reference (image@sha256:...) keeps the digest as its tag.
19
+ this.digest = this.tag;
20
+ }
15
21
  if (!this.registry && process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX) {
16
22
  const prefix = process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
17
23
  // Parse the registry. If it's undefined - then the whole prefix is a registry.
@@ -30,12 +36,13 @@ class ImageName {
30
36
  }
31
37
  common_1.log.info(message);
32
38
  }
39
+ const tagAndDigest = this.digest ? `${this.tag}@${this.digest}` : this.tag;
33
40
  if (this.registry) {
34
41
  if (this.tag.startsWith("sha256:")) {
35
42
  this.string = `${this.registry}/${this.image}@${this.tag}`;
36
43
  }
37
44
  else {
38
- this.string = `${this.registry}/${this.image}:${this.tag}`;
45
+ this.string = `${this.registry}/${this.image}:${tagAndDigest}`;
39
46
  }
40
47
  }
41
48
  else if (this.tag === "latest" && ImageName.hexRE.test(this.image)) {
@@ -51,25 +58,28 @@ class ImageName {
51
58
  this.string = `${this.image}@${this.tag}`;
52
59
  }
53
60
  else {
54
- this.string = `${this.image}:${this.tag}`;
61
+ this.string = `${this.image}:${tagAndDigest}`;
55
62
  }
56
63
  }
57
64
  equals(other) {
58
- return this.registry === other.registry && this.image === other.image && this.tag === other.tag;
65
+ return (this.registry === other.registry &&
66
+ this.image === other.image &&
67
+ this.tag === other.tag &&
68
+ this.digest === other.digest);
59
69
  }
60
70
  static fromString(string) {
61
71
  const registry = this.getRegistry(string);
62
72
  const stringWithoutRegistry = registry ? string.split("/").slice(1).join("/") : string;
63
- if (stringWithoutRegistry.includes("@")) {
64
- const [image, tag] = stringWithoutRegistry.split("@");
65
- return new ImageName(registry, image, tag);
73
+ const [imageAndTag, digest] = stringWithoutRegistry.split("@");
74
+ if (imageAndTag.includes(":")) {
75
+ const [image, tag] = imageAndTag.split(":");
76
+ return new ImageName(registry, image, tag, digest);
66
77
  }
67
- else if (stringWithoutRegistry.includes(":")) {
68
- const [image, tag] = stringWithoutRegistry.split(":");
69
- return new ImageName(registry, image, tag);
78
+ else if (digest) {
79
+ return new ImageName(registry, imageAndTag, digest);
70
80
  }
71
81
  else {
72
- return new ImageName(registry, stringWithoutRegistry, "latest");
82
+ return new ImageName(registry, imageAndTag, "latest");
73
83
  }
74
84
  }
75
85
  static getRegistry(string) {
@@ -98,6 +98,10 @@ class DockerComposeEnvironment {
98
98
  if (this.build) {
99
99
  commandOptions.push("--build");
100
100
  }
101
+ const shouldPull = this.pullPolicy.shouldPull();
102
+ if (shouldPull === "never") {
103
+ commandOptions.push("--pull", "never");
104
+ }
101
105
  if (!this.recreate) {
102
106
  commandOptions.push("--no-recreate");
103
107
  }
@@ -106,7 +110,7 @@ class DockerComposeEnvironment {
106
110
  composeOptions.push("--env-file", this.environmentFile);
107
111
  }
108
112
  this.profiles.forEach((profile) => composeOptions.push("--profile", profile));
109
- if (this.pullPolicy.shouldPull()) {
113
+ if (shouldPull === true) {
110
114
  await client.compose.pull(options, services);
111
115
  }
112
116
  await client.compose.up({
@@ -52,6 +52,11 @@ class GenericContainerBuilder {
52
52
  return this;
53
53
  }
54
54
  async build(image = `localhost/${this.uuid.nextUuid()}:${this.uuid.nextUuid()}`, options = { deleteOnExit: true }) {
55
+ const shouldPull = this.pullPolicy.shouldPull();
56
+ // https://github.com/docker/buildx/issues/1889
57
+ if (shouldPull === "never") {
58
+ throw new Error("Never-pull policies are not supported for Dockerfile builds: the Docker build API cannot forbid image pulls");
59
+ }
55
60
  const client = await (0, container_runtime_1.getContainerRuntimeClient)();
56
61
  const reaper = await (0, reaper_1.getReaper)(client);
57
62
  const imageName = container_runtime_1.ImageName.fromString(image);
@@ -75,7 +80,7 @@ class GenericContainerBuilder {
75
80
  platform: this.platform,
76
81
  version: this.buildkit ? "2" : "1",
77
82
  };
78
- if (this.pullPolicy.shouldPull()) {
83
+ if (shouldPull === true) {
79
84
  buildOptions.pull = true;
80
85
  }
81
86
  await client.image.build(this.context, buildOptions);
@@ -59,11 +59,19 @@ class GenericContainer {
59
59
  return this.imageName.string === (0, reaper_1.getReaperImage)();
60
60
  }
61
61
  async start() {
62
+ const shouldPull = this.pullPolicy.shouldPull();
62
63
  const client = await (0, container_runtime_1.getContainerRuntimeClient)();
63
- await client.image.pull(this.imageName, {
64
- force: this.pullPolicy.shouldPull(),
65
- platform: this.createOpts.platform,
66
- });
64
+ if (shouldPull === "never") {
65
+ if (!(await client.image.exists(this.imageName))) {
66
+ throw new Error(`Image "${this.imageName.string}" does not exist locally and pull policy is "never"`);
67
+ }
68
+ }
69
+ else {
70
+ await client.image.pull(this.imageName, {
71
+ force: shouldPull,
72
+ platform: this.createOpts.platform,
73
+ });
74
+ }
67
75
  if (this.beforeContainerCreated) {
68
76
  await this.beforeContainerCreated();
69
77
  }
@@ -1,7 +1,8 @@
1
1
  export interface ImagePullPolicy {
2
- shouldPull(): boolean;
2
+ shouldPull(): boolean | "never";
3
3
  }
4
4
  export declare class PullPolicy {
5
5
  static defaultPolicy(): ImagePullPolicy;
6
6
  static alwaysPull(): ImagePullPolicy;
7
+ static neverPull(): ImagePullPolicy;
7
8
  }
@@ -11,6 +11,11 @@ class AlwaysPullPolicy {
11
11
  return true;
12
12
  }
13
13
  }
14
+ class NeverPullPolicy {
15
+ shouldPull() {
16
+ return "never";
17
+ }
18
+ }
14
19
  class PullPolicy {
15
20
  static defaultPolicy() {
16
21
  return new DefaultPullPolicy();
@@ -18,5 +23,8 @@ class PullPolicy {
18
23
  static alwaysPull() {
19
24
  return new AlwaysPullPolicy();
20
25
  }
26
+ static neverPull() {
27
+ return new NeverPullPolicy();
28
+ }
21
29
  }
22
30
  exports.PullPolicy = PullPolicy;
@@ -1 +1 @@
1
- export declare const LIB_VERSION = "12.1.0";
1
+ export declare const LIB_VERSION = "12.2.0";
package/build/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LIB_VERSION = void 0;
4
- exports.LIB_VERSION = "12.1.0";
4
+ exports.LIB_VERSION = "12.2.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testcontainers",
3
- "version": "12.1.0",
3
+ "version": "12.2.0",
4
4
  "author": "Cristian Greco",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -36,7 +36,7 @@
36
36
  "async-lock": "^1.4.1",
37
37
  "byline": "^5.0.0",
38
38
  "debug": "^4.4.3",
39
- "docker-compose": "^1.4.2",
39
+ "docker-compose": "^1.5.0",
40
40
  "dockerode": "^5.0.1",
41
41
  "get-port": "^5.1.1",
42
42
  "proper-lockfile": "^4.1.2",
@@ -44,7 +44,7 @@
44
44
  "ssh-remote-port-forward": "^1.0.4",
45
45
  "tar-fs": "^3.1.3",
46
46
  "tmp": "^0.2.7",
47
- "undici": "^8.9.0"
47
+ "undici": "^8.10.2"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@types/archiver": "^7.0.0",