This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathstream_test.go
91 lines (80 loc) · 1.88 KB
/
stream_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
// Copyright (c) 2020 Baidu Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package containerdshim
import (
"context"
"io"
"io/ioutil"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/containerd/fifo"
"github.com/stretchr/testify/assert"
)
func TestNewTtyIOFifoReopen(t *testing.T) {
var outr io.ReadWriteCloser
var errr io.ReadWriteCloser
var tty *ttyIO
assert := assert.New(t)
ctx := context.TODO()
fifoPath, err := ioutil.TempDir(testDir, "fifo-path-")
assert.NoError(err)
stdout := filepath.Join(fifoPath, "stdout")
stderr := filepath.Join(fifoPath, "stderr")
createReadFifo := func(f string) io.ReadWriteCloser {
rf, err := fifo.OpenFifo(ctx, f, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
if err != nil {
t.Fatal(err)
}
return rf
}
outr = createReadFifo(stdout)
defer outr.Close()
errr = createReadFifo(stderr)
defer errr.Close()
tty, err = newTtyIO(ctx, "", stdout, stderr, false)
assert.NoError(err)
defer tty.close()
testBytes := []byte("T")
checkFifoWrite := func(w io.Writer) {
_, err = w.Write(testBytes)
assert.NoError(err)
}
checkFifoRead := func(r io.Reader) {
var err error
buf := make([]byte, 1)
done := make(chan struct{})
timer := time.NewTimer(2 * time.Second)
go func() {
_, err = r.Read(buf)
close(done)
}()
select {
case <-done:
assert.NoError(err)
assert.Equal(buf, testBytes)
case <-timer.C:
t.Fatal("read fifo timeout")
}
}
checkFifoWrite(tty.Stdout)
checkFifoRead(outr)
checkFifoWrite(tty.Stderr)
checkFifoRead(errr)
err = outr.Close()
assert.NoError(err)
err = errr.Close()
assert.NoError(err)
// Make sure that writing to tty fifo will not get `EPIPE`
// when the read side is closed
checkFifoWrite(tty.Stdout)
checkFifoWrite(tty.Stderr)
// Reopen the fifo
outr = createReadFifo(stdout)
errr = createReadFifo(stderr)
checkFifoRead(outr)
checkFifoRead(errr)
}