terraform test: allow providers in test files to reference variables #34069
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR updates the testing framework so that providers defined in testing files can reference variables that are available within the test file.
We've defined a
ProviderConfig
struct in themoduletest
package which wraps ahcl.Body
and preprocesses the returned attributes before they're made available to the Terraform graph. We then update our provider definitions so the embeddedconfig
uses this as the body instead of the raw body. The Terraform graph executes as normally, just with the providers never returning values that need to be evaluated, because we handled that directly in thehcl.Body
.I've also moved the implementation of the
hcl.EvalContext
from within the Test command into a dedicated package. This means it can be shared by all the places in the test file that need to evaluate variables. For now, that's just when processing variables in run blocks, and when processing variables in test provider configurations.One alternative approach I looked at was modifying the Terraform graph so you could provide external variable references, or even providing externally configured providers. The problem I found with this was that we were making sweeping changes within the Terraform graph, and we don't want the actual execution of Terraform to be significantly different during real operations and during test runs - this leads to more chance that a test will pass when a normal run would fail.
A second approach, was to just pass all the variables into the graph alongside the external provider definitions into the graph, so the variables could be located naturally by the graph and the providers initialised. The problem here is that we then start letting tests pass when a variable isn't defined in the config, but only defined within the test file, which isn't a good look.
First part in addressing #34007
We'll follow up by allowing data source blocks to be defined in test files, and these can then be referenced from providers.
Target Release
1.7.0
Draft CHANGELOG entry
ENHANCEMENTS
terraform test
: Providers defined within test files can now reference variables in their configuration that are defined within the test file.