Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
comcalvi committed Oct 3, 2024
1 parent 54f3b69 commit f565701
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
}
}

0 comments on commit f565701

Please sign in to comment.