terraform test: Disallow version constraints within test files #35161
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 fixes a bug in which non-hashicorp providers defined within test files and referenced within alternate testing modules were wrongly being assumed as hashicorp providers and were failing to install.
We do this by no longer actually looking at test files for provider information. Since test files don't actually use providers directly and just hand them off to the main files, we don't need to consider the provider blocks in test files when calculating provider address and version constraints. This simplifies the provider constraint gathering and solves the underlying bug since the provider types don't need to be assumed within the test file at all now. It does mean that version constraints within provider blocks within test files will no longer be consulted. As such, I've updated the configuration parsing so that it errors if test files contain version constraints. (as an aside, I wish I'd done this when we first launched the test framework but oh well should have been braver)
Unfortunately, this is a breaking change so I won't backport this change. Instead, we should launch it in 1.9.0 with warnings in the changelog and upgrade guide. There is a fairly simple workaround in 1.8 currently so there is no urgency requiring a quick backport.
I did notice that it's been 4 years since we deprecated the version constraint within provider blocks at all, so maybe we could remove it entirely? I suspect this is a bad idea, as modules with embedded version constraints will just start completely failing to load and there would generally nothing a user could do to fix this as the module maybe downloaded remotely. Note, that the testing framework doesn't have this issue as it doesn't recursively load testing files from linked modules, so there will never be a case where the user can't fix this simply by updating their own configuration.
Fixes #35160
Target Release
1.9.0
Draft CHANGELOG entry
BUG FIXES
terraform test
: Fix bug in which non-Hashicorp providers required by testing modules and initialised within the test files were assigned incorrect registry addresses.UPGRADE NOTES
terraform test
: It is no longer valid to specify version constraints withinprovider
blocks within.tftest.hcl
files. Instead, version constraints must be supplied within the main configuration where the provider is in use.