-
Notifications
You must be signed in to change notification settings - Fork 12
/
healthcheck_test.go
87 lines (78 loc) · 2 KB
/
healthcheck_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
package dnsredir
import (
"fmt"
"github.com/miekg/dns"
"strings"
"testing"
"time"
)
const (
defaultProto = "" // Alias to UDP
udpProto = "udp"
tcpProto = "tcp"
tcpTlsProto = "tcp-tls"
ms = time.Millisecond
s = time.Second
)
type testCaseSend struct {
addr string
proto string
timeout time.Duration
shouldErr bool
expectedErr string
}
func (t testCaseSend) String() string {
return fmt.Sprintf("{%T addr=%v proto=%v timeout=%v shouldErr=%v expectedErr=%q}",
t, t.addr, t.proto, t.timeout, t.shouldErr, t.expectedErr)
}
// Return true if test passed, false otherwise
func (t *testCaseSend) Pass(err error) bool {
// Empty expected error isn't allow
if t.shouldErr == (len(t.expectedErr) == 0) {
panic(fmt.Sprintf("Bad test case %v", t))
}
pass := true
if t.shouldErr == (err != nil) {
if err != nil {
s := strings.ToLower(err.Error())
t := strings.ToLower(t.expectedErr)
if !strings.Contains(s, t) {
pass = false
}
}
} else {
pass = false
}
return pass
}
func TestSend(t *testing.T) {
tests := []testCaseSend{
// Positive
{"8.8.8.8:53", udpProto, 1 * s, false, ""},
{"8.8.4.4:53", tcpProto, 1 * s, false, ""},
{"8.8.8.8:853", tcpTlsProto, 1 * s, false, ""},
{"1.1.1.1:53", defaultProto, 1 * s, false, ""},
{"9.9.9.9:53", tcpProto, 1 * s, false, ""},
// Negative
{"1.2.3.4", defaultProto, 500 * ms, true, "missing port in address"},
{"127.0.0.1:853", tcpTlsProto, 100 * ms, true, "connection refused"},
// DNSPod doesn't support DNS over TCP/TLS
{"119.29.29.29:53", tcpProto, 500 * ms, true, "connection refused"},
{"119.29.29.29:853", tcpTlsProto, 1 * s, true, "i/o timeout"},
{"114.114.114.114", "foobar", 1 * s, true, "unknown network "},
}
for i, test := range tests {
uh := &UpstreamHost{
addr: test.addr,
c: &dns.Client{
Net: test.proto,
Timeout: test.timeout,
},
transport: newTransport(),
}
err := uh.Check()
if !test.Pass(err) {
t.Errorf("Test#%v failed %v vs err: %v", i, test, err)
}
}
}