2015-12-07 22:37:05 +00:00
|
|
|
package internal
|
|
|
|
|
2016-04-29 01:23:45 +00:00
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2015-12-07 22:37:05 +00:00
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
type SnakeTest struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []SnakeTest{
|
|
|
|
{"a", "a"},
|
|
|
|
{"snake", "snake"},
|
|
|
|
{"A", "a"},
|
|
|
|
{"ID", "id"},
|
|
|
|
{"MOTD", "motd"},
|
|
|
|
{"Snake", "snake"},
|
|
|
|
{"SnakeTest", "snake_test"},
|
|
|
|
{"APIResponse", "api_response"},
|
|
|
|
{"SnakeID", "snake_id"},
|
|
|
|
{"SnakeIDGoogle", "snake_id_google"},
|
|
|
|
{"LinuxMOTD", "linux_motd"},
|
|
|
|
{"OMGWTFBBQ", "omgwtfbbq"},
|
|
|
|
{"omg_wtf_bbq", "omg_wtf_bbq"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnakeCase(t *testing.T) {
|
|
|
|
for _, test := range tests {
|
|
|
|
if SnakeCase(test.input) != test.output {
|
|
|
|
t.Errorf(`SnakeCase("%s"), wanted "%s", got \%s"`, test.input, test.output, SnakeCase(test.input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 01:23:45 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
sleepbin, _ = exec.LookPath("sleep")
|
|
|
|
echobin, _ = exec.LookPath("echo")
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunTimeout(t *testing.T) {
|
|
|
|
if sleepbin == "" {
|
|
|
|
t.Skip("'sleep' binary not available on OS, skipping.")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(sleepbin, "10")
|
|
|
|
start := time.Now()
|
|
|
|
err := RunTimeout(cmd, time.Millisecond*20)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
assert.Equal(t, TimeoutErr, err)
|
|
|
|
// Verify that command gets killed in 20ms, with some breathing room
|
|
|
|
assert.True(t, elapsed < time.Millisecond*75)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCombinedOutputTimeout(t *testing.T) {
|
|
|
|
if sleepbin == "" {
|
|
|
|
t.Skip("'sleep' binary not available on OS, skipping.")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(sleepbin, "10")
|
|
|
|
start := time.Now()
|
|
|
|
_, err := CombinedOutputTimeout(cmd, time.Millisecond*20)
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
assert.Equal(t, TimeoutErr, err)
|
|
|
|
// Verify that command gets killed in 20ms, with some breathing room
|
|
|
|
assert.True(t, elapsed < time.Millisecond*75)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCombinedOutput(t *testing.T) {
|
|
|
|
if echobin == "" {
|
|
|
|
t.Skip("'echo' binary not available on OS, skipping.")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(echobin, "foo")
|
|
|
|
out, err := CombinedOutputTimeout(cmd, time.Second)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo\n", string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that CombinedOutputTimeout and exec.Cmd.CombinedOutput return
|
|
|
|
// the same output from a failed command.
|
|
|
|
func TestCombinedOutputError(t *testing.T) {
|
|
|
|
if sleepbin == "" {
|
|
|
|
t.Skip("'sleep' binary not available on OS, skipping.")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(sleepbin, "foo")
|
2016-10-15 20:58:04 +00:00
|
|
|
expected, expectedErr := cmd.CombinedOutput()
|
2016-04-29 01:23:45 +00:00
|
|
|
|
|
|
|
cmd2 := exec.Command(sleepbin, "foo")
|
2016-10-15 20:58:04 +00:00
|
|
|
actual, actualErr := CombinedOutputTimeout(cmd2, time.Second)
|
2016-04-29 01:23:45 +00:00
|
|
|
|
2016-10-15 20:58:04 +00:00
|
|
|
if expectedErr != nil {
|
|
|
|
assert.Error(t, actualErr)
|
|
|
|
}
|
2016-04-29 01:23:45 +00:00
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunError(t *testing.T) {
|
|
|
|
if sleepbin == "" {
|
|
|
|
t.Skip("'sleep' binary not available on OS, skipping.")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(sleepbin, "foo")
|
2016-10-15 20:58:04 +00:00
|
|
|
expectedErr := cmd.Run()
|
|
|
|
|
|
|
|
cmd2 := exec.Command(sleepbin, "foo")
|
|
|
|
actualErr := RunTimeout(cmd2, time.Second)
|
2016-04-29 01:23:45 +00:00
|
|
|
|
2016-10-15 20:58:04 +00:00
|
|
|
if expectedErr != nil {
|
|
|
|
assert.Error(t, actualErr)
|
|
|
|
}
|
2016-04-29 01:23:45 +00:00
|
|
|
}
|
2016-05-26 09:40:03 +00:00
|
|
|
|
2016-05-30 22:24:42 +00:00
|
|
|
func TestRandomSleep(t *testing.T) {
|
|
|
|
// test that zero max returns immediately
|
|
|
|
s := time.Now()
|
|
|
|
RandomSleep(time.Duration(0), make(chan struct{}))
|
|
|
|
elapsed := time.Since(s)
|
|
|
|
assert.True(t, elapsed < time.Millisecond)
|
|
|
|
|
|
|
|
// test that max sleep is respected
|
|
|
|
s = time.Now()
|
|
|
|
RandomSleep(time.Millisecond*50, make(chan struct{}))
|
|
|
|
elapsed = time.Since(s)
|
2016-09-06 10:34:57 +00:00
|
|
|
assert.True(t, elapsed < time.Millisecond*100)
|
2016-05-30 22:24:42 +00:00
|
|
|
|
|
|
|
// test that shutdown is respected
|
|
|
|
s = time.Now()
|
|
|
|
shutdown := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
close(shutdown)
|
|
|
|
}()
|
|
|
|
RandomSleep(time.Second, shutdown)
|
|
|
|
elapsed = time.Since(s)
|
|
|
|
assert.True(t, elapsed < time.Millisecond*150)
|
|
|
|
}
|