Skip to content

Commit

Permalink
test(test-tooling): filter whitespaces from container logs #1247
Browse files Browse the repository at this point in the history
Adds a utility function in the Containers class to stream logs and
automatically exclude newline-only log messages.

Resolves #1247

Signed-off-by: jgusta <jgustafx@gmail.com>
  • Loading branch information
jgusta authored and petermetz committed Oct 28, 2021
1 parent 4a4e38f commit 67ddebe
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ export class BesuMpTestLedger {
this._containerId = Optional.ofNonNull(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.imageFqn}]`;
this.log.debug(`${fnTag} %o`, data.toString("utf-8"));
const fnTag = `[${this.imageFqn}]`;
await Containers.streamLogs({
container: this.container.get(),
tag: fnTag,
log: this.log,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ export class BesuTestLedger implements ITestLedger {
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"));
const fnTag = `[${this.getContainerImageName()}]`;
await Containers.streamLogs({
container: this.getContainer(),
tag: fnTag,
log: this.log,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
LoggerProvider,
Strings,
ILoggerOptions,
Logger,
} from "@hyperledger/cactus-common";
import { IDockerPullProgress } from "./i-docker-pull-progress";

Expand Down Expand Up @@ -630,4 +631,23 @@ export class Containers {
}
return response;
}

public static async streamLogs(req: IStreamLogsRequest): Promise<void> {
const logOptions = { follow: true, stderr: true, stdout: true };
const logStream = await req.container.logs(logOptions);
const newLineOnlyLogMessages = [`\r\n`, `+\r\n`, `.\r\n`];

logStream.on("data", (data: Buffer) => {
const msg = data.toString("utf-8");
if (!newLineOnlyLogMessages.includes(msg)) {
req.log.debug(`${req.tag} %o`, msg);
}
});
}
}

export interface IStreamLogsRequest {
readonly container: Container;
readonly log: Logger;
readonly tag: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ export class CordaConnectorContainer {
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"));
const fnTag = `[${this.getContainerImageName()}]`;
await Containers.streamLogs({
container: this.getContainer(),
tag: fnTag,
log: this.log,
});
}

try {
await Containers.waitForHealthCheck(this.containerId);
resolve(container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,16 @@ 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"));
const fnTag = `[${this.getContainerImageName()}]`;
await Containers.streamLogs({
container: this.getContainer(),
tag: fnTag,
log: this.log,
});
}

try {
let isHealthy = false;
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,11 @@ export class FabricTestLedgerV1 implements ITestLedger {
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"));
const fnTag = `[${this.getContainerImageName()}]`;
await Containers.streamLogs({
container: this.getContainer(),
tag: fnTag,
log: this.log,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ export class GoIpfsTestContainer {
this._containerId = Optional.ofNonNull(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.imageFqn}]`;
this.log.debug(`${fnTag} %o`, data.toString("utf-8"));
const fnTag = `[${this.imageFqn}]`;
await Containers.streamLogs({
container: this.container.get(),
tag: fnTag,
log: this.log,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,16 @@ export class IrohaTestLedger implements ITestLedger {
this.log.debug(`Started container OK. Waiting for healthcheck...`);
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) => {
this.log.debug(`[${this.imageFqn}] %o`, data.toString("utf-8"));
const fnTag = `[${this.imageFqn}]`;
await Containers.streamLogs({
container: this.container,
tag: fnTag,
log: this.log,
});
}

try {
await this.waitForHealthCheck();
this.log.debug(`Healthcheck passing OK.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,16 @@ export class OpenEthereumTestLedger {
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) => {
this.log.debug(`[${this.imageFqn}] %o`, data.toString("utf-8"));
const fnTag = `[${this.imageFqn}]`;
await Containers.streamLogs({
container: this.container,
tag: fnTag,
log: this.log,
});
}

try {
await Containers.waitForHealthCheck(this._containerId);
resolve(container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ITestLedger } from "../i-test-ledger";
import { Streams } from "../common/streams";
import { IKeyPair } from "../i-key-pair";
import { IQuorumGenesisOptions } from "./i-quorum-genesis-options";
import { Containers } from "../common/containers";

export interface IQuorumTestLedgerConstructorOptions {
containerImageVersion?: string;
Expand Down Expand Up @@ -241,11 +242,11 @@ export class QuorumTestLedger implements ITestLedger {
this.log.debug("Quorum Test Ledger container started booting OK.");

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"));
const fnTag = `[${this.getContainerImageName()}]`;
await Containers.streamLogs({
container: this.container,
tag: fnTag,
log: this.log,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ export class RustcContainer {
this._containerId = Optional.ofNonNull(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.imageFqn}]`;
this.log.debug(`${fnTag} %o`, data.toString("utf-8"));
const fnTag = `[${this.imageFqn}]`;
await Containers.streamLogs({
container: this.container.get(),
tag: fnTag,
log: this.log,
});
}

this.log.debug(`Registered container log stream callbacks OK`);

try {
Expand Down

0 comments on commit 67ddebe

Please sign in to comment.