@@ -8,16 +8,23 @@ import (
8
8
"errors"
9
9
"io/ioutil"
10
10
"os"
11
+ "os/exec"
11
12
"path/filepath"
12
13
"runtime"
13
14
"testing"
15
+ "time"
14
16
15
17
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
16
18
)
17
19
18
20
var tfCache * testutil.TFCache
19
21
20
22
func TestMain (m * testing.M ) {
23
+ if rawDuration := os .Getenv ("MOCK_SLEEP_DURATION" ); rawDuration != "" {
24
+ sleepMock (rawDuration )
25
+ return
26
+ }
27
+
21
28
os .Exit (func () int {
22
29
var err error
23
30
installDir , err := ioutil .TempDir ("" , "tfinstall" )
@@ -813,6 +820,78 @@ func TestCheckpointDisablePropagation_v1(t *testing.T) {
813
820
})
814
821
}
815
822
823
+ func TestGracefulCancellation_interruption (t * testing.T ) {
824
+ if runtime .GOOS == "windows" {
825
+ t .Skip ("graceful cancellation not supported on windows" )
826
+ }
827
+ mockExecPath , err := os .Executable ()
828
+ if err != nil {
829
+ t .Fatal (err )
830
+ }
831
+
832
+ td := t .TempDir ()
833
+
834
+ tf , err := NewTerraform (td , mockExecPath )
835
+ if err != nil {
836
+ t .Fatal (err )
837
+ }
838
+
839
+ ctx := context .Background ()
840
+ ctx , cancelFunc := context .WithTimeout (ctx , 100 * time .Millisecond )
841
+ t .Cleanup (cancelFunc )
842
+
843
+ _ , _ , err = tf .version (ctx )
844
+ if err != nil {
845
+ var exitErr * exec.ExitError
846
+ isExitErr := errors .As (err , & exitErr )
847
+ if isExitErr && exitErr .ProcessState .String () == "signal: interrupt" {
848
+ return
849
+ }
850
+ if isExitErr {
851
+ t .Fatalf ("expected interrupt signal, received %q" , exitErr )
852
+ }
853
+ t .Fatalf ("unexpected command error: %s" , err )
854
+ }
855
+ }
856
+
857
+ func TestGracefulCancellation_withDelay (t * testing.T ) {
858
+ if runtime .GOOS == "windows" {
859
+ t .Skip ("graceful cancellation not supported on windows" )
860
+ }
861
+ mockExecPath , err := os .Executable ()
862
+ if err != nil {
863
+ t .Fatal (err )
864
+ }
865
+
866
+ td := t .TempDir ()
867
+ tf , err := NewTerraform (td , mockExecPath )
868
+ if err != nil {
869
+ t .Fatal (err )
870
+ }
871
+ tf .SetEnv (map [string ]string {
872
+ "MOCK_SLEEP_DURATION" : "5s" ,
873
+ })
874
+ tf .SetLogger (testutil .TestLogger ())
875
+ tf .SetWaitDelay (100 * time .Millisecond )
876
+
877
+ ctx := context .Background ()
878
+ ctx , cancelFunc := context .WithTimeout (ctx , 100 * time .Millisecond )
879
+ t .Cleanup (cancelFunc )
880
+
881
+ _ , _ , err = tf .version (ctx )
882
+ if err != nil {
883
+ var exitErr * exec.ExitError
884
+ isExitErr := errors .As (err , & exitErr )
885
+ if isExitErr && exitErr .ProcessState .String () == "signal: killed" {
886
+ return
887
+ }
888
+ if isExitErr {
889
+ t .Fatalf ("expected kill signal, received %q" , exitErr )
890
+ }
891
+ t .Fatalf ("unexpected command error: %s" , err )
892
+ }
893
+ }
894
+
816
895
// test that a suitable error is returned if NewTerraform is called without a valid
817
896
// executable path
818
897
func TestNoTerraformBinary (t * testing.T ) {
0 commit comments