|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: MIT-0 |
| 3 | + |
| 4 | +/** |
| 5 | + * Test logger basic features |
| 6 | + * |
| 7 | + * @group e2e/logger/logEventEnvVarSetting |
| 8 | + */ |
| 9 | + |
| 10 | +import path from 'path'; |
| 11 | +import { App, Stack } from 'aws-cdk-lib'; |
| 12 | +import { v4 } from 'uuid'; |
| 13 | +import { |
| 14 | + createStackWithLambdaFunction, |
| 15 | + generateUniqueName, |
| 16 | + invokeFunction, |
| 17 | + isValidRuntimeKey |
| 18 | +} from '../../../commons/tests/utils/e2eUtils'; |
| 19 | +import { InvocationLogs } from '../../../commons/tests/utils/InvocationLogs'; |
| 20 | +import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli'; |
| 21 | +import { |
| 22 | + RESOURCE_NAME_PREFIX, |
| 23 | + STACK_OUTPUT_LOG_GROUP, |
| 24 | + SETUP_TIMEOUT, |
| 25 | + TEST_CASE_TIMEOUT, |
| 26 | + TEARDOWN_TIMEOUT |
| 27 | +} from './constants'; |
| 28 | + |
| 29 | +const runtime: string = process.env.RUNTIME || 'nodejs16x'; |
| 30 | + |
| 31 | +if (!isValidRuntimeKey(runtime)) { |
| 32 | + throw new Error(`Invalid runtime key value: ${runtime}`); |
| 33 | +} |
| 34 | + |
| 35 | +const uuid = v4(); |
| 36 | +const stackName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'LogEventEnvVarSetting-Middy'); |
| 37 | +const functionName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'LogEventEnvVarSetting-Middy'); |
| 38 | +const lambdaFunctionCodeFile = 'logEventEnvVarSetting.middy.test.FunctionCode.ts'; |
| 39 | + |
| 40 | +const integTestApp = new App(); |
| 41 | +let logGroupName: string; // We do not know it until deployment |
| 42 | +let stack: Stack; |
| 43 | + |
| 44 | +describe(`logger E2E tests log event via env var setting (middy) for runtime: ${runtime}`, () => { |
| 45 | + |
| 46 | + let invocationLogs: InvocationLogs[]; |
| 47 | + |
| 48 | + beforeAll(async () => { |
| 49 | + // Create and deploy a stack with AWS CDK |
| 50 | + stack = createStackWithLambdaFunction({ |
| 51 | + app: integTestApp, |
| 52 | + stackName: stackName, |
| 53 | + functionName: functionName, |
| 54 | + functionEntry: path.join(__dirname, lambdaFunctionCodeFile), |
| 55 | + environment: { |
| 56 | + LOG_LEVEL: 'INFO', |
| 57 | + POWERTOOLS_SERVICE_NAME: 'logger-e2e-testing', |
| 58 | + UUID: uuid, |
| 59 | + |
| 60 | + // Enabling the logger to log events via env var |
| 61 | + POWERTOOLS_LOGGER_LOG_EVENT: 'true', |
| 62 | + }, |
| 63 | + logGroupOutputKey: STACK_OUTPUT_LOG_GROUP, |
| 64 | + runtime: runtime, |
| 65 | + }); |
| 66 | + |
| 67 | + const result = await deployStack(integTestApp, stack); |
| 68 | + logGroupName = result.outputs[STACK_OUTPUT_LOG_GROUP]; |
| 69 | + |
| 70 | + // Invoke the function twice (one for cold start, another for warm start) |
| 71 | + invocationLogs = await invokeFunction(functionName, 2, 'SEQUENTIAL'); |
| 72 | + |
| 73 | + console.log('logGroupName', logGroupName); |
| 74 | + |
| 75 | + }, SETUP_TIMEOUT); |
| 76 | + |
| 77 | + describe('Log event', () => { |
| 78 | + |
| 79 | + it('should log the event on the first invocation', async () => { |
| 80 | + const firstInvocationMessages = invocationLogs[0].getAllFunctionLogs(); |
| 81 | + let eventLoggedInFirstInvocation = false; |
| 82 | + for (const message of firstInvocationMessages) { |
| 83 | + if (message.includes(`event`)) { |
| 84 | + eventLoggedInFirstInvocation = true; |
| 85 | + expect(message).toContain(`"event":{"invocation":0}`); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const secondInvocationMessages = invocationLogs[1].getAllFunctionLogs(); |
| 90 | + let eventLoggedInSecondInvocation = false; |
| 91 | + for (const message of secondInvocationMessages) { |
| 92 | + if (message.includes(`event`)) { |
| 93 | + eventLoggedInSecondInvocation = true; |
| 94 | + expect(message).toContain(`"event":{"invocation":1}`); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + expect(eventLoggedInFirstInvocation).toBe(true); |
| 99 | + expect(eventLoggedInSecondInvocation).toBe(true); |
| 100 | + |
| 101 | + }, TEST_CASE_TIMEOUT); |
| 102 | + |
| 103 | + }); |
| 104 | + |
| 105 | + afterAll(async () => { |
| 106 | + if (!process.env.DISABLE_TEARDOWN) { |
| 107 | + await destroyStack(integTestApp, stack); |
| 108 | + } |
| 109 | + }, TEARDOWN_TIMEOUT); |
| 110 | +}); |
0 commit comments