-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_postActionCmd(t *testing.T) { | ||
cmd := NewRootCmd() | ||
|
||
tmp := makeTempHostsFile(t, "postActionCmd") | ||
defer os.Remove(tmp.Name()) | ||
|
||
t.Run("Wait and disable", func(t *testing.T) { | ||
b := bytes.NewBufferString("") | ||
args := []string{"enable", "profile1", "--host-file", tmp.Name(), "--wait", "10ms"} | ||
|
||
cmd.SetOut(b) | ||
cmd.SetArgs(args) | ||
|
||
err := cmd.Execute() | ||
assert.NoError(t, err) | ||
|
||
out, _ := ioutil.ReadAll(b) | ||
assert.Contains(t, string(out), "Waiting for 10ms or ctrl+c to disable from profile 'profile1'") | ||
}) | ||
|
||
t.Run("Wait and disable on SIGTERM", func(t *testing.T) { | ||
b := bytes.NewBufferString("") | ||
args := []string{"enable", "profile1", "--host-file", tmp.Name(), "--wait", "0"} | ||
|
||
cmd.SetOut(b) | ||
cmd.SetArgs(args) | ||
|
||
proc, err := os.FindProcess(os.Getpid()) | ||
assert.NoError(t, err) | ||
|
||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
err = proc.Signal(os.Interrupt) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
err = cmd.Execute() | ||
assert.NoError(t, err) | ||
|
||
out, _ := ioutil.ReadAll(b) | ||
assert.Contains(t, string(out), "Waiting until ctrl+c to disable from profile 'profile1'") | ||
}) | ||
|
||
} | ||
|
||
func Test_waitSignalOrDuration(t *testing.T) { | ||
t.Run("timeout", func(t *testing.T) { | ||
d := 10 * time.Millisecond | ||
done := waitSignalOrDuration(d) | ||
v, ok := <-done | ||
assert.Equal(t, v, struct{}{}) | ||
assert.Equal(t, ok, true) | ||
}) | ||
|
||
t.Run("timeout abs", func(t *testing.T) { | ||
d := -10 * time.Millisecond | ||
done := waitSignalOrDuration(d) | ||
v, ok := <-done | ||
assert.Equal(t, v, struct{}{}) | ||
assert.Equal(t, ok, true) | ||
}) | ||
|
||
t.Run("SIGINT", func(t *testing.T) { | ||
proc, err := os.FindProcess(os.Getpid()) | ||
assert.NoError(t, err) | ||
|
||
d := 1 * time.Hour | ||
done := waitSignalOrDuration(d) | ||
|
||
time.Sleep(10 * time.Millisecond) | ||
err = proc.Signal(os.Interrupt) | ||
assert.NoError(t, err) | ||
|
||
v, ok := <-done | ||
assert.Equal(t, v, struct{}{}) | ||
assert.Equal(t, ok, true) | ||
}) | ||
|
||
t.Run("Wait forever", func(t *testing.T) { | ||
proc, err := os.FindProcess(os.Getpid()) | ||
assert.NoError(t, err) | ||
|
||
done := waitSignalOrDuration(0) | ||
|
||
time.Sleep(10 * time.Millisecond) | ||
err = proc.Signal(os.Interrupt) | ||
assert.NoError(t, err) | ||
|
||
v, ok := <-done | ||
assert.Equal(t, v, struct{}{}) | ||
assert.Equal(t, ok, true) | ||
}) | ||
|
||
} |