Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assertions): hasResourceProperties is incompatible with Match.not and Match.absent #16678

Merged
merged 12 commits into from
Oct 6, 2021
14 changes: 12 additions & 2 deletions packages/@aws-cdk/assertions/lib/private/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { formatFailure, matchSection } from './section';
// Partial type for CloudFormation Resource
type Resource = {
Type: string;
Properties?: {};
}

export function findResources(inspector: StackInspector, type: string, props: any = {}): { [key: string]: { [key: string]: any } } {
Expand All @@ -17,9 +18,9 @@ export function findResources(inspector: StackInspector, type: string, props: an
return result.matches;
}

export function hasResource(inspector: StackInspector, type: string, props: any): string | void {
export function hasResource(inspector: StackInspector, type: string, props: any, addProperties?: boolean): string | void {
const section: { [key: string]: Resource } = inspector.value.Resources;
const result = matchSection(filterType(section, type), props);
kaizencc marked this conversation as resolved.
Show resolved Hide resolved
const result = matchSection(addProperties ? addEmptyProperties(filterType(section, type)) : filterType(section, type), props);

if (result.match) {
return;
Expand All @@ -35,6 +36,15 @@ export function hasResource(inspector: StackInspector, type: string, props: any)
].join('\n');
}

function addEmptyProperties(sections: { [key: string]: Resource}): { [key: string]: Resource } {
Object.keys(sections).map((key) => {
if (!sections[key].Properties) {
sections[key].Properties = {};
}
});
return sections;
}

function filterType(section: { [key: string]: Resource }, type: string): { [key: string]: Resource } {
return Object.entries(section ?? {})
.filter(([_, v]) => v.Type === type)
Expand Down
11 changes: 9 additions & 2 deletions packages/@aws-cdk/assertions/lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ export class Template {
* @param props the 'Properties' section of the resource as should be expected in the template.
*/
public hasResourceProperties(type: string, props: any): void {
this.hasResource(type, Match.objectLike({
// const addProperties = !(props instanceof AbsentMatch);
// Add the above line in place of below when this is merged: https://github.com/aws/aws-cdk/pull/16653
const addProperties = true;

const matchError = hasResource(this.inspector, type, Match.objectLike({
Properties: Matcher.isMatcher(props) ? props : Match.objectLike(props),
}));
}), addProperties);
if (matchError) {
throw new Error(matchError);
}
}

/**
Expand Down
58 changes: 58 additions & 0 deletions packages/@aws-cdk/assertions/test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,64 @@ describe('Template', () => {
});
});

describe('hasResourceProperties', () => {
test('exact match', () => {
const stack = new Stack();
new CfnResource(stack, 'Foo', {
type: 'Foo::Bar',
properties: { baz: 'qux' },
});

const inspect = Template.fromStack(stack);
inspect.hasResourceProperties('Foo::Bar', { baz: 'qux' });

expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' }))
.toThrow(/Expected waldo but received qux at \/Properties\/baz/);

expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' }))
.toThrow(/Missing key at \/Properties\/fred/);
});

test('absent with properties', () => {
const stack = new Stack();
new CfnResource(stack, 'Foo', {
type: 'Foo::Bar',
properties: { baz: 'qux' },
});

const inspect = Template.fromStack(stack);
inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty() });

expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: Match.absentProperty() }))
.toThrow(/Key should be absent at \/Properties\/baz/);
});

test('absent with no properties', () => {
const stack = new Stack();
new CfnResource(stack, 'Foo', {
type: 'Foo::Bar',
});

const inspect = Template.fromStack(stack);
inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty() });

expect(() => inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty(), baz: 'qux' }))
.toThrow(/Missing key at \/Properties\/baz/);
// Add the below line when this is merged: https://github.com/aws/aws-cdk/pull/16653
// expect(inspect.hasResourceProperties('Foo::Bar', Match.absent()));
kaizencc marked this conversation as resolved.
Show resolved Hide resolved
});

test('match with not', () => {
const stack = new Stack();
new CfnResource(stack, 'Foo', {
type: 'Foo::Bar',
});

const inspect = Template.fromStack(stack);
expect(inspect.hasResourceProperties('Foo::Bar', Match.not({ baz: 'qux' })));
});
});

describe('getResources', () => {
test('matching resource type', () => {
const stack = new Stack();
Expand Down