Skip to content

chore(ci): move e2e utils into testing #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions layers/src/canary-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CustomResource, Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { v4 } from 'uuid';
import { randomUUID } from 'node:crypto';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Provider } from 'aws-cdk-lib/custom-resources';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
Expand All @@ -20,7 +20,7 @@ export class CanaryStack extends Stack {
super(scope, id, props);
const { layerName, powertoolsPackageVersion } = props;

const suffix = v4().substring(0, 5);
const suffix = randomUUID().substring(0, 5);

const layerArn = StringParameter.fromStringParameterAttributes(
this,
Expand Down
11 changes: 9 additions & 2 deletions layers/src/layer-publisher-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
CfnLayerVersionPermission,
} from 'aws-cdk-lib/aws-lambda';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { resolve } from 'node:path';

export interface LayerPublisherStackProps extends StackProps {
readonly layerName?: string;
readonly powertoolsPackageVersion?: string;
readonly ssmParameterLayerArn: string;
readonly removeLayerVersion?: boolean;
}

export class LayerPublisherStack extends Stack {
Expand All @@ -23,7 +25,7 @@ export class LayerPublisherStack extends Stack {
) {
super(scope, id, props);

const { layerName, powertoolsPackageVersion } = props;
const { layerName, powertoolsPackageVersion, removeLayerVersion } = props;

console.log(
`publishing layer ${layerName} version : ${powertoolsPackageVersion}`
Expand All @@ -40,7 +42,11 @@ export class LayerPublisherStack extends Stack {
license: 'MIT-0',
// This is needed because the following regions do not support the compatibleArchitectures property #1400
// ...(![ 'eu-south-2', 'eu-central-2', 'ap-southeast-4' ].includes(Stack.of(this).region) ? { compatibleArchitectures: [Architecture.X86_64] } : {}),
code: Code.fromAsset('../tmp'),
code: Code.fromAsset(resolve(__dirname, '..', '..', 'tmp')),
removalPolicy:
removeLayerVersion === true
? RemovalPolicy.DESTROY
: RemovalPolicy.RETAIN,
});

const layerPermission = new CfnLayerVersionPermission(
Expand All @@ -60,6 +66,7 @@ export class LayerPublisherStack extends Stack {
parameterName: props.ssmParameterLayerArn,
stringValue: this.lambdaLayerVersion.layerVersionArn,
});

new CfnOutput(this, 'LatestLayerArn', {
value: this.lambdaLayerVersion.layerVersionArn,
exportName: props?.layerName ?? `LambdaPowerToolsForTypeScriptLayerARN`,
Expand Down
1 change: 1 addition & 0 deletions layers/tests/e2e/layerPublisher.class.test.functionCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const handler = (): void => {

// Check that the tracer is working
const segment = tracer.getSegment();
if (!segment) throw new Error('Segment not found');
const handlerSegment = segment.addNewSubsegment('### index.handler');
tracer.setSegment(handlerSegment);
tracer.annotateColdStart();
Expand Down
162 changes: 76 additions & 86 deletions layers/tests/e2e/layerPublisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,129 +4,119 @@
* @group e2e/layers/all
*/
import { App } from 'aws-cdk-lib';
import { LayerVersion, Tracing } from 'aws-cdk-lib/aws-lambda';
import { LayerVersion } from 'aws-cdk-lib/aws-lambda';
import { LayerPublisherStack } from '../../src/layer-publisher-stack';
import {
TestNodejsFunction,
TestStack,
defaultRuntime,
TestInvocationLogs,
invokeFunctionOnce,
generateTestUniqueName,
} from '@aws-lambda-powertools/testing-utils';
import {
generateUniqueName,
invokeFunction,
isValidRuntimeKey,
createStackWithLambdaFunction,
} from '../../../packages/commons/tests/utils/e2eUtils';
import {
RESOURCE_NAME_PREFIX,
SETUP_TIMEOUT,
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT,
} from './constants';
import {
LEVEL,
InvocationLogs,
} from '../../../packages/commons/tests/utils/InvocationLogs';
import { v4 } from 'uuid';
import path from 'path';
import { join } from 'node:path';
import packageJson from '../../package.json';

const runtime: string = process.env.RUNTIME || defaultRuntime;
/**
* This test has two stacks:
* 1. LayerPublisherStack - publishes a layer version using the LayerPublisher construct and containing the Powertools utilities from the repo
* 2. TestStack - uses the layer published in the first stack and contains a lambda function that uses the Powertools utilities from the layer
*
* The lambda function is invoked once and the logs are collected. The goal of the test is to verify that the layer creation and usage works as expected.
*/
describe(`Layers E2E tests, publisher stack`, () => {
const testStack = new TestStack({
stackNameProps: {
stackNamePrefix: RESOURCE_NAME_PREFIX,
testName: 'functionStack',
},
});

if (!isValidRuntimeKey(runtime)) {
throw new Error(`Invalid runtime key: ${runtime}`);
}
let invocationLogs: TestInvocationLogs;

describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () => {
const uuid = v4();
let invocationLogs: InvocationLogs[];
const stackNameLayers = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'layerStack'
);
const stackNameFunction = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'functionStack'
);
const functionName = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'function'
);
const ssmParameterLayerName = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'parameter'
const ssmParameterLayerName = generateTestUniqueName({
testPrefix: `${RESOURCE_NAME_PREFIX}`,
testName: 'parameter',
});

// Location of the lambda function code
const lambdaFunctionCodeFilePath = join(
__dirname,
'layerPublisher.class.test.functionCode.ts'
);
const lambdaFunctionCodeFile = 'layerPublisher.class.test.functionCode.ts';

const invocationCount = 1;
const powerToolsPackageVersion = packageJson.version;
const layerName = generateUniqueName(
RESOURCE_NAME_PREFIX,
uuid,
runtime,
'layer'
);

const testStack = new TestStack(stackNameFunction);
const layerApp = new App();
const layerStack = new LayerPublisherStack(layerApp, stackNameLayers, {
layerName,
const layerId = generateTestUniqueName({
testPrefix: RESOURCE_NAME_PREFIX,
testName: 'layerStack',
});
const layerStack = new LayerPublisherStack(layerApp, layerId, {
layerName: layerId,
powertoolsPackageVersion: powerToolsPackageVersion,
ssmParameterLayerArn: ssmParameterLayerName,
removeLayerVersion: true,
});
const testLayerStack = new TestStack({
stackNameProps: {
stackNamePrefix: RESOURCE_NAME_PREFIX,
testName: 'layerStack',
},
app: layerApp,
stack: layerStack,
});
const testLayerStack = new TestStack(stackNameLayers, layerApp, layerStack);

beforeAll(async () => {
const outputs = await testLayerStack.deploy();
await testLayerStack.deploy();

const layerVersion = LayerVersion.fromLayerVersionArn(
testStack.stack,
'LayerVersionArnReference',
outputs['LatestLayerArn']
testLayerStack.findAndGetStackOutputValue('LatestLayerArn')
);
createStackWithLambdaFunction({
stack: testStack.stack,
functionName: functionName,
functionEntry: path.join(__dirname, lambdaFunctionCodeFile),
tracing: Tracing.ACTIVE,
environment: {
UUID: uuid,
POWERTOOLS_PACKAGE_VERSION: powerToolsPackageVersion,
POWERTOOLS_SERVICE_NAME: 'LayerPublisherStack',
},
runtime: runtime,
bundling: {
externalModules: [
'@aws-lambda-powertools/commons',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/tracer',
],
new TestNodejsFunction(
testStack,
{
entry: lambdaFunctionCodeFilePath,
environment: {
POWERTOOLS_PACKAGE_VERSION: powerToolsPackageVersion,
POWERTOOLS_SERVICE_NAME: 'LayerPublisherStack',
},
bundling: {
externalModules: [
'@aws-lambda-powertools/commons',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/tracer',
],
},
layers: [layerVersion],
},
layers: [layerVersion],
});
{
nameSuffix: 'testFn',
}
);

await testStack.deploy();

invocationLogs = await invokeFunction(
const functionName = testStack.findAndGetStackOutputValue('testFn');

invocationLogs = await invokeFunctionOnce({
functionName,
invocationCount,
'SEQUENTIAL'
);
});
}, SETUP_TIMEOUT);

describe('LayerPublisherStack usage', () => {
it(
'should have no errors in the logs, which indicates the pacakges version matches the expected one',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.ERROR);
const logs = invocationLogs.getFunctionLogs('ERROR');

expect(logs.length).toBe(0);
},
Expand All @@ -136,7 +126,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
it(
'should have one warning related to missing Metrics namespace',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.WARN);
const logs = invocationLogs.getFunctionLogs('WARN');

expect(logs.length).toBe(1);
expect(logs[0]).toContain('Namespace should be defined, default used');
Expand All @@ -147,7 +137,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
it(
'should have one info log related to coldstart metric',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.INFO);
const logs = invocationLogs.getFunctionLogs('INFO');

expect(logs.length).toBe(1);
expect(logs[0]).toContain('ColdStart');
Expand All @@ -158,7 +148,7 @@ describe(`layers E2E tests (LayerPublisherStack) for runtime: ${runtime}`, () =>
it(
'should have one debug log that says Hello World!',
() => {
const logs = invocationLogs[0].getFunctionLogs(LEVEL.DEBUG);
const logs = invocationLogs.getFunctionLogs('DEBUG');

expect(logs.length).toBe(1);
expect(logs[0]).toContain('Hello World!');
Expand Down
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"ts-node": "^10.9.1",
"typedoc": "^0.24.7",
"typedoc-plugin-missing-exports": "^2.0.0",
"typescript": "^4.9.4",
"uuid": "^9.0.0"
"typescript": "^4.9.4"
},
"engines": {
"node": ">=14"
Expand Down
Loading