Set the initial state for a state files from a backend, allow the run that defines a backend to write state to the backend
This PR builds on https://github.com/hashicorp/terraform/pull/36541
Now that test files' run blocks can include backend blocks, this PR allows:
- Backend blocks to set the initial value for a given internal state/state key
- Writes to the state contained in the backend. This is only allowed for the apply operation that comes from the run block that defines the backend.
Target Release
1.12.x
CHANGELOG entry
- [ ] This change is user-facing and I added a changelog entry.
- [x] This change is not user-facing.
Will add a changelog in the final PR into main
Collapsed previous content of this message
Hmm, the failure in TestTest_UseOfBackends_happyPath on this PR is something I'm seeing inconsistently on my machine:
--- FAIL: TestTest_UseOfBackends_happyPath (0.20s)
--- FAIL: TestTest_UseOfBackends_happyPath/When_there_is_pre-existing_state,_the_state_is_updated_by_the_run_containing_the_backend_block (0.10s)
test_test.go:3664: state didn't match expected:
expected:
test_resource.a:
ID = 12345
provider = provider["registry.terraform.io/hashicorp/test"]
destroy_fail = false
value = value-from-run-that-controls-backend
Outputs:
supplied_input_value = value-from-run-that-controls-backend
actual:
test_resource.a:
ID = 1[234](https://github.com/hashicorp/terraform/actions/runs/13857148981/job/38776416114?pr=36646#step:5:235)5
provider = provider["registry.terraform.io/hashicorp/test"]
destroy_fail = false
value = this-value-should-not-enter-state
test_resource.b.0:
ID = 67890
provider = provider["registry.terraform.io/hashicorp/test"]
destroy_fail = false
value = this-value-should-not-enter-state
Outputs:
supplied_input_value = this-value-should-not-enter-state
diff:
(
"""
... // 2 identical lines
provider = provider["registry.terraform.io/hashicorp/test"]
destroy_fail = false
- value = this-value-should-not-enter-state
- test_resource.b.0:
- ID = 67890
- provider = provider["registry.terraform.io/hashicorp/test"]
- destroy_fail = false
- value = this-value-should-not-enter-state
+ value = value-from-run-that-controls-backend
Outputs:
- supplied_input_value = this-value-should-not-enter-state
+ supplied_input_value = value-from-run-that-controls-backend
"""
)
When I use a debugger to find out how the run block that doesn't contain the backend block is able to write to the state (i.e. how test_resource.b.0 and "this-value-should-not-enter-state" gets into the state) the test passes, so maybe there's a race somewhere? The backend value is always set to its old value (https://github.com/hashicorp/terraform/pull/36646/commits/c15e17a71d61f7d80a2339f8565500f6f6368f98) so the run name associated with the backend shouldn't be being changed
This test failure is solved following https://github.com/hashicorp/terraform/pull/36646#issuecomment-2724917346
@SarahFrench I think there is a bug in https://github.com/hashicorp/terraform/blob/44583080719bc42550aedf3f9de2b3475fcaca2b/internal/moduletest/graph/transform_state.go#L68
That line needs to be if bc, exists := t.File.Config.BackendConfigs[key]; exists && bc.Run.Name == node.run.Name {, because the nodes are not sorted, and we may be adding attaching the backend to subsequent runs instead of the first one.
(this doesn't entirely gets the test to pass 🫤 )
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.