From 6ba8d6145f5919667821ff75d0b1bdcca785e8be Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 21 Feb 2023 13:56:08 +0100 Subject: [PATCH 1/4] chore: add run e2e --- .github/workflows/run-e2e-tests.yml | 2 +- packages/parameters/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-e2e-tests.yml b/.github/workflows/run-e2e-tests.yml index 55775c82e1..ff56739b19 100644 --- a/.github/workflows/run-e2e-tests.yml +++ b/.github/workflows/run-e2e-tests.yml @@ -19,7 +19,7 @@ jobs: contents: read strategy: matrix: - package: [logger, metrics, tracer] + package: [logger, metrics, tracer, parameters] version: [14, 16, 18] fail-fast: false steps: diff --git a/packages/parameters/package.json b/packages/parameters/package.json index 32c9d56d69..469e72f70b 100644 --- a/packages/parameters/package.json +++ b/packages/parameters/package.json @@ -16,7 +16,7 @@ "test:e2e:nodejs14x": "RUNTIME=nodejs14x jest --group=e2e", "test:e2e:nodejs16x": "RUNTIME=nodejs16x jest --group=e2e", "test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e", - "test:e2e": "echo \"Not implemented\"", + "test:e2e": "jest --group=e2e", "watch": "jest --watch", "build": "tsc", "lint": "eslint --ext .ts --no-error-on-unmatched-pattern src tests", From 9f74c7cc5da01ca73caac804d03cbb0ffeb0b798 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 21 Feb 2023 15:27:12 +0100 Subject: [PATCH 2/4] Revert "chore: add run e2e" This reverts commit 6ba8d6145f5919667821ff75d0b1bdcca785e8be. --- .github/workflows/run-e2e-tests.yml | 2 +- packages/parameters/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-e2e-tests.yml b/.github/workflows/run-e2e-tests.yml index ff56739b19..55775c82e1 100644 --- a/.github/workflows/run-e2e-tests.yml +++ b/.github/workflows/run-e2e-tests.yml @@ -19,7 +19,7 @@ jobs: contents: read strategy: matrix: - package: [logger, metrics, tracer, parameters] + package: [logger, metrics, tracer] version: [14, 16, 18] fail-fast: false steps: diff --git a/packages/parameters/package.json b/packages/parameters/package.json index 469e72f70b..32c9d56d69 100644 --- a/packages/parameters/package.json +++ b/packages/parameters/package.json @@ -16,7 +16,7 @@ "test:e2e:nodejs14x": "RUNTIME=nodejs14x jest --group=e2e", "test:e2e:nodejs16x": "RUNTIME=nodejs16x jest --group=e2e", "test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e", - "test:e2e": "jest --group=e2e", + "test:e2e": "echo \"Not implemented\"", "watch": "jest --watch", "build": "tsc", "lint": "eslint --ext .ts --no-error-on-unmatched-pattern src tests", From 992f78b153019d71f332e7d72582e4d231d7d803 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 21 Feb 2023 15:50:42 +0100 Subject: [PATCH 3/4] tests: refactored table seeding --- .../tests/e2e/dynamoDBProvider.class.test.ts | 135 ++++++++++-------- .../tests/helpers/parametersUtils.ts | 34 ++++- 2 files changed, 109 insertions(+), 60 deletions(-) diff --git a/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts b/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts index ec468cd2b5..293bf22088 100644 --- a/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts +++ b/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts @@ -6,8 +6,7 @@ import path from 'path'; import { AttributeType } from 'aws-cdk-lib/aws-dynamodb'; import { App, Stack, Aspects } from 'aws-cdk-lib'; -import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'; -import { marshall } from '@aws-sdk/util-dynamodb'; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { v4 } from 'uuid'; import { generateUniqueName, @@ -24,7 +23,7 @@ import { TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT } from './constants'; -import { createDynamoDBTable } from '../helpers/parametersUtils'; +import { createDynamoDBTable, putDynamoDBItem } from '../helpers/parametersUtils'; const runtime: string = process.env.RUNTIME || 'nodejs18x'; @@ -37,8 +36,6 @@ const stackName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'dynam const functionName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'dynamoDBProvider'); const lambdaFunctionCodeFile = 'dynamoDBProvider.class.test.functionCode.ts'; -const dynamoDBClient = new DynamoDBClient({}); - const invocationCount = 1; // Parameters to be used by Parameters in the Lambda function @@ -209,102 +206,122 @@ describe(`parameters E2E tests (dynamoDBProvider) for runtime: ${runtime}`, () = ddbTabelGetMultipleCustomKeys, ])); - // Deploy the stack - await deployStack(integTestApp, stack); - // Seed tables with test data // Test 1 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGet, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test1', + table: ddbTableGet, + item: { id: 'my-param', value: 'foo', - }), - })); + }, + }); // Test 2 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetMultiple, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test2-a', + table: ddbTableGetMultiple, + item: { id: 'my-params', sk: 'config', value: 'bar', - }), - })); - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetMultiple, - Item: marshall({ + }, + }); + putDynamoDBItem({ + stack, + id: 'my-param-test2-b', + table: ddbTableGetMultiple, + item: { id: 'my-params', sk: 'key', value: 'baz', - }), - })); + }, + }); // Test 3 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetCustomkeys, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test3', + table: ddbTableGetCustomKeys, + item: { [keyAttr]: 'my-param', [valueAttr]: 'foo', - }), - })); + }, + }); // Test 4 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetMultipleCustomkeys, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test4-a', + table: ddbTabelGetMultipleCustomKeys, + item: { [keyAttr]: 'my-params', [sortAttr]: 'config', [valueAttr]: 'bar', - }), - })); - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetMultipleCustomkeys, - Item: marshall({ + }, + }); + putDynamoDBItem({ + stack, + id: 'my-param-test4-b', + table: ddbTabelGetMultipleCustomKeys, + item: { [keyAttr]: 'my-params', [sortAttr]: 'key', [valueAttr]: 'baz', - }), - })); + }, + }); // Test 5 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGet, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test5', + table: ddbTableGet, + item: { id: 'my-param-json', value: JSON.stringify({ foo: 'bar' }), - }), - })); + }, + }); // Test 6 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGet, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test6', + table: ddbTableGet, + item: { id: 'my-param-binary', value: 'YmF6', // base64 encoded 'baz' - }), - })); - + }, + }); + // Test 7 - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetMultiple, - Item: marshall({ + putDynamoDBItem({ + stack, + id: 'my-param-test7-a', + table: ddbTableGetMultiple, + item: { id: 'my-encoded-params', sk: 'config.json', value: JSON.stringify({ foo: 'bar' }), - }), - })); - await dynamoDBClient.send(new PutItemCommand({ - TableName: tableGetMultiple, - Item: marshall({ + }, + }); + putDynamoDBItem({ + stack, + id: 'my-param-test7-b', + table: ddbTableGetMultiple, + item: { id: 'my-encoded-params', sk: 'key.binary', value: 'YmF6', // base64 encoded 'baz' - }), - })); + }, + }); // Test 8 & 9 use the same items as Test 1 + // Deploy the stack + await deployStack(integTestApp, stack); + // and invoke the Lambda function invocationLogs = await invokeFunction(functionName, invocationCount, 'SEQUENTIAL'); diff --git a/packages/parameters/tests/helpers/parametersUtils.ts b/packages/parameters/tests/helpers/parametersUtils.ts index 3bd785bbed..3e769f9d3e 100644 --- a/packages/parameters/tests/helpers/parametersUtils.ts +++ b/packages/parameters/tests/helpers/parametersUtils.ts @@ -1,5 +1,5 @@ import { Stack, RemovalPolicy, CustomResource, Duration } from 'aws-cdk-lib'; -import { Provider } from 'aws-cdk-lib/custom-resources'; +import { PhysicalResourceId, Provider } from 'aws-cdk-lib/custom-resources'; import { RetentionDays } from 'aws-cdk-lib/aws-logs'; import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; import { Runtime } from 'aws-cdk-lib/aws-lambda'; @@ -14,6 +14,11 @@ import { CfnEnvironment, CfnHostedConfigurationVersion, } from 'aws-cdk-lib/aws-appconfig'; +import { + AwsCustomResource, + AwsCustomResourcePolicy +} from 'aws-cdk-lib/custom-resources'; +import { marshall } from '@aws-sdk/util-dynamodb'; export type CreateDynamoDBTableOptions = { stack: Stack @@ -201,10 +206,37 @@ const createSSMSecureString = (options: CreateSSMSecureStringOptions): IStringPa return param; }; +export type PutDynamoDBItemOptions = { + stack: Stack + id: string + table: Table + item: Record +}; + +const putDynamoDBItem = async (options: PutDynamoDBItemOptions): Promise => { + const { stack, id, table, item } = options; + + new AwsCustomResource(stack, id, { + onCreate: { + service: 'DynamoDB', + action: 'putItem', + parameters: { + TableName: table.tableName, + Item: marshall(item), + }, + physicalResourceId: PhysicalResourceId.of(id), + }, + policy: AwsCustomResourcePolicy.fromSdkCalls({ + resources: [table.tableArn], + }), + }); +}; + export { createDynamoDBTable, createBaseAppConfigResources, createAppConfigConfigurationProfile, createSSMSecureString, createSecureStringProvider, + putDynamoDBItem, }; From 8bcc34c6ff05edae26f3e139b7ff4d7147396342 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 21 Feb 2023 15:58:32 +0100 Subject: [PATCH 4/4] chore: fix linting --- packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts b/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts index 293bf22088..62e8bd6c39 100644 --- a/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts +++ b/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts @@ -6,7 +6,6 @@ import path from 'path'; import { AttributeType } from 'aws-cdk-lib/aws-dynamodb'; import { App, Stack, Aspects } from 'aws-cdk-lib'; -import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { v4 } from 'uuid'; import { generateUniqueName,