-
Notifications
You must be signed in to change notification settings - Fork 148
/
process_test.go
172 lines (148 loc) · 3.38 KB
/
process_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
package tableflip
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"golang.org/x/sys/unix"
)
func TestFilesAreNonblocking(t *testing.T) {
pipe := func() (r, w *os.File) {
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
r.Close()
w.Close()
})
return r, w
}
// Set up our own blocking stdin since CI runs tests with stdin closed.
rStdin, _ := pipe()
rStdin.Fd()
r, _ := pipe()
if !isNonblock(t, r) {
t.Fatal("Read pipe is blocking")
}
proc, err := newOSProcess("cat", nil, []*os.File{rStdin, os.Stdout, os.Stderr, r}, nil)
if err != nil {
t.Fatal(err)
}
if err := proc.Signal(os.Kill); err != nil {
t.Fatal("Can't signal:", err)
}
var exitErr *exec.ExitError
if err := proc.Wait(); !errors.As(err, &exitErr) {
t.Fatalf("Wait should return an ExitError after sending os.Kill, have %T: %s", err, err)
}
if err := proc.Wait(); err == nil {
t.Fatal("Waiting a second time should return an error")
}
if !isNonblock(t, r) {
t.Fatal("Read pipe is blocking after newOSProcess")
}
}
func TestArgumentsArePassedCorrectly(t *testing.T) {
proc, err := newOSProcess("printf", []string{""}, []*os.File{os.Stdin, os.Stdout, os.Stderr}, nil)
if err != nil {
t.Fatal("Can't execute printf:", err)
}
// If the argument handling is wrong we'll call printf without any arguments.
// In that case printf exits non-zero.
if err = proc.Wait(); err != nil {
t.Fatal("printf exited non-zero:", err)
}
}
func isNonblock(tb testing.TB, file *os.File) (nonblocking bool) {
tb.Helper()
raw, err := file.SyscallConn()
if err != nil {
tb.Fatal("SyscallConn:", err)
}
err = raw.Control(func(fd uintptr) {
flags, err := unix.FcntlInt(fd, unix.F_GETFL, 0)
if err != nil {
tb.Fatal("IsNonblock:", err)
}
nonblocking = flags&unix.O_NONBLOCK > 0
})
if err != nil {
tb.Fatal("Control:", err)
}
return
}
type testProcess struct {
fds []*os.File
env env
signals chan os.Signal
sigErr chan error
waitErr chan error
quit chan struct{}
}
func newTestProcess(fds []*os.File, envstr []string) (*testProcess, error) {
environ := make(map[string]string)
for _, entry := range envstr {
parts := strings.SplitN(entry, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid env entry: %s", entry)
}
environ[parts[0]] = parts[1]
}
return &testProcess{
fds,
env{
newFile: func(fd uintptr, name string) *os.File {
return fds[fd]
},
getenv: func(key string) string {
return environ[key]
},
closeOnExec: func(int) {},
},
make(chan os.Signal, 1),
make(chan error),
make(chan error),
make(chan struct{}),
}, nil
}
func (tp *testProcess) Signal(sig os.Signal) error {
select {
case tp.signals <- sig:
return <-tp.sigErr
case <-tp.quit:
return nil
}
}
func (tp *testProcess) Wait() error {
select {
case err := <-tp.waitErr:
return err
case <-tp.quit:
return nil
}
}
func (tp *testProcess) String() string {
return fmt.Sprintf("tp=%p", tp)
}
func (tp *testProcess) exit(err error) {
select {
case tp.waitErr <- err:
close(tp.quit)
case <-tp.quit:
}
}
func (tp *testProcess) recvSignal(err error) os.Signal {
sig := <-tp.signals
tp.sigErr <- err
return sig
}
func (tp *testProcess) notify() (map[fileName]*file, <-chan error, error) {
parent, files, err := newParent(&tp.env)
if err != nil {
return nil, nil, err
}
return files, parent.result, parent.sendReady()
}