Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS-CDK Unit-Testing and Integration Testing

Tags:

aws-cdk

What is the proper way to unit-test and integration-test my newly built constructs?

What should be actually tested? How should it be tested?

Thanks!

like image 933
Romande Avatar asked Dec 01 '18 01:12

Romande


People also ask

How do I test AWS local CDK?

You can use the AWS SAM CLI to locally test your AWS CDK applications by running the following commands from the project root directory of your AWS CDK application: sam local invoke. sam local start-api. sam local start-lambda.

What is an AWS CDK app?

AWS CDK is an open-source framework that lets you model and provision AWS cloud resources using the programming language of your choice. It enables you to model application infrastructure using TypeScript, Python, Java, or . NET.

What is a construct in AWS CDK?

Constructs are the basic building blocks of AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component.

What is Monocdk?

An experiment to bundle all of the CDK into a single module.


1 Answers

As far as unit tests are concerned, we've got a library (currently only in TypeScript) to make assertions against synthesized CloudFormation templates. It's called @aws-cdk/assert. So what we usually do in unit tests is define a stack, add our construct, interact with it and then make assertions against the synthesized template using the assertion library.

Here's a sketch:

import { expect, haveResource } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/cdk');

const stack = new cdk.Stack();
const myConstruct = new MySpecialBucket(stack, 'Test');
myConstruct.doSomething();

expect(stack).to(haveResource('AWS::S3::Bucket', {
  Prop: 1234
});

You can find many examples in the AWS CDK GitHub repository (look for "test" directories).

Integration tests are a bit more tricky. What we've been doing is basically write little CDK apps as integration tests (for example) and compare the result of cdk synth to a checked-in expectation file. If they differ, we fail the build and request that the user manually deploy the app and update the checked-in file. This approach stems from the assumption that if the CFN template did not change, the resulting behavior would not change. That assumption has so far held quite well.

Hope that helps.

like image 68
Elad Ben-Israel Avatar answered Sep 18 '22 09:09

Elad Ben-Israel