Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OneShotWaitStrategy #730

Merged
merged 10 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { GenericContainer } from "../generic-container/generic-container";
import { Wait } from "./wait";
import { checkContainerIsHealthy } from "../utils/test-helper";

jest.setTimeout(180_000);

const mockImageInspect = jest.fn();
cristianrgreco marked this conversation as resolved.
Show resolved Hide resolved
jest.mock(
"dockerode",
() =>
function () {
return {
getContainer: () => ({
inspect: mockImageInspect,
}),
};
}
);

describe("OneShotStartupCheckStrategy", () => {
it("should wait for log", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withExposedPorts(8080)
.withWaitStrategy(Wait.forOneShotStartup())
.start();

await checkContainerIsHealthy(container);

await container.stop();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Dockerode, { ContainerInspectInfo } from "dockerode";
import { StartupCheckStrategy, StartupStatus } from "./startup-check-strategy";

export class OneShotStartupCheckStrategy extends StartupCheckStrategy {
DOCKER_TIMESTAMP_ZERO = "0001-01-01T00:00:00Z";

private isDockerTimestampNonEmpty(dockerTimestamp: string) {
return dockerTimestamp !== "" && dockerTimestamp !== this.DOCKER_TIMESTAMP_ZERO && Date.parse(dockerTimestamp) > 0;
}

private isContainerStopped({ State: state }: ContainerInspectInfo): boolean {
if (state.Running || state.Paused) {
return false;
}

return this.isDockerTimestampNonEmpty(state.StartedAt) && this.isDockerTimestampNonEmpty(state.FinishedAt);
}

public async checkStartupState(dockerClient: Dockerode, containerId: string): Promise<StartupStatus> {
const info = await dockerClient.getContainer(containerId).inspect();

if (!this.isContainerStopped(info)) {
return "PENDING";
}

if (info.State.ExitCode === 0) {
return "SUCCESS";
}

return "FAIL";
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { AbstractWaitStrategy } from "./wait-strategy";
import Dockerode from "dockerode";
import { ContainerRuntimeClient } from "../container-runtime";
import { getContainerRuntimeClient } from "../container-runtime";
import { IntervalRetry, log } from "../common";

export type StartupStatus = "PENDING" | "SUCCESS" | "FAIL";

export abstract class StartupCheckStrategy extends AbstractWaitStrategy {
constructor(private readonly client: ContainerRuntimeClient) {
constructor() {
super();
}

public abstract checkStartupState(dockerClient: Dockerode, containerId: string): Promise<StartupStatus>;

public override async waitUntilReady(container: Dockerode.Container): Promise<void> {
const client = await getContainerRuntimeClient();

const startupStatus = await new IntervalRetry<StartupStatus, Error>(1000).retryUntil(
async () => await this.checkStartupState(this.client.container.dockerode, container.id),
async () => await this.checkStartupState(client.container.dockerode, container.id),
(startupStatus) => startupStatus === "SUCCESS" || startupStatus === "FAIL",
() => {
const message = `Container not accessible after ${this.startupTimeout}ms`;
Expand Down
5 changes: 5 additions & 0 deletions packages/testcontainers/src/wait-strategies/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Log, LogWaitStrategy } from "./log-wait-strategy";
import { ShellWaitStrategy } from "./shell-wait-strategy";
import { HostPortWaitStrategy } from "./host-port-wait-strategy";
import { CompositeWaitStrategy } from "./composite-wait-strategy";
import { OneShotStartupCheckStrategy } from "./one-shot-startup-startegy";

export class Wait {
public static forAll(waitStrategies: WaitStrategy[]): CompositeWaitStrategy {
Expand All @@ -23,6 +24,10 @@ export class Wait {
return new HealthCheckWaitStrategy();
}

public static forOneShotStartup(): WaitStrategy {
return new OneShotStartupCheckStrategy();
}

public static forHttp(
path: string,
port: number,
Expand Down
Loading