-
Notifications
You must be signed in to change notification settings - Fork 11
/
opener_test.go
120 lines (109 loc) · 2.16 KB
/
opener_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
package main
import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"path/filepath"
"testing"
)
func TestOpenerOptionsValidate(t *testing.T) {
tt := []struct {
test string
o *OpenerOptions
expectedErr string
}{
{
"unix domain socket can be used",
&OpenerOptions{
Network: "unix",
Address: filepath.Join("/", "tmp", fmt.Sprintf("%03d", rand.Intn(1000)), "opener.sock"),
},
"",
},
{
"tcp can be used",
&OpenerOptions{
Network: "tcp",
Address: "127.0.0.1:8888",
},
"",
},
{
"udp cannot be used",
&OpenerOptions{
Network: "udp",
Address: "127.0.0.1:8888",
},
"allowed network are: unix,tcp",
},
}
for _, tc := range tt {
t.Run(tc.test, func(t *testing.T) {
err := tc.o.Validate()
if err == nil {
if tc.expectedErr != "" {
t.Errorf("expect err nil, but actual %q", err)
}
} else {
if tc.expectedErr != err.Error() {
t.Errorf("expect err %q, but actual %q", tc.expectedErr, err)
}
}
})
}
}
func TestHandleConnection(t *testing.T) {
tt := []struct {
test string
openURLFunc func(string) (string, error)
data string
err error
}{
{
"Say nothing when successful",
func(line string) (string, error) {
return "pong\n", nil
},
"",
io.EOF,
},
{
"Sending back the logs when failure",
func(line string) (string, error) {
return "pong\n", errors.New("exit status 1")
},
"pong\n",
nil,
},
}
ln, _ := net.Listen("tcp", "127.0.0.1:0")
defer ln.Close()
for _, tc := range tt {
t.Run(tc.test, func(t *testing.T) {
openURL = tc.openURLFunc
go func() {
conn, _ := ln.Accept()
go handleConnection(conn, io.Discard)
}()
client, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
defer client.Close()
if _, err := client.Write([]byte("ping\n")); err != nil {
t.Fatal(err)
}
buf := make([]byte, 1024)
n, err := client.Read(buf)
data := string(buf[:n])
if tc.data != data {
t.Errorf("expect %q, but actual %q", tc.data, data)
}
if tc.err != err {
t.Errorf("expect %v, but actual %v", tc.err, err)
}
})
}
}