Skip to content

Instantly share code, notes, and snippets.

@tamakiii
Last active February 21, 2023 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamakiii/e46a4a716c6de4e198e36e0909abc195 to your computer and use it in GitHub Desktop.
Save tamakiii/e46a4a716c6de4e198e36e0909abc195 to your computer and use it in GitHub Desktop.
AWS CDK Fine-grained Assertion Test
// lib/cdk-demo-stack.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
export class CdkDemoStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'MainVpc', {
maxAzs: 2,
subnetConfiguration: [
{
cidrMask: 24,
name: 'public-subnet',
subnetType: ec2.SubnetType.PUBLIC
}
]
})
}
}
// test/cdk-demo.test.ts
import * as cdk from 'aws-cdk-lib';
import { Capture, Template } from 'aws-cdk-lib/assertions';
import * as CdkDemo from '../lib/cdk-demo-stack';
import { strict as assert } from 'node:assert/strict';
const app = new cdk.App();
const stack = new CdkDemo.CdkDemoStack(app, 'MainVpc');
const template = Template.fromStack(stack);
test('assert resources', () => {
const json = template.toJSON();
const types = Object.values(json.Resources).map((resource: any) => resource.Type);
assert.equal(types.length, 11);
assert.deepEqual(types, [
'AWS::EC2::VPC',
"AWS::EC2::Subnet",
"AWS::EC2::RouteTable",
"AWS::EC2::SubnetRouteTableAssociation",
"AWS::EC2::Route",
"AWS::EC2::Subnet",
"AWS::EC2::RouteTable",
"AWS::EC2::SubnetRouteTableAssociation",
"AWS::EC2::Route",
"AWS::EC2::InternetGateway",
"AWS::EC2::VPCGatewayAttachment",
]);
});
test('VPC created', () => {
template.resourceCountIs('AWS::EC2::VPC', 1);
template.hasResourceProperties('AWS::EC2::VPC', {
CidrBlock: "10.0.0.0/16",
EnableDnsHostnames: true,
EnableDnsSupport: true,
InstanceTenancy: 'default',
Tags: [
{Key:'Name', Value:'MainVpc/MainVpc'},
],
});
});
test('subnets created', () => {
const captures = {
vpc: [new Capture(), new Capture()],
az: [new Capture(), new Capture()],
};
template.resourceCountIs('AWS::EC2::Subnet', 2);
template.hasResourceProperties('AWS::EC2::Subnet', {
VpcId: captures.vpc[0],
AvailabilityZone: captures.az[0],
CidrBlock: "10.0.0.0/24",
MapPublicIpOnLaunch: true,
Tags: [
{Key: 'aws-cdk:subnet-name', Value: 'public-subnet'},
{Key: 'aws-cdk:subnet-type', Value: 'Public'},
{Key: 'Name', Value: 'MainVpc/MainVpc/public-subnetSubnet1'},
]
});
template.hasResourceProperties('AWS::EC2::Subnet', {
VpcId: captures.vpc[1],
AvailabilityZone: captures.az[1],
CidrBlock: "10.0.1.0/24",
MapPublicIpOnLaunch: true,
Tags: [
{Key: 'aws-cdk:subnet-name', Value: 'public-subnet'},
{Key: 'aws-cdk:subnet-type', Value: 'Public'},
{Key: 'Name', Value: 'MainVpc/MainVpc/public-subnetSubnet2'},
]
});
const vpcIds = captures.vpc.map(capture => capture.asObject().Ref);
assert.equal([...new Set(vpcIds)].length, 1); // Unique
const azNumbers = captures.az.map(capture => capture.asObject()['Fn::Select'][0]);
assert.equal(azNumbers.length, 2);
assert.equal(azNumbers[0], 0);
assert.equal(azNumbers[1], 1);
const azNames = captures.az.map(capture => capture.asObject()['Fn::Select'][1]['Fn::GetAZs']);
assert.equal(azNames.length, 2);
assert.equal(azNames[0], '');
assert.equal(azNames[1], '');
});
test('Snapshot', () => {
expect(template.toJSON()).toMatchSnapshot();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment