Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform test: fix crash when non-applyable plans are applied #36582

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/v1.11/BUG FIXES-20250226-140931.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: '`terraform test`: Fix crash when a run block attempts to cleanup after a non-applyable plan.'
time: 2025-02-26T14:09:31.83904+01:00
custom:
Issue: "36582"
12 changes: 7 additions & 5 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,13 @@ func (runner *TestFileRunner) cleanup(file *moduletest.File) {
for key, state := range runner.RelevantStates {

empty := true
for _, module := range state.State.Modules {
for _, resource := range module.Resources {
if resource.Addr.Resource.Mode == addrs.ManagedResourceMode {
empty = false
break
if !state.State.Empty() {
for _, module := range state.State.Modules {
for _, resource := range module.Resources {
if resource.Addr.Resource.Mode == addrs.ManagedResourceMode {
empty = false
break
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ func TestTest_Runs(t *testing.T) {
expectedErr: []string{"Test assertion failed", "resource renamed without moved block"},
code: 1,
},
"unapplyable-plan": {
expectedOut: []string{"0 passed, 1 failed."},
expectedErr: []string{"Cannot apply non-applyable plan"},
code: 1,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions internal/command/testdata/test/unapplyable-plan/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

resource "test_resource" "example" {
value = "bar"
}

output "value" {
value = test_resource.example.value
}
11 changes: 11 additions & 0 deletions internal/command/testdata/test/unapplyable-plan/main.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

run "test" {
command = apply
plan_options {
mode = refresh-only
}
assert {
condition = test_resource.example.value == "bar"
error_message = "wrong value"
}
}
Loading