-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
158 lines (142 loc) · 5.69 KB
/
Copy pathexec_test.go
File metadata and controls
158 lines (142 loc) · 5.69 KB
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
package command_test
import (
"context"
"strings"
"testing"
"time"
gloo "github.com/gloo-foo/framework"
"github.com/gloo-foo/testable"
"github.com/gloo-foo/testable/assertion"
command "github.com/gloo-foo/cmd-xargs"
)
// recordRun is a fake Factory: instead of running a real process it emits a
// single line recording the exact argv it was handed, so a test can assert how
// xargs grouped input and built each invocation's argv.
func recordRun(argv []string) gloo.Command[[]byte, []byte] {
return gloo.FuncCommand[[]byte, []byte](func(ctx context.Context, _ gloo.Stream[[]byte]) gloo.Stream[[]byte] {
return gloo.Generate(ctx, func(_ context.Context, send func([]byte) bool, _ func(error)) {
send([]byte("run:" + strings.Join(argv, ",")))
})
})
}
func TestXargs_ExecTearsDownChildOnDownstreamStop(t *testing.T) {
stopped := make(chan struct{})
streaming := func(_ []string) gloo.Command[[]byte, []byte] {
return gloo.FuncCommand[[]byte, []byte](func(ctx context.Context, _ gloo.Stream[[]byte]) gloo.Stream[[]byte] {
return gloo.Generate(ctx, func(_ context.Context, send func([]byte) bool, _ func(error)) {
running := true
for running {
running = send([]byte("tick")) // emit until the consumer stops
}
close(stopped) // send returned false: the downstream stop reached the child
})
})
}
cmd := command.Xargs(gloo.File("x"), command.XargsExec(streaming))
out := cmd.Execute(context.Background(), gloo.StreamOf([]byte("a")))
<-out.Chan() // pull one item, so the child is actively streaming
out.Discard()
select {
case <-stopped:
case <-time.After(5 * time.Second):
t.Fatal("child kept running after the consumer stopped reading")
}
}
func TestXargs_ExecPropagatesChildError(t *testing.T) {
boom := func(_ []string) gloo.Command[[]byte, []byte] {
return gloo.FuncCommand[[]byte, []byte](func(ctx context.Context, _ gloo.Stream[[]byte]) gloo.Stream[[]byte] {
return gloo.Generate(ctx, func(_ context.Context, _ func([]byte) bool, sendErr func(error)) {
sendErr(gloo.Error("child failed"))
})
})
}
cmd := command.Xargs(gloo.File("x"), command.XargsExec(boom))
_, err := testable.TestLines(cmd, "a\n")
assertion.ErrorContains(t, err, "child failed")
}
func TestXargs_ExecForwardsEveryOutputLineInOrder(t *testing.T) {
echoEach := func(argv []string) gloo.Command[[]byte, []byte] {
return gloo.FuncCommand[[]byte, []byte](func(ctx context.Context, _ gloo.Stream[[]byte]) gloo.Stream[[]byte] {
return gloo.Generate(ctx, func(_ context.Context, send func([]byte) bool, _ func(error)) {
for _, a := range argv {
if !send([]byte(a)) {
return
}
}
})
})
}
cmd := command.Xargs(command.XargsMaxArgs(2), command.XargsExec(echoEach))
lines, err := testable.TestLines(cmd, "a b c\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"a", "b", "c"})
}
func TestXargs_ExecDefaultsToSubprocessForPositionalCommand(t *testing.T) {
cmd := command.Xargs(gloo.File("echo"), command.XargsMaxArgs(2))
lines, err := testable.TestLines(cmd, "a b c\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"a b", "c"})
}
func TestXargs_ExecPropagatesInputError(t *testing.T) {
errIn := gloo.Generate(context.Background(), func(_ context.Context, _ func([]byte) bool, sendErr func(error)) {
sendErr(gloo.Error("input broke"))
})
out := command.Xargs(gloo.File("x"), command.XargsExec(recordRun)).
Execute(context.Background(), errIn)
_, err := out.Collect()
assertion.ErrorContains(t, err, "input broke")
}
func TestXargs_ExecReplaceSkipsBlankLines(t *testing.T) {
cmd := command.Xargs(
gloo.File("echo"), gloo.File("[{}]"),
command.XargsReplace("{}"), command.XargsExec(recordRun),
)
lines, err := testable.TestLines(cmd, "a\n\nb\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,[a]", "run:echo,[b]"})
}
func TestXargs_ExecNullSkipsEmptyRuns(t *testing.T) {
cmd := command.Xargs(gloo.File("echo"), command.XargsNull(true), command.XargsExec(recordRun))
lines, err := testable.TestLines(cmd, "a\x00\x00b\x00")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,a,b"})
}
func TestXargs_ExecRunsOnceForAllFields(t *testing.T) {
cmd := command.Xargs(gloo.File("echo"), command.XargsExec(recordRun))
lines, err := testable.TestLines(cmd, "a b c\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,a,b,c"})
}
func TestXargs_ExecGroupsByMaxArgsAcrossInput(t *testing.T) {
cmd := command.Xargs(gloo.File("echo"), command.XargsMaxArgs(2), command.XargsExec(recordRun))
lines, err := testable.TestLines(cmd, "a b\nc d e\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,a,b", "run:echo,c,d", "run:echo,e"})
}
func TestXargs_ExecMaxProcsPreservesOutputOrder(t *testing.T) {
cmd := command.Xargs(
gloo.File("echo"),
command.XargsMaxArgs(1), command.XargsMaxProcs(4), command.XargsExec(recordRun),
)
lines, err := testable.TestLines(cmd, "a b c d\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,a", "run:echo,b", "run:echo,c", "run:echo,d"})
}
func TestXargs_ExecNullSplitsOnNULPreservingSpaces(t *testing.T) {
cmd := command.Xargs(
gloo.File("echo"),
command.XargsNull(true), command.XargsMaxArgs(2), command.XargsExec(recordRun),
)
lines, err := testable.TestLines(cmd, "a b\x00c d\x00e")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,a b,c d", "run:echo,e"})
}
func TestXargs_ExecReplaceSubstitutesTokenOncePerLine(t *testing.T) {
cmd := command.Xargs(
gloo.File("echo"), gloo.File("[{}]"),
command.XargsReplace("{}"), command.XargsExec(recordRun),
)
lines, err := testable.TestLines(cmd, "a b\nc\n")
assertion.NoError(t, err)
assertion.Lines(t, lines, []string{"run:echo,[a b]", "run:echo,[c]"})
}