From c7929c192b6d84bea0b402ef1b909513c7c4aa47 Mon Sep 17 00:00:00 2001 From: Ryan Ling Date: Sun, 7 Jan 2024 22:56:03 +1100 Subject: [PATCH] Use `aws-cdk-lib/assertions` This is in the docs and lets you add fine-grained assertions on top. https://docs.aws.amazon.com/cdk/v2/guide/testing.html#testing_snapshot --- .../infra/__snapshots__/appStack.test.ts.snap | 68 +++++++++++++++++++ .../infra/appStack.test.ts | 6 +- template/lambda-sqs-worker-cdk/package.json | 1 - 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap b/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap index 219b3cfeb..2ef08a6fc 100644 --- a/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap +++ b/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap @@ -2,6 +2,13 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` { + "Parameters": { + "BootstrapVersion": { + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]", + "Type": "AWS::SSM::Parameter::Value", + }, + }, "Resources": { "kmskey49FBC3B3": { "DeletionPolicy": "Retain", @@ -358,11 +365,45 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` "UpdateReplacePolicy": "Delete", }, }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5", + ], + { + "Ref": "BootstrapVersion", + }, + ], + }, + ], + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.", + }, + ], + }, + }, } `; exports[`returns expected CloudFormation stack for prod 1`] = ` { + "Parameters": { + "BootstrapVersion": { + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]", + "Type": "AWS::SSM::Parameter::Value", + }, + }, "Resources": { "kmskey49FBC3B3": { "DeletionPolicy": "Retain", @@ -719,5 +760,32 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` "UpdateReplacePolicy": "Delete", }, }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5", + ], + { + "Ref": "BootstrapVersion", + }, + ], + }, + ], + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.", + }, + ], + }, + }, } `; diff --git a/template/lambda-sqs-worker-cdk/infra/appStack.test.ts b/template/lambda-sqs-worker-cdk/infra/appStack.test.ts index 12f3458fa..4e31029cf 100644 --- a/template/lambda-sqs-worker-cdk/infra/appStack.test.ts +++ b/template/lambda-sqs-worker-cdk/infra/appStack.test.ts @@ -1,5 +1,5 @@ -import { SynthUtils } from '@aws-cdk/assert'; import { App } from 'aws-cdk-lib'; +import { Template } from 'aws-cdk-lib/assertions'; import cdkJson from '../cdk.json'; @@ -24,7 +24,9 @@ it.each(contexts)( const stack = new AppStack(app, 'appStack'); - const json = JSON.stringify(SynthUtils.toCloudFormation(stack)).replace( + const template = Template.fromStack(stack); + + const json = JSON.stringify(template.toJSON()).replace( /"S3Key":"([0-9a-f]+)\.zip"/g, (_, hash) => `"S3Key":"${'x'.repeat(hash.length)}.zip"`, ); diff --git a/template/lambda-sqs-worker-cdk/package.json b/template/lambda-sqs-worker-cdk/package.json index 28e4fe5dc..7f43b28a6 100644 --- a/template/lambda-sqs-worker-cdk/package.json +++ b/template/lambda-sqs-worker-cdk/package.json @@ -16,7 +16,6 @@ "zod": "^3.19.1" }, "devDependencies": { - "@aws-cdk/assert": "^2.24.0", "@types/aws-lambda": "^8.10.82", "@types/node": "^20.9.0", "aws-cdk": "^2.109.0",