forked from presotto/go-mdns-sd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdns_test.go
207 lines (183 loc) · 5.46 KB
/
mdns_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mdns
import (
"errors"
"flag"
"fmt"
"log"
"net"
"reflect"
"testing"
"time"
)
var (
// common options
logLevelFlag = flag.Int("v", 0, "log level")
)
type instance struct {
host string
port uint16
txt []string
}
func createInstance(service string, inst instance) *MDNS {
s, err := NewMDNS(inst.host, "224.0.0.254:9999", "[FF02::FF]:9998", true, *logLevelFlag)
if err != nil {
log.Fatalf("can't translate address: %v", err)
}
s.AddService(service, inst.host, inst.port, inst.txt...)
return s
}
func checkDiscovered(host string, discovered []ServiceInstance, instances ...instance) error {
log.Printf("%s: instances %v %v", host, discovered, instances)
if len(instances) != len(discovered) {
return fmt.Errorf("%s found %d instances, but expected %d", host, len(instances), len(discovered))
}
// Make sure the answers are what we were hoping for.
foundsrv := make(map[int]bool)
foundtxt := make(map[int]bool)
for _, x := range discovered {
if len(x.SrvRRs) == 0 && len(x.TxtRRs) == 0 {
for i, inst := range instances {
if x.Name == inst.host && inst.port == 0 && len(inst.txt) == 0 {
foundsrv[i] = true
foundtxt[i] = true
}
}
continue
}
for _, rr := range x.SrvRRs {
found := false
for i, inst := range instances {
if x.Name == inst.host && rr.Target == hostFQDN(inst.host) && rr.Port == inst.port {
found = true
foundsrv[i] = true
}
}
if !found {
return fmt.Errorf("%s found unexpected SRV %s:%d", host, rr.Target, rr.Port)
}
}
for _, rr := range x.TxtRRs {
found := false
for i, inst := range instances {
if x.Name == inst.host && reflect.DeepEqual(rr.Txt, inst.txt) {
found = true
foundtxt[i] = true
}
}
if !found {
return fmt.Errorf("%s found unexpected TXT %v", host, rr.Txt)
}
}
}
for i, inst := range instances {
if !foundsrv[i] {
return fmt.Errorf("%s didn't find SRV %s:%d", host, hostFQDN(inst.host), inst.port)
}
if !foundtxt[i] {
return fmt.Errorf("%s didn't find TXT %s:%d", host, inst.txt, inst.port)
}
}
return nil
}
func checkIps(ips []net.IP) error {
log.Printf("%v", ips)
if len(ips) == 0 {
return errors.New("no ips found")
}
return nil
}
func watchFor(host string, c <-chan ServiceInstance, wants ...instance) error {
discovered := make([]ServiceInstance, 0, len(wants))
loop:
for len(discovered) < len(wants) {
select {
case inst := <-c:
discovered = append(discovered, inst)
case <-time.After(5 * time.Second):
break loop
}
}
return checkDiscovered(host+" watcher", discovered, wants...)
}
func watchForRemoved(host string, c <-chan ServiceInstance, wants ...instance) error {
removed := make([]instance, len(wants))
for i, want := range wants {
removed[i] = instance{host: want.host}
}
return watchFor(host, c, removed...)
}
func TestMdns(t *testing.T) {
instances := []instance{
{"system1", 666, []string{""}},
{"system2", 667, []string{"hoo haa", "haa hoo"}},
}
// Create two mdns instances.
s1 := createInstance("veyronns", instances[0])
w1, _ := s1.ServiceMemberWatch("veyronns")
if err := watchFor(instances[0].host, w1, instances[0]); err != nil {
t.Error(err)
}
s2 := createInstance("veyronns", instances[1])
// Multicast on each interface our desire to know about veyronns instances.
s1.SubscribeToService("veyronns")
s2.SubscribeToService("veyronns")
// Wait for all messages to get out and get reflected back.
time.Sleep(3 * time.Second)
// Make sure service discovery returns both instances.
discovered := s1.ServiceDiscovery("veyronns")
if err := checkDiscovered(instances[0].host, discovered, instances...); err != nil {
t.Error(err)
}
discovered = s2.ServiceDiscovery("veyronns")
if err := checkDiscovered(instances[1].host, discovered, instances...); err != nil {
t.Error(err)
}
// Look up addresses for both systems.
ips, _ := s1.ResolveAddress(instances[1].host)
if err := checkIps(ips); err != nil {
t.Error(err)
}
ips, _ = s2.ResolveAddress(instances[0].host)
if err := checkIps(ips); err != nil {
t.Error(err)
}
ips, _ = s2.ResolveAddress(instances[0].host)
if err := checkIps(ips); err != nil {
t.Error(err)
}
// Make sure the watcher learned about both systems.
if err := watchFor(instances[0].host, w1, instances[1]); err != nil {
t.Error(err)
}
// Make sure multiple watchers for the same service work as well.
w2, stopw2 := s1.ServiceMemberWatch("veyronns")
if err := watchFor(instances[0].host, w2, instances...); err != nil {
t.Error(err)
}
// Make sure the watcher closed the channel when stopped.
stopw2()
if _, ok := <-w2; ok {
t.Errorf("watcher didn't close the channel")
}
// Remove a service from one of the mdns instances.
s1.RemoveService("veyronns", instances[0].host, instances[0].port, instances[0].txt...)
// Wait for a goodbye message to get out and get reflected back.
time.Sleep(3 * time.Second)
// Make sure watcher learns the removed service.
if err := watchForRemoved(instances[0].host, w1, instances[0]); err != nil {
t.Error(err)
}
// Make sure service discovery doesn't return the removed service.
discovered = s1.ServiceDiscovery("veyronns")
if err := checkDiscovered(instances[0].host, discovered, instances[1]); err != nil {
t.Error(err)
}
discovered = s2.ServiceDiscovery("veyronns")
if err := checkDiscovered(instances[1].host, discovered, instances[1]); err != nil {
t.Error(err)
}
s1.Stop()
s2.Stop()
}