From 657784694eb7cf549ec4a6688778279e908d0598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andony=20Nu=C3=B1ez?= Date: Thu, 12 Oct 2023 15:18:45 -0600 Subject: [PATCH 1/4] Show error message if e2e file is not found --- Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts b/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts index c353fb61..bac74e36 100644 --- a/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts +++ b/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts @@ -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 () => { From 75d8e9ab0279b57bb4c4d9e3c9b80e81e2405990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andony=20Nu=C3=B1ez?= Date: Thu, 12 Oct 2023 19:58:48 -0600 Subject: [PATCH 2/4] Removing with reuse option since this leaves a dirty environment on failure --- .../test/setup/PostgresqlContainer.ts | 1 - Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts | 1 - .../tests/e2e/setup/containers/ElasticSearchContainer.ts | 1 - Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts | 1 - Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts | 1 - Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts | 1 - 6 files changed, 6 deletions(-) diff --git a/Meadowlark-js/backends/meadowlark-postgresql-backend/test/setup/PostgresqlContainer.ts b/Meadowlark-js/backends/meadowlark-postgresql-backend/test/setup/PostgresqlContainer.ts index 388ceb71..8036d385 100644 --- a/Meadowlark-js/backends/meadowlark-postgresql-backend/test/setup/PostgresqlContainer.ts +++ b/Meadowlark-js/backends/meadowlark-postgresql-backend/test/setup/PostgresqlContainer.ts @@ -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); diff --git a/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts index 7ce4d5db..abb3f48b 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts @@ -23,7 +23,6 @@ export async function setup(network: StartedNetwork) { container: fastifyPort, host: fastifyPort, }) - .withReuse() .withEnvironment({ OAUTH_SIGNING_KEY: process.env.OAUTH_SIGNING_KEY ?? '', OAUTH_HARD_CODED_CREDENTIALS_ENABLED: 'true', diff --git a/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts index 61fe0217..c2fc3889 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts @@ -16,7 +16,6 @@ export async function setup(network: StartedNetwork) { ) .withName('elasticsearch-node-test') .withNetwork(network) - .withReuse() .withExposedPorts({ container: elasticSearchPort, host: elasticSearchPort, diff --git a/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts index 22292846..f072c389 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts @@ -17,7 +17,6 @@ export async function setup(network: StartedNetwork) { .withNetwork(network) .withNetworkAliases('mongo-t1') .withName('mongo-test') - .withReuse() .withCommand([ '/usr/bin/mongod', '--bind_ip_all', diff --git a/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts index 115ec8c0..ce67bd89 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts @@ -16,7 +16,6 @@ export async function setup(network: StartedNetwork) { ) .withName('opensearch-test') .withNetwork(network) - .withReuse() .withExposedPorts({ container: openSearchPort, host: openSearchPort, diff --git a/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts index 05511f3c..b53a1de5 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts @@ -23,7 +23,6 @@ export async function setup(network: StartedNetwork) { .withName('postgres-test') .withNetwork(network) .withNetworkAliases('pg-test') - .withReuse() .withDatabase(process.env.MEADOWLARK_DATABASE_NAME) .withUsername(process.env.POSTGRES_USER) .withPassword(process.env.POSTGRES_PASSWORD) From e53444c149516f3489f0d145cd7f0a8f660c045b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andony=20Nu=C3=B1ez?= Date: Thu, 12 Oct 2023 20:54:03 -0600 Subject: [PATCH 3/4] Catching error when unable to start containers --- Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts b/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts index bac74e36..b721adbb 100644 --- a/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts +++ b/Meadowlark-js/tests/e2e/setup/SetupTestEnvironment.ts @@ -30,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}`; From b2125b21b2cb89992a0ea5026e58d98e1e43f7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andony=20Nu=C3=B1ez?= Date: Thu, 12 Oct 2023 21:19:39 -0600 Subject: [PATCH 4/4] [RND-650] Refactor log collection from TestContainers to get information since start --- Meadowlark-js/tests/e2e/setup/LogConfig.ts | 27 ++++++++----------- .../e2e/setup/containers/ApiContainer.ts | 2 +- .../containers/ElasticSearchContainer.ts | 3 +-- .../e2e/setup/containers/MongoContainer.ts | 3 +-- .../setup/containers/OpenSearchContainer.ts | 3 +-- .../setup/containers/PostgresqlContainer.ts | 3 +-- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Meadowlark-js/tests/e2e/setup/LogConfig.ts b/Meadowlark-js/tests/e2e/setup/LogConfig.ts index 7b20dab6..d37270a2 100644 --- a/Meadowlark-js/tests/e2e/setup/LogConfig.ts +++ b/Meadowlark-js/tests/e2e/setup/LogConfig.ts @@ -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; @@ -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}`); } diff --git a/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts index abb3f48b..1fff707f 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/ApiContainer.ts @@ -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, @@ -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 { diff --git a/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts index c2fc3889..bd053bc4 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/ElasticSearchContainer.ts @@ -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, @@ -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 { diff --git a/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts index f072c389..ab11c4a5 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/MongoContainer.ts @@ -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', @@ -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 { diff --git a/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts index ce67bd89..a5bac812 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/OpenSearchContainer.ts @@ -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, @@ -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 { diff --git a/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts b/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts index b53a1de5..aa389875 100644 --- a/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts +++ b/Meadowlark-js/tests/e2e/setup/containers/PostgresqlContainer.ts @@ -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) @@ -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}`); }