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 support for abortOnContainerExit to HTTP wait strategy #693

Merged
Changes from 1 commit
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
33 changes: 30 additions & 3 deletions packages/testcontainers/src/wait-strategies/http-wait-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class HttpWaitStrategy extends AbstractWaitStrategy {
private _allowInsecure = false;
private readTimeout = 1000;

constructor(private readonly path: string, private readonly port: number) {
constructor(private readonly path: string, private readonly port: number, private readonly shutdownOnExit = false) {
cristianrgreco marked this conversation as resolved.
Show resolved Hide resolved
super();
}

Expand Down Expand Up @@ -66,9 +66,9 @@ export class HttpWaitStrategy extends AbstractWaitStrategy {

public async waitUntilReady(container: Dockerode.Container, boundPorts: BoundPorts): Promise<void> {
log.debug(`Waiting for HTTP...`, { containerId: container.id });
const client = await getContainerRuntimeClient();

await new IntervalRetry<Response | undefined, Error>(this.readTimeout).retryUntil(
const client = await getContainerRuntimeClient();
const healthCheckPromise = new IntervalRetry<Response | undefined, Error>(this.readTimeout).retryUntil(
async () => {
try {
const url = `${this.protocol}://${client.info.containerRuntime.host}:${boundPorts.getBinding(this.port)}${
Expand Down Expand Up @@ -107,9 +107,36 @@ export class HttpWaitStrategy extends AbstractWaitStrategy {
this.startupTimeout
);

await Promise.race([this.waitContainerNotExited(container), healthCheckPromise]);

log.debug(`HTTP wait strategy complete`, { containerId: container.id });
}

private async waitContainerNotExited(container: Dockerode.Container) {
cristianrgreco marked this conversation as resolved.
Show resolved Hide resolved
const exitStatus = 'exited';
const timeout = this.startupTimeout + 1000; // added 1 sec to avoid race condition
const status = await new IntervalRetry<string, null>(500).retryUntil(
async () => (await container.inspect()).State.Status,
(status) => status === exitStatus,
() => null,
timeout
);

if (status === null) {
return;
}

if (status === exitStatus) {
const tail = 50;
const lastLogs = (await container.logs({ tail, stdout: true, stderr: true })).toString();
const message = `container exited, last ${tail} logs: ${lastLogs}`;
cristianrgreco marked this conversation as resolved.
Show resolved Hide resolved

log.error(message, { containerId: container.id });
cristianrgreco marked this conversation as resolved.
Show resolved Hide resolved

if (this.shutdownOnExit) throw new Error(message);
}
}

private getAgent(): Agent | undefined {
if (this._allowInsecure) {
return new https.Agent({
Expand Down