From acba4b5a2220f8b2e0b86aecede50d84c59458f2 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Mon, 17 Mar 2025 12:29:10 +0100 Subject: [PATCH 1/2] test: fix transient fails --- .../sampleRate.decorator.test.FunctionCode.ts | 13 +---- .../tests/e2e/sampleRate.decorator.test.ts | 6 ++- packages/testing/src/TestStack.ts | 50 +++++++++++++++++-- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts b/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts index bc770fe560..b0cd540189 100644 --- a/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts +++ b/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts @@ -9,23 +9,12 @@ const LOG_MSG = process.env.LOG_MSG || 'Hello World'; const logger = new Logger({ sampleRateValue: SAMPLE_RATE, }); -let firstInvocation = true; class Lambda implements LambdaInterface { - private readonly logMsg: string; + private readonly logMsg = LOG_MSG; - public constructor() { - this.logMsg = LOG_MSG; - } - - // Decorate your handler class method @logger.injectLambdaContext() public async handler(_event: TestEvent, context: Context): TestOutput { - if (firstInvocation) { - firstInvocation = false; - } else { - logger.refreshSampleRateCalculation(); - } this.printLogInAllLevels(); return { diff --git a/packages/logger/tests/e2e/sampleRate.decorator.test.ts b/packages/logger/tests/e2e/sampleRate.decorator.test.ts index 011a72d02d..8386c91172 100644 --- a/packages/logger/tests/e2e/sampleRate.decorator.test.ts +++ b/packages/logger/tests/e2e/sampleRate.decorator.test.ts @@ -5,7 +5,7 @@ import { TestStack, invokeFunction, } from '@aws-lambda-powertools/testing-utils'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; import { LoggerTestNodejsFunction } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, @@ -15,6 +15,10 @@ import { TEST_CASE_TIMEOUT, } from './constants.js'; +vi.hoisted(() => { + process.env.AWS_PROFILE = 'aamorosi-Admin'; +}); + describe('Logger E2E tests, sample rate and injectLambdaContext()', () => { const testStack = new TestStack({ stackNameProps: { diff --git a/packages/testing/src/TestStack.ts b/packages/testing/src/TestStack.ts index 63b5a193c3..5ed69d1448 100644 --- a/packages/testing/src/TestStack.ts +++ b/packages/testing/src/TestStack.ts @@ -66,19 +66,63 @@ class TestStack { Service: 'Powertools-for-AWS-e2e-tests', }, }); + let lastCreateLog = 0; + let lastDestroyLog = 0; + const creationDeleteLogFrequency = 10000; // 10 seconds + const that = this; this.#cli = new Toolkit({ color: false, ioHost: { + /** + * Log messages to the console depending on the log level. + * + * If the `RUNNER_DEBUG` environment variable is set to `1`, all messages are logged. + * + * Otherwise, we log messages that are either warnings or errors as well as periodic + * updates on the stack creation and destruction process. + * + * @param msg - Message to log sent by the CDK CLI + */ async notify(msg) { + if (process.env.RUNNER_DEBUG === '1') { + testConsole.log(msg); + return; + } + if (msg.message.includes('destroyed') && msg.message.includes('✅')) { + testConsole.log(msg.message); + return; + } + if (msg.message.includes('✅') && !msg.message.includes('deployed')) { + testConsole.log(`${that.testName} deployed successfully`); + return; + } + if (msg.message.includes('CREATE_IN_PROGRESS')) { + if (Date.now() - lastCreateLog < creationDeleteLogFrequency) { + return; + } + lastCreateLog = Date.now(); + testConsole.log(`${that.testName} stack is being created...`); + return; + } + if (msg.message.includes('DELETE_IN_PROGRESS')) { + if (Date.now() - lastDestroyLog < creationDeleteLogFrequency) { + return; + } + lastDestroyLog = Date.now(); + testConsole.log(`${that.testName} stack is being destroyed...`); + return; + } + if (['warning', 'error'].includes(msg.level)) { + testConsole.log(msg); + } + }, + async requestResponse(msg) { if ( process.env.RUNNER_DEBUG === '1' || ['warning', 'error'].includes(msg.level) ) { testConsole.log(msg); } - }, - async requestResponse(msg) { - testConsole.log(msg); return msg.defaultResponse; }, }, From f92056c06c207f6cf3a3beddf4987125c6fb2c0e Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Mon, 17 Mar 2025 12:55:23 +0100 Subject: [PATCH 2/2] chore: remove unused import --- packages/logger/tests/e2e/sampleRate.decorator.test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/logger/tests/e2e/sampleRate.decorator.test.ts b/packages/logger/tests/e2e/sampleRate.decorator.test.ts index 8386c91172..011a72d02d 100644 --- a/packages/logger/tests/e2e/sampleRate.decorator.test.ts +++ b/packages/logger/tests/e2e/sampleRate.decorator.test.ts @@ -5,7 +5,7 @@ import { TestStack, invokeFunction, } from '@aws-lambda-powertools/testing-utils'; -import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { LoggerTestNodejsFunction } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, @@ -15,10 +15,6 @@ import { TEST_CASE_TIMEOUT, } from './constants.js'; -vi.hoisted(() => { - process.env.AWS_PROFILE = 'aamorosi-Admin'; -}); - describe('Logger E2E tests, sample rate and injectLambdaContext()', () => { const testStack = new TestStack({ stackNameProps: {