forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tfexec: add InterruptChannel option to allow a user triggered interrupt
This is an alternative to hashicorp#332, adding an option that enables the user to implement graceful shutdowns for Apply and Destroy operations. Rather than adding an option that changes the behavior of the input context, we instead add an option that specifically sends an interrupt to the terraform process. The input context behavior remains unchanged. This requires the caller to do a bit more orchestration work for timeouts, but keeps context truer to the "abandon work" intent. This also allows users to force quit _even if_ they are in the middle of a graceful shutdown, rathern than having one behavior mutually exclusive with the other.
- Loading branch information
Showing
11 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package e2etest | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/terraform-exec/tfexec" | ||
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil" | ||
) | ||
|
||
func Test_gracefulTerminationRunTerraformCmd(t *testing.T) { | ||
runTestVersions(t, []string{testutil.Latest_v1_1}, "infinite_loop", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
var bufStdout bytes.Buffer | ||
var bufStderr bytes.Buffer | ||
tf.SetStderr(&bufStdout) | ||
tf.SetStdout(&bufStderr) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
err := tf.Init(ctx) | ||
if err != nil { | ||
t.Fatalf("error running Init in test directory: %s", err) | ||
} | ||
|
||
doneCh := make(chan error) | ||
shutdown := make(chan struct{}) | ||
go func() { | ||
doneCh <- tf.Apply(ctx, tfexec.InterruptChannel(shutdown)) | ||
}() | ||
|
||
time.Sleep(3 * time.Second) | ||
close(shutdown) | ||
err = <-doneCh | ||
close(doneCh) | ||
if err != nil { | ||
t.Log(err) | ||
} | ||
output := bufStderr.String() + bufStdout.String() | ||
t.Log(output) | ||
if !strings.Contains(output, "Gracefully shutting down...") { | ||
t.Fatal("canceling context should gracefully shut terraform down") | ||
} | ||
}) | ||
} | ||
|
||
func Test_gracefulTerminationRunTerraformCmdWithNoGracefulShutdownTimeout(t *testing.T) { | ||
runTestVersions(t, []string{testutil.Latest_v1_1}, "infinite_loop", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
var bufStdout bytes.Buffer | ||
var bufStderr bytes.Buffer | ||
tf.SetStderr(&bufStdout) | ||
tf.SetStdout(&bufStderr) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
err := tf.Init(ctx) | ||
if err != nil { | ||
t.Fatalf("error running Init in test directory: %s", err) | ||
} | ||
|
||
doneCh := make(chan error) | ||
go func() { | ||
doneCh <- tf.Apply(ctx, tfexec.InterruptChannel(make(chan struct{}))) | ||
}() | ||
|
||
time.Sleep(3 * time.Second) | ||
cancel() | ||
err = <-doneCh | ||
close(doneCh) | ||
if err != nil { | ||
t.Log(err) | ||
} | ||
output := bufStderr.String() + bufStdout.String() | ||
t.Log(output) | ||
if strings.Contains(output, "Gracefully shutting down...") { | ||
t.Fatal("canceling context with no graceful shutdown timeout should immediately kill the process and not start a graceful cancellation") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
resource "null_resource" "example1" { | ||
triggers = { | ||
always_run = "${timestamp()}" | ||
} | ||
provisioner "local-exec" { | ||
command = " while true; do echo 'Hit CTRL+C'; sleep 1; done" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters