forked from elastic/go-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery_internal_test.go
174 lines (148 loc) · 4.4 KB
/
discovery_internal_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
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
// +build !integration
package estransport
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"os"
"testing"
"time"
)
func TestDiscovery(t *testing.T) {
defaultHandler := func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open("testdata/nodes.info.json")
if err != nil {
http.Error(w, fmt.Sprintf("Fixture error: %s", err), 500)
return
}
io.Copy(w, f)
}
srv := &http.Server{Addr: "localhost:10001", Handler: http.HandlerFunc(defaultHandler)}
srvTLS := &http.Server{Addr: "localhost:12001", Handler: http.HandlerFunc(defaultHandler)}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
t.Fatalf("Unable to start server: %s", err)
}
}()
go func() {
if err := srvTLS.ListenAndServeTLS("testdata/cert.pem", "testdata/key.pem"); err != nil && err != http.ErrServerClosed {
t.Fatalf("Unable to start server: %s", err)
}
}()
defer func() { srv.Close() }()
defer func() { srvTLS.Close() }()
time.Sleep(50 * time.Millisecond)
t.Run("getNodesInfo()", func(t *testing.T) {
u, _ := url.Parse("http://" + srv.Addr)
tp, _ := New(Config{URLs: []*url.URL{u}})
nodes, err := tp.getNodesInfo()
if err != nil {
t.Fatalf("ERROR: %s", err)
}
fmt.Printf("NodesInfo: %+v\n", nodes)
if len(nodes) != 3 {
t.Errorf("Unexpected number of nodes, want=3, got=%d", len(nodes))
}
for _, node := range nodes {
switch node.Name {
case "es1":
if node.URL.String() != "http://127.0.0.1:10001" {
t.Errorf("Unexpected URL: %s", node.URL.String())
}
case "es2":
if node.URL.String() != "http://localhost:10002" {
t.Errorf("Unexpected URL: %s", node.URL.String())
}
case "es3":
if node.URL.String() != "http://127.0.0.1:10003" {
t.Errorf("Unexpected URL: %s", node.URL.String())
}
}
}
})
t.Run("DiscoverNodes()", func(t *testing.T) {
u, _ := url.Parse("http://" + srv.Addr)
tp, _ := New(Config{URLs: []*url.URL{u}})
tp.DiscoverNodes()
pool, ok := tp.pool.(*statusConnectionPool)
if !ok {
t.Fatalf("Unexpected pool, want=statusConnectionPool, got=%T", tp.pool)
}
if len(pool.live) != 2 {
t.Errorf("Unexpected number of nodes, want=2, got=%d", len(pool.live))
}
for _, conn := range pool.live {
switch conn.Name {
case "es1":
if conn.URL.String() != "http://127.0.0.1:10001" {
t.Errorf("Unexpected URL: %s", conn.URL.String())
}
case "es2":
if conn.URL.String() != "http://localhost:10002" {
t.Errorf("Unexpected URL: %s", conn.URL.String())
}
default:
t.Errorf("Unexpected node: %s", conn.Name)
}
}
})
t.Run("DiscoverNodes() with SSL and authorization", func(t *testing.T) {
u, _ := url.Parse("https://" + srvTLS.Addr)
tp, _ := New(Config{
URLs: []*url.URL{u},
Username: "foo",
Password: "bar",
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
})
tp.DiscoverNodes()
pool, ok := tp.pool.(*statusConnectionPool)
if !ok {
t.Fatalf("Unexpected pool, want=statusConnectionPool, got=%T", tp.pool)
}
if len(pool.live) != 2 {
t.Errorf("Unexpected number of nodes, want=2, got=%d", len(pool.live))
}
for _, conn := range pool.live {
switch conn.Name {
case "es1":
if conn.URL.String() != "https://127.0.0.1:10001" {
t.Errorf("Unexpected URL: %s", conn.URL.String())
}
case "es2":
if conn.URL.String() != "https://localhost:10002" {
t.Errorf("Unexpected URL: %s", conn.URL.String())
}
default:
t.Errorf("Unexpected node: %s", conn.Name)
}
}
})
t.Run("scheduleDiscoverNodes()", func(t *testing.T) {
t.Skip("Skip") // TODO(karmi): Investigate the intermittent failures of this test
var numURLs int
u, _ := url.Parse("http://" + srv.Addr)
tp, _ := New(Config{URLs: []*url.URL{u}, DiscoverNodesInterval: 10 * time.Millisecond})
tp.Lock()
numURLs = len(tp.pool.URLs())
tp.Unlock()
if numURLs != 1 {
t.Errorf("Unexpected number of nodes, want=1, got=%d", numURLs)
}
time.Sleep(18 * time.Millisecond) // Wait until (*Client).scheduleDiscoverNodes()
tp.Lock()
numURLs = len(tp.pool.URLs())
tp.Unlock()
if numURLs != 2 {
t.Errorf("Unexpected number of nodes, want=2, got=%d", numURLs)
}
})
}