Skip to content

Commit

Permalink
fix(assert): CfnParameter MatchStyle diff support (#3408)
Browse files Browse the repository at this point in the history
* `NO_REPLACES` now verifies that no parameters are updated
* `SUPERSET` now verifies that no parameters are added

Fixes #3399
  • Loading branch information
nmussy authored and Elad Ben-Israel committed Jul 24, 2019
1 parent 9565b9b commit 2747a76
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/@aws-cdk/assert/lib/assertions/match-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,22 @@ class StackMatchesTemplateAssertion extends Assertion<StackInspector> {
if (change.changeImpact === cfnDiff.ResourceImpact.MAY_REPLACE) { return false; }
if (change.changeImpact === cfnDiff.ResourceImpact.WILL_REPLACE) { return false; }
}

for (const key of Object.keys(diff.parameters.changes)) {
const change = diff.parameters.changes[key]!;
if (change.isUpdate) { return false; }
}
return true;
case MatchStyle.SUPERSET:
for (const key of Object.keys(diff.resources.changes)) {
const change = diff.resources.changes[key]!;
if (change.changeImpact !== cfnDiff.ResourceImpact.WILL_CREATE) { return false; }
}

for (const key of Object.keys(diff.parameters.changes)) {
const change = diff.parameters.changes[key]!;
if (change.isAddition) { return false; }
}
return true;
}
throw new Error(`Unsupported match style: ${this.matchStyle}`);
Expand Down
65 changes: 65 additions & 0 deletions packages/@aws-cdk/assert/test/test.assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ passingExample('sugar for matching stack to a template', () => {
}
});
});
passingExample('expect <synthStack> to match (no replaces) <template> with parameters', () => {
const parameterType = 'Test::Parameter';
const synthStack = synthesizedStack(stack => {
new TestParameter(stack, 'TestParameter', { type: parameterType });
});
const expected = {};
expect(synthStack).to(matchTemplate(expected, MatchStyle.NO_REPLACES));
});
passingExample('expect <synthStack> to be a superset of <template> with parameters', () => {
const parameterType = 'Test::Parameter';
const synthStack = synthesizedStack(stack => {
// Added
new TestResource(stack, 'NewResource', { type: 'AWS::S3::Bucket' });
// Expected
new TestParameter(stack, 'TestParameterA', {type: parameterType});
new TestParameter(stack, 'TestParameterB', {type: parameterType, default: { Foo: 'Bar' } });
});
const expected = {
Parameters: {
TestParameterA: { Type: 'Test::Parameter' },
TestParameterB: { Type: 'Test::Parameter', Default: { Foo: 'Bar' } }
}
};
expect(synthStack).to(matchTemplate(expected, MatchStyle.SUPERSET));
});

failingExample('expect <synthStack> at <some path> *not* to have <some type>', () => {
const resourceType = 'Test::Resource';
Expand Down Expand Up @@ -146,6 +171,36 @@ failingExample('expect <synthStack> to be a superset of <template>', () => {
};
expect(synthStack).to(matchTemplate(expected, MatchStyle.SUPERSET));
});
failingExample('expect <synthStack> to match (no replaces) <template> with parameters', () => {
const parameterType = 'Test::Parameter';
const synthStack = synthesizedStack(stack => {
new TestParameter(stack, 'TestParameter', { type: parameterType });
});
const expected = {
Parameters: {
TestParameter: { Type: 'AWS::S3::Bucket' }
}
};
expect(synthStack).to(matchTemplate(expected, MatchStyle.NO_REPLACES));
});
failingExample('expect <synthStack> to be a superset of <template> with parameters', () => {
const parameterType = 'Test::Parameter';
const synthStack = synthesizedStack(stack => {
// Added
new TestParameter(stack, 'NewParameter', { type: 'AWS::S3::Bucket' });
// Expected
new TestParameter(stack, 'TestParameterA', { type: parameterType });
// Expected, but has different properties - will break
new TestParameter(stack, 'TestParameterB', { type: parameterType, default: { Foo: 'Bar' } });
});
const expected = {
Parameters: {
TestParameterA: { Type: 'Test::Parameter' },
TestParameterB: { Type: 'Test::Parameter', Default: { Foo: 'Baz' } }
}
};
expect(synthStack).to(matchTemplate(expected, MatchStyle.SUPERSET));
});

// countResources

Expand Down Expand Up @@ -222,3 +277,13 @@ class TestResource extends cdk.CfnResource {
super(scope, id, props);
}
}

interface TestParameterProps extends cdk.CfnParameterProps {
type: string;
}

class TestParameter extends cdk.CfnParameter {
constructor(scope: cdk.Construct, id: string, props: TestParameterProps) {
super(scope, id, props);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export = {
},
}
},
Parameters: {
SsmParameterValueawsserviceamiamazonlinuxlatestamznamihvmx8664gp2C96584B6F00A464EAD1953AFF4B05118Parameter: {
Type: "AWS::SSM::Parameter::Value<String>",
Default: "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2"
}
}
}, MatchStyle.SUPERSET);

test.done();
Expand Down

0 comments on commit 2747a76

Please sign in to comment.