Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/features/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,26 @@ const container = await new GenericContainer("alpine:3.10").start();
Testcontainers will automatically pull an image if it doesn't exist. This is configurable:

```javascript
const { GenericContainer, AlwaysPullPolicy } = require("testcontainers");
const { GenericContainer, PullPolicy } = require("testcontainers");

const container = await new GenericContainer("alpine")
.withPullPolicy(new AlwaysPullPolicy())
.withPullPolicy(PullPolicy.alwaysPull())
.start();
```

#### Custom pull policy

```typescript
const { GenericContainer, ImagePullPolicy } = require("testcontainers");

class CustomPullPolicy implements ImagePullPolicy {
public shouldPull(): boolean {
return true;
}
}

const container = await new GenericContainer("alpine")
.withPullPolicy(new CustomPullPolicy())
.start();
```

Expand Down
23 changes: 20 additions & 3 deletions docs/features/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,33 @@ const container = await GenericContainer
const startedContainer = await container.start();
```

### With pull policy
### With a pull policy

Testcontainers will automatically pull an image if it doesn't exist. This is configurable:

```javascript
const { GenericContainer, AlwaysPullPolicy } = require("testcontainers");
const { GenericContainer, PullPolicy } = require("testcontainers");

const container = await GenericContainer
.fromDockerfile("/path/to/build-context")
.withPullPolicy(new AlwaysPullPolicy())
.withPullPolicy(PullPolicy.alwaysPull())
.build();
```

#### Custom pull policy

```typescript
const { GenericContainer, ImagePullPolicy } = require("testcontainers");

class CustomPullPolicy implements ImagePullPolicy {
public shouldPull(): boolean {
return true;
}
}

const container = await GenericContainer
.fromDockerfile("/path/to/build-context")
.withPullPolicy(new CustomPullPolicy())
.build();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import fetch from "node-fetch";
import path from "path";
import { DockerComposeEnvironment } from "./docker-compose-environment";
import { Wait } from "../wait-strategy/wait";
import { PullPolicy } from "../pull-policy";
import {
checkEnvironmentContainerIsHealthy,
composeContainerName,
getRunningContainerNames,
getVolumeNames,
waitForDockerEvent,
} from "../test-helper";
import { AlwaysPullPolicy } from "../pull-policy";

describe("DockerComposeEnvironment", () => {
jest.setTimeout(180_000);
Expand Down Expand Up @@ -45,7 +45,7 @@ describe("DockerComposeEnvironment", () => {

const startedEnv1 = await env.up();
const dockerPullEventPromise = waitForDockerEvent("pull", 2);
const startedEnv2 = await env.withPullPolicy(new AlwaysPullPolicy()).up();
const startedEnv2 = await env.withPullPolicy(PullPolicy.alwaysPull()).up();
await dockerPullEventPromise;

await startedEnv1.stop();
Expand All @@ -57,7 +57,7 @@ describe("DockerComposeEnvironment", () => {

const startedEnv1 = await env.up(["service_2"]);
const dockerPullEventPromise = waitForDockerEvent("pull");
const startedEnv2 = await env.withPullPolicy(new AlwaysPullPolicy()).up(["service_2"]);
const startedEnv2 = await env.withPullPolicy(PullPolicy.alwaysPull()).up(["service_2"]);
await dockerPullEventPromise;

await startedEnv1.stop();
Expand Down
6 changes: 3 additions & 3 deletions src/docker-compose-environment/docker-compose-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { dockerComposeDown } from "../docker-compose/functions/docker-compose-do
import { dockerComposeUp } from "../docker-compose/functions/docker-compose-up";
import { waitForContainer } from "../wait-for-container";
import { defaultWaitStrategy } from "../wait-strategy/default-wait-strategy";
import { DefaultPullPolicy, PullPolicy } from "../pull-policy";
import { ImagePullPolicy, PullPolicy } from "../pull-policy";
import { dockerComposePull } from "../docker-compose/functions/docker-compose-pull";

export class DockerComposeEnvironment {
Expand All @@ -30,7 +30,7 @@ export class DockerComposeEnvironment {
private environmentFile = "";
private profiles: string[] = [];
private environment: Environment = {};
private pullPolicy: PullPolicy = new DefaultPullPolicy();
private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy();
private waitStrategy: { [containerName: string]: WaitStrategy } = {};
private startupTimeout = 60_000;

Expand Down Expand Up @@ -66,7 +66,7 @@ export class DockerComposeEnvironment {
return this;
}

public withPullPolicy(pullPolicy: PullPolicy): this {
public withPullPolicy(pullPolicy: ImagePullPolicy): this {
this.pullPolicy = pullPolicy;
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/docker/functions/image/build-image.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DockerImageName } from "../../../docker-image-name";
import { PullPolicy } from "../../../pull-policy";
import { ImagePullPolicy } from "../../../pull-policy";
import { log } from "../../../logger";
import tar from "tar-fs";
import byline from "byline";
Expand All @@ -16,7 +16,7 @@ export type BuildImageOptions = {
context: string;
dockerfileName: string;
buildArgs: BuildArgs;
pullPolicy: PullPolicy;
pullPolicy: ImagePullPolicy;
registryConfig: RegistryConfig;
cache: boolean;
};
Expand Down
6 changes: 3 additions & 3 deletions src/generic-container/generic-container-builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthConfig, BuildArgs, RegistryConfig } from "../docker/types";
import { DefaultPullPolicy, PullPolicy } from "../pull-policy";
import { ImagePullPolicy, PullPolicy } from "../pull-policy";
import { RandomUuid, Uuid } from "../uuid";
import { ReaperInstance } from "../reaper";
import { DockerImageName } from "../docker-image-name";
Expand All @@ -14,7 +14,7 @@ import { imageExists } from "../docker/functions/image/image-exists";

export class GenericContainerBuilder {
private buildArgs: BuildArgs = {};
private pullPolicy: PullPolicy = new DefaultPullPolicy();
private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy();
private cache = true;

constructor(
Expand All @@ -28,7 +28,7 @@ export class GenericContainerBuilder {
return this;
}

public withPullPolicy(pullPolicy: PullPolicy): this {
public withPullPolicy(pullPolicy: ImagePullPolicy): this {
this.pullPolicy = pullPolicy;
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/generic-container/generic-container-dockerfile.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import { GenericContainer } from "./generic-container";
import { AlwaysPullPolicy } from "../pull-policy";
import { PullPolicy } from "../pull-policy";
import { Wait } from "../wait-strategy/wait";
import { checkContainerIsHealthy, waitForDockerEvent } from "../test-helper";

Expand All @@ -23,7 +23,7 @@ describe("GenericContainer Dockerfile", () => {
if (!process.env["CI_PODMAN"]) {
it("should use pull policy", async () => {
const dockerfile = path.resolve(fixtures, "docker");
const containerSpec = GenericContainer.fromDockerfile(dockerfile).withPullPolicy(new AlwaysPullPolicy());
const containerSpec = GenericContainer.fromDockerfile(dockerfile).withPullPolicy(PullPolicy.alwaysPull());

await containerSpec.build();
const dockerPullEventPromise = waitForDockerEvent("pull");
Expand Down
4 changes: 2 additions & 2 deletions src/generic-container/generic-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fetch from "node-fetch";
import path from "path";
import getPort from "get-port";
import { GenericContainer } from "./generic-container";
import { AlwaysPullPolicy } from "../pull-policy";
import { PullPolicy } from "../pull-policy";
import { checkContainerIsHealthy, waitForDockerEvent } from "../test-helper";
import { getContainerById } from "../docker/functions/container/get-container";

Expand Down Expand Up @@ -224,7 +224,7 @@ describe("GenericContainer", () => {

const startedContainer1 = await container.start();
const dockerPullEventPromise = waitForDockerEvent("pull");
const startedContainer2 = await container.withPullPolicy(new AlwaysPullPolicy()).start();
const startedContainer2 = await container.withPullPolicy(PullPolicy.alwaysPull()).start();
await dockerPullEventPromise;

await startedContainer1.stop();
Expand Down
6 changes: 3 additions & 3 deletions src/generic-container/generic-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AsyncLock from "async-lock";
import { BoundPorts } from "../bound-ports";
import { containerLog, log } from "../logger";
import { PortWithOptionalBinding } from "../port";
import { DefaultPullPolicy, PullPolicy } from "../pull-policy";
import { ImagePullPolicy, PullPolicy } from "../pull-policy";
import { ReaperInstance } from "../reaper";
import { DockerImageName } from "../docker-image-name";
import { StartedTestContainer, TestContainer } from "../test-container";
Expand Down Expand Up @@ -53,7 +53,7 @@ export class GenericContainer implements TestContainer {
protected tarToCopy?: archiver.Archiver;
protected networkMode?: string;
protected networkAliases: string[] = [];
protected pullPolicy: PullPolicy = new DefaultPullPolicy();
protected pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy();

constructor(readonly image: string) {
const imageName = DockerImageName.fromString(image);
Expand Down Expand Up @@ -362,7 +362,7 @@ export class GenericContainer implements TestContainer {
return this;
}

public withPullPolicy(pullPolicy: PullPolicy): this {
public withPullPolicy(pullPolicy: ImagePullPolicy): this {
this.pullPolicy = pullPolicy;
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { Network, StartedNetwork, StoppedNetwork } from "./network";

export { Wait } from "./wait-strategy/wait";
export { StartupCheckStrategy, StartupStatus } from "./wait-strategy/startup-check-strategy";
export { PullPolicy, DefaultPullPolicy, AlwaysPullPolicy } from "./pull-policy";
export { PullPolicy, ImagePullPolicy } from "./pull-policy";
export { InspectResult } from "./docker/functions/container/inspect-container";

export { AbstractStartedContainer } from "./modules/abstract-started-container";
Expand Down
19 changes: 19 additions & 0 deletions src/pull-policy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PullPolicy, ImagePullPolicy } from "./index";

test("default pull policy should return false", () => {
expect(PullPolicy.defaultPolicy().shouldPull()).toBe(false);
});

test("always pull policy should return true", () => {
expect(PullPolicy.alwaysPull().shouldPull()).toBe(true);
});

test("should be able to create a custom pull policy", () => {
class CustomPullPolicy implements ImagePullPolicy {
public shouldPull(): boolean {
return true;
}
}

expect(new CustomPullPolicy().shouldPull()).toBe(true);
});
16 changes: 13 additions & 3 deletions src/pull-policy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
export interface PullPolicy {
export interface ImagePullPolicy {
shouldPull(): boolean;
}

export class DefaultPullPolicy implements PullPolicy {
class DefaultPullPolicy implements ImagePullPolicy {
public shouldPull(): boolean {
return false;
}
}

export class AlwaysPullPolicy implements PullPolicy {
class AlwaysPullPolicy implements ImagePullPolicy {
public shouldPull(): boolean {
return true;
}
}

export class PullPolicy {
public static defaultPolicy(): ImagePullPolicy {
return new DefaultPullPolicy();
}

public static alwaysPull(): ImagePullPolicy {
return new AlwaysPullPolicy();
}
}
4 changes: 2 additions & 2 deletions src/test-container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PortWithOptionalBinding } from "./port";
import { PullPolicy } from "./pull-policy";
import { ImagePullPolicy } from "./pull-policy";
import { WaitStrategy } from "./wait-strategy/wait-strategy";
import { Readable } from "stream";
import {
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface TestContainer {

withUser(user: string): this;

withPullPolicy(pullPolicy: PullPolicy): this;
withPullPolicy(pullPolicy: ImagePullPolicy): this;

withReuse(): this;

Expand Down