Skip to content

Commit

Permalink
[RND-650] GitHub Actions is not saving docker logs after failure (#307)
Browse files Browse the repository at this point in the history
* Show error message if e2e file is not found

* Removing with reuse option since this leaves a dirty environment on failure

* Catching error when unable to start containers

* [RND-650] Refactor log collection from TestContainers to get information since start
  • Loading branch information
andonyns authored Oct 13, 2023
1 parent 4dcd19e commit 456b260
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export async function setup() {
try {
const container = new PostgreSqlContainer(image)
.withName('postgres-test')
.withReuse()
.withDatabase(process.env.MEADOWLARK_DATABASE_NAME)
.withUsername(process.env.POSTGRES_USER)
.withPassword(process.env.POSTGRES_PASSWORD);
Expand Down
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
12 changes: 10 additions & 2 deletions Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const environment = require('./SetupTestContainers');
if (process.env.USE_EXISTING_ENVIRONMENT) {
dotenv.config({ path: join(process.cwd(), './services/meadowlark-fastify/.env') });
} else {
dotenv.config({ path: join(__dirname, './.env-e2e') });
const result = dotenv.config({ path: join(__dirname, './.env-e2e') });

if (result.error) {
throw new Error(`An error ocurred loading .env-e2e file:\n${result.error}`);
}
}

module.exports = async () => {
Expand All @@ -26,7 +30,11 @@ module.exports = async () => {
if (process.env.USE_EXISTING_ENVIRONMENT) {
console.info('Using existing environment, Verify that variables are set');
} else {
await environment.configure(initialize);
try {
await environment.configure(initialize);
} catch (error) {
throw new Error(`⚠️ Error initializing containers.⚠️\n${error}`);
}
}

process.env.ROOT_URL = `http://localhost:${process.env.FASTIFY_PORT ?? 3001}`;
Expand Down
3 changes: 1 addition & 2 deletions Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ 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,
})
.withReuse()
.withEnvironment({
OAUTH_SIGNING_KEY: process.env.OAUTH_SIGNING_KEY ?? '',
OAUTH_HARD_CODED_CREDENTIALS_ENABLED: 'true',
Expand Down Expand Up @@ -63,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,7 +16,7 @@ export async function setup(network: StartedNetwork) {
)
.withName('elasticsearch-node-test')
.withNetwork(network)
.withReuse()
.withLogConsumer(async (stream) => setElasticSearchLog(stream))
.withExposedPorts({
container: elasticSearchPort,
host: elasticSearchPort,
Expand All @@ -34,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
4 changes: 1 addition & 3 deletions Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function setup(network: StartedNetwork) {
.withNetwork(network)
.withNetworkAliases('mongo-t1')
.withName('mongo-test')
.withReuse()
.withLogConsumer(async (stream) => setMongoLog(stream))
.withCommand([
'/usr/bin/mongod',
'--bind_ip_all',
Expand All @@ -33,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,7 +16,7 @@ export async function setup(network: StartedNetwork) {
)
.withName('opensearch-test')
.withNetwork(network)
.withReuse()
.withLogConsumer(async (stream) => setOpenSearchLog(stream))
.withExposedPorts({
container: openSearchPort,
host: openSearchPort,
Expand All @@ -33,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,7 +23,7 @@ export async function setup(network: StartedNetwork) {
.withName('postgres-test')
.withNetwork(network)
.withNetworkAliases('pg-test')
.withReuse()
.withLogConsumer(async (stream) => setPostgresLog(stream))
.withDatabase(process.env.MEADOWLARK_DATABASE_NAME)
.withUsername(process.env.POSTGRES_USER)
.withPassword(process.env.POSTGRES_PASSWORD)
Expand All @@ -33,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 456b260

Please sign in to comment.