From f5657018560404da824e31d1fe89ec58a1763576 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 3 Oct 2024 11:07:34 -0700 Subject: [PATCH] tests --- .../cloudformation-include/lib/cfn-include.ts | 6 +++ .../test/invalid-templates.test.ts | 52 ++++++++++++++++++ .../intrinsics-tags-resource-validation.json | 54 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 packages/aws-cdk-lib/cloudformation-include/test/test-templates/invalid/intrinsics-tags-resource-validation.json diff --git a/packages/aws-cdk-lib/cloudformation-include/lib/cfn-include.ts b/packages/aws-cdk-lib/cloudformation-include/lib/cfn-include.ts index edd6d6b88d495..a2849e5c794a6 100644 --- a/packages/aws-cdk-lib/cloudformation-include/lib/cfn-include.ts +++ b/packages/aws-cdk-lib/cloudformation-include/lib/cfn-include.ts @@ -138,6 +138,12 @@ export class CfnInclude extends core.CfnElement { this.dehydratedResources = props.dehydratedResources ?? []; + for (const logicalId of this.dehydratedResources) { + if (!Object.keys(this.template.Resources).includes(logicalId)) { + throw new Error(`Logical ID '${logicalId}' was specified in 'dehydratedResources', but does not belong to a resource in the template.`); + } + } + // check if all user specified parameter values exist in the template for (const logicalId of Object.keys(this.parametersToReplace)) { if (!(logicalId in (this.template.Parameters || {}))) { diff --git a/packages/aws-cdk-lib/cloudformation-include/test/invalid-templates.test.ts b/packages/aws-cdk-lib/cloudformation-include/test/invalid-templates.test.ts index 3f3bad52156c2..3802c00477d37 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/invalid-templates.test.ts +++ b/packages/aws-cdk-lib/cloudformation-include/test/invalid-templates.test.ts @@ -373,6 +373,58 @@ describe('CDK Include', () => { }, })); }); + + test('synth-time validation does not run on dehydrated resources', () => { + // synth-time validation fails if resource is hydrated + expect(() => { + includeTestTemplate(stack, 'intrinsics-tags-resource-validation.json'); + Template.fromStack(stack); + }).toThrow(`Resolution error: Supplied properties not correct for \"CfnLoadBalancerProps\" + tags: element 1: {} should have a 'key' and a 'value' property.`); + + app = new core.App({ context: { [cxapi.NEW_STYLE_STACK_SYNTHESIS_CONTEXT]: false } }); + stack = new core.Stack(app); + + // synth-time validation not run if resource is dehydrated + includeTestTemplate(stack, 'intrinsics-tags-resource-validation.json', { + dehydratedResources: ['MyLoadBalancer'], + }); + + expect(Template.fromStack(stack).hasResource('AWS::ElasticLoadBalancingV2::LoadBalancer', { + Properties: { + Tags: [ + { + Key: 'Name', + Value: 'MyLoadBalancer', + }, + { + data: [ + 'IsExtraTag', + { + Key: 'Name2', + Value: 'MyLoadBalancer2', + }, + { + data: 'AWS::NoValue', + type: 'Ref', + isCfnFunction: true, + }, + ], + type: 'Fn::If', + isCfnFunction: true, + }, + ], + }, + })); + }); + + test('throws on dehydrated resources not present in the template', () => { + expect(() => { + includeTestTemplate(stack, 'intrinsics-tags-resource-validation.json', { + dehydratedResources: ['ResourceNotExistingHere'], + }); + }).toThrow(/Logical ID 'ResourceNotExistingHere' was specified in 'dehydratedResources', but does not belong to a resource in the template./); + }); }); interface IncludeTestTemplateProps { diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/invalid/intrinsics-tags-resource-validation.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/invalid/intrinsics-tags-resource-validation.json new file mode 100644 index 0000000000000..b162016bb2577 --- /dev/null +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/invalid/intrinsics-tags-resource-validation.json @@ -0,0 +1,54 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Parameters": { + "IsExtraTag": { + "Type": "String", + "AllowedValues": [ + "true", + "false" + ], + "Default": "false" + } + }, + "Conditions": { + "AddExtraTag": { + "Fn::Equals": [ + { + "data": "IsExtraTag", + "type": "Ref", + "isCfnFunction": true + }, + "true" + ] + } + }, + "Resources": { + "MyLoadBalancer": { + "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer", + "Properties": { + "Tags": [ + { + "Key": "Name", + "Value": "MyLoadBalancer" + }, + { + "data": [ + "IsExtraTag", + { + "Key": "Name2", + "Value": "MyLoadBalancer2" + }, + { + "data": "AWS::NoValue", + "type": "Ref", + "isCfnFunction": true + } + ], + "type": "Fn::If", + "isCfnFunction": true + } + ] + } + } + } +} \ No newline at end of file