This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
panicwrap_test.go
364 lines (300 loc) · 7.33 KB
/
panicwrap_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package panicwrap
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"time"
)
var wrapRe = regexp.MustCompile(`wrapped: \d{3,4}`)
func helperProcess(s ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, s...)
env := []string{
"GO_WANT_HELPER_PROCESS=1",
}
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(env, os.Environ()...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd
}
// This is executed by `helperProcess` in a separate process in order to
// provider a proper sub-process environment to test some of our functionality.
func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
// Find the arguments to our helper, which are the arguments past
// the "--" in the command line.
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No command\n")
os.Exit(2)
}
panicHandler := func(s string) {
fmt.Fprintf(os.Stdout, "wrapped: %d", len(s))
os.Exit(0)
}
cmd, args := args[0], args[1:]
switch cmd {
case "no-panic-ordered-output":
exitStatus, err := BasicWrap(panicHandler)
if err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
if exitStatus < 0 {
for i := 0; i < 1000; i++ {
os.Stdout.Write([]byte("a"))
os.Stderr.Write([]byte("b"))
}
os.Exit(0)
}
os.Exit(exitStatus)
case "no-panic-output":
fmt.Fprint(os.Stdout, "i am output")
fmt.Fprint(os.Stderr, "stderr out")
os.Exit(0)
case "panic-boundary":
exitStatus, err := BasicWrap(panicHandler)
if err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
if exitStatus < 0 {
// Simulate a panic but on two boundaries...
fmt.Fprint(os.Stderr, "pan")
os.Stderr.Sync()
time.Sleep(100 * time.Millisecond)
fmt.Fprint(os.Stderr, "ic: oh crap")
os.Exit(2)
}
os.Exit(exitStatus)
case "panic-long":
exitStatus, err := BasicWrap(panicHandler)
if err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
if exitStatus < 0 {
// Make a fake panic by faking the header and adding a
// bunch of garbage.
fmt.Fprint(os.Stderr, "panic: foo\n\n")
for i := 0; i < 1024; i++ {
fmt.Fprint(os.Stderr, "foobarbaz")
}
// Sleep so that it dumps the previous data
//time.Sleep(1 * time.Millisecond)
time.Sleep(500 * time.Millisecond)
// Make a real panic
panic("I AM REAL!")
}
os.Exit(exitStatus)
case "panic":
hidePanic := false
if args[0] == "hide" {
hidePanic = true
}
config := &WrapConfig{
Handler: panicHandler,
HidePanic: hidePanic,
}
exitStatus, err := Wrap(config)
if err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
if exitStatus < 0 {
panic("uh oh")
}
os.Exit(exitStatus)
case "wrapped":
child := false
if len(args) > 0 && args[0] == "child" {
child = true
}
config := &WrapConfig{
Handler: panicHandler,
}
exitStatus, err := Wrap(config)
if err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
if exitStatus < 0 {
if child {
fmt.Printf("%v", Wrapped(nil))
}
os.Exit(0)
}
if !child {
fmt.Printf("%v", Wrapped(nil))
}
os.Exit(exitStatus)
case "recursive":
config := &WrapConfig{
Handler: panicHandler,
}
if len(args) > 0 && args[0] == "child" {
fmt.Printf("%v", Wrapped(config))
os.Exit(0)
}
exitCode, err := Wrap(config)
if err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
if Wrapped(nil) {
p := helperProcess("recursive", "child")
if err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
os.Exit(1)
}
os.Exit(0)
}
os.Exit(exitCode)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %q\n", cmd)
os.Exit(2)
}
}
func TestPanicWrap_Output(t *testing.T) {
stderr := new(bytes.Buffer)
stdout := new(bytes.Buffer)
p := helperProcess("no-panic-output")
p.Stdout = stdout
p.Stderr = stderr
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(stdout.String(), "i am output") {
t.Fatalf("didn't forward: %#v", stdout.String())
}
if !strings.Contains(stderr.String(), "stderr out") {
t.Fatalf("didn't forward: %#v", stderr.String())
}
}
/*
TODO(mitchellh): This property would be nice to gain.
func TestPanicWrap_Output_Order(t *testing.T) {
output := new(bytes.Buffer)
p := helperProcess("no-panic-ordered-output")
p.Stdout = output
p.Stderr = output
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
expectedBuf := new(bytes.Buffer)
for i := 0; i < 1000; i++ {
expectedBuf.WriteString("ab")
}
actual := strings.TrimSpace(output.String())
expected := strings.TrimSpace(expectedBuf.String())
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
}
*/
func TestPanicWrap_panicHide(t *testing.T) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
p := helperProcess("panic", "hide")
p.Stdout = stdout
p.Stderr = stderr
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if wrapRe.FindString(stdout.String()) == "" {
t.Fatalf("didn't wrap: %#v", stdout.String())
}
if strings.Contains(stderr.String(), "panic:") {
t.Fatalf("shouldn't have panic: %#v", stderr.String())
}
}
func TestPanicWrap_panicShow(t *testing.T) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
p := helperProcess("panic", "show")
p.Stdout = stdout
p.Stderr = stderr
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if wrapRe.FindString(stdout.String()) == "" {
t.Fatalf("didn't wrap: %#v", stdout.String())
}
if !strings.Contains(stderr.String(), "panic:") {
t.Fatalf("should have panic: %#v", stderr.String())
}
}
func TestPanicWrap_panicLong(t *testing.T) {
stdout := new(bytes.Buffer)
p := helperProcess("panic-long")
p.Stdout = stdout
p.Stderr = new(bytes.Buffer)
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if wrapRe.FindString(stdout.String()) == "" {
t.Fatalf("didn't wrap: %#v", stdout.String())
}
}
func TestPanicWrap_panicBoundary(t *testing.T) {
// TODO(mitchellh): panics are currently lost on boundaries
t.SkipNow()
stdout := new(bytes.Buffer)
p := helperProcess("panic-boundary")
p.Stdout = stdout
//p.Stderr = new(bytes.Buffer)
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if wrapRe.FindString(stdout.String()) == "" {
t.Fatalf("didn't wrap: %#v", stdout.String())
}
}
func TestPanicWrap_recursive(t *testing.T) {
stdout := new(bytes.Buffer)
p := helperProcess("recursive")
p.Stdout = stdout
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(stdout.String(), "false") {
t.Fatalf("Wrapped false positive: %#v", stdout.String())
}
}
func TestWrapped(t *testing.T) {
stdout := new(bytes.Buffer)
p := helperProcess("wrapped", "child")
p.Stdout = stdout
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(stdout.String(), "true") {
t.Fatalf("bad: %#v", stdout.String())
}
}
func TestWrapped_parent(t *testing.T) {
stdout := new(bytes.Buffer)
p := helperProcess("wrapped")
p.Stdout = stdout
if err := p.Run(); err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(stdout.String(), "false") {
t.Fatalf("bad: %#v", stdout.String())
}
}