forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnschecker_test.go
142 lines (129 loc) · 3.67 KB
/
dnschecker_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
package checkup
import (
"net"
"testing"
"time"
)
func TestDNSChecker(t *testing.T) {
// Listen on localhost, random port
srv, err := net.Listen("tcp", "localhost:8382")
if err != nil {
t.Errorf("Couldn't start TCP test server with error: %v", err)
}
defer srv.Close()
// Accept a future connection
go func() {
for {
conn, err := srv.Accept()
if err != nil {
break
}
conn.Close()
}
}()
// Should know the host:port by now
endpt := srv.Addr().String()
testName := "TestDNS"
hc := DNSChecker{Name: testName, URL: endpt, Attempts: 2}
// Try an up server
result, err := hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Title, testName; got != want {
t.Errorf("Expected result.Title='%s', got '%s'", want, got)
}
if got, want := result.Endpoint, endpt; got != want {
t.Errorf("Expected result.Endpoint='%s', got '%s'", want, got)
}
if got, want := result.Down, false; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
if got, want := result.Degraded, false; got != want {
t.Errorf("Expected result.Degraded=%v, got %v", want, got)
}
if got, want := result.Healthy, true; got != want {
t.Errorf("Expected result.Healthy=%v, got %v", want, got)
}
if got, want := len(result.Times), hc.Attempts; got != want {
t.Errorf("Expected %d attempts, got %d", want, got)
}
ts := time.Unix(0, result.Timestamp)
if time.Since(ts) > 5*time.Second {
t.Errorf("Expected timestamp to be recent, got %s", ts)
}
// Try various different down criteria
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Healthy, true; got != want {
t.Errorf("Expected result.Healthy=%v, got %v", want, got)
}
hc.ThresholdRTT = 1 * time.Nanosecond
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Degraded, true; got != want {
t.Errorf("Expected result.Degraded=%v, got %v", want, got)
}
hc.ThresholdRTT = 0
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Down, false; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
// Try when the server is not even online
srv.Close()
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := len(result.Times), hc.Attempts; got != want {
t.Errorf("Expected %d attempts, got %d", want, got)
}
if got, want := result.Down, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
if got, want := result.Healthy, false; got != want {
t.Errorf("Expected result.Healthy=%v, got %v", want, got)
}
}
func TestDNSCheckerWithAgressiveTimeout(t *testing.T) {
// Listen on localhost, random port
srv, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Errorf("Couldn't start TCP test server with error: %v", err)
}
defer srv.Close()
// Accept a future connection
go func() {
for {
conn, err := srv.Accept()
if err != nil {
break
}
conn.Close()
}
}()
// Should know the host:port by now
endpt := srv.Addr().String()
testName := "TestTCP"
hc := DNSChecker{Name: testName, URL: endpt, Attempts: 2, Timeout: 1 * time.Nanosecond}
result, err := hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := len(result.Times), hc.Attempts; got != want {
t.Errorf("Expected %d attempts, got %d", want, got)
}
if got, want := result.Down, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
if got, want := result.Healthy, false; got != want {
t.Errorf("Expected result.Healthy=%v, got %v", want, got)
}
}