Skip to content

Commit

Permalink
[RND-650] Refactor log collection from TestContainers to get informat…
Browse files Browse the repository at this point in the history
…ion since start
  • Loading branch information
andonyns committed Oct 13, 2023
1 parent e53444c commit b2125b2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 25 deletions.
27 changes: 11 additions & 16 deletions Meadowlark-js/tests/e2e/setup/LogConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// See the LICENSE and NOTICES files in the project root for more information.

import fs from 'fs-extra';
import { StartedTestContainer } from 'testcontainers';
import { Stream } from 'stream';

let apiWriteStream: fs.WriteStream;
let mongoWriteStream: fs.WriteStream;
Expand Down Expand Up @@ -32,56 +32,51 @@ export function endLog() {
}
}

export async function setMongoLog(container: StartedTestContainer) {
export async function setMongoLog(stream: Stream) {
try {
const mongoStream = await container.logs();
mongoWriteStream = fs.createWriteStream(`${logFolder}/mongo.log`);

mongoStream.on('data', (line) => mongoWriteStream.write(line)).on('err', (line) => mongoWriteStream.write(line));
stream.on('data', (line) => mongoWriteStream.write(line)).on('err', (line) => mongoWriteStream.write(line));
} catch (error) {
throw new Error(`\nUnexpected error setting up mongo logs:\n${error}`);
}
}

export async function setAPILog(container: StartedTestContainer) {
export async function setAPILog(stream: Stream) {
try {
const apiStream = await container.logs();
apiWriteStream = fs.createWriteStream(`${logFolder}/meadowlark-api.log`);

apiStream.on('data', (line) => apiWriteStream.write(line)).on('err', (line) => apiWriteStream.write(line));
stream.on('data', (line) => apiWriteStream.write(line)).on('err', (line) => apiWriteStream.write(line));
} catch (error) {
throw new Error(`\nUnexpected error setting up api logs:\n${error}`);
}
}

export async function setOpenSearchLog(container: StartedTestContainer) {
export async function setOpenSearchLog(stream: Stream) {
try {
const osStream = await container.logs();
opSearchWriteStream = fs.createWriteStream(`${logFolder}/openSearch.log`);

osStream.on('data', (line) => opSearchWriteStream.write(line)).on('err', (line) => opSearchWriteStream.write(line));
stream.on('data', (line) => opSearchWriteStream.write(line)).on('err', (line) => opSearchWriteStream.write(line));
} catch (error) {
throw new Error(`\nUnexpected error setting up open search logs:\n${error}`);
}
}

export async function setElasticSearchLog(container: StartedTestContainer) {
export async function setElasticSearchLog(stream: Stream) {
try {
const osStream = await container.logs();
elasticSearchWriteStream = fs.createWriteStream(`${logFolder}/elasticSearch.log`);

osStream.on('data', (line) => elasticSearchWriteStream.write(line)).on('err', (line) => opSearchWriteStream.write(line));
stream.on('data', (line) => elasticSearchWriteStream.write(line)).on('err', (line) => opSearchWriteStream.write(line));
} catch (error) {
throw new Error(`\nUnexpected error setting up elastic search logs:\n${error}`);
}
}

export async function setPostgresLog(container: StartedTestContainer) {
export async function setPostgresLog(stream: Stream) {
try {
const pgStream = await container.logs();
pgWriteStream = fs.createWriteStream(`${logFolder}/postgres.log`);

pgStream.on('data', (line) => pgWriteStream.write(line)).on('err', (line) => pgWriteStream.write(line));
stream.on('data', (line) => pgWriteStream.write(line)).on('err', (line) => pgWriteStream.write(line));
} catch (error) {
throw new Error(`\nUnexpected error setting up postgres logs:\n${error}`);
}
Expand Down
2 changes: 1 addition & 1 deletion Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function setup(network: StartedNetwork) {
container = new GenericContainer(process.env.API_IMAGE_NAME ?? 'meadowlark')
.withName('meadowlark-api-test')
.withNetwork(network)
.withLogConsumer(async (stream) => setAPILog(stream))
.withExposedPorts({
container: fastifyPort,
host: fastifyPort,
Expand Down Expand Up @@ -62,7 +63,6 @@ export async function setup(network: StartedNetwork) {

throw new Error(`\nUnexpected error setting up API container:\n${error}`);
}
await setAPILog(startedContainer);
}

export async function stop(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function setup(network: StartedNetwork) {
)
.withName('elasticsearch-node-test')
.withNetwork(network)
.withLogConsumer(async (stream) => setElasticSearchLog(stream))
.withExposedPorts({
container: elasticSearchPort,
host: elasticSearchPort,
Expand All @@ -33,8 +34,6 @@ export async function setup(network: StartedNetwork) {
} catch (error) {
throw new Error(`\nUnexpected error setting up elastic search container:\n${error}`);
}

await setElasticSearchLog(startedContainer);
}

export async function stop(): Promise<void> {
Expand Down
3 changes: 1 addition & 2 deletions Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function setup(network: StartedNetwork) {
.withNetwork(network)
.withNetworkAliases('mongo-t1')
.withName('mongo-test')
.withLogConsumer(async (stream) => setMongoLog(stream))
.withCommand([
'/usr/bin/mongod',
'--bind_ip_all',
Expand All @@ -32,8 +33,6 @@ export async function setup(network: StartedNetwork) {
} catch (error) {
throw new Error(`\nUnexpected error setting up mongo container:\n${error}`);
}

await setMongoLog(startedContainer);
}

export async function stop(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function setup(network: StartedNetwork) {
)
.withName('opensearch-test')
.withNetwork(network)
.withLogConsumer(async (stream) => setOpenSearchLog(stream))
.withExposedPorts({
container: openSearchPort,
host: openSearchPort,
Expand All @@ -32,8 +33,6 @@ export async function setup(network: StartedNetwork) {
} catch (error) {
throw new Error(`\nUnexpected error setting up open search container:\n${error}`);
}

await setOpenSearchLog(startedContainer);
}

export async function stop(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export async function setup(network: StartedNetwork) {
.withName('postgres-test')
.withNetwork(network)
.withNetworkAliases('pg-test')
.withLogConsumer(async (stream) => setPostgresLog(stream))
.withDatabase(process.env.MEADOWLARK_DATABASE_NAME)
.withUsername(process.env.POSTGRES_USER)
.withPassword(process.env.POSTGRES_PASSWORD)
Expand All @@ -32,8 +33,6 @@ export async function setup(network: StartedNetwork) {

process.env.POSTGRES_HOST = startedContainer.getHost();
process.env.POSTGRES_PORT = `${startedContainer.getFirstMappedPort()}`;

await setPostgresLog(startedContainer);
} catch (error) {
throw new Error(`\nUnexpected error setting up postgres container:\n${error}`);
}
Expand Down

0 comments on commit b2125b2

Please sign in to comment.