forked from coder/wsep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localexec_test.go
150 lines (124 loc) · 3.33 KB
/
localexec_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
package wsep
import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"strings"
"sync"
"testing"
"time"
"cdr.dev/slog/sloggers/slogtest/assert"
)
func TestLocalExec(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
testExecer(ctx, t, LocalExecer{})
}
func testExecer(ctx context.Context, t *testing.T, execer Execer) {
process, err := execer.Start(ctx, Command{
Command: "pwd",
})
assert.Success(t, "start local cmd", err)
var (
stderr = process.Stderr()
stdout = process.Stdout()
wg sync.WaitGroup
)
wg.Add(1)
go func() {
defer wg.Done()
stdoutByt, err := ioutil.ReadAll(stdout)
assert.Success(t, "read stdout", err)
wd, err := os.Getwd()
assert.Success(t, "get real working dir", err)
assert.Equal(t, "stdout", wd, strings.TrimSuffix(string(stdoutByt), "\n"))
}()
wg.Add(1)
go func() {
defer wg.Done()
stderrByt, err := ioutil.ReadAll(stderr)
assert.Success(t, "read stderr", err)
assert.True(t, "len stderr", len(stderrByt) == 0)
}()
wg.Wait()
err = process.Wait()
assert.Success(t, "wait for process to complete", err)
}
func TestExitCode(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
process, err := LocalExecer{}.Start(ctx, Command{
Command: "sh",
Args: []string{"-c", `"fakecommand"`},
})
assert.Success(t, "start local cmd", err)
err = process.Wait()
exitErr, ok := err.(ExitError)
assert.True(t, "error is ExitError", ok)
assert.Equal(t, "exit error", exitErr.Code, 127)
}
func TestStdin(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var execer LocalExecer
process, err := execer.Start(ctx, Command{
Command: "cat",
Stdin: true,
})
assert.Success(t, "start command", err)
go func() {
stdin := process.Stdin()
defer stdin.Close()
_, err := io.Copy(stdin, strings.NewReader("testing value"))
assert.Success(t, "copy stdin", err)
}()
io.Copy(os.Stdout, process.Stdout())
err = process.Wait()
assert.Success(t, "process wait", err)
}
func TestStdinFail(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var execer LocalExecer
process, err := execer.Start(ctx, Command{
Command: "cat",
Stdin: false,
})
assert.Success(t, "start command", err)
go func() {
stdin := process.Stdin()
defer stdin.Close()
_, err := io.Copy(stdin, strings.NewReader("testing value"))
assert.Error(t, "copy stdin should fail", err)
}()
io.Copy(os.Stdout, process.Stdout())
err = process.Wait()
assert.Success(t, "process wait", err)
}
func TestStdoutVsStderr(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var (
execer LocalExecer
stdout bytes.Buffer
stderr bytes.Buffer
)
process, err := execer.Start(ctx, Command{
Command: "sh",
Args: []string{"-c", "echo stdout-message; echo 1>&2 stderr-message"},
Stdin: false,
TTY: false,
})
assert.Success(t, "start command", err)
go io.Copy(&stdout, process.Stdout())
go io.Copy(&stderr, process.Stderr())
err = process.Wait()
assert.Success(t, "wait for process to complete", err)
assert.Equal(t, "stdout", "stdout-message", strings.TrimSpace(stdout.String()))
assert.Equal(t, "stderr", "stderr-message", strings.TrimSpace(stderr.String()))
}