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

feat(assertions): support for conditions #18577

Merged
merged 6 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/assertions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ expect(result.Foo).toEqual({ Value: 'Fred', Description: 'FooFred' });
expect(result.Bar).toEqual({ Value: 'Fred', Description: 'BarFred' });
```

The APIs `hasMapping()` and `findMappings()` provide similar functionalities.
The APIs `hasMapping()`, `findMappings()`, `hasCondition()`, and `hasCondtions()` provide similar functionalities.

## Special Matchers

Expand Down
30 changes: 30 additions & 0 deletions packages/@aws-cdk/assertions/lib/private/conditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { filterLogicalId, formatFailure, matchSection } from './section';
import { Template } from './template';

export function findConditions(template: Template, logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
const section: { [key: string] : {} } = template.Conditions;
const result = matchSection(filterLogicalId(section, logicalId), props);

if (!result.match) {
return {};
}

return result.matches;
}

export function hasCondition(template: Template, logicalId: string, props: any): string | void {
const section: { [key: string] : {} } = template.Conditions;
const result = matchSection(filterLogicalId(section, logicalId), props);
if (result.match) {
return;
}

if (result.closestResult === undefined) {
return 'No conditions found in the template';
}

return [
`Template has ${result.analyzedCount} conditions, but none match as expected.`,
formatFailure(result.closestResult),
].join('\n');
}
7 changes: 5 additions & 2 deletions packages/@aws-cdk/assertions/lib/private/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export type Template = {
Resources: { [logicalId: string]: Resource },
Outputs: { [logicalId: string]: Output },
Mappings: { [logicalId: string]: Mapping },
Parameters: { [logicalId: string]: Parameter }
Parameters: { [logicalId: string]: Parameter },
Conditions: { [logicalId: string]: Condition },
}

export type Resource = {
Expand All @@ -19,4 +20,6 @@ export type Mapping = { [key: string]: any };
export type Parameter = {
Type: string;
[key: string]: any;
}
}

export type Condition = { [key: string]: any };
26 changes: 26 additions & 0 deletions packages/@aws-cdk/assertions/lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Stack, Stage } from '@aws-cdk/core';
import * as fs from 'fs-extra';
import { Match } from './match';
import { Matcher } from './matcher';
import { findConditions, hasCondition } from './private/conditions';
import { findMappings, hasMapping } from './private/mappings';
import { findOutputs, hasOutput } from './private/outputs';
import { findParameters, hasParameter } from './private/parameters';
Expand Down Expand Up @@ -183,6 +184,31 @@ export class Template {
return findMappings(this.template, logicalId, props);
}

/**
* Assert that a Condition with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
* To configure different behavour, use other matchers in the `Match` class.
* @param logicalId the name of the mapping. Provide `'*'` to match all conditions in the template.
* @param props the output as should be expected in the template.
*/
public hasCondition(logicalId: string, props: any): void {
const matchError = hasCondition(this.template, logicalId, props);
if (matchError) {
throw new Error(matchError);
}
}

/**
* Get the set of matching Conditions that match the given properties in the CloudFormation template.
* @param logicalId the name of the condition. Provide `'*'` to match all conditions in the template.
* @param props by default, matches all Conditions in the template.
* When a literal object is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour.
*/
public findConditions(logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findConditions(this.template, logicalId, props);
}

/**
* Assert that the CloudFormation template matches the given value
* @param expected the expected CloudFormation template as key-value pairs.
Expand Down
146 changes: 145 additions & 1 deletion packages/@aws-cdk/assertions/test/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, CfnMapping, CfnOutput, CfnParameter, CfnResource, NestedStack, Stack } from '@aws-cdk/core';
import { App, CfnCondition, CfnMapping, CfnOutput, CfnParameter, CfnResource, Fn, NestedStack, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Capture, Match, Template } from '../lib';

Expand Down Expand Up @@ -940,6 +940,150 @@ describe('Template', () => {
expect(Object.keys(result).length).toEqual(0);
});
});

describe('hasCondition', () => {
test('matching', () => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

const inspect = Template.fromStack(stack);
expect(() => inspect.hasCondition('*', { 'Fn::Equals': ['Bar', 'Baz'] })).not.toThrow();
});

test('not matching', (done) => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

new CfnCondition(stack, 'Qux', {
expression: Fn.conditionNot(Fn.conditionEquals('Quux', 'Quuz')),
});

const inspect = Template.fromStack(stack);
expectToThrow(
() => inspect.hasCondition('*', {
'Fn::Equals': ['Baz', 'Bar'],
}),
[
/2 conditions/,
/Missing key/,
],
done,
);
done();
});

test('matching specific outputName', () => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

const inspect = Template.fromStack(stack);
expect(() => inspect.hasCondition('Foo', { 'Fn::Equals': ['Bar', 'Baz'] })).not.toThrow();
});

test('not matching specific outputName', (done) => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Baz', 'Bar'),
});

const inspect = Template.fromStack(stack);
expectToThrow(
() => inspect.hasCondition('Foo', {
'Fn::Equals': ['Bar', 'Baz'],
}),
[
/1 conditions/,
/Expected Baz but received Bar/,
],
done,
);
done();
});
});

describe('findConditions', () => {
test('matching', () => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

new CfnCondition(stack, 'Qux', {
expression: Fn.conditionNot(Fn.conditionEquals('Quux', 'Quuz')),
});

const inspect = Template.fromStack(stack);
const firstCondition = inspect.findConditions('Foo');
expect(firstCondition).toEqual({
Foo: {
'Fn::Equals': [
'Bar',
'Baz',
],
},
});

const secondCondition = inspect.findConditions('Qux');
expect(secondCondition).toEqual({
Qux: {
'Fn::Not': [
{
'Fn::Equals': [
'Quux',
'Quuz',
],
},
],
},
});
});

test('not matching', () => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

const inspect = Template.fromStack(stack);
const result = inspect.findMappings('Bar');
expect(Object.keys(result).length).toEqual(0);
});

test('matching with specific outputName', () => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

const inspect = Template.fromStack(stack);
const result = inspect.findConditions('Foo', { 'Fn::Equals': ['Bar', 'Baz'] });
expect(result).toEqual({
Foo: {
'Fn::Equals': [
'Bar',
'Baz',
],
},
});
});

test('not matching specific output name', () => {
const stack = new Stack();
new CfnCondition(stack, 'Foo', {
expression: Fn.conditionEquals('Bar', 'Baz'),
});

const inspect = Template.fromStack(stack);
const result = inspect.findConditions('Foo', { 'Fn::Equals': ['Bar', 'Qux'] });
expect(Object.keys(result).length).toEqual(0);
});
});
});

function expectToThrow(fn: () => void, msgs: (RegExp | string)[], done: jest.DoneCallback): void {
Expand Down