From de5e289798c781433e83f02421b105085e991a2c Mon Sep 17 00:00:00 2001 From: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com> Date: Fri, 28 Feb 2025 15:26:18 -0300 Subject: [PATCH] compose.ts throws on container start error --- tests/support/compose.ts | 43 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/tests/support/compose.ts b/tests/support/compose.ts index 519ebef..1fba476 100644 --- a/tests/support/compose.ts +++ b/tests/support/compose.ts @@ -1,6 +1,7 @@ import { downAll, logs, + ps, upAll, type IDockerComposeOptions, } from "docker-compose"; @@ -14,25 +15,59 @@ const options: IDockerComposeOptions = { // log: true, }; -export async function testnetUp(): Promise { +export async function testnetUp(): Promise { await upAll(options); - const targetString = "QCReady"; // Replace with the actual string you expect - + const targetString = "QCReady"; + console.log("waiting for logs"); while (true) { const logOutput = await logs("alice", options); - // const targetString = `"event":"NewRound","reason":"QCReady"`; // Replace with the actual string you expect if (logOutput.out.includes(targetString)) { console.log("testnet started, proceeding..."); break; // Exit the loop when the message is found } + if (logOutput.out.includes("exited with code")) { + console.log(`last logs: ${logOutput.out}`); + throw new Error("containers exited with non zero code!"); + } + + const runs = await isComposeRunning(composeFilePath); + if (!runs) { + console.log(`last logs: ${logOutput.out}`); + + throw new Error("containers are not running!"); + } // check logs every second await new Promise((resolve) => setTimeout(resolve, 1_000)); } + return true; } export async function testnetDown() { await downAll(options); } + +async function isComposeRunning(path: string): Promise { + try { + const result = await ps({ + cwd: path, + commandOptions: [["--format", "json"]], + }); + if (!result || result.data.services.length == 0) { + console.error("[ERROR] no containers running"); + return false; + } + for (const service of result.data.services) { + if (service.state != "running") { + console.error(`[ERROR] service ${service.name} is not running`); + return false; + } + } + } catch { + console.error("[ERROR] could not run docker-compose ps"); + return false; + } + return true; +}