Skip to content

Commit

Permalink
end-2-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromevdl committed Aug 29, 2023
1 parent 6f09ef6 commit 680669b
Show file tree
Hide file tree
Showing 10 changed files with 509 additions and 11 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/run-e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Run e2e tests

on:
# TODO remove before merging to main
push:
branches:
- test/end-2-end-tests

workflow_dispatch:
inputs:
prNumber:
description: "(Optional) PR Number. If you specify a value here, the value of the branch field will be ignored."
required: false
default: ""

env:
NODE_VERSION: 18

jobs:
run-e2e-tests:
runs-on: ubuntu-latest
env:
NODE_ENV: dev
PR_NUMBER: ${{ inputs.prNumber }}
permissions:
id-token: write # needed to interact with GitHub's OIDC Token endpoint.
contents: write
steps:
- name: Checkout Repo
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
# If we pass a PR Number when triggering the workflow we will retrieve the PR info and get its headSHA
- name: Extract PR details
id: extract_PR_details
if: ${{ inputs.prNumber != '' }}
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
with:
script: |
const script = require('.github/scripts/get-pr-info.js');
await script({github, context, core});
# Only if a PR Number was passed and the headSHA of the PR extracted,
# we checkout the PR at that point in time
- name: Checkout PR code
if: ${{ inputs.prNumber != '' }}
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
with:
ref: ${{ steps.extract_PR_details.outputs.headSHA }}
- name: Setup NodeJS and dependency cache
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'yarn'
cache-dependency-path: |
yarn.lock
framework/yarn.lock
solutions/yarn.lock
- name: Install dependencies
run: yarn install --check-files --frozen-lockfile
- name: Build
run: npx projen build
- name: Setup AWS credentials
uses: aws-actions/configure-aws-credentials@04b98b3f9e85f563fb061be8751a0352327246b0 # v3.0.1
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN_TO_ASSUME }}
aws-region: eu-west-1
- name: Run e2e tests
run: npx projen test:e2e
9 changes: 9 additions & 0 deletions .projen/tasks.json

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

27 changes: 25 additions & 2 deletions .projenrc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LernaProject } from 'lerna-projen';
import { awscdk } from 'projen';
import { DependabotScheduleInterval } from 'projen/lib/github';
import { Transform } from "projen/lib/javascript";

const CDK_VERSION = '2.84.0';
const CDK_CONSTRUCTS_VERSION = '10.2.55';
Expand Down Expand Up @@ -58,7 +59,7 @@ const rootProject = new LernaProject({
jest: false
});

new awscdk.AwsCdkConstructLibrary({
const fwkProject = new awscdk.AwsCdkConstructLibrary({
name: 'framework',
description: 'L3 CDK Constructs used to build data solutions with AWS',

Expand Down Expand Up @@ -93,16 +94,34 @@ new awscdk.AwsCdkConstructLibrary({
devDeps: [
'cdk-nag@^2.0.0',
'@types/jest',
'@jest/globals',
'ts-jest',
'jest-runner-groups',
`@aws-cdk/cli-lib-alpha@${CDK_VERSION}-alpha.0`,
],

jestOptions: {
jestConfig: {
runner: 'groups',
},
transform: {
'^.+\\.ts?$': new Transform('ts-jest', {
tsconfig: 'tsconfig.dev.json',
}),
},
globals: {
'ts-jest': null // remove jest deprecation warning caused by projen-generated default config
}
}
},
});

fwkProject.setScript('test', 'npx projen test --group=-e2e');

fwkProject.addTask('test:e2e', {
description: 'Run framework end-to-end tests',
exec: 'npx projen test --group=e2e'
});

new awscdk.AwsCdkConstructLibrary({
name: 'solutions',
description: 'Pre-packaged data solutions built with the AWS Data Solutions Framework',
Expand Down Expand Up @@ -137,4 +156,8 @@ new awscdk.AwsCdkConstructLibrary({
packageName: 'aws-data-solutions',
});

rootProject.addTask('test:e2e', {
description: 'Run end-to-end tests'
});

rootProject.synth();
9 changes: 9 additions & 0 deletions framework/.projen/deps.json

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

13 changes: 11 additions & 2 deletions framework/.projen/tasks.json

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

23 changes: 16 additions & 7 deletions framework/package.json

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

47 changes: 47 additions & 0 deletions framework/test/e2e/sample.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

// TODO : remove this file once we have real e2e tests

/**
* E2E test sample
*
* @group e2e/sample
*/

import * as cdk from 'aws-cdk-lib';
import { Names, RemovalPolicy } from 'aws-cdk-lib';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { TestStack } from './test-stack';

jest.setTimeout(6000000);

// GIVEN
const testStack = new TestStack('SampleTest');
const { stack } = testStack;

// creation of the construct(s) under test
const bucket = new Bucket(stack, 'sampleTestBucket', {
bucketName: 'sample-test-bucket-' + Names.uniqueResourceName(stack, {}).toLowerCase(),
removalPolicy: RemovalPolicy.DESTROY,
});
new cdk.CfnOutput(stack, 'BucketName', {
value: bucket.bucketName,
exportName: 'bucketName',
});

let deployResult: Record<string, string>;

beforeAll(async() => {
// WHEN
deployResult = await testStack.deploy();
}, 900000);

it('bucket created successfully', async () => {
// THEN
expect(deployResult.BucketName).toContain('sample-test-bucket-');
});

afterAll(async () => {
await testStack.destroy();
}, 900000);
87 changes: 87 additions & 0 deletions framework/test/e2e/test-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0


import { randomUUID } from 'crypto';
import { readFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { AwsCdkCli, ICloudAssemblyDirectoryProducer, RequireApproval } from '@aws-cdk/cli-lib-alpha';
import { App, Stack } from 'aws-cdk-lib';

/**
* Test stack that can be deployed to the selected environment.
*/
class TestStack implements ICloudAssemblyDirectoryProducer {
/**
* Reference to the AWS CDK App object.
* @default new App()
*/
public app: App;
/**
* Reference to the AWS CDK Stack object.
* @default new Stack(this.app, stackName)
*/
public stack: Stack;
/**
* @internal
* Reference to the AWS CDK CLI object.
*/
#cli: AwsCdkCli;

public constructor(stackName: string, app?: App, stack?: Stack) {
this.app = app ?? new App();
this.stack = stack ?? new Stack(this.app, stackName + '-' + randomUUID().substring(0, 8).toLowerCase());
this.#cli = AwsCdkCli.fromCloudAssemblyDirectoryProducer(this);
}

/**
* Deploy the test stack to the selected environment.
*
* It returns the outputs of the deployed stack.
*/
public async deploy(): Promise<Record<string, string>> {
const outputFilePath = join(
tmpdir(),
'powertools-e2e-testing',
`${this.stack.stackName}.outputs.json`,
);
await this.#cli.deploy({
stacks: [this.stack.stackName],
requireApproval: RequireApproval.NEVER,
outputsFile: outputFilePath,
});

return JSON.parse(await readFile(outputFilePath, 'utf-8'))[
this.stack.stackName
];
}

/**
* Destroy the test stack.
*/
public async destroy(): Promise<void> {
await this.#cli.destroy({
stacks: [this.stack.stackName],
requireApproval: false,
});
}

/**
* Produce the Cloud Assembly directory.
*/
public async produce(_context: Record<string, unknown>): Promise<string> {
return this.app.synth().directory;
}

/**
* Synthesize the test stack.
*/
public async synth(): Promise<void> {
await this.#cli.synth({
stacks: [this.stack.stackName],
});
}
}

export { TestStack };
Loading

0 comments on commit 680669b

Please sign in to comment.