Open OOTS opened 8 months ago
Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the đź‘Ť reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!
In my case I'm using an external submodule.
I need the ability to mock the submodule instead of just override because I don't want the submodule to be executed, just values returned.
This is because I want to implement a unit test (plan) on the main module and I don't want to have to "apply" a large external module just on a unit test because of errors like "xxx will be known only after apply" see below
I'm facing a similar case where I have my custom module that uses an external third-party module and I want to do just unit tests to validate the logic including known values generated by the submodule. It would be ideal just to refer to the submodule resource the same way as shown in the plan without mocking anything because I want to check the actual value that is generated.
Terraform Version
Use Cases
I want to test a terraform module ("main module") that creates/instantiates submodules. In my scenario, the submodule parameters (values passed from the main module as the input variables of the submodule) for the submodules are determined by a more or less complex process.
A dummy example can be found below:
Now, imagine that instead of simply hardcoding the parameters of the submodule in the main module's code and only instantiating the submodule once, I'm actually creating many instances of the submodule using
for_each
and the parameters of the submodule are determined using a more complex process (e.g. derived by doing some manipulations on the main module's variables and/or derived from some data fetched using adata
block).I'm looking for a way to unit-test the main module as a whole, especially verifying that the logic computing the submodule parameters is working as intended.
Attempted Solutions
With the recently added
terraform test
command, I can write unit tests for the submodule, to make sure the submodule works correctly internally. Using tests in the main module, I can writeassert
blocks on the submodule outputs (which might give me some hints about the input variables that were passed to the submodule instance), but in my real-world use-case, the submodule doesn't even have outputs.I tried writing
assert
blocks on the submodule parameters in the tests of the main module, but the parameters passed as variables to the submodule do not show up as attributes of the resultingmodule
object, only the submoduleoutput
s do. Example:Similarly, I tried accessing the resources created by the submodule from the main module, but again, that doesn't appear to be possible/supported. Example:
I tried if
would help somehow, but that just gives me an error that
override_resource
is for overriding resources, notmodule
s.Next, I could simply add
output
s to my submodule that mirror the submodule input variable values 1-to-1, but that seems like an ugly hack to me.Finally, I could try write something like
or similar in the tests of my main module. Here, I'd be (ab-)using the fact that one can use resources created in previous
run
blocks as variable values for laterrun
blocks as a way of accessing the resources created within the submodule. (Thetest/comparison-module
would have to simply output it's input value/object unchanged to make it visible outside.) However, this doesn't actually enable me to test the logic (in my main module) that determines the input parameters for the submodule.I'm attaching a non-working test file for the main module as a basis for playing around:
Proposal
Ideally, I'd like to be able to mock the submodule for my tests (in a similar way that I mock providers using
mock_provider
or override data usingoverride_data
).For example, I could imagine something like this:
The above would work fine as long as there's only one instance of a given module. However, in my case (since I'm creating many instances of the submodule using
for_each
), maybe a slightly more advanced version would be required. E.g.:However, in my case, where the submodule doesn't even have outputs, that wouldn't help me much.
Or, to give the programmer/tester even more flexibility:
Then, I could add module in
tests/replacement-submodule
that does whatever I want (e.g. simply mirror the input variables asoutput
s).If having a mocked version of a module is not possible, I'd like to be able to make assertions (in the main module) about the resources created in the submodule.
References
No response