/
pipeline_test.go
230 lines (203 loc) · 6.81 KB
/
pipeline_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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gosh_test
import (
"fmt"
"os"
"testing"
"time"
"github.com/asadovsky/gosh"
)
func TestPipeline(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
echo := sh.FuncCmd(echoFunc)
echo.Args = append(echo.Args, "foo")
replace := sh.FuncCmd(replaceFunc, byte('f'), byte('Z'))
cat := sh.FuncCmd(catFunc)
p := gosh.NewPipeline(echo, replace, cat)
eq(t, p.Stdout(), "Zoo\n")
eq(t, p.Clone().Stdout(), "Zoo\n")
// Try piping only stdout.
p = gosh.NewPipeline(sh.FuncCmd(writeFunc, true, true))
p.PipeStdout(sh.FuncCmd(replaceFunc, byte('A'), byte('Z')))
eq(t, p.Stdout(), "ZZ")
eq(t, p.Clone().Stdout(), "ZZ")
// Try piping only stderr.
p = gosh.NewPipeline(sh.FuncCmd(writeFunc, true, true))
p.PipeStderr(sh.FuncCmd(replaceFunc, byte('B'), byte('Z')))
eq(t, p.Stdout(), "ZZ")
eq(t, p.Clone().Stdout(), "ZZ")
// Try piping both stdout and stderr.
p = gosh.NewPipeline(sh.FuncCmd(writeFunc, true, true))
p.PipeCombinedOutput(sh.FuncCmd(catFunc))
// Note, we can't assume any particular ordering of stdout and stderr, so we
// simply check the length of the combined output.
eq(t, len(p.Stdout()), 4)
eq(t, len(p.Clone().Stdout()), 4)
// Try piping combinations.
p = gosh.NewPipeline(sh.FuncCmd(writeFunc, true, true))
p.PipeStderr(sh.FuncCmd(replaceFunc, byte('B'), byte('x')))
p.PipeStdout(sh.FuncCmd(replaceFunc, byte('x'), byte('Z')))
p.PipeStdout(sh.FuncCmd(catFunc))
eq(t, p.Stdout(), "ZZ")
eq(t, p.Clone().Stdout(), "ZZ")
}
func TestPipelineDifferentShells(t *testing.T) {
sh1 := gosh.NewShell(t)
defer sh1.Cleanup()
sh2 := gosh.NewShell(t)
defer sh2.Cleanup()
setsErr(t, sh1, func() { gosh.NewPipeline(sh1.FuncCmd(echoFunc), sh2.FuncCmd(catFunc)) })
setsErr(t, sh2, func() { gosh.NewPipeline(sh2.FuncCmd(echoFunc), sh1.FuncCmd(catFunc)) })
p := gosh.NewPipeline(sh1.FuncCmd(echoFunc))
setsErr(t, sh1, func() { p.PipeStdout(sh2.FuncCmd(catFunc)) })
p = gosh.NewPipeline(sh1.FuncCmd(echoFunc))
setsErr(t, sh1, func() { p.PipeStderr(sh2.FuncCmd(catFunc)) })
p = gosh.NewPipeline(sh1.FuncCmd(echoFunc))
setsErr(t, sh1, func() { p.PipeCombinedOutput(sh2.FuncCmd(catFunc)) })
}
func TestPipelineClosedPipe(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
writeLoop, readLine := sh.FuncCmd(writeLoopFunc), sh.FuncCmd(readFunc)
// WriteLoop finishes because it gets a closed pipe write error after readLine
// finishes. Note that the closed pipe error is ignored.
p := gosh.NewPipeline(writeLoop, readLine)
eq(t, p.Stdout(), "")
ok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
p = p.Clone()
eq(t, p.Stdout(), "")
ok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
}
func TestPipelineCmdFailure(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
cat := sh.FuncCmd(catFunc)
exit1 := sh.FuncCmd(exitFunc, 1)
writeLoop := sh.FuncCmd(writeLoopFunc)
echoFoo := sh.FuncCmd(echoFunc)
echoFoo.Args = append(echoFoo.Args, "foo")
// Exit1 fails, and cat finishes with success since it sees an EOF.
p := gosh.NewPipeline(exit1.Clone(), cat.Clone())
setsErr(t, sh, p.Run)
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
p = p.Clone()
setsErr(t, sh, p.Run)
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
// Exit1 fails, and echoFoo finishes with success since it ignores stdin.
p = gosh.NewPipeline(exit1.Clone(), echoFoo.Clone())
setsErr(t, sh, p.Run)
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
p = p.Clone()
setsErr(t, sh, p.Run)
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
// Exit1 fails, causing writeLoop to finish and succeed.
p = gosh.NewPipeline(writeLoop.Clone(), exit1.Clone())
setsErr(t, sh, p.Run)
ok(t, p.Cmds()[0].Err)
nok(t, p.Cmds()[1].Err)
p = p.Clone()
setsErr(t, sh, p.Run)
ok(t, p.Cmds()[0].Err)
nok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
// Same tests, but allowing the exit error from exit1.
exit1.ExitErrorIsOk = true
// Exit1 fails, and cat finishes with success since it sees an EOF.
p = gosh.NewPipeline(exit1.Clone(), cat.Clone())
eq(t, p.Stdout(), "")
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
p = p.Clone()
eq(t, p.Stdout(), "")
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
// Exit1 fails, and echoFoo finishes with success since it ignores stdin.
p = gosh.NewPipeline(exit1.Clone(), echoFoo.Clone())
eq(t, p.Stdout(), "foo\n")
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
p = p.Clone()
eq(t, p.Stdout(), "foo\n")
nok(t, p.Cmds()[0].Err)
ok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
// Exit1 fails, causing writeLoop to finish and succeed.
p = gosh.NewPipeline(writeLoop.Clone(), exit1.Clone())
eq(t, p.Stdout(), "")
ok(t, p.Cmds()[0].Err)
nok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
p = p.Clone()
eq(t, p.Stdout(), "")
ok(t, p.Cmds()[0].Err)
nok(t, p.Cmds()[1].Err)
ok(t, sh.Err)
}
func TestPipelineSignal(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
for _, d := range []time.Duration{0, time.Hour} {
for _, s := range []os.Signal{os.Interrupt, os.Kill} {
fmt.Println(d, s)
p := gosh.NewPipeline(sh.FuncCmd(sleepFunc, d, 0), sh.FuncCmd(sleepFunc, d, 0))
p.Start()
p.Cmds()[0].AwaitVars("ready")
p.Cmds()[1].AwaitVars("ready")
// Wait for a bit to allow the zero-sleep commands to exit.
time.Sleep(100 * time.Millisecond)
p.Signal(s)
switch {
case s == os.Interrupt:
// Wait should succeed as long as the exit code was 0, regardless of
// whether the signal arrived or the processes had already exited.
p.Wait()
case d != 0:
// Note: We don't call Wait in the {d: 0, s: os.Kill} case because doing
// so makes the test flaky on slow systems.
setsErr(t, sh, func() { p.Wait() })
}
}
}
// Signal should fail if Wait has been called.
z := time.Duration(0)
p := gosh.NewPipeline(sh.FuncCmd(sleepFunc, z, 0), sh.FuncCmd(sleepFunc, z, 0))
p.Run()
setsErr(t, sh, func() { p.Signal(os.Interrupt) })
}
func TestPipelineTerminate(t *testing.T) {
sh := gosh.NewShell(t)
defer sh.Cleanup()
for _, d := range []time.Duration{0, time.Hour} {
for _, s := range []os.Signal{os.Interrupt, os.Kill} {
fmt.Println(d, s)
p := gosh.NewPipeline(sh.FuncCmd(sleepFunc, d, 0), sh.FuncCmd(sleepFunc, d, 0))
p.Start()
p.Cmds()[0].AwaitVars("ready")
p.Cmds()[1].AwaitVars("ready")
// Wait for a bit to allow the zero-sleep commands to exit.
time.Sleep(100 * time.Millisecond)
// Terminate should succeed regardless of the exit code, and regardless of
// whether the signal arrived or the processes had already exited.
p.Terminate(s)
}
}
// Terminate should fail if Wait has been called.
z := time.Duration(0)
p := gosh.NewPipeline(sh.FuncCmd(sleepFunc, z, 0), sh.FuncCmd(sleepFunc, z, 0))
p.Run()
setsErr(t, sh, func() { p.Terminate(os.Interrupt) })
}