Skip to content

Commit 603c650

Browse files
committed
Add backend tests
1 parent f66f0d0 commit 603c650

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package localcommand
2+
3+
import (
4+
"bytes"
5+
"reflect"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestNewFactory(t *testing.T) {
11+
factory, err := NewFactory("/bin/false", []string{}, &Options{CloseSignal: 123, CloseTimeout: 321})
12+
if err != nil {
13+
t.Errorf("NewFactory() returned error")
14+
return
15+
}
16+
if factory.command != "/bin/false" {
17+
t.Errorf("factory.command = %v, expected %v", factory.command, "/bin/false")
18+
}
19+
if !reflect.DeepEqual(factory.argv, []string{}) {
20+
t.Errorf("factory.argv = %v, expected %v", factory.argv, []string{})
21+
}
22+
if !reflect.DeepEqual(factory.options, &Options{CloseSignal: 123, CloseTimeout: 321}) {
23+
t.Errorf("factory.options = %v, expected %v", factory.options, &Options{})
24+
}
25+
26+
slave, _ := factory.New(nil)
27+
lcmd := slave.(*LocalCommand)
28+
if lcmd.closeSignal != 123 {
29+
t.Errorf("lcmd.closeSignal = %v, expected %v", lcmd.closeSignal, 123)
30+
}
31+
if lcmd.closeTimeout != time.Second*321 {
32+
t.Errorf("lcmd.closeTimeout = %v, expected %v", lcmd.closeTimeout, time.Second*321)
33+
}
34+
}
35+
36+
func TestFactoryNew(t *testing.T) {
37+
factory, err := NewFactory("/bin/cat", []string{}, &Options{})
38+
if err != nil {
39+
t.Errorf("NewFactory() returned error")
40+
return
41+
}
42+
43+
slave, err := factory.New(nil)
44+
if err != nil {
45+
t.Errorf("factory.New() returned error")
46+
return
47+
}
48+
49+
writeBuf := []byte("foobar\n")
50+
n, err := slave.Write(writeBuf)
51+
if err != nil {
52+
t.Errorf("write() failed: %v", err)
53+
return
54+
}
55+
if n != 7 {
56+
t.Errorf("Unexpected write length. n = %d, expected n = %d", n, 7)
57+
return
58+
}
59+
60+
// Local echo is on, so we get the output twice:
61+
// Once because we're "typing" it, and once more
62+
// repeated back to us by `cat`. Also, \r\n
63+
// because we're a terminal.
64+
expectedBuf := []byte("foobar\r\nfoobar\r\n")
65+
readBuf := make([]byte, 1024)
66+
var totalRead int
67+
for totalRead < 16 {
68+
n, err = slave.Read(readBuf[totalRead:])
69+
if err != nil {
70+
t.Errorf("read() failed: %v", err)
71+
return
72+
}
73+
totalRead += n
74+
}
75+
if totalRead != 16 {
76+
t.Errorf("Unexpected read length. totalRead = %d, expected totalRead = %d", totalRead, 16)
77+
return
78+
}
79+
if !bytes.Equal(readBuf[:totalRead], expectedBuf) {
80+
t.Errorf("unexpected output from slave: got %v, expected %v", readBuf[:totalRead], expectedBuf)
81+
}
82+
err = slave.Close()
83+
if err != nil {
84+
t.Errorf("close() failed: %v", err)
85+
return
86+
}
87+
88+
}

0 commit comments

Comments
 (0)