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

Unable to write unit tests for StackSetStacks #372

Open
nhaynes opened this issue Mar 2, 2024 · 1 comment
Open

Unable to write unit tests for StackSetStacks #372

nhaynes opened this issue Mar 2, 2024 · 1 comment

Comments

@nhaynes
Copy link

nhaynes commented Mar 2, 2024

I've been trying to write a CDK unit test for my StackSetStack but I keep running into issues with this no matter how I structure it.

Targeting the Parent Stack

With this setup, the test runs but the template that is synthesized is not of any interest to me as it only contains the inner workings (Lambda, CustomResource, Assets, etc.) that makes cdk-stacksets function but it doesn't contain any of the details of my MyStackSetStack that I would like to test.

it('matches the snapshot', () => {
  const app = new App();
  const stack = new Stack(app, 'TestStack');
  const stackSetStack = new MyStackSetStack(stack, 'MyStackSetStack', {
    assetBuckets: [Bucket.fromBucketName(stack, 'Assets', 'stackset-assets-us-east-2')],
    assetBucketPrefix: 'stackset-assets',
  });

  const template = Template.fromStack(stack);

  expect(template).toMatchSnapshot();
});

Targeting the StackSetStack

Since StackSetStack extends Stack, I figured I could just switch Template.fromStack to target the stackSetStack directly and then I would get the template I want to run my tests against.

But with this setup, I always get this error:

Unable to find artifact with id "TestStackMyStackSetStackXXXXXXXX"

  const app = new App();
  const stack = new Stack(app, 'TestStack');
  const stackSetStack = new MyStackSetStack(stack, 'MyStackSetStack', {
    assetBuckets: [Bucket.fromBucketName(stack, 'Assets', 'stackset-assets-us-east-2')],
    assetBucketPrefix: 'stackset-assets',
  });

  const template = Template.fromStack(stackSetStack);

  expect(template).toMatchSnapshot();

Removing Parent Stack All Together

Next I tried to just remove the Parent stack entirely and just create the MyStackSetStack on the app itself.

But this gave me this error because of the asset bucket:

App at '' should be created in the scope of a Stack, but no Stack found

image

  const app = new App();
  const stackSetStack = new MyStackSetStack(app, 'MyStackSetStack', {
    assetBuckets: [Bucket.fromBucketName(app, 'Assets', 'stackset-assets-us-east-2')],
    assetBucketPrefix: 'stackset-assets',
  });

  const template = Template.fromStack(stackSetStack);

  expect(template).toMatchSnapshot();

Removing Asset Bucket

So my last attempt was to do it without the asset bucket but this resulted in this error:

image

  const app = new App();
  const stackSetStack = new MyStackSetStack(app, 'MyStackSetStack', {
    assetBuckets: [Bucket.fromBucketName(app, 'Assets', 'stackset-assets-us-east-2')],
    assetBucketPrefix: 'stackset-assets',
  });

  const template = Template.fromStack(stackSetStack);

  expect(template).toMatchSnapshot();

Again, since the StackSetStack is a Stack, it would be nice if we could test them just like we do any other AWS CDK stack.

@maherio
Copy link

maherio commented Oct 23, 2024

Fwiw, as a workaround I move the entirety of my template code into a separate construct, then test that as if it was put into a normal Stack.

class MyTemplateStack(StackSetStack):
    def __init__(...):
        super().__init__(...)

        MyCustomConstruct(...)

class MyCustomConstruct(Construct):
    def __init__(...):
        # here is where i build all my actual resources

def test_custom_construct():
    regular_stack = Stack()
    MyCustomConstruct(scope=regular_stack, ...)
    template = Template.from_stack(regular_stack)

    # make assertions on template like normal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants