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 use stack transformation from unit test #4669

Open
clstokes opened this issue May 19, 2020 · 4 comments
Open

Unable to use stack transformation from unit test #4669

clstokes opened this issue May 19, 2020 · 4 comments
Labels
area/testing kind/enhancement Improvements or new features

Comments

@clstokes
Copy link
Contributor

If I call pulumi.runtime.registerStackTransformation from my Pulumi application and run a unit test against my application, the tests fail to run with the error The root stack resource was referenced before it was initialized. The same Pulumi application runs successfully without error with pulumi preview.

Steps to reproduce

  1. git clone https://gist.github.com/20db9640bc9f3c8607f6d67d8390fb45 gh-test && cd gh-test
  2. npm i
  3. pulumi stack init gh-test
  4. npm test
    • Observe error.
  5. pulumi preview
    • Observe successful preview.
@lukehoban
Copy link
Member

This happens because we do not currently create a root Stack resource automatically in test mode. It is likely that we should, and that setMocks should initialize a root Stack automatically . Coincidentally, we are looking at making that change for Python for other reasons in #4670, but it is likely we should make that same change in Node.js which we fix this issue.

In the meantime, a workaround is to use pulumi.runtime.runInPulumiStack to construct a Stack resource.

pulumi.runtime.setMocks({
    call: x => {
        return {};
    },
    newResource: x => {
        return {id: "", state: {}};
    }
})
pulumi.runtime.runInPulumiStack(async () => {
    pulumi.runtime.registerStackTransformation(args => {
        console.log(`transofming: ${args.name}`)
        return {
            opts: args.opts,
            props: args.props,
        }
    })
    
    // Create an AWS resource (S3 Bucket)
    const bucket = new aws.s3.Bucket("my-bucket");
    
    // Export the name of the bucket
    return {
        bucketName: bucket.id
    };
})

@amichel
Copy link

amichel commented Mar 10, 2021

This problem scope is bigger than just unit tests. I have common transformations that I want to be able to reuse in all projects as npm package. It also causes problem the other way around, when I register stack transformation, it doesn't apply on resources created by modules from shared npm packages.

@clstokes
Copy link
Contributor Author

clstokes commented Sep 3, 2021

Related - #6214

@infin8x infin8x added area/testing kind/bug Some behavior is incorrect or out of spec labels Sep 17, 2021
joeduffy added a commit that referenced this issue Dec 31, 2021
This change initializes a root stack resource when run in
mocks mode, fixing #4669. This lets you use, for instance,
stack transformations during unit testing. This is effectively the
equivalent to what we did for Python in
https://github.com/pulumi/pulumi/pull/4670/files, just for Node.js.
@arwilczek90
Copy link
Contributor

I agree with @amichel I get the same traceback when running pulumi up on a project with a stack transformation.

@mikhailshilkov mikhailshilkov added kind/enhancement Improvements or new features and removed kind/bug Some behavior is incorrect or out of spec labels Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing kind/enhancement Improvements or new features
Projects
None yet
Development

No branches or pull requests

6 participants