forked from linkedin/go-zk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnshostprovider_test.go
242 lines (211 loc) · 6.81 KB
/
dnshostprovider_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
package zk
import (
"errors"
"fmt"
"log"
"testing"
"time"
)
// localhostLookupHost is a test replacement for net.LookupHost that
// always returns 127.0.0.1
func localhostLookupHost(host string) ([]string, error) {
return []string{"127.0.0.1"}, nil
}
// TestDNSHostProviderCreate is just like TestCreate, but with an
// overridden HostProvider that ignores the provided hostname.
func TestDNSHostProviderCreate(t *testing.T) {
ts, err := StartTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
port := ts.Servers[0].Port
server := fmt.Sprintf("foo.example.com:%d", port)
hostProvider := &DNSHostProvider{lookupHost: localhostLookupHost}
zk, _, err := Connect([]string{server}, time.Second*15, WithHostProvider(hostProvider))
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk.Close()
path := "/gozk-test"
if err := zk.Delete(path, -1); err != nil && !errors.Is(err, ErrNoNode) {
t.Fatalf("Delete returned error: %+v", err)
}
if p, err := zk.Create(path, []byte{1, 2, 3, 4}, 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create returned error: %+v", err)
} else if p != path {
t.Fatalf("Create returned different path '%s' != '%s'", p, path)
}
if data, stat, err := zk.Get(path); err != nil {
t.Fatalf("Get returned error: %+v", err)
} else if stat == nil {
t.Fatal("Get returned nil stat")
} else if len(data) < 4 {
t.Fatal("Get returned wrong size data")
}
}
// localHostPortsFacade wraps a HostProvider, remapping the
// address/port combinations it returns to "localhost:$PORT" where
// $PORT is chosen from the provided ports.
type localHostPortsFacade struct {
inner HostProvider // The wrapped HostProvider
ports []int // The provided list of ports
nextPort int // The next port to use
mapped map[string]string // Already-mapped address/port combinations
}
func newLocalHostPortsFacade(inner HostProvider, ports []int) *localHostPortsFacade {
return &localHostPortsFacade{
inner: inner,
ports: ports,
mapped: make(map[string]string),
}
}
func (lhpf *localHostPortsFacade) Connected() { lhpf.inner.Connected() }
func (lhpf *localHostPortsFacade) Init(servers []string) error { return lhpf.inner.Init(servers) }
func (lhpf *localHostPortsFacade) Next() (string, bool) {
server, retryStart := lhpf.inner.Next()
// If we've already set up a mapping for that server, just return it.
if localMapping := lhpf.mapped[server]; localMapping != "" {
return localMapping, retryStart
}
if lhpf.nextPort == len(lhpf.ports) {
log.Fatalf("localHostPortsFacade out of ports to assign to %q; current config: %q", server, lhpf.mapped)
}
localMapping := fmt.Sprintf("localhost:%d", lhpf.ports[lhpf.nextPort])
lhpf.mapped[server] = localMapping
lhpf.nextPort++
return localMapping, retryStart
}
var _ HostProvider = &localHostPortsFacade{}
// TestDNSHostProviderReconnect tests that the zk.Conn correctly
// reconnects when the Zookeeper instance it's connected to
// restarts. It wraps the DNSHostProvider in a lightweight facade that
// remaps addresses to localhost:$PORT combinations corresponding to
// the test ZooKeeper instances.
func TestDNSHostProviderReconnect(t *testing.T) {
ts, err := StartTestCluster(t, 3, nil, logWriter{t: t, p: "[ZKERR] "})
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
innerHp := &DNSHostProvider{lookupHost: func(host string) ([]string, error) {
return []string{"192.0.2.1", "192.0.2.2", "192.0.2.3"}, nil
}}
ports := make([]int, 0, len(ts.Servers))
for _, server := range ts.Servers {
ports = append(ports, server.Port)
}
hp := newLocalHostPortsFacade(innerHp, ports)
zk, _, err := Connect([]string{"foo.example.com:12345"}, time.Second, WithHostProvider(hp))
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk.Close()
path := "/gozk-test"
// Initial operation to force connection.
if err := zk.Delete(path, -1); err != nil && err != ErrNoNode {
t.Fatalf("Delete returned error: %+v", err)
}
// Figure out which server we're connected to.
currentServer := zk.Server()
t.Logf("Connected to %q. Finding test server index…", currentServer)
serverIndex := -1
for i, server := range ts.Servers {
server := fmt.Sprintf("localhost:%d", server.Port)
t.Logf("…trying %q", server)
if currentServer == server {
serverIndex = i
t.Logf("…found at index %d", i)
break
}
}
if serverIndex == -1 {
t.Fatalf("Cannot determine test server index.")
}
// Restart the connected server.
ts.Servers[serverIndex].Srv.Stop()
ts.Servers[serverIndex].Srv.Start()
// Continue with the basic TestCreate tests.
if p, err := zk.Create(path, []byte{1, 2, 3, 4}, 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create returned error: %+v", err)
} else if p != path {
t.Fatalf("Create returned different path '%s' != '%s'", p, path)
}
if data, stat, err := zk.Get(path); err != nil {
t.Fatalf("Get returned error: %+v", err)
} else if stat == nil {
t.Fatal("Get returned nil stat")
} else if len(data) < 4 {
t.Fatal("Get returned wrong size data")
}
if zk.Server() == currentServer {
t.Errorf("Still connected to %q after restart.", currentServer)
}
}
// TestHostProvidersRetryStart tests the `retryStart` functionality of DNSHostProvider and
// StaticHostProvider.
// It's also probably the clearest visual explanation of exactly how it works.
func TestHostProvidersRetryStart(t *testing.T) {
t.Parallel()
lookupHost := func(host string) ([]string, error) {
return []string{host}, nil
}
providers := []HostProvider{
&DNSHostProvider{
lookupHost: lookupHost,
},
&StaticHostProvider{
lookupHost: lookupHost,
},
}
for _, hp := range providers {
t.Run(fmt.Sprintf("%T", hp), func(t *testing.T) {
if err := hp.Init([]string{"foo.com:2121", "bar.com:2121", "baz.com:2121"}); err != nil {
t.Fatal(err)
}
testdata := []struct {
retryStartWant bool
callConnected bool
}{
// Repeated failures.
{false, false},
{false, false},
{true, false},
{false, false},
{false, false},
{true, false},
{false, true},
// One success offsets things.
{false, false},
{false, true},
{false, true},
// Repeated successes.
{false, true},
{false, true},
{false, true},
{false, true},
{false, true},
// And some more failures.
{false, false},
{false, false},
{true, false}, // Looped back to last known good server: all alternates failed.
{false, false},
{false, false},
{true, false},
{false, false},
{false, false},
{true, false},
{false, false},
}
for i, td := range testdata {
_, retryStartGot := hp.Next()
if retryStartGot != td.retryStartWant {
t.Errorf("%d: retryStart=%v; want %v", i, retryStartGot, td.retryStartWant)
}
if td.callConnected {
hp.Connected()
}
}
})
}
}