forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_shell_cmd_unix_test.go
117 lines (94 loc) · 2.86 KB
/
run_shell_cmd_unix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//go:build linux || darwin
// +build linux darwin
package shell
import (
"errors"
"os"
"os/exec"
"strconv"
"testing"
"time"
"github.com/coveooss/terragrunt/v2/options"
"github.com/stretchr/testify/assert"
)
func TestExitCodeUnix(t *testing.T) {
t.Parallel()
for i := 0; i <= 255; i++ {
cmd := exec.Command("../testdata/test_exit_code.sh", strconv.Itoa(i))
err := cmd.Run()
if i == 0 {
assert.Nil(t, err)
} else {
assert.Error(t, err)
}
retCode, err := GetExitCode(err)
assert.Nil(t, err)
assert.Equal(t, i, retCode)
}
// assert a non exec.ExitError returns an error
err := errors.New("This is an explicit error")
retCode, retErr := GetExitCode(err)
assert.Error(t, retErr, "An error was expected")
assert.Equal(t, err, retErr)
assert.Equal(t, 0, retCode)
}
func TestNewSignalsForwarderWaitUnix(t *testing.T) {
t.Parallel()
expectedWait := 5
terragruntOptions := options.NewTerragruntOptionsForTest("")
cmd := exec.Command("../testdata/test_sigint_wait.sh", strconv.Itoa(expectedWait))
cmdChannel := make(chan error)
runChannel := make(chan error)
signalChannel := NewSignalsForwarder(forwardSignals, cmd, terragruntOptions.Logger, cmdChannel)
defer signalChannel.Close()
go func() {
runChannel <- cmd.Run()
}()
time.Sleep(1000 * time.Millisecond)
start := time.Now()
cmd.Process.Signal(os.Interrupt)
err := <-runChannel
cmdChannel <- err
assert.Error(t, err)
retCode, err := GetExitCode(err)
assert.Nil(t, err)
assert.Equal(t, retCode, expectedWait)
assert.WithinDuration(t, time.Now(), start.Add(time.Duration(expectedWait)*time.Second), time.Second,
"Expected to wait 5 (+/-1) seconds after SIGINT")
}
// There isn't a proper way to catch interrupts in Windows batch scripts, so this test exists only for Unix
func TestNewSignalsForwarderMultipleUnix(t *testing.T) {
t.Parallel()
expectedInterrupts := 10
terragruntOptions := options.NewTerragruntOptionsForTest("")
cmd := exec.Command("../testdata/test_sigint_multiple.sh", strconv.Itoa(expectedInterrupts))
cmdChannel := make(chan error)
runChannel := make(chan error)
signalChannel := NewSignalsForwarder(forwardSignals, cmd, terragruntOptions.Logger, cmdChannel)
defer signalChannel.Close()
go func() {
runChannel <- cmd.Run()
}()
time.Sleep(1000 * time.Millisecond)
interruptAndWaitForProcess := func() (int, error) {
var interrupts int
var err error
for {
time.Sleep(500 * time.Millisecond)
select {
case err = <-runChannel:
return interrupts, err
default:
cmd.Process.Signal(os.Interrupt)
interrupts++
}
}
}
interrupts, err := interruptAndWaitForProcess()
cmdChannel <- err
assert.Error(t, err)
retCode, err := GetExitCode(err)
assert.Nil(t, err)
assert.True(t, retCode <= interrupts, "Subprocess received wrong number of signals")
assert.Equal(t, retCode, expectedInterrupts, "Subprocess didn't receive multiple signals")
}