Skip to content

Commit

Permalink
GenericContainerBuilder false pull policy should not pull image (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar authored Jun 16, 2024
1 parent ab46673 commit 441b61f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AuthConfig, BuildArgs, RegistryConfig } from "../types";
import type { ImageBuildOptions } from "dockerode";
import path from "path";
import { GenericContainer } from "./generic-container";
import { ImagePullPolicy, PullPolicy } from "../utils/pull-policy";
Expand Down Expand Up @@ -62,16 +63,22 @@ export class GenericContainerBuilder {
}

log.info(`Building Dockerfile "${dockerfile}" as image "${imageName.string}"...`);
await client.image.build(this.context, {

const buildOptions: ImageBuildOptions = {
t: imageName.string,
dockerfile: this.dockerfileName,
buildargs: this.buildArgs,
pull: this.pullPolicy ? "true" : undefined,
nocache: !this.cache,
registryconfig: registryConfig,
labels,
target: this.target,
});
};

if (this.pullPolicy.shouldPull()) {
buildOptions.pull = "true";
}

await client.image.build(this.context, buildOptions);

const container = new GenericContainer(imageName.string);
if (!(await client.image.exists(imageName))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GenericContainer } from "./generic-container";
import { Wait } from "../wait-strategies/wait";
import { PullPolicy } from "../utils/pull-policy";
import { RandomUuid } from "../common";
import { getContainerRuntimeClient } from "../container-runtime";
import { ImageName, getContainerRuntimeClient } from "../container-runtime";
import { getReaper } from "../reaper/reaper";
import { LABEL_TESTCONTAINERS_SESSION_ID } from "../utils/labels";
import {
Expand Down Expand Up @@ -70,6 +70,25 @@ describe("GenericContainer Dockerfile", () => {

dockerEventStream.destroy();
});

it("should not pull existing image without pull policy", async () => {
const client = await getContainerRuntimeClient();
await client.image.pull(new ImageName("docker.io", "node", "10-alpine"));

const dockerfile = path.resolve(fixtures, "docker");
const containerSpec = GenericContainer.fromDockerfile(dockerfile);

await containerSpec.build();
const dockerEventStream = await getDockerEventStream();
const dockerPullEventPromise = waitForDockerEvent(dockerEventStream, "pull");
let hasResolved = false;
dockerPullEventPromise.then(() => (hasResolved = true));
await containerSpec.build();

expect(hasResolved).toBeFalsy();

dockerEventStream.destroy();
});
}

it("should build and start with custom file name", async () => {
Expand Down

0 comments on commit 441b61f

Please sign in to comment.