forked from argoproj/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_test.go
147 lines (119 loc) · 5.08 KB
/
exec_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package exec
import (
"os/exec"
"syscall"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunCommand(t *testing.T) {
hook := test.NewGlobal()
log.SetLevel(log.DebugLevel)
defer log.SetLevel(log.InfoLevel)
message, err := RunCommand("echo", CmdOpts{Redactor: Redact([]string{"world"})}, "hello world")
require.NoError(t, err)
assert.Equal(t, "hello world", message)
assert.Len(t, hook.Entries, 2)
entry := hook.Entries[0]
assert.Equal(t, log.InfoLevel, entry.Level)
assert.Equal(t, "echo hello ******", entry.Message)
assert.Contains(t, entry.Data, "dir")
assert.Contains(t, entry.Data, "execID")
entry = hook.Entries[1]
assert.Equal(t, log.DebugLevel, entry.Level)
assert.Equal(t, "hello ******\n", entry.Message)
assert.Contains(t, entry.Data, "duration")
assert.Contains(t, entry.Data, "execID")
}
func TestRunCommandTimeout(t *testing.T) {
hook := test.NewGlobal()
log.SetLevel(log.DebugLevel)
defer log.SetLevel(log.InfoLevel)
output, err := RunCommand("sh", CmdOpts{Timeout: 500 * time.Millisecond}, "-c", "echo hello world && echo my-error >&2 && sleep 2")
assert.Equal(t, "hello world", output)
require.EqualError(t, err, "`sh -c echo hello world && echo my-error >&2 && sleep 2` failed timeout after 500ms")
assert.Len(t, hook.Entries, 3)
entry := hook.Entries[0]
assert.Equal(t, log.InfoLevel, entry.Level)
assert.Equal(t, "sh -c echo hello world && echo my-error >&2 && sleep 2", entry.Message)
assert.Contains(t, entry.Data, "dir")
assert.Contains(t, entry.Data, "execID")
entry = hook.Entries[1]
assert.Equal(t, log.DebugLevel, entry.Level)
assert.Equal(t, "hello world\n", entry.Message)
assert.Contains(t, entry.Data, "duration")
assert.Contains(t, entry.Data, "execID")
entry = hook.Entries[2]
assert.Equal(t, log.ErrorLevel, entry.Level)
assert.Equal(t, "`sh -c echo hello world && echo my-error >&2 && sleep 2` failed timeout after 500ms", entry.Message)
assert.Contains(t, entry.Data, "execID")
}
func TestRunCommandSignal(t *testing.T) {
hook := test.NewGlobal()
log.SetLevel(log.DebugLevel)
defer log.SetLevel(log.InfoLevel)
var timeoutBehavior = TimeoutBehavior{Signal: syscall.SIGTERM, ShouldWait: true}
output, err := RunCommand("sh", CmdOpts{Timeout: 200 * time.Millisecond, TimeoutBehavior: timeoutBehavior}, "-c", "trap 'trap - 15 && echo captured && exit' 15 && sleep 2")
assert.Equal(t, "captured", output)
require.EqualError(t, err, "`sh -c trap 'trap - 15 && echo captured && exit' 15 && sleep 2` failed timeout after 200ms")
assert.Len(t, hook.Entries, 3)
}
func TestTrimmedOutput(t *testing.T) {
message, err := RunCommand("printf", CmdOpts{}, "hello world")
require.NoError(t, err)
assert.Equal(t, "hello world", message)
}
func TestRunCommandExitErr(t *testing.T) {
hook := test.NewGlobal()
log.SetLevel(log.DebugLevel)
defer log.SetLevel(log.InfoLevel)
output, err := RunCommand("sh", CmdOpts{Redactor: Redact([]string{"world"})}, "-c", "echo hello world && echo my-error >&2 && exit 1")
assert.Equal(t, "hello world", output)
require.EqualError(t, err, "`sh -c echo hello ****** && echo my-error >&2 && exit 1` failed exit status 1: my-error")
assert.Len(t, hook.Entries, 3)
entry := hook.Entries[0]
assert.Equal(t, log.InfoLevel, entry.Level)
assert.Equal(t, "sh -c echo hello ****** && echo my-error >&2 && exit 1", entry.Message)
assert.Contains(t, entry.Data, "dir")
assert.Contains(t, entry.Data, "execID")
entry = hook.Entries[1]
assert.Equal(t, log.DebugLevel, entry.Level)
assert.Equal(t, "hello ******\n", entry.Message)
assert.Contains(t, entry.Data, "duration")
assert.Contains(t, entry.Data, "execID")
entry = hook.Entries[2]
assert.Equal(t, log.ErrorLevel, entry.Level)
assert.Equal(t, "`sh -c echo hello ****** && echo my-error >&2 && exit 1` failed exit status 1: my-error", entry.Message)
assert.Contains(t, entry.Data, "execID")
}
func TestRunCommandErr(t *testing.T) {
log.SetLevel(log.DebugLevel)
defer log.SetLevel(log.InfoLevel)
output, err := RunCommand("sh", CmdOpts{Redactor: Redact([]string{"world"})}, "-c", ">&2 echo 'failure'; false")
assert.Empty(t, output)
assert.EqualError(t, err, "`sh -c >&2 echo 'failure'; false` failed exit status 1: failure")
}
func TestRunInDir(t *testing.T) {
cmd := exec.Command("pwd")
cmd.Dir = "/"
message, err := RunCommandExt(cmd, CmdOpts{})
require.NoError(t, err)
assert.Equal(t, "/", message)
}
func TestRedact(t *testing.T) {
assert.Equal(t, "", Redact(nil)(""))
assert.Equal(t, "", Redact([]string{})(""))
assert.Equal(t, "", Redact([]string{"foo"})(""))
assert.Equal(t, "foo", Redact([]string{})("foo"))
assert.Equal(t, "******", Redact([]string{"foo"})("foo"))
assert.Equal(t, "****** ******", Redact([]string{"foo", "bar"})("foo bar"))
assert.Equal(t, "****** ******", Redact([]string{"foo"})("foo foo"))
}
func TestRunCaptureStderr(t *testing.T) {
output, err := RunCommand("sh", CmdOpts{CaptureStderr: true}, "-c", "echo hello world && echo my-error >&2 && exit 0")
assert.Equal(t, "hello world\nmy-error", output)
assert.NoError(t, err)
}