-
-
Notifications
You must be signed in to change notification settings - Fork 996
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from asobrien/bugfix/shell_signals
Wait for shell when forwarding signals; shell package test coverage
- Loading branch information
Showing
7 changed files
with
184 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package shell | ||
|
||
import ( | ||
goerrors "errors" | ||
"github.com/gruntwork-io/terragrunt/options" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRunShellCommand(t *testing.T) { | ||
t.Parallel() | ||
|
||
terragruntOptions := options.NewTerragruntOptionsForTest("") | ||
cmd := RunShellCommand(terragruntOptions, "/bin/bash", "-c", "true") | ||
assert.Nil(t, cmd) | ||
|
||
cmd = RunShellCommand(terragruntOptions, "/bin/bash", "-c", "false") | ||
assert.Error(t, cmd) | ||
} | ||
|
||
func TestExitCode(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 := goerrors.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 TestNewSignalsForwarderWait(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") | ||
|
||
} | ||
|
||
func TestNewSignalsForwarderMultiple(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.Equal(t, retCode, interrupts) | ||
|
||
} |
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,3 @@ | ||
#!/bin/bash -e | ||
|
||
exit $1 |
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,16 @@ | ||
#!/bin/bash -e | ||
|
||
INT_REQUIRED=$1 | ||
INT_COUNTER=0 | ||
|
||
trap int_handler INT | ||
|
||
function int_handler() { | ||
INT_COUNTER=$((INT_COUNTER + 1)) | ||
} | ||
|
||
while [ $INT_COUNTER -lt $INT_REQUIRED ] | ||
do sleep 0.1 | ||
done | ||
|
||
exit $INT_COUNTER |
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,12 @@ | ||
#!/bin/bash -e | ||
|
||
WAIT_TIME=$1 | ||
|
||
trap int_handler INT | ||
|
||
function int_handler() { | ||
sleep $WAIT_TIME | ||
exit $WAIT_TIME | ||
} | ||
|
||
while true; do sleep 0.1; done |