-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecuter_test.go
76 lines (71 loc) · 1.76 KB
/
executer_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
package flexentry_test
import (
"bytes"
"context"
"os"
"strings"
"testing"
"time"
"github.com/mashiike/flexentry"
"github.com/stretchr/testify/require"
)
func TestExecuter(t *testing.T) {
testShell := "sh"
if s := os.Getenv("FLEXENTRY_TEST_SHELL"); s != "" {
testShell = s
}
testShellArgs := "-c"
if args := os.Getenv("FLEXENTRY_TEST_SHELL_ARGS"); args != "" {
testShellArgs = args
}
testShellExecuter := flexentry.NewShellExecuter().
SetShell(testShell, strings.Split(testShellArgs, " "))
cases := []struct {
commands []string
stdin []byte
timeout time.Duration
exceptedErr string
exceptedStderr string
exceptedStdout string
}{
{
commands: []string{"echo hoge"},
exceptedStdout: "hoge\n",
},
{
commands: []string{"hoge"},
exceptedErr: "exit status 127",
exceptedStderr: "sh: 1: hoge: not found\n",
},
{
commands: []string{"sleep 30"},
timeout: 50 * time.Millisecond,
exceptedErr: "signal: killed",
},
}
for _, c := range cases {
t.Run(strings.Join(c.commands, "_"), func(t *testing.T) {
stdin := bytes.NewReader(c.stdin)
var stdout, stderr bytes.Buffer
executer := testShellExecuter
timeout := 1 * time.Minute
if c.timeout != 0 {
timeout = c.timeout
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := executer.Execute(ctx, &flexentry.ExecuteOption{
Stdin: stdin,
Stdout: &stdout,
Stderr: &stderr,
}, c.commands...)
if c.exceptedErr == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, c.exceptedErr)
}
require.EqualValues(t, c.exceptedStdout, stdout.String(), "stdout")
require.EqualValues(t, c.exceptedStderr, stderr.String(), "stderr")
})
}
}