Writing Tests
A key advantage of using imperative IaC is the ability to write test automation using the respective languages full ecossystem. This is a big deal. One can use familiar tools, patterns, and workflows that are mature.
Pure declaritive IaC will never be able to compete on this front, and will always be lagging behind.
AWS CDK
AWS CDK comes with an assertion library which makes life easy. It enables you to construct various assertions against the synthesized plan output in JSON format. Read the AWS CDK testing docs for a full refence:
Tip on JSON Output
There are two ways to output the plan as json.
- CLI: run
cdk synth --json
on a project. - Code in the test: add the following line to the body of a test. This is convenient when working on CDF Packages.
console.log(JSON.stringify(template.toJSON(), null, 2));
Here's an exmple of how package authors can write tests for a TypeScript Package with Jest.
- Supply a config file that's scoped to the Service or Component being tested. This helps at both test dev time by making it easier to scan the JSON. It will also make tests run faster.
- Setup a
Stack
instance, same as the project bootstrap where the Package is used for real. - This line outputs the synthesized plan in JSON to the console. This is convenient during development.
- Check that there's one of each resource
- Check that all ECS Services have the prop
{ LaunchType: "FARGATE" }
Run Jest to execute tests. In the sampke Package this is mapped to npm test
.
See the AWS CDK Package test folder /samples/packages/awscdk-typescript/test for fully working tests.
CDK for Terraform
Coming...
Pulumi
Coming...