forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assertions): queries and assertions against the Outputs and Mapp…
…ings sections (aws#15892) Introduce APIs `hasOutput()`, `findOutputs()`, `hasMapping()` and `findMappings()` to assert the `Mappings` and `Outputs` section of the CloudFormation template. Also, refactored the implementation of `hasResource()` to increase re-usability of its implementation across these new APIs. Migrated the modules `aws-kinesisfirehose` and `aws-neptune` that use these new APIs. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
18 changed files
with
566 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { StackInspector } from '../vendored/assert'; | ||
import { formatFailure, matchSection } from './section'; | ||
|
||
export function findMappings(inspector: StackInspector, props: any = {}): { [key: string]: any }[] { | ||
const section: { [key: string] : {} } = inspector.value.Mappings; | ||
const result = matchSection(section, props); | ||
|
||
if (!result.match) { | ||
return []; | ||
} | ||
|
||
return result.matches; | ||
} | ||
|
||
export function hasMapping(inspector: StackInspector, props: any): string | void { | ||
const section: { [key: string]: {} } = inspector.value.Mappings; | ||
const result = matchSection(section, props); | ||
|
||
if (result.match) { | ||
return; | ||
} | ||
|
||
if (result.closestResult === undefined) { | ||
return 'No mappings found in the template'; | ||
} | ||
|
||
return [ | ||
`Template has ${result.analyzedCount} mappings, but none match as expected.`, | ||
formatFailure(result.closestResult), | ||
].join('\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { StackInspector } from '../vendored/assert'; | ||
import { formatFailure, matchSection } from './section'; | ||
|
||
export function findOutputs(inspector: StackInspector, props: any = {}): { [key: string]: any }[] { | ||
const section: { [key: string] : {} } = inspector.value.Outputs; | ||
const result = matchSection(section, props); | ||
|
||
if (!result.match) { | ||
return []; | ||
} | ||
|
||
return result.matches; | ||
} | ||
|
||
export function hasOutput(inspector: StackInspector, props: any): string | void { | ||
const section: { [key: string]: {} } = inspector.value.Outputs; | ||
const result = matchSection(section, props); | ||
|
||
if (result.match) { | ||
return; | ||
} | ||
|
||
if (result.closestResult === undefined) { | ||
return 'No outputs found in the template'; | ||
} | ||
|
||
return [ | ||
`Template has ${result.analyzedCount} outputs, but none match as expected.`, | ||
formatFailure(result.closestResult), | ||
].join('\n'); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { StackInspector } from '../vendored/assert'; | ||
import { formatFailure, matchSection } from './section'; | ||
|
||
// Partial type for CloudFormation Resource | ||
type Resource = { | ||
Type: string; | ||
} | ||
|
||
export function findResources(inspector: StackInspector, type: string, props: any = {}): { [key: string]: any }[] { | ||
const section: { [key: string] : Resource } = inspector.value.Resources; | ||
const result = matchSection(filterType(section, type), props); | ||
|
||
if (!result.match) { | ||
return []; | ||
} | ||
|
||
return result.matches; | ||
} | ||
|
||
export function hasResource(inspector: StackInspector, type: string, props: any): string | void { | ||
const section: { [key: string]: Resource } = inspector.value.Resources; | ||
const result = matchSection(filterType(section, type), props); | ||
|
||
if (result.match) { | ||
return; | ||
} | ||
|
||
if (result.closestResult === undefined) { | ||
return `No resource with type ${type} found`; | ||
} | ||
|
||
return [ | ||
`Template has ${result.analyzedCount} resources with type ${type}, but none match as expected.`, | ||
formatFailure(result.closestResult), | ||
].join('\n'); | ||
} | ||
|
||
function filterType(section: { [key: string]: Resource }, type: string): { [key: string]: Resource } { | ||
return Object.entries(section ?? {}) | ||
.filter(([_, v]) => v.Type === type) | ||
.reduce((agg, [k, v]) => { return { ...agg, [k]: v }; }, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Match } from '../match'; | ||
import { Matcher, MatchResult } from '../matcher'; | ||
|
||
export type MatchSuccess = { match: true, matches: any[] }; | ||
export type MatchFailure = { match: false, closestResult?: MatchResult, analyzedCount: number }; | ||
|
||
export function matchSection(section: any, props: any): MatchSuccess | MatchFailure { | ||
const matcher = Matcher.isMatcher(props) ? props : Match.objectLike(props); | ||
let closestResult: MatchResult | undefined = undefined; | ||
let matching: any[] = []; | ||
let count = 0; | ||
|
||
eachEntryInSection( | ||
section, | ||
|
||
(entry) => { | ||
const result = matcher.test(entry); | ||
if (!result.hasFailed()) { | ||
matching.push(entry); | ||
} else { | ||
count++; | ||
if (closestResult === undefined || closestResult.failCount > result.failCount) { | ||
closestResult = result; | ||
} | ||
} | ||
}, | ||
); | ||
|
||
if (matching.length > 0) { | ||
return { match: true, matches: matching }; | ||
} else { | ||
return { match: false, closestResult, analyzedCount: count }; | ||
} | ||
} | ||
|
||
function eachEntryInSection( | ||
section: any, | ||
cb: (entry: {[key: string]: any}) => void): void { | ||
|
||
for (const logicalId of Object.keys(section ?? {})) { | ||
const resource: { [key: string]: any } = section[logicalId]; | ||
cb(resource); | ||
} | ||
} | ||
|
||
export function formatFailure(closestResult: MatchResult): string { | ||
return [ | ||
'The closest result is:', | ||
leftPad(JSON.stringify(closestResult.target, undefined, 2)), | ||
'with the following mismatches:', | ||
...closestResult.toHumanStrings().map(s => `\t${s}`), | ||
].join('\n'); | ||
} | ||
|
||
function leftPad(x: string, indent: number = 2): string { | ||
const pad = ' '.repeat(indent); | ||
return pad + x.split('\n').join(`\n${pad}`); | ||
} |
Oops, something went wrong.