aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(assert): matchTemplate with superset fails when additional parameters are present

Open lintal opened this issue 5 years ago • 3 comments

I have a stack which I am importing the majority of its content from a JSON template. I subsequently add a nestedStack to this template.

I then have a test where I want to ensure I have loaded the content from the JSON template, however this now fails. Commenting out the inclusion of the nestedStack however allows the test to pass.

I have tried both the MatchStyle.EXACT and MatchStyle.SUPERSET styles using the matchTemplate method, and both return the same results. Similarly using the beASupersetOfTemplate method also results in error.

Reproduction Steps

./lib/MyStack.ts

import { CfnInclude } from '@aws-cdk/cloudformation-include';
import { Construct, Stack, StackProps } from '@aws-cdk/core';
import MyNestedStack from './MyNestedStack';

export default class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    new CfnInclude(this, 'template', {
      templateFile: 'template.json',
    });

    new MyNestedStack(this, 'NestedStack'); // Commenting out this line allows test to pass.
  }
}

./test/MyStack.test.ts

import {
  expect as expectCDK, matchTemplate, MatchStyle, beASupersetOfTemplate,
} from '@aws-cdk/assert';
import * as cdk from '@aws-cdk/core';
import MyStack from '../lib/MyStack';
import sourceTemplate from '../template.json';

const stack = new MyStack(new cdk.App(), 'MyStack');

test('Stack contains resources specified in our JSON template', () => {
  expectCDK(stack).to(matchTemplate(sourceTemplate, MatchStyle.SUPERSET));

  // The following also fail:
  // expectCDK(stack).to(matchTemplate(sourceTemplate, MatchStyle.EXACT));
  // expectCDK(stack).to(beASupersetOfTemplate(sourceTemplate));
});

./lib/MyNestedStack.ts -> A suitable cdk.NestedStack class. ./template.json -> A suitable "vanilla" CloudFormation template.

What did you expect to happen?

I would expect the test to pass, probably when using MatchStyle.SUPERSET or the beASupersetOfTemplate method only.

What actually happened?

The test failed with the following output:

npx jest test/MyStack.test.ts
Parameters
[+] Parameter AssetParameters37f4297963a7660b43c52afd0d3fdbefe142b42cc382863f120d73096290c3b9S3BucketD3FE1973: {"Type":"String","Description":"S3 bucket for asset \"37f4297963a7660b43c52afd0d3fdbefe142b42cc382863f120d73096290c3b9\""}
[+] Parameter AssetParameters37f4297963a7660b43c52afd0d3fdbefe142b42cc382863f120d73096290c3b9S3VersionKey6A0D4C30: {"Type":"String","Description":"S3 key for asset version \"37f4297963a7660b43c52afd0d3fdbefe142b42cc382863f120d73096290c3b9\""}
[+] Parameter AssetParameters37f4297963a7660b43c52afd0d3fdbefe142b42cc382863f120d73096290c3b9ArtifactHashCB76FB31: {"Type":"String","Description":"Artifact hash for asset \"37f4297963a7660b43c52afd0d3fdbefe142b42cc382863f120d73096290c3b9\""}

Resources
[+] AWS::CloudFormation::Stack RepublisherStackNestedStackRepublisherStackNestedStackResourceB9CE3637 

 FAIL  test/MyStack.test.ts (14.669 s)
  ✕ Stack contains resources specified in our JSON template (229 ms)

  ● Stack contains resources specified in our JSON template

    Template comparison produced unacceptable match

      17 | 
      18 |   // expectCDK(stack).to(beASupersetOfTemplate(stackTemplate));
    > 19 |   expectCDK(stack).to(matchTemplate(stackTemplate, MatchStyle.SUPERSET));
         |                    ^
      20 | });
      21 | 

      at StackMatchesTemplateAssertion.assertOrThrow (node_modules/@aws-cdk/assert/lib/assertions/match-template.ts:38:13)
      at StackInspector._to (node_modules/@aws-cdk/assert/lib/inspector.ts:25:15)
      at StackInspector.to (node_modules/@aws-cdk/assert/lib/inspector.ts:15:14)
      at Object.<anonymous> (test/ManagementApiStack.test.ts:19:20)

Environment

  • CDK CLI Version: 1.116.0 (build d04661d)
  • Framework Version: 1.117.0 (build 0047c98)
  • Node.js Version: v16.1.0
  • OS: MacOS Catalina v10.15.7
  • Language (Version): TypeScript v4.3.5

Other

N/A


This is :bug: Bug Report

lintal avatar Aug 09 '21 15:08 lintal

The issue is not with nested stack or additional resources.

The bug is coming because the NestedStack construct introduces new Parameters into the CloudFormation template, that the SUPERSET match style does not seem to honour.

nija-at avatar Aug 12 '21 10:08 nija-at

I've posted a fix here - https://github.com/aws/aws-cdk/pull/16015

nija-at avatar Aug 12 '21 10:08 nija-at

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Aug 12 '21 11:08 github-actions[bot]