-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexec_test.go
271 lines (222 loc) · 6.22 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package execute
import (
"bytes"
"context"
"errors"
"os"
"strings"
"testing"
"time"
)
func TestExec_ReturnErrorForUnknownCommand(t *testing.T) {
ctx := context.Background()
task := ExecTask{Command: "/bin/go_execute_you_cant_find_me /"}
res, err := task.Execute(ctx)
if err == nil {
t.Fatalf("want error, but got nil")
}
if !strings.Contains(strings.ToLower(err.Error()), "no such file or directory") {
t.Fatalf("want context.Canceled, but got %v", err)
}
// expect and empty default response
if res.Cancelled != false {
t.Fatalf("want not cancelled, but got true")
}
if res.ExitCode != 0 {
t.Fatalf("want exit code 0, but got %d", res.ExitCode)
}
if res.Stderr != "" {
t.Fatalf("want empty stderr, but got %s", res.Stderr)
}
if res.Stdout != "" {
t.Fatalf("want empty stdout, but got %s", res.Stdout)
}
}
func TestExec_ContextAlreadyCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
task := ExecTask{Command: "/bin/ls /"}
res, err := task.Execute(ctx)
if err == nil {
t.Fatalf("want error, but got nil")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("want context.Canceled, but got %v", err)
}
if res.Cancelled != true {
t.Fatalf("want cancelled, but got false")
}
if res.ExitCode != -1 {
t.Fatalf("want exit code -1, but got %d", res.ExitCode)
}
}
func TestExec_ContextCancelledDuringExecution(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.AfterFunc(time.Millisecond*100, cancel)
}()
task := ExecTask{Command: "sleep 10"}
res, err := task.Execute(ctx)
if err == nil {
t.Fatalf("want error, but got nil")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("want context.Canceled, but got %v", err)
}
if res.Cancelled != true {
t.Fatalf("want cancelled, but got false")
}
if res.ExitCode != -1 {
t.Fatalf("want exit code -1, but got %d", res.ExitCode)
}
}
func TestExecShell_ContextCancelledDuringExecution(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.AfterFunc(time.Millisecond*100, cancel)
}()
task := ExecTask{Command: "sleep 10", Shell: true}
res, err := task.Execute(ctx)
if err == nil {
t.Fatalf("want error, but got nil")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("want context.Canceled, but got %v", err)
}
if res.Cancelled != true {
t.Fatalf("want cancelled, but got false")
}
if res.ExitCode != -1 {
t.Fatalf("want exit code -1, but got %d", res.ExitCode)
}
}
func TestExec_WithShell(t *testing.T) {
ctx := context.Background()
task := ExecTask{Command: "/bin/ls /", Shell: true}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if len(res.Stdout) == 0 {
t.Fatalf("want data, but got empty")
}
if len(res.Stderr) != 0 {
t.Fatalf("want empty, but got: %s", res.Stderr)
}
}
func TestExec_WithShellAndArgs(t *testing.T) {
ctx := context.Background()
task := ExecTask{Command: "/bin/ls", Args: []string{"/"}, Shell: true}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if len(res.Stdout) == 0 {
t.Fatalf("want data, but got empty")
}
if len(res.Stderr) != 0 {
t.Fatalf("want empty, but got: %s", res.Stderr)
}
}
func TestExec_CatTransformString(t *testing.T) {
input := "1 line 1"
reader := bytes.NewReader([]byte(input))
want := " 1\t1 line 1"
ctx := context.Background()
task := ExecTask{Command: "cat", Shell: false, Args: []string{"-b"}, Stdin: reader}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if res.Stdout != want {
t.Fatalf("want %q, got %q", want, res.Stdout)
}
}
func TestExec_CatWC(t *testing.T) {
input := `this
has
four
lines
`
reader := bytes.NewReader([]byte(input))
want := "4"
ctx := context.Background()
task := ExecTask{Command: "wc", Shell: false, Args: []string{"-l"}, Stdin: reader}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
got := strings.TrimSpace(res.Stdout)
if got != want {
t.Fatalf("want %q, got %q", want, got)
}
}
func TestExec_WithEnvVars(t *testing.T) {
ctx := context.Background()
task := ExecTask{Command: "env", Shell: false, Env: []string{"GOTEST=1", "GOTEST2=2"}}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if !strings.Contains(res.Stdout, "GOTEST") {
t.Fatalf("want env to show GOTEST=1 since we passed that variable")
}
if !strings.Contains(res.Stdout, "GOTEST2") {
t.Fatalf("want env to show GOTEST2=2 since we passed that variable")
}
}
func TestExec_WithEnvVarsInheritedFromParent(t *testing.T) {
os.Setenv("TEST", "value")
ctx := context.Background()
task := ExecTask{Command: "env", Shell: false, Env: []string{"GOTEST=1"}}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if !strings.Contains(res.Stdout, "TEST") {
t.Fatalf("want env to show TEST=value since we passed that variable")
}
if !strings.Contains(res.Stdout, "GOTEST") {
t.Fatalf("want env to show GOTEST=1 since we passed that variable")
}
}
func TestExec_WithEnvVarsAndShell(t *testing.T) {
ctx := context.Background()
task := ExecTask{Command: "env", Shell: true, Env: []string{"GOTEST=1"}}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if !strings.Contains(res.Stdout, "GOTEST") {
t.Fatalf("want env to show GOTEST=1 since we passed that variable")
}
}
func TestExec_CanStreamStdout(t *testing.T) {
input := "1 line 1"
reader := bytes.NewReader([]byte(input))
want := " 1\t1 line 1"
ctx := context.Background()
task := ExecTask{Command: "cat", Shell: false, Args: []string{"-b"}, Stdin: reader, StreamStdio: true}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if res.Stdout != want {
t.Fatalf("want %q, got %q", want, res.Stdout)
}
}
func TestExec_CanStreamStderr(t *testing.T) {
ctx := context.Background()
task := ExecTask{Command: "ls /unknown/location/should/fail", StreamStdio: true}
res, err := task.Execute(ctx)
if err != nil {
t.Fatalf(err.Error())
}
if res.Stdout != "" {
t.Fatalf("want empty string Stdout, got %q", res.Stdout)
}
wantContains := "no such file or directory"
if !strings.Contains(strings.ToLower(res.Stderr), wantContains) {
t.Fatalf("want stderr to contain %q, got %q", wantContains, res.Stderr)
}
}