-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec_test.go
291 lines (246 loc) · 6.64 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package basic
import (
"bufio"
"bytes"
"context"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"os"
"os/exec"
"syscall"
"testing"
"time"
)
func TestSingleCommand(t *testing.T) {
stdout, err := exec.Command("uname", "-a").CombinedOutput()
if err != nil {
t.Error(err)
return
}
t.Log(stdout)
}
func TestSingleTimeoutCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
stdout, err := exec.CommandContext(ctx, "ping", "-c 2", "-i 1", "www.baidu.com").CombinedOutput()
if err != nil {
t.Error(err)
return
}
t.Log(string(stdout))
}
// can not get stdout
//
// === RUN TestSingleTimeoutCommand
// exec_test.go:27: signal: killed
func TestLongTimeoutCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
stdout, err := exec.CommandContext(ctx, "ping", "www.baidu.com").CombinedOutput()
if err != nil {
t.Error(err)
return
}
t.Log(stdout)
}
// can not cancel when timeout
// cat /tmp/a.sh
// ping www.baidu.com
//
// === RUN TestSingleTimeoutCommand
// exec_test.go:27: signal: killed
func TestTimeoutCancelFailureCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
stdout, err := exec.CommandContext(ctx, "/bin/bash", "/tmp/a.sh").CombinedOutput()
if err != nil {
t.Error(err)
return
}
t.Log(stdout)
}
// cancel command && get stdout\ stderr
// cat /tmp/a.sh
// ping www.baidu.com
//
// === RUN TestSingleTimeoutCommand
// exec_test.go:27: signal: killed
func TestTimeoutCancelCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "/bin/bash", "/tmp/a.sh")
//cmd := exec.CommandContext(ctx, "lspci")
stdoutPipe, _ := cmd.StdoutPipe()
stderrPipe, _ := cmd.StderrPipe()
outReader := bufio.NewReaderSize(stdoutPipe, 2*1024*1024)
errReader := bufio.NewReaderSize(stderrPipe, 2*1024*1024)
stdoutChan := make(chan string, 0)
stderrChan := make(chan string, 0)
err := cmd.Start()
if err != nil {
fmt.Println(err.Error())
return
}
go func() {
for {
line, err := outReader.ReadString('\n')
if line != "" {
stdoutChan <- line
}
if err != nil && err != io.EOF && err.Error() != "read |0: file already closed" {
stderrChan <- err.Error()
return
}
if line == "" {
break
}
}
}()
go func() {
for {
line, err := errReader.ReadString('\n')
if line != "" {
stderrChan <- line
}
if err != nil && err != io.EOF && err.Error() != "read |0: file already closed" {
stderrChan <- err.Error()
return
}
if line == "" {
break
}
}
}()
var stdoutStr string
var stderrStr string
LoopBreak:
for {
select {
case <-ctx.Done():
break LoopBreak
case str := <-stdoutChan:
stdoutStr += str
case str := <-stderrChan:
stderrStr += str
case <-time.After(100 * time.Millisecond):
if err = cmd.Wait(); err != nil {
if err.Error() == "exec: Wait was already called" || err == os.ErrClosed {
break LoopBreak
}
if exiterr, ok := err.(*exec.ExitError); ok {
status := exiterr.Sys().(syscall.WaitStatus)
if status.ExitStatus() == 0 {
stderrStr = fmt.Sprintf("wrong exit status: %d", status.ExitStatus())
break LoopBreak
}
}
}
}
}
fmt.Println("stdout", stdoutStr)
fmt.Println("stderr", stderrStr)
fmt.Println("exec done")
}
// get stdout
//
// === RUN TestSingleTimeoutCommand
// exec_test.go:27: signal: killed
func TestLongTimeoutStdoutCommand(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "/bin/bash", "/tmp/a.sh")
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
var errStdout, errStderr error
var stdoutBuf, stderrBuf bytes.Buffer
// 将stdout写入os.stdout当中
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
err := cmd.Start()
if err != nil {
fmt.Println(err.Error())
return
}
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
}()
go func() {
_, errStderr = io.Copy(stderr, stderrIn)
}()
err = cmd.Wait()
if err != nil {
fmt.Println(err.Error())
return
}
if errStdout != nil || errStderr != nil {
fmt.Printf("failed to capture stdout or stderr\n")
return
}
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
}
func TestOutputTimeout(t *testing.T) {
argv := []string{"-c", "/tmp/a.sh"}
attr := new(os.ProcAttr)
newProcess, err := os.StartProcess("/bin/bash", argv, attr) //运行脚本
if err != nil {
fmt.Println(err)
}
fmt.Println("Process PID", newProcess.Pid)
processState, err := newProcess.Wait() //等待命令执行完
if err != nil {
fmt.Println(err)
}
fmt.Println("processState PID:", processState.Pid()) //获取PID
fmt.Println("ProcessExit:", processState.Exited()) //获取进程是否退出
}
func TestCmdRunTimeout2(t *testing.T) {
err, _ := CmdRunWithTimeout("ping www.baidu.com", 10*time.Second)
if err != nil {
t.Error(err.Error())
}
}
func TestCmdOutput(t *testing.T) {
ast := assert.New(t)
output, err := exec.Command("cat", "/tmp/aa.sh").CombinedOutput()
ast.NotNil(err) // exit status 1
ast.NotEmpty(output) // cat: /tmp/aa.sh: No such file or directory
}
func TestCmdOutputPipeline(t *testing.T) {
ast := assert.New(t)
output, err := exec.Command("cat", "/tmp/aa.sh", "|", "grep", "aa").CombinedOutput()
ast.NotNil(err) // exit status 1
//cat: /tmp/aa.sh: No such file or directory
//cat: |: No such file or directory
//cat: grep: No such file
ast.NotEmpty(output)
}
func TestCmdOutputPipeline1(t *testing.T) {
ast := assert.New(t)
output, err := exec.Command(`cat /tmp/aa.sh | grep aa`).CombinedOutput()
ast.NotNil(err) // fork/exec cat /tmp/aa.sh | grep aa: no such file or directory
ast.Empty(output) // nil
}
func TestCmdOutputPipeline2(t *testing.T) {
ast := assert.New(t)
var stdout, stderr bytes.Buffer
cmd := exec.Command(`cat /tmp/aa.sh | grep aa`)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
ast.NotNil(err) // fork/exec cat /tmp/aa.sh | grep aa: no such file or directory
ast.Empty(stderr.Bytes())
ast.Empty(stdout.Bytes())
}
func TestCmdOutputPipeline3(t *testing.T) {
ast := assert.New(t)
var stdout, stderr bytes.Buffer
cmd := exec.Command("/bin/bash", "-c", `cat /tmp/aa.sh | grep aa`)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
ast.NotNil(err) // exit status 1
ast.NotEmpty(stderr.Bytes()) // cat: /tmp/aa.sh: No such file or directory
ast.Empty(stdout.Bytes())
}