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

check for cancellation before apply confirmation #30979

Merged
merged 1 commit into from May 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/backend/local/backend_apply.go
Expand Up @@ -2,6 +2,7 @@ package local

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -16,6 +17,9 @@ import (
"github.com/hashicorp/terraform/internal/tfdiags"
)

// test hook called between plan+apply during opApply
var testHookStopPlanApply func()

func (b *Local) opApply(
stopCtx context.Context,
cancelCtx context.Context,
Expand Down Expand Up @@ -88,6 +92,22 @@ func (b *Local) opApply(
mustConfirm := hasUI && !op.AutoApprove && !trivialPlan
op.View.Plan(plan, schemas)

if testHookStopPlanApply != nil {
testHookStopPlanApply()
}

// Check if we've been stopped before going through confirmation, or
// skipping confirmation in the case of -auto-approve.
// This can currently happen if a single stop request was received
// during the final batch of resource plan calls, so no operations were
// forced to abort, and no errors were returned from Plan.
if stopCtx.Err() != nil {
diags = diags.Append(errors.New("execution halted"))
runningOp.Result = backend.OperationFailure
op.ReportResult(runningOp, diags)
return
}

if mustConfirm {
var desc, query string
switch op.PlanMode {
Expand Down
33 changes: 33 additions & 0 deletions internal/backend/local/backend_apply_test.go
Expand Up @@ -351,3 +351,36 @@ func applyFixtureSchema() *terraform.ProviderSchema {
},
}
}

func TestApply_applyCanceledAutoApprove(t *testing.T) {
b := TestLocal(t)

TestLocalProvider(t, b, "test", applyFixtureSchema())

op, configCleanup, done := testOperationApply(t, "./testdata/apply")
op.AutoApprove = true
defer configCleanup()
defer func() {
output := done(t)
if !strings.Contains(output.Stderr(), "execution halted") {
t.Fatal("expected 'execution halted', got:\n", output.All())
}
}()

ctx, cancel := context.WithCancel(context.Background())
testHookStopPlanApply = cancel
defer func() {
testHookStopPlanApply = nil
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance of this leaking into another parallel test and causing a mid-process cancel?

Copy link
Member Author

@jbardin jbardin May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests must be explicitly marked with t.Parallel() in order to run concurrently, so there should be no conflict there.


run, err := b.Operation(ctx, op)
if err != nil {
t.Fatalf("error starting operation: %v", err)
}

<-run.Done()
if run.Result == backend.OperationSuccess {
t.Fatal("expected apply operation to fail")
}

}