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): queries and assertions against the Outputs and Mappings sections #15892

Merged
merged 15 commits into from
Aug 24, 2021

Conversation

nija-at
Copy link
Contributor

@nija-at nija-at commented Aug 4, 2021

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

@nija-at nija-at requested review from njlynch and a team August 4, 2021 15:46
@nija-at nija-at self-assigned this Aug 4, 2021
@gitpod-io
Copy link

gitpod-io bot commented Aug 4, 2021

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 4, 2021
@nija-at nija-at changed the title feat(assertions): queries and assertions against the Outputs and Templates sections feat(assertions): queries and assertions against the Outputs and Mappings sections Aug 4, 2021
@nija-at nija-at changed the title feat(assertions): queries and assertions against the Outputs and Mappings sections feat(assertions): queries and assertions against the Outputs and Mappings sections Aug 4, 2021
Copy link
Contributor

@njlynch njlynch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking, but consider some of the DRY ideas in my first comment. The rest is nitpicking.

import { StackInspector } from '../vendored/assert';
import { formatFailure, matchSection } from './section';

export function findMappings(inspector: StackInspector, props: any = {}): { [key: string]: any }[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider DRYing up mappings/outputs/resources, or am I over-optimizing too soon? It seems like they could all be written as one method with two extra inputs. I just see a lot of copy/paste between all three files.

function findSection(inspector: StackInspector, props: any = {}, sectionFn: (x: StackInspector) => any): MatchSuccess | MatchFailure {
  const matcher = Matcher.isMatcher(props) ? props : Match.objectLike(props);
  return matchSection(sectionFn(inspector), matcher);
}

function hasSection(inspector: StackInspector, props: any = {}, sectionFn: (x: StackInspector) => any, sectionName: string): string | void {
  const result = findSection(inspector, props, sectionFn);
  if (result.match) {
    return;
  }

  if (result.closestResult === undefined) {
    return `No ${sectionName} found in the template`;
  }

  return [
    `Template has ${result.analyzedCount} ${sectionName}, but none match as expected.`,
    formatFailure(result.closestResult),
  ].join('\n');
}

export function findMappings(inspector: StackInspector, props: any = {}): { [key: string]: any }[] {
  const result = findSection(inspector, props, (x) => x.value.Mappings ?? {});
  return result.match ? result.matches : [];
}

export function hasMapping(inspector: StackInspector, props: any): string | void {
  return hasSection(inspector, props, (x) => x.value.Mappings ?? {}, 'mappings');
}

export function findOutputs(inspector: StackInspector, props: any = {}): { [key: string]: any }[] {
  const result = findSection(inspector, props, (x) => x.value.Outputs ?? {});
  return result.match ? result.matches : [];
}

export function hasOutput(inspector: StackInspector, props: any): string | void {
  return hasSection(inspector, props, (x) => x.value.Outputs ?? {}, 'outputs');
}

(Now that I've seen section.ts: Maybe it's just that some/all of this could be moved into matchSection? Certainly the const matcher = Matcher.isMatcher(props) ? props : Match.objectLike(props); line could be moved, right?)

Copy link
Contributor Author

@nija-at nija-at Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have considered this and played around with how much control these methods should retain. It was the usual design choices of 'separation of concerns' and 'layering'.

I decided to not expose StackInspector into the section methods (lower layer) and let the individual methods keep control over the values returned / exception messages thrown.

Certainly the const matcher = Matcher.isMatcher(props) ? props : Match.objectLike(props); line could be moved, right?

You are correct about this. Moved this to the layer below.

packages/@aws-cdk/assertions/lib/private/section.ts Outdated Show resolved Hide resolved
@njlynch njlynch added the pr/do-not-merge This PR should not be merged at this time. label Aug 5, 2021
@nija-at nija-at removed the pr/do-not-merge This PR should not be merged at this time. label Aug 5, 2021
@mergify
Copy link
Contributor

mergify bot commented Aug 5, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
Copy link
Contributor

mergify bot commented Aug 24, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 90f95e1 into master Aug 24, 2021
@mergify mergify bot deleted the nija-at/assertions-outputmapping branch August 24, 2021 16:08
@mergify
Copy link
Contributor

mergify bot commented Aug 24, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 4870506
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

hollanddd pushed a commit to hollanddd/aws-cdk that referenced this pull request Aug 26, 2021
…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*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Sep 6, 2021
…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*
david-doyle-as24 pushed a commit to david-doyle-as24/aws-cdk that referenced this pull request Sep 7, 2021
…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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants