-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy_test.go
619 lines (497 loc) · 11.5 KB
/
proxy_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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package bintest_test
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"runtime"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/buildkite/bintest/v3"
"github.com/buildkite/bintest/v3/testutil"
"github.com/fortytw2/leaktest"
)
func TestProxyWithStdin(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
outBuf := &bytes.Buffer{}
cmd := exec.Command(proxy.Path)
cmd.Stdin = strings.NewReader("This is my stdin\n")
cmd.Stdout = outBuf
cmd.Stderr = os.Stderr
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
fmt.Fprintln(call.Stdout, "Copied to stdout")
_, err = io.Copy(call.Stdout, call.Stdin)
if err != nil {
t.Fatal(err)
}
call.Exit(0)
// wait for the command to finish
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
if expected := "Copied to stdout\nThis is my stdin\n"; outBuf.String() != expected {
t.Fatalf("Expected stdout to be %q, got %q", expected, outBuf.String())
}
}
func TestProxyWithStdout(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
outBuf := &bytes.Buffer{}
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Stdout = outBuf
cmd.Stderr = os.Stderr
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
fmt.Fprintln(call.Stdout, "Yup!")
call.Exit(0)
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
if expected := "Yup!\n"; outBuf.String() != expected {
t.Fatalf("Expected stdout to be %q, got %q", expected, outBuf.String())
}
}
func TestProxyWithStderr(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
errBuf := &bytes.Buffer{}
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Stderr = errBuf
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
fmt.Fprintln(call.Stderr, "Yup!")
call.Exit(0)
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
if expected := "Yup!\n"; errBuf.String() != expected {
t.Fatalf("Expected stderr to be %q, got %q", expected, errBuf.String())
}
}
func TestProxyWithStdoutAndStderr(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
if !reflect.DeepEqual(call.Args[1:], []string{"test", "arguments"}) {
t.Errorf("Unexpected args %#v", call.Args)
return
}
fmt.Fprintln(call.Stdout, "To stdout")
fmt.Fprintln(call.Stderr, "To stderr")
call.Exit(0)
// wait for the command to finish
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
if stdout.String() != "To stdout\n" {
t.Fatalf("Got unexpected output: %q", stdout.String())
}
if stderr.String() != "To stderr\n" {
t.Fatalf("Got unexpected output: %q", stderr.String())
}
}
func TestProxyWithNoOutput(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(proxy.Path, "test", "arguments")
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Exit(0)
// wait for the command to finish
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
}
func TestProxyWithLotsOfOutput(t *testing.T) {
defer leaktest.Check(t)()
var expected string
for i := 0; i < 10; i++ {
expected += strings.Repeat("llamas", 10)
}
actual := &bytes.Buffer{}
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Stdout = actual
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
n, err := io.Copy(call.Stdout, strings.NewReader(expected))
if err != nil {
t.Fatal(err)
} else if n != int64(len(expected)) {
t.Fatalf("Wrote %d bytes, expected %d", n, len(expected))
}
call.Exit(0)
// wait for the command to finish
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
if len(expected) != actual.Len() {
t.Fatalf("Wanted %d bytes of output, got %d", len(expected), actual.Len())
}
}
func TestProxyWithNonZeroExitCode(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Exit(24)
// wait for the command to finish
err = cmd.Wait()
if exiterr, ok := err.(*exec.ExitError); !ok {
t.Fatal("Should have gotten an error from wait")
} else {
if status, ok := exiterr.Sys().(syscall.WaitStatus); !ok {
t.Fatalf("Should have gotten an syscall.WaitStatus, got %v", exiterr)
} else if status.ExitStatus() != 24 {
t.Fatalf("Expected exit code %d, got %d", 24, status.ExitStatus())
}
}
}
func TestProxyCloseRemovesFile(t *testing.T) {
defer leaktest.Check(t)()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
if _, err = os.Stat(proxy.Path); os.IsNotExist(err) {
t.Fatalf("%s doesn't exist, but should: %v", proxy.Path, err)
}
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Exit(0)
err = cmd.Wait()
if err != nil {
t.Fatal(err)
}
err = proxy.Close()
if err != nil {
t.Fatal(err)
}
if _, err = os.Stat(proxy.Path); os.IsExist(err) {
t.Fatalf("%s still exists, but shouldn't", proxy.Path)
}
}
func TestProxyGetsWorkingDirectoryFromClient(t *testing.T) {
defer leaktest.Check(t)()
tempDir, err := os.MkdirTemp("", "proxy-wd-test")
if err != nil {
t.Fatal(err)
}
defer func() {
_ = os.RemoveAll(tempDir)
}()
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
cmd := exec.Command(proxy.Path, "test", "arguments")
cmd.Dir = tempDir
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
dir := strings.TrimPrefix(call.Dir, "/private")
if dir != tempDir {
t.Fatalf("Expected call dir to be %q, got %q", tempDir, dir)
}
call.Exit(0)
// wait for the command to finish
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
}
func TestProxyWithPassthroughWithNoStdin(t *testing.T) {
defer leaktest.Check(t)()
echoCmd := `/bin/echo`
if runtime.GOOS == `windows` {
// Question every life choice that has lead you to want to understand the below
// https://ss64.com/nt/syntax-esc.html
echoCmd = testutil.WriteBatchFile(t, "echo.bat", []string{
`@ECHO OFF`,
`Set _string=%~1`,
`Echo %_string%`,
})
}
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
outBuf := &bytes.Buffer{}
cmd := exec.Command(proxy.Path, `hello world`)
cmd.Stdout = outBuf
cmd.Stderr = os.Stderr
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Passthrough(echoCmd)
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
out := testutil.NormalizeNewlines(outBuf.String())
if expected := "hello world\n"; out != expected {
t.Fatalf("Expected stdout to be %q, got %q", expected, out)
}
}
func TestProxyWithPassthroughWithStdin(t *testing.T) {
defer leaktest.Check(t)()
catCmd := `/bin/cat`
if runtime.GOOS == `windows` {
catCmd = testutil.WriteBatchFile(t, "cat.bat", []string{
`@ECHO OFF`,
`FIND/V ""`,
})
}
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
inBuf := bytes.NewBufferString(testutil.NormalizeNewlines("hello world\n"))
outBuf := &bytes.Buffer{}
cmd := exec.Command(proxy.Path)
cmd.Stdin = inBuf
cmd.Stdout = outBuf
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Passthrough(catCmd)
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
out := testutil.NormalizeNewlines(outBuf.String())
if expected := "hello world\n"; out != expected {
t.Fatalf("Expected stdout to be %q, got %q", expected, out)
}
}
func TestProxyWithPassthroughWithFailingCommand(t *testing.T) {
defer leaktest.Check(t)()
falseCmd := `/usr/bin/false`
if runtime.GOOS == `windows` {
falseCmd = testutil.WriteBatchFile(t, "false.bat", []string{
`@ECHO OFF`,
`EXIT /B 1`,
})
}
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
cmd := exec.Command(proxy.Path)
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Passthrough(falseCmd)
if err = cmd.Wait(); err == nil {
t.Fatalf("Expected an error")
}
}
func TestProxyWithPassthroughWithTimeout(t *testing.T) {
defer leaktest.Check(t)()
if runtime.GOOS == `windows` {
t.Skipf("Not implemented for windows")
}
proxy, err := bintest.CompileProxy("test")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
b := &bytes.Buffer{}
cmd := exec.Command(proxy.Path, "100")
cmd.Stdout = b
cmd.Stderr = b
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.PassthroughWithTimeout(`/bin/sleep`, time.Millisecond*100)
if err = cmd.Wait(); err == nil {
t.Fatalf("Expected an error!")
}
}
func TestProxyCallingInParallel(t *testing.T) {
defer leaktest.Check(t)()
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
proxy, err := bintest.LinkTestBinaryAsProxy(fmt.Sprintf("test%d", i))
if err != nil {
t.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
t.Error(err)
}
}()
cmd := exec.Command(proxy.Path)
cmd.Env = proxy.Environ()
if err = cmd.Start(); err != nil {
t.Fatal(err)
}
call := <-proxy.Ch
call.Exit(0)
if err = cmd.Wait(); err != nil {
t.Fatal(err)
}
}(i)
}
wg.Wait()
}
func BenchmarkCreatingProxies(b *testing.B) {
for n := 0; n < b.N; n++ {
proxy, err := bintest.CompileProxy("test")
if err != nil {
b.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
b.Error(err)
}
}()
}
}
func BenchmarkCallingProxies(b *testing.B) {
proxy, err := bintest.CompileProxy("test")
if err != nil {
b.Fatal(err)
}
defer func() {
if err := proxy.Close(); err != nil {
b.Error(err)
}
}()
var expected string
for i := 0; i < 1000; i++ {
expected += strings.Repeat("llamas", 10)
}
b.SetBytes(int64(len(expected)))
b.ResetTimer()
for n := 0; n < b.N; n++ {
b.StopTimer()
cmd := exec.Command(proxy.Path, "test", "arguments")
if err = cmd.Start(); err != nil {
b.Fatal(err)
}
b.StartTimer()
call := <-proxy.Ch
_, _ = io.Copy(call.Stdout, strings.NewReader(expected))
call.Exit(0)
b.StopTimer()
if err = cmd.Wait(); err != nil {
b.Fatal(err)
}
}
}