-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec_test.go
209 lines (175 loc) · 4 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
package basic
import (
"bufio"
"bytes"
"context"
"fmt"
"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")
stdoutPipe, _ := cmd.StdoutPipe()
stderrPipe, _ := cmd.StderrPipe()
outReader := bufio.NewReader(stdoutPipe)
errReader := bufio.NewReader(stderrPipe)
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 {
stderrChan <- err.Error()
return
}
if line == "" {
break
}
}
}()
go func() {
for {
line, err := errReader.ReadString('\n')
if line != "" {
stderrChan <- line
}
if err != nil {
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
}
}
err = cmd.Wait()
if err != nil {
exitErr := err.(*exec.ExitError)
status := exitErr.Sys().(syscall.WaitStatus)
if status.ExitStatus() == 0 {
fmt.Printf("wrong exit status: %v", status.ExitStatus())
}
}
fmt.Println(stdoutStr)
fmt.Println(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)
}