-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathconsole_test.go
102 lines (88 loc) · 1.93 KB
/
console_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
//go:build linux || zos || freebsd
// +build linux zos freebsd
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package console
import (
"bytes"
"io"
"os"
"os/exec"
"sync"
"testing"
)
func TestWinSize(t *testing.T) {
c, _, err := NewPty()
if err != nil {
t.Fatal(err)
}
defer c.Close()
if err := c.Resize(WinSize{
Width: 11,
Height: 10,
}); err != nil {
t.Error(err)
return
}
size, err := c.Size()
if err != nil {
t.Error(err)
return
}
if size.Width != 11 {
t.Errorf("width should be 11 but received %d", size.Width)
}
if size.Height != 10 {
t.Errorf("height should be 10 but received %d", size.Height)
}
}
func TestConsolePty(t *testing.T) {
console, slavePath, err := NewPty()
if err != nil {
t.Fatal(err)
}
defer console.Close()
slave, err := os.OpenFile(slavePath, os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer slave.Close()
iteration := 10
var (
b bytes.Buffer
wg sync.WaitGroup
)
wg.Add(1)
go func() {
io.Copy(&b, console)
wg.Done()
}()
for i := 0; i < iteration; i++ {
cmd := exec.Command("sh", "-c", "printf test")
cmd.Stdin = slave
cmd.Stdout = slave
cmd.Stderr = slave
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}
slave.Close()
wg.Wait()
expectedOutput := ""
for i := 0; i < iteration; i++ {
expectedOutput += "test"
}
if out := b.String(); out != expectedOutput {
t.Errorf("unexpected output %q", out)
}
}