-
Notifications
You must be signed in to change notification settings - Fork 2
/
discovery_test.go
289 lines (256 loc) · 6.07 KB
/
discovery_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package discovery
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/miekg/dns"
"github.com/wish/discovery/resolver"
)
func RunLocalUDPServer(laddr string) (*dns.Server, string, error) {
pc, err := net.ListenPacket("udp", laddr)
if err != nil {
return nil, "", err
}
server := &dns.Server{PacketConn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour}
waitLock := sync.Mutex{}
waitLock.Lock()
server.NotifyStartedFunc = waitLock.Unlock
go func() {
server.ActivateAndServe()
pc.Close()
}()
waitLock.Lock()
return server, pc.LocalAddr().String(), nil
}
func TestDiscovery(t *testing.T) {
tests := []struct {
q string
r []ServiceAddress
err bool
}{
// Query with IP, we expect that same IP back
{
q: "127.0.0.1",
r: []ServiceAddress{ServiceAddress{
Name: "127.0.0.1",
IP: net.ParseIP("127.0.0.1"),
isStatic: true,
}},
},
// Query with IP+port, expect back IP+port
{
q: "127.0.0.1:1234",
r: []ServiceAddress{ServiceAddress{
Name: "127.0.0.1",
IP: net.ParseIP("127.0.0.1"),
Port: 1234,
isStatic: true,
}},
},
// Just a name, no port
{
q: "example.com",
r: []ServiceAddress{ServiceAddress{
Name: "1.2.3.4",
IP: net.ParseIP("1.2.3.4"),
}},
},
// SRV response
{
q: "srv.com",
r: []ServiceAddress{ServiceAddress{
Name: "example.com",
IP: net.ParseIP("1.2.3.4"),
Port: 5060,
}},
},
// SRV with a port-- since SRV exists the port won't match
{
q: "srv.com:80",
r: []ServiceAddress{ServiceAddress{
Name: "example.com",
IP: net.ParseIP("1.2.3.4"),
Port: 5060,
}},
},
// ipv6 destination
{
q: "v6.com",
r: []ServiceAddress{ServiceAddress{
Name: "::102:304",
IP: net.ParseIP("::1.2.3.4"),
}},
},
// SRV ipv6 response
{
q: "srvV6.com",
r: []ServiceAddress{ServiceAddress{
Name: "v6.com",
IP: net.ParseIP("::1.2.3.4"),
Port: 5060,
}},
},
// SRV ipv6 a port-- since SRV exists the port won't match
{
q: "srvV6.com:80",
r: []ServiceAddress{ServiceAddress{
Name: "v6.com",
IP: net.ParseIP("::1.2.3.4"),
Port: 5060,
}},
},
}
zones := map[string]string{
"example.com.": "example.com. 1 IN A 1.2.3.4",
"v6.com.": "example.com. 1 IN AAAA ::1.2.3.4",
"srv.com.": "srv.com. 1 IN SRV 0 0 5060 example.com.\nsrv.com. 600 IN A 1.2.3.4",
"srvV6.com.": "srv.com. 1 IN SRV 0 0 5060 v6.com.\nsrv.com. 600 IN AAAA ::1.2.3.4",
}
// Start DNS server
dns.HandleFunc(".", func(w dns.ResponseWriter, req *dns.Msg) {
msg := dns.Msg{}
msg.SetReply(req)
msg.Authoritative = true
domain := msg.Question[0].Name
zoneStr, ok := zones[domain]
if ok {
parser := dns.NewZoneParser(strings.NewReader(zoneStr), domain, "")
for {
rr, ok := parser.Next()
if !ok {
break
}
if rr.Header().Rrtype == req.Question[0].Qtype {
msg.Answer = append(msg.Answer, rr)
}
}
}
w.WriteMsg(&msg)
})
defer dns.HandleRemove(".")
s, addrstr, err := RunLocalUDPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
_, port, err := net.SplitHostPort(addrstr)
if err != nil {
t.Fatal(err)
}
r := resolver.NewResolverFromConfig(&dns.ClientConfig{
Servers: []string{"127.0.0.1"},
Port: port,
})
d := discovery{c: DefaultConfig, r: r}
ctx := context.TODO()
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
addrs, err := d.GetServiceAddresses(ctx, test.q)
if (err != nil) != test.err {
t.Fatalf("Wrong error, err=%v expectedErr=%v", err, test.err)
}
// Hack to avoid creating separate comparator
for x, a := range addrs {
test.r[x].expiresAt = a.expiresAt
}
if !ServiceAddresses(test.r).Equal(addrs) {
t.Fatalf("Mismatch in addrs \nexpected=%v \nactual=%v", test.r, addrs)
}
})
}
}
func TestDiscoverySubscribe(t *testing.T) {
zones := map[string]string{
"example.com.": "example.com. 1 IN A 1.2.3.4",
}
// Start DNS server
dns.HandleFunc(".", func(w dns.ResponseWriter, req *dns.Msg) {
msg := dns.Msg{}
msg.SetReply(req)
msg.Authoritative = true
domain := msg.Question[0].Name
zoneStr, ok := zones[domain]
if ok {
parser := dns.NewZoneParser(strings.NewReader(zoneStr), domain, "")
for {
rr, ok := parser.Next()
if !ok {
break
}
if rr.Header().Rrtype == req.Question[0].Qtype {
msg.Answer = append(msg.Answer, rr)
}
}
}
w.WriteMsg(&msg)
})
defer dns.HandleRemove(".")
s, addrstr, err := RunLocalUDPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
_, port, err := net.SplitHostPort(addrstr)
if err != nil {
t.Fatal(err)
}
r := resolver.NewResolverFromConfig(&dns.ClientConfig{
Servers: []string{"127.0.0.1"},
Port: port,
})
d := discovery{c: DefaultConfig, r: r}
ctx := context.TODO()
var cbAddrs ServiceAddresses
var cbErr error
cbCh := make(chan struct{})
cb := func(_ context.Context, addrs ServiceAddresses) error {
select {
case cbCh <- struct{}{}:
default:
}
cbAddrs = addrs
return cbErr
}
cbWait := func() {
select {
case <-cbCh:
case <-time.After(time.Second * 2):
t.Fatalf("CB failed")
}
}
// Do a subscribe
if err := d.SubscribeServiceAddresses(ctx, "example.com", cb); err != nil {
t.Fatalf("Error doing initial subscribe: %v", err)
}
// wait a second, ensure that we got another
cbWait()
// set an error, ensure that we get some more retires
cbErr = fmt.Errorf("some error")
prevAddrs := cbAddrs
for i := 0; i < 3; i++ {
cbWait()
}
// Clear error ensure that we get an update
cbErr = nil
cbWait()
if cbAddrs.Equal(prevAddrs) {
t.Fatalf("no update!")
}
// test that an update is seen immediately
prevAddrs = cbAddrs
cbWait()
// Update immediately
zones["example.com."] = "example.com. 1 IN A 1.2.3.4\nexample.com. 1 IN A 1.2.3.5"
// Wait, and see if we get updates
for i := 0; i < 3; i++ {
cbWait()
if len(prevAddrs) == len(cbAddrs) {
t.Fatalf("%d callback missing update!", i)
}
}
}