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

(assertions): Can't make templates from stacks with cross-stack references #23814

Open
ymhiroki opened this issue Jan 24, 2023 · 1 comment
Open
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@ymhiroki
Copy link
Contributor

Describe the bug

I try to test a stack that receives resources from other stacks as props.

I received an error when generating a template from a stack and couldn't make its snapshots. It seems to be derived from cross-stack references.

Expected Behavior

Snapshots of a stack with cross-stack references are generated successfully.

Current Behavior

% npm run test -- -u

> cross-stack-snapshot-test@0.1.0 test
> jest -u

 FAIL  test/cross-stack-snapshot-test.test.ts (9.66 s)
  ✕ Cross stack snapshot test (75 ms)

  ● Cross stack snapshot test

    Unable to calculate a unique id for an empty set of components

      12 |   const stack = new SampleInstanceStack(app, 'MyTestStack', { vpc });
      13 |
    > 14 |   const template = Template.fromStack(stack);
         |                             ^
      15 |   expect(template).toMatchSnapshot();
      16 | });
      17 |

      at Object.makeUniqueId (node_modules/aws-cdk-lib/core/lib/private/uniqueid.js:1:400)
      at Function.uniqueId (node_modules/aws-cdk-lib/core/lib/names.js:1:626)
      at SampleInstanceStack._addAssemblyDependency (node_modules/aws-cdk-lib/core/lib/stack.js:1:9885)
      at operateOnDependency (node_modules/aws-cdk-lib/core/lib/deps.js:1:1657)
      at Object.addDependency (node_modules/aws-cdk-lib/core/lib/deps.js:1:341)
      at SampleInstanceStack.addDependency (node_modules/aws-cdk-lib/core/lib/stack.js:1:6829)
      at resolveValue (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:3243)
      at Object.resolveReferences (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:890)
      at Object.prepareApp (node_modules/aws-cdk-lib/core/lib/private/prepare-app.js:1:585)
      at Object.synthesize (node_modules/aws-cdk-lib/core/lib/private/synthesis.js:1:626)
      at App.synth (node_modules/aws-cdk-lib/core/lib/stage.js:1:1942)
      at toTemplate (node_modules/aws-cdk-lib/assertions/lib/template.js:2:305)
      at Function.fromStack (node_modules/aws-cdk-lib/assertions/lib/template.js:1:1157)
      at Object.<anonymous> (test/cross-stack-snapshot-test.test.ts:14:29)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        9.886 s
Ran all test suites.

Reproduction Steps

Add a test: test/sample-stack.test.ts.

import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { SampleInstanceStack } from '../lib/sample-stack';

test('Cross stack snapshot test', () => {
  const app = new cdk.App();

  const mockStack = new cdk.Stack(app);
  const vpc = new ec2.Vpc(mockStack, 'Vpc');

  const stack = new SampleInstanceStack(app, 'MyTestStack', { vpc });

  const template = Template.fromStack(stack);
  expect(template).toMatchSnapshot();
});

Define a stack: lib/cross-stack-snapshot-test.test.ts.

import * as cdk from "aws-cdk-lib";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";

interface SampleInstanceStackProps extends cdk.StackProps {
    readonly vpc: ec2.Vpc;
}

export class SampleInstanceStack extends cdk.Stack {
    constructor(scope: Construct, id: string, props: SampleInstanceStackProps) {
        super(scope, id, props);

        const { vpc } = props;
        new ec2.Instance(this, 'Instance', {
            vpc,
            instanceType: new ec2.InstanceType('t3.small'),
            machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }),
        });
    }
}

Then, run tests: npm run test -- -u.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.61.1 (build d319d9c)

Framework Version

No response

Node.js Version

v16.16.0

OS

MacOS Monterey version 12.6.2 (21G320)

Language

Typescript

Language Version

No response

Other information

Related issues:

@ymhiroki ymhiroki added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 24, 2023
@github-actions github-actions bot added the @aws-cdk/assertions Related to the @aws-cdk/assertv2 package label Jan 24, 2023
@ymhiroki ymhiroki changed the title (assertions): Can't make snapshots with cross-stack references (assertions): Can't make templates from Stacks with cross-stack references Jan 24, 2023
@ymhiroki ymhiroki changed the title (assertions): Can't make templates from Stacks with cross-stack references (assertions): Can't make templates from stacks with cross-stack references Jan 24, 2023
@pahud pahud added p2 feature-request A feature should be added or improved. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 16, 2023
@tmokmss
Copy link
Contributor

tmokmss commented Jun 2, 2023

I found that the problem in the reproduction code is that the mockStack does not have any ID set.

So the below diff fixes the problem:

- const mockStack = new cdk.Stack(app);
+ const mockStack = new cdk.Stack(app, 'MockStack');

@khushail khushail added the effort/medium Medium work item – several days of effort label Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

4 participants