Skip to content

Commit

Permalink
feat(test-tooling): add corda AIO emitContainerLogs option
Browse files Browse the repository at this point in the history
This is the same thing that we added to the Fabric
AIO image earlier:
Setting the flag controls whether the CordaTestLedger
class will automatically pipe the container's own logs onto the logger object of the
class instance (CordaTestLedger) or not.
By default it does pipe the logs but if it gets spammy
developers can turn it off via the flag to avoid
having to scroll through thousands of lines of
logs that may or may not be useful to them.

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Mar 25, 2021
1 parent ba2cacd commit 13fe677
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,8 @@ test(testCase, async (t: Test) => {
t.ok(ledger, "CordaTestLedger instantaited OK");

test.onFinish(async () => {
try {
const logBuffer = ((await ledgerContainer.logs({
follow: false,
stdout: true,
stderr: true,
})) as unknown) as Buffer;
const logs = logBuffer.toString("utf-8");
t.comment(`[CordaAllInOne] ${logs}`);
} finally {
try {
await ledger.stop();
} finally {
await ledger.destroy();
}
}
await ledger.stop();
await ledger.destroy();
});
const ledgerContainer = await ledger.start();
t.ok(ledgerContainer, "CordaTestLedger container truthy post-start() OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Logger,
LoggerProvider,
Checks,
Bools,
} from "@hyperledger/cactus-common";
import { Base64File } from "../common/base64-file";
import {
Expand All @@ -32,6 +33,7 @@ export interface ICordaTestLedgerConstructorOptions {
rpcPortC?: number;
logLevel?: LogLevelDesc;
envVars?: string[];
emitContainerLogs?: boolean;
}

/*
Expand Down Expand Up @@ -81,6 +83,7 @@ export class CordaTestLedger implements ITestLedger {
public readonly rpcPortA: number;
public readonly rpcPortB: number;
public readonly rpcPortC: number;
public readonly emitContainerLogs: boolean;

private container: Container | undefined;
private containerId: string | undefined;
Expand All @@ -96,6 +99,9 @@ export class CordaTestLedger implements ITestLedger {
this.rpcPortB = opts.rpcPortB || DEFAULTS.rpcPortB;
this.rpcPortC = opts.rpcPortC || DEFAULTS.rpcPortC;
this.rpcPortNotary = opts.rpcPortNotary || DEFAULTS.rpcPortNotary;
this.emitContainerLogs = Bools.isBooleanStrict(opts.emitContainerLogs)
? (opts.emitContainerLogs as boolean)
: true;

this.envVars = opts.envVars ? opts.envVars : DEFAULTS.envVars;
Checks.truthy(Array.isArray(this.envVars), `${fnTag}:envVars not an array`);
Expand Down Expand Up @@ -168,6 +174,14 @@ export class CordaTestLedger implements ITestLedger {
eventEmitter.once("start", async (container: Container) => {
this.container = container;
this.containerId = container.id;
if (this.emitContainerLogs) {
const logOptions = { follow: true, stderr: true, stdout: true };
const logStream = await container.logs(logOptions);
logStream.on("data", (data: Buffer) => {
const fnTag = `[${this.getContainerImageName()}]`;
this.log.debug(`${fnTag} %o`, data.toString("utf-8"));
});
}
try {
let isHealthy = false;
do {
Expand Down

0 comments on commit 13fe677

Please sign in to comment.