Skip to content

Commit

Permalink
Merge branch 'master' into codedeploy-assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jan 21, 2022
2 parents d276edc + c626513 commit 9c477d5
Show file tree
Hide file tree
Showing 46 changed files with 1,173 additions and 291 deletions.
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
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/authorizers/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Duration, Lazy, Names, Stack } from '@aws-cdk/core';
import { Arn, ArnFormat, Duration, Lazy, Names, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnAuthorizer } from '../apigateway.generated';
import { Authorizer, IAuthorizer } from '../authorizer';
Expand Down Expand Up @@ -254,5 +254,6 @@ export class RequestAuthorizer extends LambdaAuthorizer {
* constructs the authorizerURIArn.
*/
function lambdaAuthorizerArn(handler: lambda.IFunction) {
return `arn:${Stack.of(handler).partition}:apigateway:${Stack.of(handler).region}:lambda:path/2015-03-31/functions/${handler.functionArn}/invocations`;
const { region, partition } = Arn.split( handler.functionArn, ArnFormat.COLON_RESOURCE_NAME);
return `arn:${partition}:apigateway:${region}:lambda:path/2015-03-31/functions/${handler.functionArn}/invocations`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,37 @@
[
"arn:",
{
"Ref": "AWS::Partition"
"Fn::Select": [
1,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"MyAuthorizerFunction70F1223E",
"Arn"
]
}
]
}
]
},
":apigateway:",
{
"Ref": "AWS::Region"
"Fn::Select": [
3,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"MyAuthorizerFunction70F1223E",
"Arn"
]
}
]
}
]
},
":lambda:path/2015-03-31/functions/",
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,37 @@
[
"arn:",
{
"Ref": "AWS::Partition"
"Fn::Select": [
1,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"MyAuthorizerFunction70F1223E",
"Arn"
]
}
]
}
]
},
":apigateway:",
{
"Ref": "AWS::Region"
"Fn::Select": [
3,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"MyAuthorizerFunction70F1223E",
"Arn"
]
}
]
}
]
},
":lambda:path/2015-03-31/functions/",
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,37 @@
[
"arn:",
{
"Ref": "AWS::Partition"
"Fn::Select": [
1,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"MyAuthorizerFunction70F1223E",
"Arn"
]
}
]
}
]
},
":apigateway:",
{
"Ref": "AWS::Region"
"Fn::Select": [
3,
{
"Fn::Split": [
":",
{
"Fn::GetAtt": [
"MyAuthorizerFunction70F1223E",
"Arn"
]
}
]
}
]
},
":lambda:path/2015-03-31/functions/",
{
Expand Down
Loading

0 comments on commit 9c477d5

Please sign in to comment.