Skip to content

Commit 2747a76

Browse files
nmussyElad Ben-Israel
authored and
Elad Ben-Israel
committed
fix(assert): CfnParameter MatchStyle diff support (#3408)
* `NO_REPLACES` now verifies that no parameters are updated * `SUPERSET` now verifies that no parameters are added Fixes #3399
1 parent 9565b9b commit 2747a76

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

packages/@aws-cdk/assert/lib/assertions/match-template.ts

+10
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,22 @@ class StackMatchesTemplateAssertion extends Assertion<StackInspector> {
6262
if (change.changeImpact === cfnDiff.ResourceImpact.MAY_REPLACE) { return false; }
6363
if (change.changeImpact === cfnDiff.ResourceImpact.WILL_REPLACE) { return false; }
6464
}
65+
66+
for (const key of Object.keys(diff.parameters.changes)) {
67+
const change = diff.parameters.changes[key]!;
68+
if (change.isUpdate) { return false; }
69+
}
6570
return true;
6671
case MatchStyle.SUPERSET:
6772
for (const key of Object.keys(diff.resources.changes)) {
6873
const change = diff.resources.changes[key]!;
6974
if (change.changeImpact !== cfnDiff.ResourceImpact.WILL_CREATE) { return false; }
7075
}
76+
77+
for (const key of Object.keys(diff.parameters.changes)) {
78+
const change = diff.parameters.changes[key]!;
79+
if (change.isAddition) { return false; }
80+
}
7181
return true;
7282
}
7383
throw new Error(`Unsupported match style: ${this.matchStyle}`);

packages/@aws-cdk/assert/test/test.assertions.ts

+65
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ passingExample('sugar for matching stack to a template', () => {
8282
}
8383
});
8484
});
85+
passingExample('expect <synthStack> to match (no replaces) <template> with parameters', () => {
86+
const parameterType = 'Test::Parameter';
87+
const synthStack = synthesizedStack(stack => {
88+
new TestParameter(stack, 'TestParameter', { type: parameterType });
89+
});
90+
const expected = {};
91+
expect(synthStack).to(matchTemplate(expected, MatchStyle.NO_REPLACES));
92+
});
93+
passingExample('expect <synthStack> to be a superset of <template> with parameters', () => {
94+
const parameterType = 'Test::Parameter';
95+
const synthStack = synthesizedStack(stack => {
96+
// Added
97+
new TestResource(stack, 'NewResource', { type: 'AWS::S3::Bucket' });
98+
// Expected
99+
new TestParameter(stack, 'TestParameterA', {type: parameterType});
100+
new TestParameter(stack, 'TestParameterB', {type: parameterType, default: { Foo: 'Bar' } });
101+
});
102+
const expected = {
103+
Parameters: {
104+
TestParameterA: { Type: 'Test::Parameter' },
105+
TestParameterB: { Type: 'Test::Parameter', Default: { Foo: 'Bar' } }
106+
}
107+
};
108+
expect(synthStack).to(matchTemplate(expected, MatchStyle.SUPERSET));
109+
});
85110

86111
failingExample('expect <synthStack> at <some path> *not* to have <some type>', () => {
87112
const resourceType = 'Test::Resource';
@@ -146,6 +171,36 @@ failingExample('expect <synthStack> to be a superset of <template>', () => {
146171
};
147172
expect(synthStack).to(matchTemplate(expected, MatchStyle.SUPERSET));
148173
});
174+
failingExample('expect <synthStack> to match (no replaces) <template> with parameters', () => {
175+
const parameterType = 'Test::Parameter';
176+
const synthStack = synthesizedStack(stack => {
177+
new TestParameter(stack, 'TestParameter', { type: parameterType });
178+
});
179+
const expected = {
180+
Parameters: {
181+
TestParameter: { Type: 'AWS::S3::Bucket' }
182+
}
183+
};
184+
expect(synthStack).to(matchTemplate(expected, MatchStyle.NO_REPLACES));
185+
});
186+
failingExample('expect <synthStack> to be a superset of <template> with parameters', () => {
187+
const parameterType = 'Test::Parameter';
188+
const synthStack = synthesizedStack(stack => {
189+
// Added
190+
new TestParameter(stack, 'NewParameter', { type: 'AWS::S3::Bucket' });
191+
// Expected
192+
new TestParameter(stack, 'TestParameterA', { type: parameterType });
193+
// Expected, but has different properties - will break
194+
new TestParameter(stack, 'TestParameterB', { type: parameterType, default: { Foo: 'Bar' } });
195+
});
196+
const expected = {
197+
Parameters: {
198+
TestParameterA: { Type: 'Test::Parameter' },
199+
TestParameterB: { Type: 'Test::Parameter', Default: { Foo: 'Baz' } }
200+
}
201+
};
202+
expect(synthStack).to(matchTemplate(expected, MatchStyle.SUPERSET));
203+
});
149204

150205
// countResources
151206

@@ -222,3 +277,13 @@ class TestResource extends cdk.CfnResource {
222277
super(scope, id, props);
223278
}
224279
}
280+
281+
interface TestParameterProps extends cdk.CfnParameterProps {
282+
type: string;
283+
}
284+
285+
class TestParameter extends cdk.CfnParameter {
286+
constructor(scope: cdk.Construct, id: string, props: TestParameterProps) {
287+
super(scope, id, props);
288+
}
289+
}

packages/@aws-cdk/aws-autoscaling/test/test.scheduled-action.ts

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ export = {
9696
},
9797
}
9898
},
99+
Parameters: {
100+
SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter: {
101+
Type: "AWS::SSM::Parameter::Value<String>",
102+
Default: "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
103+
}
104+
}
99105
}, MatchStyle.SUPERSET);
100106

101107
test.done();

0 commit comments

Comments
 (0)