-
Notifications
You must be signed in to change notification settings - Fork 10
/
goq_test.go
128 lines (103 loc) · 3.27 KB
/
goq_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
package main
import (
"fmt"
"net"
"os/exec"
"strings"
"testing"
cv "github.com/glycerine/goconvey/convey"
)
//func TestDemo(t *testing.T) {
// t.Logf("use the -test.v flag to see this output")
//}
// basic functionality:
// starting server should bind the supplied endpoint
func TestServerBinds(t *testing.T) {
// *** universal test cfg setup
skipbye := false
cfg := NewTestConfig(t)
defer cfg.ByeTestConfig(&skipbye)
// *** end universal test setup
ServerBindHelper(t, cfg.JservPort, cfg.JservPort, cfg)
}
// and the netstat validation should implode if we
// give the wrong endpoint:
func TestBadEndpointMeansServerEndpointTestShouldImplode(t *testing.T) {
// *** universal test cfg setup
skipbye := false
cfg := NewTestConfig(t)
defer cfg.ByeTestConfig(&skipbye)
// *** end universal test setup
cv.Convey("bad endpoints should be detected and rejected", t, func() {
cv.ShouldPanic(func() { panic("test the goconvey ShouldPanic function") })
cv.ShouldPanic(func() { ServerBindHelper(t, 2776, 2779, cfg) })
cv.ShouldPanic(func() { ServerBindHelper(t, 2777, 2778, cfg) })
cv.ShouldNotPanic(func() { ServerBindHelper(t, 2779, 2779, cfg) })
})
}
// make addr separate from cfg.JservAddr, so we
// can validate that the test detects a problem
// when they are different.
func ServerBindHelper(t *testing.T, port_use int, port_expect int, cfg *Config) {
cfg.JservPort = port_use
serv, err := NewJobServ(cfg)
if err != nil {
panic(err)
}
defer serv.Nnsock.Close()
defer CleanupOutdir(cfg)
defer CleanupServer(cfg, -1, serv, false, nil)
// but get the host from
servA := removeNetworkPrefix(serv.Addr)
vv("serv.Addr = '%v'", servA)
host, portStr, err := net.SplitHostPort(servA)
panicOn(err)
vv("host='%v', portStr='%v'; from='%v'", host, portStr, servA)
addr_expect := fmt.Sprintf("%v:%d", host, port_expect)
found := false
if cfg.UseQUIC {
found = UDP_PortIsListenedOn(t, addr_expect)
} else {
found = PortIsListenedOn(t, addr_expect)
}
if !found {
panic("no server at expected endpoint")
}
}
func PortIsListenedOn(t *testing.T, addr_expect string) bool {
// Equivalent to: $(netstat -nuptl| grep <ip:port> | grep LISTEN).
// Input addr_expect: is of the form "tcp://127.0.0.1:1777"
// and we discard everyting before the // to get ip:port.
netstatcmd, netstatargs := netstat_commandline()
out, err := exec.Command(netstatcmd, netstatargs...).Output()
if err != nil {
t.Fatal(err)
}
lines := strings.Split(string(out), "\n")
needle := addr_expect
//e.g. needle := "127.0.0.1:1776"
//t.Logf("using netstat -nuptl to look for %v LISTEN line.\n", needle)
found := false
for _, haystack := range lines {
if strings.Contains(haystack, needle) &&
strings.Contains(haystack, "LISTEN") {
found = true
//t.Logf("'netstat -nuptl | grep %v | grep LISTEN' found: %v", needle, haystack)
break
}
}
return found
}
func UDP_PortIsListenedOn(t *testing.T, addr_expect string) (bound bool) {
//host, portStr, err := net.SplitHostPort(addr_expect)
//panicOn(err)
// Attempt to create a UDP connection on the specified port
//addr := fmt.Sprintf(":%d", port)
conn, err := net.ListenPacket("udp", addr_expect)
vv("net.ListenPacket: conn ='%v', err='%v'", conn, err)
if err != nil {
return true
}
conn.Close()
return false // Port is not in use
}