terraform test: rearrange the order of destroy operations #34293
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 is my second attempt at this, see #34285.
I think I overcomplicated this logic initially. Previously, we'd destroy the main state first, and then iterate through the remaining states from alternate modules in reverse order. We had documentation advising users not to create dependencies on the main state so this ordering should have been safe. I hadn't considered data sources requiring read operations during the pre-destroy refresh which leads to the referenced bug.
After this PR, we just destroy in reverse ordering with the documentation updated to simply say don't create references into later run blocks (eg. don't make a resource that depends on the creation of an earlier run block, and then reference the underlying module of that earlier run block later). I think this is actually acceptable given the way people are using the testing framework - it's much simpler this way and easier to explain. It also gives more control to the users - they can simply do a plan only run block against a module to tweak the destroy order.
A complicated extension to this would be to not only destroy during the destroy operation, but to essentially rollback states to previous run blocks until you find the first time they are created, at which point you could destroy them. I think this would be overkill for what we're trying to solve here but we could revisit that idea if this solution is inadequate.
I'm not sure if we can backport this change (given the change in the docs) but also this is quite advanced and I doubt many (if any) users will have created test configurations at the required complexity for this to be an issue yet. So maybe we could backport it? If not, we can fix this as part of the upcoming 1.7 release.
Fixes #34280
Target Release
1.7.0 or 1.6.5 - I'm not sure.
Draft CHANGELOG entry
UPGRADE NOTES
terraform test
: Simplify the ordering of destroy operations during test cleanup to simple reverserun
block order.