Skip to content

test(all): move e2e tests to cdk v2 #676

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 2 commits into from
Mar 23, 2022
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,016 changes: 1,680 additions & 2,336 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
},
"homepage": "https://github.com/awslabs/aws-lambda-powertools-typescript#readme",
"devDependencies": {
"@aws-cdk/aws-dynamodb": "^1.146.0",
"@aws-cdk/aws-lambda-nodejs": "^1.146.0",
"@aws-cdk/cloudformation-diff": "^1.146.0",
"@aws-cdk/core": "^1.146.0",
"@aws-cdk/cx-api": "^2.17.0",
"@aws-cdk/cloudformation-diff": "^2.17.0",
"aws-cdk-lib": "^2.17.0",
"constructs": "^10.0.92",
"@commitlint/cli": "^16.2.1",
"@middy/core": "^2.5.6",
"@types/aws-lambda": "^8.10.72",
Expand All @@ -59,9 +59,9 @@
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"archiver": "^5.3.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed? I think it's used by cdk to create the asset archives but just checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed might not be needed. will merge that one to let @ijemmy work and give it a try through another PR

"aws-cdk": "^1.146.0",
"aws-cdk": "^2.17.0",
"aws-sdk": "^2.1082.0",
"cdk-assets": "^1.146.0",
"cdk-assets": "^2.17.0",
"esbuild": "^0.14.23",
"eslint": "^8.4.0",
"eslint-import-resolver-node": "^0.3.6",
Expand All @@ -88,4 +88,4 @@
"dependencies": {
"hosted-git-info": "^4.0.2"
}
}
}
49 changes: 49 additions & 0 deletions packages/commons/tests/utils/cdk-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { App, Stack } from 'aws-cdk-lib';
import * as cxapi from '@aws-cdk/cx-api';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { DeployStackResult } from 'aws-cdk/lib/api/deploy-stack';

export const deployStack = async (app: App, stack: Stack, quiet?: boolean): Promise<DeployStackResult> => {
const stackArtifact = getStackArtifact(app, stack);

const cloudFormation = await createCloudFormationDeployments();

return cloudFormation.deployStack({
stack: stackArtifact,
quiet: quiet ? quiet : true,
});
};

export const destroyStack = async (app: App, stack: Stack, quiet?: boolean): Promise<void> => {
const stackArtifact = getStackArtifact(app, stack);

const cloudFormation = await createCloudFormationDeployments();

await cloudFormation.destroyStack({
stack: stackArtifact,
quiet: quiet ? quiet : true,
});
};

const getStackArtifact = (app: App, stack: Stack): cxapi.CloudFormationStackArtifact => {
const synthesized = app.synth();

// Reload the synthesized artifact for stack using the cxapi from dependencies
const assembly = new cxapi.CloudAssembly(synthesized.directory);

return cxapi.CloudFormationStackArtifact.fromManifest(
assembly,
stack.artifactId,
synthesized.getStackArtifact(stack.artifactId).manifest
) as cxapi.CloudFormationStackArtifact;
};

const createCloudFormationDeployments = async (): Promise<CloudFormationDeployments> => {
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

return cloudFormation;
};
11 changes: 6 additions & 5 deletions packages/logger/tests/e2e/basicFeatures.middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import path from 'path';
import { randomUUID } from 'crypto';
import { App, Stack } from '@aws-cdk/core';
import { createStackWithLambdaFunction, deployStack, destroyStack, generateUniqueName, invokeFunction, isValidRuntimeKey } from '../helpers/e2eUtils';
import { App, Stack } from 'aws-cdk-lib';
import { createStackWithLambdaFunction, generateUniqueName, invokeFunction, isValidRuntimeKey } from '../helpers/e2eUtils';
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli';
import { InvocationLogs } from '../helpers/InvocationLogs';

const runtime: string = process.env.RUNTIME || 'nodejs14x';
Expand Down Expand Up @@ -67,9 +68,9 @@ describe(`logger E2E tests basic functionalities (middy) for runtime: ${runtime}
logGroupOutputKey: STACK_OUTPUT_LOG_GROUP,
runtime: runtime,
});
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);
const outputs = await deployStack(stackArtifact);
logGroupName = outputs[STACK_OUTPUT_LOG_GROUP];

const result = await deployStack(integTestApp, stack);
logGroupName = result.outputs[STACK_OUTPUT_LOG_GROUP];

// Invoke the function twice (one for cold start, another for warm start)
invocationLogs = await invokeFunction(functionName, 2, 'SEQUENTIAL');
Expand Down
10 changes: 5 additions & 5 deletions packages/logger/tests/e2e/childLogger.manual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import path from 'path';
import { randomUUID } from 'crypto';
import { App, Stack } from '@aws-cdk/core';
import { createStackWithLambdaFunction, deployStack, destroyStack, generateUniqueName, invokeFunction, isValidRuntimeKey } from '../helpers/e2eUtils';
import { App, Stack } from 'aws-cdk-lib';
import { createStackWithLambdaFunction, generateUniqueName, invokeFunction, isValidRuntimeKey } from '../helpers/e2eUtils';
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli';
import { InvocationLogs } from '../helpers/InvocationLogs';

const runtime: string = process.env.RUNTIME || 'nodejs14x';
Expand Down Expand Up @@ -67,9 +68,8 @@ describe(`logger E2E tests child logger functionalities (manual) for runtime: ${
logGroupOutputKey: STACK_OUTPUT_LOG_GROUP,
runtime: runtime,
});
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);
const outputs = await deployStack(stackArtifact);
logGroupName = outputs[STACK_OUTPUT_LOG_GROUP];
const result = await deployStack(integTestApp, stack);
logGroupName = result.outputs[STACK_OUTPUT_LOG_GROUP];

// Invoke the function once
invocationLogs = await invokeFunction(functionName, 1);
Expand Down
10 changes: 5 additions & 5 deletions packages/logger/tests/e2e/sampleRate.decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import path from 'path';
import { randomUUID } from 'crypto';
import { App, Stack } from '@aws-cdk/core';
import { createStackWithLambdaFunction, deployStack, destroyStack, generateUniqueName, invokeFunction, isValidRuntimeKey } from '../helpers/e2eUtils';
import { App, Stack } from 'aws-cdk-lib';
import { createStackWithLambdaFunction, generateUniqueName, invokeFunction, isValidRuntimeKey } from '../helpers/e2eUtils';
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli';
import { InvocationLogs } from '../helpers/InvocationLogs';

const runtime: string = process.env.RUNTIME || 'nodejs14x';
Expand Down Expand Up @@ -61,9 +62,8 @@ describe(`logger E2E tests sample rate and injectLambdaContext() for runtime: ${
logGroupOutputKey: STACK_OUTPUT_LOG_GROUP,
runtime: runtime,
});
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);
const outputs = await deployStack(stackArtifact);
logGroupName = outputs[STACK_OUTPUT_LOG_GROUP];
const result = await deployStack(integTestApp, stack);
logGroupName = result.outputs[STACK_OUTPUT_LOG_GROUP];

invocationLogs = await invokeFunction(functionName, 20);

Expand Down
40 changes: 4 additions & 36 deletions packages/logger/tests/helpers/e2eUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

import { CloudFormationStackArtifact } from '@aws-cdk/cx-api';
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
import { App, CfnOutput, Stack } from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda-nodejs';
import { Runtime } from '@aws-cdk/aws-lambda';
import { App, CfnOutput, Stack } from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import * as AWS from 'aws-sdk';

import { InvocationLogs } from './InvocationLogs';
Expand Down Expand Up @@ -53,25 +50,10 @@ export const createStackWithLambdaFunction = (params: StackWithLambdaFunctionOpt
export const generateUniqueName = (uuid: string, runtime: string, testName: string): string =>
`${NAME_PREFIX}-${runtime}-${testName}-${uuid}`.substring(0, 64);

export const deployStack = async (stackArtifact: CloudFormationStackArtifact ): Promise<{[name:string]: string}> => {
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

// WHEN lambda function is deployed
const result = await cloudFormation.deployStack({
stack: stackArtifact,
quiet: true,
});

return result.outputs;
};

export const invokeFunction = async (functionName: string, times: number = 1, invocationMode: 'PARALLEL' | 'SEQUENTIAL' = 'PARALLEL'): Promise<InvocationLogs[]> => {
const invocationLogs: InvocationLogs[] = [];

const promiseFactory = () : Promise<void> => {
const promiseFactory = (): Promise<void> => {
const invokePromise = lambdaClient
.invoke({
FunctionName: functionName,
Expand All @@ -98,20 +80,6 @@ export const invokeFunction = async (functionName: string, times: number = 1, in
return invocationLogs;
};

export const destroyStack = async (app: App, stack: Stack): Promise<void> => {
const stackArtifact = app.synth().getStackByName(stack.stackName);

const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

await cloudFormation.destroyStack({
stack: stackArtifact,
quiet: true,
});
};

const chainPromises = async (promiseFactories: (() => Promise<void>)[]) : Promise<void> => {
let chain = Promise.resolve();
promiseFactories.forEach(factory => chain = chain.then(factory));
Expand Down
35 changes: 6 additions & 29 deletions packages/metrics/tests/e2e/decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
*/

import { randomUUID } from 'crypto';
import * as lambda from '@aws-cdk/aws-lambda-nodejs';
import { Tracing } from '@aws-cdk/aws-lambda';
import { App, Stack } from '@aws-cdk/core';
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
import { Tracing } from 'aws-cdk-lib/aws-lambda';
import { App, Stack } from 'aws-cdk-lib';
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli';
import * as AWS from 'aws-sdk';
import { MetricUnits } from '../../src';
import { getMetrics } from '../helpers/metricsUtils';
Expand Down Expand Up @@ -58,21 +57,9 @@ new lambda.NodejsFunction(stack, 'MyFunction', {
},
});

const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);

describe('happy cases', () => {
beforeAll(async () => {
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

// WHEN
// lambda function is deployed
await cloudFormation.deployStack({
stack: stackArtifact,
quiet: true,
});
await deployStack(integTestApp, stack);

// and invoked
for (let i = 0; i < invocationCount; i++) {
Expand Down Expand Up @@ -161,17 +148,7 @@ describe('happy cases', () => {

afterAll(async () => {
if (!process.env.DISABLE_TEARDOWN) {
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);

const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

await cloudFormation.destroyStack({
stack: stackArtifact,
quiet: true,
});
await destroyStack(integTestApp, stack);
}
}, ONE_MINUTE * 3);
});
36 changes: 7 additions & 29 deletions packages/metrics/tests/e2e/standardFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
*/

import { randomUUID } from 'crypto';
import * as lambda from '@aws-cdk/aws-lambda-nodejs';
import { Tracing } from '@aws-cdk/aws-lambda';
import { App, Stack } from '@aws-cdk/core';
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
import { Tracing } from 'aws-cdk-lib/aws-lambda';
import { App, Stack } from 'aws-cdk-lib';
import * as AWS from 'aws-sdk';
import { MetricUnits } from '../../src';
import { getMetrics } from '../helpers/metricsUtils';
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli';

const ONE_MINUTE = 1000 * 60;

Expand Down Expand Up @@ -58,21 +57,10 @@ new lambda.NodejsFunction(stack, 'MyFunction', {
},
});

const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);

describe('happy cases', () => {
beforeAll(async () => {
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

// WHEN
// lambda function is deployed
await cloudFormation.deployStack({
stack: stackArtifact,
quiet: true,
});

await deployStack(integTestApp, stack);

// and invoked
for (let i = 0; i < invocationCount; i++) {
Expand Down Expand Up @@ -159,17 +147,7 @@ describe('happy cases', () => {

afterAll(async () => {
if (!process.env.DISABLE_TEARDOWN) {
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);

const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

await cloudFormation.destroyStack({
stack: stackArtifact,
quiet: true,
});
await destroyStack(integTestApp, stack);
}
}, ONE_MINUTE * 3);
});
34 changes: 7 additions & 27 deletions packages/tracing/tests/e2e/tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

import { randomUUID, randomBytes } from 'crypto';
import { join } from 'path';
import { Tracing, Architecture } from '@aws-cdk/aws-lambda';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { Table, AttributeType, BillingMode } from '@aws-cdk/aws-dynamodb';
import { App, Duration, Stack, RemovalPolicy } from '@aws-cdk/core';
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
import { Tracing, Architecture } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Table, AttributeType, BillingMode } from 'aws-cdk-lib/aws-dynamodb';
import { App, Duration, Stack, RemovalPolicy } from 'aws-cdk-lib';
import { deployStack, destroyStack } from '../../../commons/tests/utils/cdk-cli';
import * as AWS from 'aws-sdk';
import { getTraces, getInvocationSubsegment } from '../helpers/tracesUtils';
import type { ParsedDocument } from '../helpers/tracesUtils';
Expand Down Expand Up @@ -101,16 +100,7 @@ describe('Tracer integration tests', () => {
};
}

const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);

const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });
await cloudFormation.deployStack({
stack: stackArtifact,
quiet: true,
});
await deployStack(integTestApp, stack);

// Act
Object.values(invocationsMap).forEach(async ({ functionName }) => {
Expand All @@ -132,17 +122,7 @@ describe('Tracer integration tests', () => {
afterAll(async () => {

if (!process.env.DISABLE_TEARDOWN) {
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);

const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
profile: process.env.AWS_PROFILE,
});
const cloudFormation = new CloudFormationDeployments({ sdkProvider });

await cloudFormation.destroyStack({
stack: stackArtifact,
quiet: true,
});
await destroyStack(integTestApp, stack);
}

}, ONE_MINUTE * 2);
Expand Down