Skip to content

Commit

Permalink
feat: update exeample to use @nordicsemiconductor/bdd-markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Sep 22, 2022
1 parent 5e90536 commit d499668
Show file tree
Hide file tree
Showing 40 changed files with 6,770 additions and 9,690 deletions.
20 changes: 0 additions & 20 deletions .editorconfig

This file was deleted.

3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
dist/
node_modules/
cdk.out/
commitlint.config.js
aws/lambda.js
commitlint.config.js
14 changes: 13 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
{
"extends": "@nordicsemiconductor/eslint-config-asset-tracker-cloud-typescript"
"extends": "@nordicsemiconductor/eslint-config-asset-tracker-cloud-typescript",
"rules": {
"no-constant-condition": ["off"],
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never",
"tsx": "never"
}
]
},
"plugins": ["import"]
}
3 changes: 0 additions & 3 deletions .github/workflows/code-style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Compile TypeScript
run: npx tsc

- name: Check source code with eslint
run: npx eslint --fix --ext .js,.ts,.jsx,.tsx ./

Expand Down
23 changes: 18 additions & 5 deletions .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,50 @@ env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: eu-central-1
STACK_BASE_NAME: bddfrawsx-${{ github.sha }}
AWS_REGION: ${{ secrets.AWS_REGION }}
STACK_BASE_NAME: bddawsx-${{ github.sha }}

jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: "16.x"

- name: Install dependencies
run: npm ci --no-audit

- name: Keep npm cache around to speed up installs
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
- name: Build
run: npx tsc

- name: Bootstrap AWS account for CDK
run: |
npx cdk bootstrap aws://`aws sts get-caller-identity | jq -r '.Account' | tr -d '\n'`/${{ secrets.AWS_REGION }}
- name: Set up test resources
run: npx cdk deploy --require-approval never

- name: Test
run: npm test
run: |
npx tsx features/run-features.ts > e2e-test-result.json
cat e2e-test-result.json | npx tsx features/console-reporter.ts
cat e2e-test-result.json | npx tsx features/markdown-reporter.ts >> $GITHUB_STEP_SUMMARY
cat e2e-test-result.json | npx tsx features/traceToMermaid.ts >> $GITHUB_STEP_SUMMARY
- name: Semantic release
if: success()
continue-on-error: true
run: |
npm i --no-save semantic-release@next
npx semantic-release
- name: Clean up End-to-End test resources
if: always()
run: npx cdk destroy -f
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/dist/
/node_modules/
cdk.out/
cdk.context.json
3 changes: 0 additions & 3 deletions .prettierrc.js

This file was deleted.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# BDD Feature Runner for AWS Examples

[![GitHub Actions](https://github.com/NordicSemiconductor/cloud-e2e-bdd-test-runner-example-js/workflows/Test%20and%20Release/badge.svg)](https://github.com/NordicSemiconductor/cloud-e2e-bdd-test-runner-example-js/actions)
[![GitHub Actions](https://github.com/NordicSemiconductor/cloud-bdd-markdown-e2e-example-aws-js/workflows/Test%20and%20Release/badge.svg)](https://github.com/NordicSemiconductor/cloud-bdd-markdown-e2e-example-aws-js/actions)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Renovate](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)
[![Mergify Status](https://img.shields.io/endpoint.svg?url=https://gh.mergify.io/badges/NordicSemiconductor/cloud-e2e-bdd-test-runner-example-js)](https://mergify.io)
[![Mergify Status](https://img.shields.io/endpoint.svg?url=https://gh.mergify.io/badges/NordicSemiconductor/cloud-bdd-markdown-e2e-example-aws-js)](https://mergify.io)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier/)
[![ESLint: TypeScript](https://img.shields.io/badge/ESLint-TypeScript-blue.svg)](https://github.com/typescript-eslint/typescript-eslint)

Example use of
[@nordicsemiconductor/e2e-bdd-test-runner](https://github.com/NordicSemiconductor/cloud-e2e-bdd-test-runner-js).
[@nordicsemiconductor/bdd-markdown](https://www.npmjs.com/package/@nordicsemiconductor/bdd-markdown).

npm ci # install dependencies
npx cdk deploy # deploy the stack to your AWS account
Expand All @@ -21,7 +21,7 @@ learn more._

## Webhook receiver

The [`Webhook.feature`](./features/Webhook.feature) shows how to use AWS
The [`Webhook.feature.md`](./features/Webhook.feature.md) shows how to use AWS
ApiGateway, Lambda and SQS to set up a real test double for a webhook endpoint.
It allows to test that a component which is supposed to send a webhook is
actually sending it.
Expand All @@ -41,7 +41,7 @@ manual step done through the AWS CLI:

Then set up the continuous deployment:

npx cdk -a 'node dist/aws/cloudformation-cd.js' deploy
npx tsx cdk/cloudformation-cd.ts deploy

## Architecture decision records (ADRs)

Expand Down
9 changes: 0 additions & 9 deletions aws/TestApp.ts

This file was deleted.

6 changes: 0 additions & 6 deletions aws/cloudformation-test.ts

This file was deleted.

24 changes: 0 additions & 24 deletions aws/lambda.js

This file was deleted.

2 changes: 1 addition & 1 deletion cdk.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"app": "node dist/aws/cloudformation-test.js"
"app": "npx tsx cdk/cloudformation-test.ts"
}
2 changes: 1 addition & 1 deletion aws/CDApp.ts → cdk/CDApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as CDK from 'aws-cdk-lib'
import { CDStack } from './CDStack'
import { CDStack } from './CDStack.js'

export class CDApp extends CDK.App {
public constructor({
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions cdk/TestApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as CDK from 'aws-cdk-lib'
import { AppProps } from 'aws-cdk-lib'
import { PackedLambda } from './packLambda.js'
import { PackedLayer } from './packLayer.js'
import { WebhookReceiverStack } from './WebhookReceiverStack.js'

export class TestApp extends CDK.App {
public constructor({
stackName,
context,
lambdaSource,
layer,
}: {
stackName: string
lambdaSource: PackedLambda
layer: PackedLayer
context?: AppProps['context']
}) {
super({ context })
new WebhookReceiverStack(this, stackName, { lambdaSource, layer })
}
}
67 changes: 42 additions & 25 deletions aws/WebhookReceiverStack.ts → cdk/WebhookReceiverStack.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
import * as CDK from 'aws-cdk-lib'
import * as ApiGateway from 'aws-cdk-lib/aws-apigateway'
import * as IAM from 'aws-cdk-lib/aws-iam'
import * as Lambda from 'aws-cdk-lib/aws-lambda'
import * as Logs from 'aws-cdk-lib/aws-logs'
import * as SQS from 'aws-cdk-lib/aws-sqs'
import { readFileSync } from 'fs'
import * as path from 'path'
import { PackedLambda } from './packLambda.js'
import { PackedLayer } from './packLayer.js'

/**
* This is the CloudFormation stack which contains the webhook receiver resources.
*/
export class WebhookReceiverStack extends CDK.Stack {
public constructor(parent: CDK.App, id: string) {
public constructor(
parent: CDK.App,
id: string,
{
lambdaSource,
layer,
}: {
lambdaSource: PackedLambda
layer: PackedLayer
},
) {
super(parent, id)

const enableTracing = this.node.tryGetContext('isTest') === true

// This queue will store all the requests made to the API Gateway
const queue = new SQS.Queue(this, 'queue', {
fifo: true,
visibilityTimeout: CDK.Duration.seconds(5),
queueName: `${id}.fifo`,
})

const baseLayer = new Lambda.LayerVersion(this, 'baseLayer', {
code: Lambda.Code.fromAsset(layer.layerZipFile),
compatibleArchitectures: [Lambda.Architecture.ARM_64],
compatibleRuntimes: [Lambda.Runtime.NODEJS_16_X],
})

// This lambda will publish all requests made to the API Gateway in the queue
const lambda = new Lambda.Function(this, 'Lambda', {
description: 'Publishes webhook requests into SQS',
code: Lambda.Code.fromInline(
readFileSync(
path.resolve(process.cwd(), 'aws', 'lambda.js'),
'utf-8',
).toString(),
),
handler: 'index.handler',
runtime: Lambda.Runtime.NODEJS_12_X, // NODEJS_14_X does not support inline functions, yet. See https://github.com/aws/aws-cdk/pull/12861#discussion_r570038002
code: Lambda.Code.fromAsset(lambdaSource.lambdaZipFile),
layers: [baseLayer],
handler: lambdaSource.handler,
runtime: Lambda.Runtime.NODEJS_16_X,
architecture: Lambda.Architecture.ARM_64,
timeout: CDK.Duration.seconds(15),
initialPolicy: [
new IAM.PolicyStatement({
Expand All @@ -49,30 +63,33 @@ export class WebhookReceiverStack extends CDK.Stack {
],
environment: {
SQS_QUEUE: queue.queueUrl,
ENABLE_TRACING: enableTracing ? '1' : '0',
},
tracing: enableTracing ? Lambda.Tracing.ACTIVE : Lambda.Tracing.DISABLED,
})

if (enableTracing)
lambda.addToRolePolicy(
new IAM.PolicyStatement({
resources: ['*'],
actions: ['xray:PutTraceSegments', 'xray:PutTelemetryRecords'],
}),
)

// Create the log group here, so we can control the retention
new Logs.LogGroup(this, `LambdaLogGroup`, {
removalPolicy: CDK.RemovalPolicy.DESTROY,
logGroupName: `/aws/lambda/${lambda.functionName}`,
logGroupName: `/cdk/lambda/${lambda.functionName}`,
retention: Logs.RetentionDays.ONE_DAY,
})

// This is the API Gateway, AWS CDK automatically creates a prod stage and deployment
const api = new ApiGateway.RestApi(this, 'api', {
restApiName: 'Webhook Receiver API',
description: 'API Gateway to test webhook deliveries',
})
const proxyResource = api.root.addResource('{proxy+}')
proxyResource.addMethod('ANY', new ApiGateway.LambdaIntegration(lambda))
// API Gateway needs to be able to call the lambda
lambda.addPermission('InvokeByApiGateway', {
principal: new IAM.ServicePrincipal('apigateway.amazonaws.com'),
sourceArn: api.arnForExecuteApi(),
const fnUrl = lambda.addFunctionUrl({
authType: Lambda.FunctionUrlAuthType.NONE,
})

// Export these so the test runner can use them
new CDK.CfnOutput(this, 'ApiURL', {
value: api.url,
value: fnUrl.url,
exportName: `${this.stackName}:ApiURL`,
})
new CDK.CfnOutput(this, 'QueueURL', {
Expand Down
4 changes: 2 additions & 2 deletions aws/cloudformation-cd.ts → cdk/cloudformation-cd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from 'fs'
import * as path from 'path'
import { parse } from 'url'
import { CDApp } from './CDApp'
import { stackBaseName } from './stackBaseName'
import { CDApp } from './CDApp.js'
import { stackBaseName } from './stackBaseName.js'

const pjson = JSON.parse(
readFileSync(path.join(__dirname, '..', '..', 'package.json'), 'utf-8'),
Expand Down
22 changes: 22 additions & 0 deletions cdk/cloudformation-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from 'path'
import { packLambda } from './packLambda.js'
import { packLayer } from './packLayer.js'
import { stackBaseName } from './stackBaseName.js'
import { TestApp } from './TestApp.js'

const baseDir = path.join(process.cwd(), 'lambda')

new TestApp({
stackName: `${stackBaseName()}-test`,
lambdaSource: await packLambda({
id: 'webhookReceiver',
baseDir,
}),
layer: await packLayer({
id: 'baseLayer',
dependencies: ['@aws-sdk/client-sqs', '@aws-lambda-powertools/tracer'],
}),
context: {
isTest: true,
},
}).synth()
13 changes: 13 additions & 0 deletions cdk/listLambdaDependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dependencyTree from 'dependency-tree'
import { statSync } from 'fs'
import path from 'path'

export const listLambdaDependencies = (entryFile: string): string[] => {
statSync(entryFile)
return dependencyTree.toList({
filename: entryFile,
directory: process.cwd(),
tsConfig: path.join(process.cwd(), 'tsconfig.json'),
filter: (path) => !path.includes('node_modules'), // do not include node_modules
})
}
Loading

0 comments on commit d499668

Please sign in to comment.