pulumi: Unable to unit test stacks which references Pulumi.Config
Unit tests written for stacks which use Pulumi.Config to read configuration values fail to execute as the config values are not available for the tests. Sample stack & its unit tests are available at : https://gist.github.com/svswaminathan/591254043500e1a3a041272a2ab17065
Running dotnet test throws the following error:
Pulumi.RunException : Running program 'C:\Samples\pulumi\pc-azure-csharp\bin\Debug\netcoreapp3.1\testhost.dll' failed with an unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Pulumi.Config+ConfigMissingException: Missing Required configuration variable 'project:resourceGroupName' please set a value using the command pulumi config set project:resourceGroupName <value> at Pulumi.Config.Require(String key) at MyStack..ctor() in C:\Samples\pulumi\pc-azure-csharp\MyStack.cs:line 11
Since Pulumi.Config is a sealed class, there is no possibility to mock it as well.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 36
- Comments: 16 (3 by maintainers)
In go there seems to be a way to do this without needing to muck with environment variables.
Though better yet is to improve the
WithMocks()function here like this:Hey whiteninja76, not sure if you’re still having an issue but I was able to Jest spyOn the config:
If you’re not using Jest there may be something similar you could do.
Was able to work around this in typescript:
and in tests:
It may not be ideal if you’re using
ConfigOptions@svswaminathan Currently, we don’t have API to mock config, and the actual configuration is taken from an environment variable
PULUMI_CONFIG. So, if you have code likeYou would need to pass a JSON value of the shape
(example for macOS)
You should keep “project” because that’s the default namespace.
I understand this is less than ideal, so I’m open to suggestions for a first-class config API while unit testing.
My TypeScript workaround use a function for getting config and
pulumi.runtime.setConfig.Accessing the config lazily via the function ensures that the
pulumi.runtime.setConfigruns and sets config before the.requiremethods are called on the config.In case anyone finds this interesting, this is what I ended up doing:
Just in case this is still relevant, or for people after me. I was having this exact same issue. The problem was that all objects that are not the main one, also need to be strings, not object. If you’re doing:
you could need to stringify that object as well:
I was able to work around this one. A short Gist is pasted below. A working solution is condensed down to this code. Keep in mind that YamlDotNet is a requirement for this solution.