/
siphon_test.go
199 lines (163 loc) · 4.55 KB
/
siphon_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
package main
import (
"polydawn.net/siphon"
"github.com/kr/pty"
"bytes"
"io"
"os/exec"
"testing"
"time"
"github.com/coocood/assrt"
)
/**
Expect:
- all of the input to come back out, because terminals default to echo mode.
- then the grep'd string should come out, because the command recieved it and matched.
- the grep'd string should come out surrounded by the escape codes for color, since grep's auto mode should detect that we're in a terminal.
*/
func TestPtySanity(t *testing.T) {
assert := assrt.NewAssert(t)
c := exec.Command("grep", "--color=auto", "bar")
f, err := pty.Start(c)
if err != nil {
t.Fatal(err)
}
go func() {
f.Write([]byte("foo\nbar\nbaz\n"))
/*
All of the input must be written in a single call to prevent this test from occationally failing nondeterministically, because:
grep operates stream-wise and will start printing output before it has all of its input,
and 3/4ths of the output lines are actually from the terminal operating in echo mode on the same channel.
So there's actually a race between the terminal itself (somewhere down in kernel land I suppose?) and the output of grep.
*/
f.Write([]byte{4}) // EOT
}()
outBuffer := new(bytes.Buffer)
io.Copy(outBuffer, f)
out := string(outBuffer.Bytes())
expected := // I have no idea where the CR characters come from.
"foo\r\n"+
"bar\r\n"+
"baz\r\n"+
"[01;31m[Kbar[m[K\r\n";
assert.Equal(
expected,
out,
)
}
func TestGrep(t *testing.T) {
assert := assrt.NewAssert(t)
cmd := exec.Command("grep", "--color=auto", "bar")
host := siphon.NewHost(cmd, siphon.NewInternalAddr())
host.Start()
go func() {
stdin := host.StdinPipe()
stdin.Write([]byte("foo\nbar\nbaz\n"))
stdin.Write([]byte{4}) // EOT
}()
outBuffer := new(bytes.Buffer)
io.Copy(outBuffer, host.StdoutPipe())
out := string(outBuffer.Bytes())
expected := // I have no idea where the CR characters come from.
"foo\r\n"+
"bar\r\n"+
"baz\r\n"+
"[01;31m[Kbar[m[K\r\n";
assert.Equal(
expected,
out,
)
}
/**
Getting any answer from the tty command at all is pretty good news, since `go test` doesn't let the shell's tty come through.
*/
func TestActuallyNewTty(t *testing.T) {
assert := assrt.NewAssert(t)
cmd := exec.Command("tty")
host := siphon.NewHost(cmd, siphon.NewInternalAddr())
outBuffer := new(bytes.Buffer)
hostOut := host.StdoutPipe()
host.Start()
io.Copy(outBuffer, hostOut)
innerTty := string(outBuffer.Bytes())
assert.NotEqual(
"", // what exactly you get varies on the current state of your machine, but something like /dev/pts/12 is reasonable.
innerTty,
)
}
func TestUnixSocket(t *testing.T) {
assert := assrt.NewAssert(t)
addr := siphon.NewAddr("test", "unix", "test.sock")
cmd := exec.Command("cat", "-")
host := siphon.NewHost(cmd, addr)
host.Serve(); defer host.UnServe()
client := siphon.NewClient(addr)
client.Connect()
//client.Attach() // you can't do this in a test. there's no tty.
//FIXME: there's really no guarantee that the host finished processing the client connect request by now
host.Start()
go func() {
stdin := client.Stdin()
stdin.Write([]byte("foo\nbar\nbaz\n"))
stdin.Write([]byte{4}) // EOT
}()
outBuffer := new(bytes.Buffer)
io.Copy(outBuffer, client.Stdout())
out := string(outBuffer.Bytes())
expected :=
"foo\r\n"+
"bar\r\n"+
"baz\r\n";
expected = expected + expected
assert.Equal(
expected,
out,
)
}
func TestDoubleClient(t *testing.T) {
assert := assrt.NewAssert(t)
addr := siphon.NewAddr("test", "unix", "test.sock")
cmd := exec.Command("cat", "-")
host := siphon.NewHost(cmd, addr)
host.Serve(); defer host.UnServe()
client1 := siphon.NewClient(addr)
client1.Connect()
client2 := siphon.NewClient(addr)
client2.Connect()
host.Start()
go func() {
// timed pauses are a hack required to make sure the host side flushes the echos in an order we can test.
client1.Stdin().Write([]byte("foo\n"))
time.Sleep(50*time.Millisecond)
client2.Stdin().Write([]byte("bar\n"))
time.Sleep(50*time.Millisecond)
client1.Stdin().Write([]byte("baz\n"))
time.Sleep(50*time.Millisecond)
client2.Stdin().Write([]byte{4}) // EOT
}()
outBuffer1 := new(bytes.Buffer)
io.Copy(outBuffer1, client1.Stdout())
out1 := string(outBuffer1.Bytes())
outBuffer2 := new(bytes.Buffer)
io.Copy(outBuffer2, client2.Stdout())
out2 := string(outBuffer2.Bytes())
expected :=
"foo\r\nfoo\r\n"+
"bar\r\nbar\r\n"+
"baz\r\nbaz\r\n";
assert.Equal(
expected,
out1,
)
assert.Equal(
expected,
out2,
)
}
func Template(t *testing.T) {
assert := assrt.NewAssert(t)
assert.Equal(
"yes",
"no",
)
}