| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"encoding/json" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"log" |
| 14 |
"net" |
| 15 |
"net/http" |
| 16 |
"reflect" |
| 17 |
"regexp" |
| 18 |
"strings" |
| 19 |
"sync" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/fortytw2/leaktest" |
| 24 |
) |
| 25 |
|
| 26 |
func findConn(s string, slice ...*conn) (int, bool) { |
| 27 |
for i, t := range slice { |
| 28 |
if s == t.URL() { |
| 29 |
return i, true |
| 30 |
} |
| 31 |
} |
| 32 |
return -1, false |
| 33 |
} |
| 34 |
|
| 35 |
// -- NewClient -- |
| 36 |
|
| 37 |
func TestClientDefaults(t *testing.T) { |
| 38 |
client, err := NewClient() |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
if client.healthcheckEnabled != true { |
| 43 |
t.Errorf("expected health checks to be enabled, got: %v", client.healthcheckEnabled) |
| 44 |
} |
| 45 |
if client.healthcheckTimeoutStartup != DefaultHealthcheckTimeoutStartup { |
| 46 |
t.Errorf("expected health checks timeout on startup = %v, got: %v", DefaultHealthcheckTimeoutStartup, client.healthcheckTimeoutStartup) |
| 47 |
} |
| 48 |
if client.healthcheckTimeout != DefaultHealthcheckTimeout { |
| 49 |
t.Errorf("expected health checks timeout = %v, got: %v", DefaultHealthcheckTimeout, client.healthcheckTimeout) |
| 50 |
} |
| 51 |
if client.healthcheckInterval != DefaultHealthcheckInterval { |
| 52 |
t.Errorf("expected health checks interval = %v, got: %v", DefaultHealthcheckInterval, client.healthcheckInterval) |
| 53 |
} |
| 54 |
if client.snifferEnabled != true { |
| 55 |
t.Errorf("expected sniffing to be enabled, got: %v", client.snifferEnabled) |
| 56 |
} |
| 57 |
if client.snifferTimeoutStartup != DefaultSnifferTimeoutStartup { |
| 58 |
t.Errorf("expected sniffer timeout on startup = %v, got: %v", DefaultSnifferTimeoutStartup, client.snifferTimeoutStartup) |
| 59 |
} |
| 60 |
if client.snifferTimeout != DefaultSnifferTimeout { |
| 61 |
t.Errorf("expected sniffer timeout = %v, got: %v", DefaultSnifferTimeout, client.snifferTimeout) |
| 62 |
} |
| 63 |
if client.snifferInterval != DefaultSnifferInterval { |
| 64 |
t.Errorf("expected sniffer interval = %v, got: %v", DefaultSnifferInterval, client.snifferInterval) |
| 65 |
} |
| 66 |
if client.basicAuth != false { |
| 67 |
t.Errorf("expected no basic auth; got: %v", client.basicAuth) |
| 68 |
} |
| 69 |
if client.basicAuthUsername != "" { |
| 70 |
t.Errorf("expected no basic auth username; got: %q", client.basicAuthUsername) |
| 71 |
} |
| 72 |
if client.basicAuthPassword != "" { |
| 73 |
t.Errorf("expected no basic auth password; got: %q", client.basicAuthUsername) |
| 74 |
} |
| 75 |
if client.sendGetBodyAs != "GET" { |
| 76 |
t.Errorf("expected sendGetBodyAs to be GET; got: %q", client.sendGetBodyAs) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestClientWithoutURL(t *testing.T) { |
| 81 |
client, err := NewClient() |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
// Two things should happen here: |
| 86 |
// 1. The client starts sniffing the cluster on DefaultURL |
| 87 |
// 2. The sniffing process should find (at least) one node in the cluster, i.e. the DefaultURL |
| 88 |
if len(client.conns) == 0 { |
| 89 |
t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns) |
| 90 |
} |
| 91 |
if !isTravis() { |
| 92 |
if _, found := findConn(DefaultURL, client.conns...); !found { |
| 93 |
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func TestClientWithSingleURL(t *testing.T) { |
| 99 |
client, err := NewClient(SetURL("http://127.0.0.1:9200")) |
| 100 |
if err != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
// Two things should happen here: |
| 104 |
// 1. The client starts sniffing the cluster on DefaultURL |
| 105 |
// 2. The sniffing process should find (at least) one node in the cluster, i.e. the DefaultURL |
| 106 |
if len(client.conns) == 0 { |
| 107 |
t.Fatalf("expected at least 1 node in the cluster, got: %d (%v)", len(client.conns), client.conns) |
| 108 |
} |
| 109 |
if !isTravis() { |
| 110 |
if _, found := findConn(DefaultURL, client.conns...); !found { |
| 111 |
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestClientWithMultipleURLs(t *testing.T) { |
| 117 |
client, err := NewClient(SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201")) |
| 118 |
if err != nil { |
| 119 |
t.Fatal(err) |
| 120 |
} |
| 121 |
// The client should sniff both URLs, but only 127.0.0.1:9200 should return nodes. |
| 122 |
if len(client.conns) != 1 { |
| 123 |
t.Fatalf("expected exactly 1 node in the local cluster, got: %d (%v)", len(client.conns), client.conns) |
| 124 |
} |
| 125 |
if !isTravis() { |
| 126 |
if client.conns[0].URL() != DefaultURL { |
| 127 |
t.Errorf("expected to find node with default URL of %s in %v", DefaultURL, client.conns) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func TestClientWithBasicAuth(t *testing.T) { |
| 133 |
client, err := NewClient(SetBasicAuth("user", "secret")) |
| 134 |
if err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
if client.basicAuth != true { |
| 138 |
t.Errorf("expected basic auth; got: %v", client.basicAuth) |
| 139 |
} |
| 140 |
if got, want := client.basicAuthUsername, "user"; got != want { |
| 141 |
t.Errorf("expected basic auth username %q; got: %q", want, got) |
| 142 |
} |
| 143 |
if got, want := client.basicAuthPassword, "secret"; got != want { |
| 144 |
t.Errorf("expected basic auth password %q; got: %q", want, got) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestClientWithBasicAuthInUserInfo(t *testing.T) { |
| 149 |
client, err := NewClient(SetURL("http://user1:secret1@localhost:9200", "http://user2:secret2@localhost:9200")) |
| 150 |
if err != nil { |
| 151 |
t.Fatal(err) |
| 152 |
} |
| 153 |
if client.basicAuth != true { |
| 154 |
t.Errorf("expected basic auth; got: %v", client.basicAuth) |
| 155 |
} |
| 156 |
if got, want := client.basicAuthUsername, "user1"; got != want { |
| 157 |
t.Errorf("expected basic auth username %q; got: %q", want, got) |
| 158 |
} |
| 159 |
if got, want := client.basicAuthPassword, "secret1"; got != want { |
| 160 |
t.Errorf("expected basic auth password %q; got: %q", want, got) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestClientSniffSuccess(t *testing.T) { |
| 165 |
client, err := NewClient(SetURL("http://127.0.0.1:19200", "http://127.0.0.1:9200")) |
| 166 |
if err != nil { |
| 167 |
t.Fatal(err) |
| 168 |
} |
| 169 |
// The client should sniff both URLs, but only 127.0.0.1:9200 should return nodes. |
| 170 |
if len(client.conns) != 1 { |
| 171 |
t.Fatalf("expected exactly 1 node in the local cluster, got: %d (%v)", len(client.conns), client.conns) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func TestClientSniffFailure(t *testing.T) { |
| 176 |
_, err := NewClient(SetURL("http://127.0.0.1:19200", "http://127.0.0.1:19201")) |
| 177 |
if err == nil { |
| 178 |
t.Fatalf("expected cluster to fail with no nodes found") |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestClientSnifferCallback(t *testing.T) { |
| 183 |
var calls int |
| 184 |
cb := func(node *NodesInfoNode) bool { |
| 185 |
calls++ |
| 186 |
return false |
| 187 |
} |
| 188 |
_, err := NewClient( |
| 189 |
SetURL("http://127.0.0.1:19200", "http://127.0.0.1:9200"), |
| 190 |
SetSnifferCallback(cb)) |
| 191 |
if err == nil { |
| 192 |
t.Fatalf("expected cluster to fail with no nodes found") |
| 193 |
} |
| 194 |
if calls != 1 { |
| 195 |
t.Fatalf("expected 1 call to the sniffer callback, got %d", calls) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestClientSniffDisabled(t *testing.T) { |
| 200 |
client, err := NewClient(SetSniff(false), SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201")) |
| 201 |
if err != nil { |
| 202 |
t.Fatal(err) |
| 203 |
} |
| 204 |
// The client should not sniff, so it should have two connections. |
| 205 |
if len(client.conns) != 2 { |
| 206 |
t.Fatalf("expected 2 nodes, got: %d (%v)", len(client.conns), client.conns) |
| 207 |
} |
| 208 |
// Make two requests, so that both connections are being used |
| 209 |
for i := 0; i < len(client.conns); i++ { |
| 210 |
client.Flush().Do(context.TODO()) |
| 211 |
} |
| 212 |
// The first connection (127.0.0.1:9200) should now be okay. |
| 213 |
if i, found := findConn("http://127.0.0.1:9200", client.conns...); !found { |
| 214 |
t.Fatalf("expected connection to %q to be found", "http://127.0.0.1:9200") |
| 215 |
} else { |
| 216 |
if conn := client.conns[i]; conn.IsDead() { |
| 217 |
t.Fatal("expected connection to be alive, but it is dead") |
| 218 |
} |
| 219 |
} |
| 220 |
// The second connection (127.0.0.1:9201) should now be marked as dead. |
| 221 |
if i, found := findConn("http://127.0.0.1:9201", client.conns...); !found { |
| 222 |
t.Fatalf("expected connection to %q to be found", "http://127.0.0.1:9201") |
| 223 |
} else { |
| 224 |
if conn := client.conns[i]; !conn.IsDead() { |
| 225 |
t.Fatal("expected connection to be dead, but it is alive") |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func TestClientWillMarkConnectionsAsAliveWhenAllAreDead(t *testing.T) { |
| 231 |
client, err := NewClient(SetURL("http://127.0.0.1:9201"), |
| 232 |
SetSniff(false), SetHealthcheck(false), SetMaxRetries(0)) |
| 233 |
if err != nil { |
| 234 |
t.Fatal(err) |
| 235 |
} |
| 236 |
// We should have a connection. |
| 237 |
if len(client.conns) != 1 { |
| 238 |
t.Fatalf("expected 1 node, got: %d (%v)", len(client.conns), client.conns) |
| 239 |
} |
| 240 |
|
| 241 |
// Make a request, so that the connections is marked as dead. |
| 242 |
client.Flush().Do(context.TODO()) |
| 243 |
|
| 244 |
// The connection should now be marked as dead. |
| 245 |
if i, found := findConn("http://127.0.0.1:9201", client.conns...); !found { |
| 246 |
t.Fatalf("expected connection to %q to be found", "http://127.0.0.1:9201") |
| 247 |
} else { |
| 248 |
if conn := client.conns[i]; !conn.IsDead() { |
| 249 |
t.Fatalf("expected connection to be dead, got: %v", conn) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
// Now send another request and the connection should be marked as alive again. |
| 254 |
client.Flush().Do(context.TODO()) |
| 255 |
|
| 256 |
if i, found := findConn("http://127.0.0.1:9201", client.conns...); !found { |
| 257 |
t.Fatalf("expected connection to %q to be found", "http://127.0.0.1:9201") |
| 258 |
} else { |
| 259 |
if conn := client.conns[i]; conn.IsDead() { |
| 260 |
t.Fatalf("expected connection to be alive, got: %v", conn) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func TestClientWithRequiredPlugins(t *testing.T) { |
| 266 |
_, err := NewClient(SetRequiredPlugins("no-such-plugin")) |
| 267 |
if err == nil { |
| 268 |
t.Fatal("expected error when creating client") |
| 269 |
} |
| 270 |
if got, want := err.Error(), "elastic: plugin no-such-plugin not found"; got != want { |
| 271 |
t.Fatalf("expected error %q; got: %q", want, got) |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
func TestClientHealthcheckStartupTimeout(t *testing.T) { |
| 276 |
start := time.Now() |
| 277 |
_, err := NewClient(SetURL("http://localhost:9299"), SetHealthcheckTimeoutStartup(5*time.Second)) |
| 278 |
duration := time.Since(start) |
| 279 |
if !IsConnErr(err) { |
| 280 |
t.Fatal(err) |
| 281 |
} |
| 282 |
if !strings.Contains(err.Error(), "connection refused") { |
| 283 |
t.Fatalf("expected error to contain %q, have %q", "connection refused", err.Error()) |
| 284 |
} |
| 285 |
if duration < 5*time.Second { |
| 286 |
t.Fatalf("expected a timeout in more than 5 seconds; got: %v", duration) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func TestClientHealthcheckTimeoutLeak(t *testing.T) { |
| 291 |
// This test test checks if healthcheck requests are canceled |
| 292 |
// after timeout. |
| 293 |
// It contains couple of hacks which won't be needed once we |
| 294 |
// stop supporting Go1.7. |
| 295 |
// On Go1.7 it uses server side effects to monitor if connection |
| 296 |
// was closed, |
| 297 |
// and on Go 1.8+ we're additionally honestly monitoring routine |
| 298 |
// leaks via leaktest. |
| 299 |
mux := http.NewServeMux() |
| 300 |
|
| 301 |
var reqDoneMu sync.Mutex |
| 302 |
var reqDone bool |
| 303 |
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 304 |
cn, ok := w.(http.CloseNotifier) |
| 305 |
if !ok { |
| 306 |
t.Fatalf("Writer is not CloseNotifier, but %v", reflect.TypeOf(w).Name()) |
| 307 |
} |
| 308 |
<-cn.CloseNotify() |
| 309 |
reqDoneMu.Lock() |
| 310 |
reqDone = true |
| 311 |
reqDoneMu.Unlock() |
| 312 |
}) |
| 313 |
|
| 314 |
lis, err := net.Listen("tcp", "127.0.0.1:0") |
| 315 |
if err != nil { |
| 316 |
t.Fatalf("Couldn't setup listener: %v", err) |
| 317 |
} |
| 318 |
addr := lis.Addr().String() |
| 319 |
|
| 320 |
srv := &http.Server{ |
| 321 |
Handler: mux, |
| 322 |
} |
| 323 |
go srv.Serve(lis) |
| 324 |
|
| 325 |
cli := &Client{ |
| 326 |
c: &http.Client{}, |
| 327 |
conns: []*conn{ |
| 328 |
&conn{ |
| 329 |
url: "http://" + addr + "/", |
| 330 |
}, |
| 331 |
}, |
| 332 |
} |
| 333 |
|
| 334 |
type closer interface { |
| 335 |
Shutdown(context.Context) error |
| 336 |
} |
| 337 |
|
| 338 |
// pre-Go1.8 Server can't Shutdown |
| 339 |
cl, isServerCloseable := (interface{}(srv)).(closer) |
| 340 |
|
| 341 |
// Since Go1.7 can't Shutdown() - there will be leak from server |
| 342 |
// Monitor leaks on Go 1.8+ |
| 343 |
if isServerCloseable { |
| 344 |
defer leaktest.CheckTimeout(t, time.Second*10)() |
| 345 |
} |
| 346 |
|
| 347 |
cli.healthcheck(time.Millisecond*500, true) |
| 348 |
|
| 349 |
if isServerCloseable { |
| 350 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 351 |
defer cancel() |
| 352 |
cl.Shutdown(ctx) |
| 353 |
} |
| 354 |
|
| 355 |
<-time.After(time.Second) |
| 356 |
reqDoneMu.Lock() |
| 357 |
if !reqDone { |
| 358 |
reqDoneMu.Unlock() |
| 359 |
t.Fatal("Request wasn't canceled or stopped") |
| 360 |
} |
| 361 |
reqDoneMu.Unlock() |
| 362 |
} |
| 363 |
|
| 364 |
// -- NewSimpleClient -- |
| 365 |
|
| 366 |
func TestSimpleClientDefaults(t *testing.T) { |
| 367 |
client, err := NewSimpleClient() |
| 368 |
if err != nil { |
| 369 |
t.Fatal(err) |
| 370 |
} |
| 371 |
if client.healthcheckEnabled != false { |
| 372 |
t.Errorf("expected health checks to be disabled, got: %v", client.healthcheckEnabled) |
| 373 |
} |
| 374 |
if client.healthcheckTimeoutStartup != off { |
| 375 |
t.Errorf("expected health checks timeout on startup = %v, got: %v", off, client.healthcheckTimeoutStartup) |
| 376 |
} |
| 377 |
if client.healthcheckTimeout != off { |
| 378 |
t.Errorf("expected health checks timeout = %v, got: %v", off, client.healthcheckTimeout) |
| 379 |
} |
| 380 |
if client.healthcheckInterval != off { |
| 381 |
t.Errorf("expected health checks interval = %v, got: %v", off, client.healthcheckInterval) |
| 382 |
} |
| 383 |
if client.snifferEnabled != false { |
| 384 |
t.Errorf("expected sniffing to be disabled, got: %v", client.snifferEnabled) |
| 385 |
} |
| 386 |
if client.snifferTimeoutStartup != off { |
| 387 |
t.Errorf("expected sniffer timeout on startup = %v, got: %v", off, client.snifferTimeoutStartup) |
| 388 |
} |
| 389 |
if client.snifferTimeout != off { |
| 390 |
t.Errorf("expected sniffer timeout = %v, got: %v", off, client.snifferTimeout) |
| 391 |
} |
| 392 |
if client.snifferInterval != off { |
| 393 |
t.Errorf("expected sniffer interval = %v, got: %v", off, client.snifferInterval) |
| 394 |
} |
| 395 |
if client.basicAuth != false { |
| 396 |
t.Errorf("expected no basic auth; got: %v", client.basicAuth) |
| 397 |
} |
| 398 |
if client.basicAuthUsername != "" { |
| 399 |
t.Errorf("expected no basic auth username; got: %q", client.basicAuthUsername) |
| 400 |
} |
| 401 |
if client.basicAuthPassword != "" { |
| 402 |
t.Errorf("expected no basic auth password; got: %q", client.basicAuthUsername) |
| 403 |
} |
| 404 |
if client.sendGetBodyAs != "GET" { |
| 405 |
t.Errorf("expected sendGetBodyAs to be GET; got: %q", client.sendGetBodyAs) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
// -- Start and stop -- |
| 410 |
|
| 411 |
func TestClientStartAndStop(t *testing.T) { |
| 412 |
client, err := NewClient() |
| 413 |
if err != nil { |
| 414 |
t.Fatal(err) |
| 415 |
} |
| 416 |
|
| 417 |
running := client.IsRunning() |
| 418 |
if !running { |
| 419 |
t.Fatalf("expected background processes to run; got: %v", running) |
| 420 |
} |
| 421 |
|
| 422 |
// Stop |
| 423 |
client.Stop() |
| 424 |
running = client.IsRunning() |
| 425 |
if running { |
| 426 |
t.Fatalf("expected background processes to be stopped; got: %v", running) |
| 427 |
} |
| 428 |
|
| 429 |
// Stop again => no-op |
| 430 |
client.Stop() |
| 431 |
running = client.IsRunning() |
| 432 |
if running { |
| 433 |
t.Fatalf("expected background processes to be stopped; got: %v", running) |
| 434 |
} |
| 435 |
|
| 436 |
// Start |
| 437 |
client.Start() |
| 438 |
running = client.IsRunning() |
| 439 |
if !running { |
| 440 |
t.Fatalf("expected background processes to run; got: %v", running) |
| 441 |
} |
| 442 |
|
| 443 |
// Start again => no-op |
| 444 |
client.Start() |
| 445 |
running = client.IsRunning() |
| 446 |
if !running { |
| 447 |
t.Fatalf("expected background processes to run; got: %v", running) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
func TestClientStartAndStopWithSnifferAndHealthchecksDisabled(t *testing.T) { |
| 452 |
client, err := NewClient(SetSniff(false), SetHealthcheck(false)) |
| 453 |
if err != nil { |
| 454 |
t.Fatal(err) |
| 455 |
} |
| 456 |
|
| 457 |
running := client.IsRunning() |
| 458 |
if !running { |
| 459 |
t.Fatalf("expected background processes to run; got: %v", running) |
| 460 |
} |
| 461 |
|
| 462 |
// Stop |
| 463 |
client.Stop() |
| 464 |
running = client.IsRunning() |
| 465 |
if running { |
| 466 |
t.Fatalf("expected background processes to be stopped; got: %v", running) |
| 467 |
} |
| 468 |
|
| 469 |
// Stop again => no-op |
| 470 |
client.Stop() |
| 471 |
running = client.IsRunning() |
| 472 |
if running { |
| 473 |
t.Fatalf("expected background processes to be stopped; got: %v", running) |
| 474 |
} |
| 475 |
|
| 476 |
// Start |
| 477 |
client.Start() |
| 478 |
running = client.IsRunning() |
| 479 |
if !running { |
| 480 |
t.Fatalf("expected background processes to run; got: %v", running) |
| 481 |
} |
| 482 |
|
| 483 |
// Start again => no-op |
| 484 |
client.Start() |
| 485 |
running = client.IsRunning() |
| 486 |
if !running { |
| 487 |
t.Fatalf("expected background processes to run; got: %v", running) |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
// -- Sniffing -- |
| 492 |
|
| 493 |
func TestClientSniffNode(t *testing.T) { |
| 494 |
client, err := NewClient() |
| 495 |
if err != nil { |
| 496 |
t.Fatal(err) |
| 497 |
} |
| 498 |
|
| 499 |
ch := make(chan []*conn) |
| 500 |
go func() { ch <- client.sniffNode(context.Background(), DefaultURL) }() |
| 501 |
|
| 502 |
select { |
| 503 |
case nodes := <-ch: |
| 504 |
if len(nodes) != 1 { |
| 505 |
t.Fatalf("expected %d nodes; got: %d", 1, len(nodes)) |
| 506 |
} |
| 507 |
pattern := `http:\/\/[\d\.]+:9200` |
| 508 |
matched, err := regexp.MatchString(pattern, nodes[0].URL()) |
| 509 |
if err != nil { |
| 510 |
t.Fatal(err) |
| 511 |
} |
| 512 |
if !matched { |
| 513 |
t.Fatalf("expected node URL pattern %q; got: %q", pattern, nodes[0].URL()) |
| 514 |
} |
| 515 |
case <-time.After(2 * time.Second): |
| 516 |
t.Fatal("expected no timeout in sniff node") |
| 517 |
break |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
func TestClientSniffOnDefaultURL(t *testing.T) { |
| 522 |
client, _ := NewClient() |
| 523 |
if client == nil { |
| 524 |
t.Fatal("no client returned") |
| 525 |
} |
| 526 |
|
| 527 |
ch := make(chan error, 1) |
| 528 |
go func() { |
| 529 |
ch <- client.sniff(DefaultSnifferTimeoutStartup) |
| 530 |
}() |
| 531 |
|
| 532 |
select { |
| 533 |
case err := <-ch: |
| 534 |
if err != nil { |
| 535 |
t.Fatalf("expected sniff to succeed; got: %v", err) |
| 536 |
} |
| 537 |
if len(client.conns) != 1 { |
| 538 |
t.Fatalf("expected %d nodes; got: %d", 1, len(client.conns)) |
| 539 |
} |
| 540 |
pattern := `http:\/\/[\d\.]+:9200` |
| 541 |
matched, err := regexp.MatchString(pattern, client.conns[0].URL()) |
| 542 |
if err != nil { |
| 543 |
t.Fatal(err) |
| 544 |
} |
| 545 |
if !matched { |
| 546 |
t.Fatalf("expected node URL pattern %q; got: %q", pattern, client.conns[0].URL()) |
| 547 |
} |
| 548 |
case <-time.After(2 * time.Second): |
| 549 |
t.Fatal("expected no timeout in sniff") |
| 550 |
break |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
func TestClientSniffTimeoutLeak(t *testing.T) { |
| 555 |
// This test test checks if sniff requests are canceled |
| 556 |
// after timeout. |
| 557 |
// It contains couple of hacks which won't be needed once we |
| 558 |
// stop supporting Go1.7. |
| 559 |
// On Go1.7 it uses server side effects to monitor if connection |
| 560 |
// was closed, |
| 561 |
// and on Go 1.8+ we're additionally honestly monitoring routine |
| 562 |
// leaks via leaktest. |
| 563 |
mux := http.NewServeMux() |
| 564 |
|
| 565 |
var reqDoneMu sync.Mutex |
| 566 |
var reqDone bool |
| 567 |
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 568 |
cn, ok := w.(http.CloseNotifier) |
| 569 |
if !ok { |
| 570 |
t.Fatalf("Writer is not CloseNotifier, but %v", reflect.TypeOf(w).Name()) |
| 571 |
} |
| 572 |
<-cn.CloseNotify() |
| 573 |
reqDoneMu.Lock() |
| 574 |
reqDone = true |
| 575 |
reqDoneMu.Unlock() |
| 576 |
}) |
| 577 |
|
| 578 |
lis, err := net.Listen("tcp", "127.0.0.1:0") |
| 579 |
if err != nil { |
| 580 |
t.Fatalf("Couldn't setup listener: %v", err) |
| 581 |
} |
| 582 |
addr := lis.Addr().String() |
| 583 |
|
| 584 |
srv := &http.Server{ |
| 585 |
Handler: mux, |
| 586 |
} |
| 587 |
go srv.Serve(lis) |
| 588 |
|
| 589 |
cli := &Client{ |
| 590 |
c: &http.Client{}, |
| 591 |
conns: []*conn{ |
| 592 |
&conn{ |
| 593 |
url: "http://" + addr + "/", |
| 594 |
}, |
| 595 |
}, |
| 596 |
snifferEnabled: true, |
| 597 |
} |
| 598 |
|
| 599 |
type closer interface { |
| 600 |
Shutdown(context.Context) error |
| 601 |
} |
| 602 |
|
| 603 |
// pre-Go1.8 Server can't Shutdown |
| 604 |
cl, isServerCloseable := (interface{}(srv)).(closer) |
| 605 |
|
| 606 |
// Since Go1.7 can't Shutdown() - there will be leak from server |
| 607 |
// Monitor leaks on Go 1.8+ |
| 608 |
if isServerCloseable { |
| 609 |
defer leaktest.CheckTimeout(t, time.Second*10)() |
| 610 |
} |
| 611 |
|
| 612 |
cli.sniff(time.Millisecond * 500) |
| 613 |
|
| 614 |
if isServerCloseable { |
| 615 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 616 |
defer cancel() |
| 617 |
cl.Shutdown(ctx) |
| 618 |
} |
| 619 |
|
| 620 |
<-time.After(time.Second) |
| 621 |
reqDoneMu.Lock() |
| 622 |
if !reqDone { |
| 623 |
reqDoneMu.Unlock() |
| 624 |
t.Fatal("Request wasn't canceled or stopped") |
| 625 |
} |
| 626 |
reqDoneMu.Unlock() |
| 627 |
} |
| 628 |
|
| 629 |
func TestClientExtractHostname(t *testing.T) { |
| 630 |
tests := []struct { |
| 631 |
Scheme string |
| 632 |
Address string |
| 633 |
Output string |
| 634 |
}{ |
| 635 |
{ |
| 636 |
Scheme: "http", |
| 637 |
Address: "", |
| 638 |
Output: "", |
| 639 |
}, |
| 640 |
{ |
| 641 |
Scheme: "https", |
| 642 |
Address: "abc", |
| 643 |
Output: "", |
| 644 |
}, |
| 645 |
{ |
| 646 |
Scheme: "http", |
| 647 |
Address: "127.0.0.1:19200", |
| 648 |
Output: "http://127.0.0.1:19200", |
| 649 |
}, |
| 650 |
{ |
| 651 |
Scheme: "https", |
| 652 |
Address: "127.0.0.1:9200", |
| 653 |
Output: "https://127.0.0.1:9200", |
| 654 |
}, |
| 655 |
{ |
| 656 |
Scheme: "http", |
| 657 |
Address: "myelk.local/10.1.0.24:9200", |
| 658 |
Output: "http://10.1.0.24:9200", |
| 659 |
}, |
| 660 |
} |
| 661 |
|
| 662 |
client, err := NewClient(SetSniff(false), SetHealthcheck(false)) |
| 663 |
if err != nil { |
| 664 |
t.Fatal(err) |
| 665 |
} |
| 666 |
for _, test := range tests { |
| 667 |
got := client.extractHostname(test.Scheme, test.Address) |
| 668 |
if want := test.Output; want != got { |
| 669 |
t.Errorf("expected %q; got: %q", want, got) |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
// -- Selector -- |
| 675 |
|
| 676 |
func TestClientSelectConnHealthy(t *testing.T) { |
| 677 |
client, err := NewClient( |
| 678 |
SetSniff(false), |
| 679 |
SetHealthcheck(false), |
| 680 |
SetURL("http://127.0.0.1:9200/node1", "http://127.0.0.1:9201/node2/")) |
| 681 |
if err != nil { |
| 682 |
t.Fatal(err) |
| 683 |
} |
| 684 |
|
| 685 |
// Both are healthy, so we should get both URLs in round-robin |
| 686 |
client.conns[0].MarkAsHealthy() |
| 687 |
client.conns[1].MarkAsHealthy() |
| 688 |
|
| 689 |
// #1: Return 1st |
| 690 |
c, err := client.next() |
| 691 |
if err != nil { |
| 692 |
t.Fatal(err) |
| 693 |
} |
| 694 |
if c.URL() != client.conns[0].URL() { |
| 695 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 696 |
} |
| 697 |
// #2: Return 2nd |
| 698 |
c, err = client.next() |
| 699 |
if err != nil { |
| 700 |
t.Fatal(err) |
| 701 |
} |
| 702 |
if c.URL() != client.conns[1].URL() { |
| 703 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[1].URL()) |
| 704 |
} |
| 705 |
// #3: Return 1st |
| 706 |
c, err = client.next() |
| 707 |
if err != nil { |
| 708 |
t.Fatal(err) |
| 709 |
} |
| 710 |
if c.URL() != client.conns[0].URL() { |
| 711 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
func TestClientSelectConnHealthyWithURLPrefix(t *testing.T) { |
| 716 |
client, err := NewClient( |
| 717 |
SetSniff(false), |
| 718 |
SetHealthcheck(false), |
| 719 |
SetURL("http://127.0.0.1:9200/node1", "http://127.0.0.1:9201/node2/prefix/")) |
| 720 |
if err != nil { |
| 721 |
t.Fatal(err) |
| 722 |
} |
| 723 |
|
| 724 |
// Both are healthy, so we should get both URLs in round-robin |
| 725 |
client.conns[0].MarkAsHealthy() |
| 726 |
client.conns[1].MarkAsHealthy() |
| 727 |
|
| 728 |
// Check that the connection used the URLs, including its prefix |
| 729 |
if want, have := "http://127.0.0.1:9200/node1", client.conns[0].URL(); want != have { |
| 730 |
t.Fatalf("want Node[0] = %q, have %q", want, have) |
| 731 |
} |
| 732 |
// Note that it stripped the / off the suffix |
| 733 |
if want, have := "http://127.0.0.1:9201/node2/prefix", client.conns[1].URL(); want != have { |
| 734 |
t.Fatalf("want Node[1] = %q, have %q", want, have) |
| 735 |
} |
| 736 |
|
| 737 |
// #1: Return 1st |
| 738 |
c, err := client.next() |
| 739 |
if err != nil { |
| 740 |
t.Fatal(err) |
| 741 |
} |
| 742 |
if c.URL() != client.conns[0].URL() { |
| 743 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 744 |
} |
| 745 |
// #2: Return 2nd |
| 746 |
c, err = client.next() |
| 747 |
if err != nil { |
| 748 |
t.Fatal(err) |
| 749 |
} |
| 750 |
if c.URL() != client.conns[1].URL() { |
| 751 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[1].URL()) |
| 752 |
} |
| 753 |
// #3: Return 1st |
| 754 |
c, err = client.next() |
| 755 |
if err != nil { |
| 756 |
t.Fatal(err) |
| 757 |
} |
| 758 |
if c.URL() != client.conns[0].URL() { |
| 759 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
func TestClientSelectConnHealthyAndDead(t *testing.T) { |
| 764 |
client, err := NewClient( |
| 765 |
SetSniff(false), |
| 766 |
SetHealthcheck(false), |
| 767 |
SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201")) |
| 768 |
if err != nil { |
| 769 |
t.Fatal(err) |
| 770 |
} |
| 771 |
|
| 772 |
// 1st is healthy, second is dead |
| 773 |
client.conns[0].MarkAsHealthy() |
| 774 |
client.conns[1].MarkAsDead() |
| 775 |
|
| 776 |
// #1: Return 1st |
| 777 |
c, err := client.next() |
| 778 |
if err != nil { |
| 779 |
t.Fatal(err) |
| 780 |
} |
| 781 |
if c.URL() != client.conns[0].URL() { |
| 782 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 783 |
} |
| 784 |
// #2: Return 1st again |
| 785 |
c, err = client.next() |
| 786 |
if err != nil { |
| 787 |
t.Fatal(err) |
| 788 |
} |
| 789 |
if c.URL() != client.conns[0].URL() { |
| 790 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 791 |
} |
| 792 |
// #3: Return 1st again and again |
| 793 |
c, err = client.next() |
| 794 |
if err != nil { |
| 795 |
t.Fatal(err) |
| 796 |
} |
| 797 |
if c.URL() != client.conns[0].URL() { |
| 798 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[0].URL()) |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
func TestClientSelectConnDeadAndHealthy(t *testing.T) { |
| 803 |
client, err := NewClient( |
| 804 |
SetSniff(false), |
| 805 |
SetHealthcheck(false), |
| 806 |
SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201")) |
| 807 |
if err != nil { |
| 808 |
t.Fatal(err) |
| 809 |
} |
| 810 |
|
| 811 |
// 1st is dead, 2nd is healthy |
| 812 |
client.conns[0].MarkAsDead() |
| 813 |
client.conns[1].MarkAsHealthy() |
| 814 |
|
| 815 |
// #1: Return 2nd |
| 816 |
c, err := client.next() |
| 817 |
if err != nil { |
| 818 |
t.Fatal(err) |
| 819 |
} |
| 820 |
if c.URL() != client.conns[1].URL() { |
| 821 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[1].URL()) |
| 822 |
} |
| 823 |
// #2: Return 2nd again |
| 824 |
c, err = client.next() |
| 825 |
if err != nil { |
| 826 |
t.Fatal(err) |
| 827 |
} |
| 828 |
if c.URL() != client.conns[1].URL() { |
| 829 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[1].URL()) |
| 830 |
} |
| 831 |
// #3: Return 2nd again and again |
| 832 |
c, err = client.next() |
| 833 |
if err != nil { |
| 834 |
t.Fatal(err) |
| 835 |
} |
| 836 |
if c.URL() != client.conns[1].URL() { |
| 837 |
t.Fatalf("expected %s; got: %s", c.URL(), client.conns[1].URL()) |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
func TestClientSelectConnAllDead(t *testing.T) { |
| 842 |
client, err := NewClient( |
| 843 |
SetSniff(false), |
| 844 |
SetHealthcheck(false), |
| 845 |
SetURL("http://127.0.0.1:9200", "http://127.0.0.1:9201")) |
| 846 |
if err != nil { |
| 847 |
t.Fatal(err) |
| 848 |
} |
| 849 |
|
| 850 |
// Both are dead |
| 851 |
client.conns[0].MarkAsDead() |
| 852 |
client.conns[1].MarkAsDead() |
| 853 |
|
| 854 |
// If all connections are dead, next should make them alive again, but |
| 855 |
// still return an error when it first finds out. |
| 856 |
c, err := client.next() |
| 857 |
if !IsConnErr(err) { |
| 858 |
t.Fatal(err) |
| 859 |
} |
| 860 |
if c != nil { |
| 861 |
t.Fatalf("expected no connection; got: %v", c) |
| 862 |
} |
| 863 |
// Return a connection |
| 864 |
c, err = client.next() |
| 865 |
if err != nil { |
| 866 |
t.Fatalf("expected no error; got: %v", err) |
| 867 |
} |
| 868 |
if c == nil { |
| 869 |
t.Fatalf("expected connection; got: %v", c) |
| 870 |
} |
| 871 |
// Return a connection |
| 872 |
c, err = client.next() |
| 873 |
if err != nil { |
| 874 |
t.Fatalf("expected no error; got: %v", err) |
| 875 |
} |
| 876 |
if c == nil { |
| 877 |
t.Fatalf("expected connection; got: %v", c) |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
// -- ElasticsearchVersion -- |
| 882 |
|
| 883 |
func TestElasticsearchVersion(t *testing.T) { |
| 884 |
client, err := NewClient() |
| 885 |
if err != nil { |
| 886 |
t.Fatal(err) |
| 887 |
} |
| 888 |
version, err := client.ElasticsearchVersion(DefaultURL) |
| 889 |
if err != nil { |
| 890 |
t.Fatal(err) |
| 891 |
} |
| 892 |
if version == "" { |
| 893 |
t.Errorf("expected a version number, got: %q", version) |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
// -- IndexNames -- |
| 898 |
|
| 899 |
func TestIndexNames(t *testing.T) { |
| 900 |
client := setupTestClientAndCreateIndex(t) |
| 901 |
names, err := client.IndexNames() |
| 902 |
if err != nil { |
| 903 |
t.Fatal(err) |
| 904 |
} |
| 905 |
if len(names) == 0 { |
| 906 |
t.Fatalf("expected some index names, got: %d", len(names)) |
| 907 |
} |
| 908 |
var found bool |
| 909 |
for _, name := range names { |
| 910 |
if name == testIndexName { |
| 911 |
found = true |
| 912 |
break |
| 913 |
} |
| 914 |
} |
| 915 |
if !found { |
| 916 |
t.Fatalf("expected to find index %q; got: %v", testIndexName, found) |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
// -- PerformRequest -- |
| 921 |
|
| 922 |
func TestPerformRequest(t *testing.T) { |
| 923 |
client, err := NewClient() |
| 924 |
if err != nil { |
| 925 |
t.Fatal(err) |
| 926 |
} |
| 927 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, nil) |
| 928 |
if err != nil { |
| 929 |
t.Fatal(err) |
| 930 |
} |
| 931 |
if res == nil { |
| 932 |
t.Fatal("expected response to be != nil") |
| 933 |
} |
| 934 |
|
| 935 |
ret := new(PingResult) |
| 936 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 937 |
t.Fatalf("expected no error on decode; got: %v", err) |
| 938 |
} |
| 939 |
if ret.ClusterName == "" { |
| 940 |
t.Errorf("expected cluster name; got: %q", ret.ClusterName) |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
func TestPerformRequestWithSimpleClient(t *testing.T) { |
| 945 |
client, err := NewSimpleClient() |
| 946 |
if err != nil { |
| 947 |
t.Fatal(err) |
| 948 |
} |
| 949 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, nil) |
| 950 |
if err != nil { |
| 951 |
t.Fatal(err) |
| 952 |
} |
| 953 |
if res == nil { |
| 954 |
t.Fatal("expected response to be != nil") |
| 955 |
} |
| 956 |
|
| 957 |
ret := new(PingResult) |
| 958 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 959 |
t.Fatalf("expected no error on decode; got: %v", err) |
| 960 |
} |
| 961 |
if ret.ClusterName == "" { |
| 962 |
t.Errorf("expected cluster name; got: %q", ret.ClusterName) |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
func TestPerformRequestWithLogger(t *testing.T) { |
| 967 |
var w bytes.Buffer |
| 968 |
out := log.New(&w, "LOGGER ", log.LstdFlags) |
| 969 |
|
| 970 |
client, err := NewClient(SetInfoLog(out), SetSniff(false)) |
| 971 |
if err != nil { |
| 972 |
t.Fatal(err) |
| 973 |
} |
| 974 |
|
| 975 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, nil) |
| 976 |
if err != nil { |
| 977 |
t.Fatal(err) |
| 978 |
} |
| 979 |
if res == nil { |
| 980 |
t.Fatal("expected response to be != nil") |
| 981 |
} |
| 982 |
|
| 983 |
ret := new(PingResult) |
| 984 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 985 |
t.Fatalf("expected no error on decode; got: %v", err) |
| 986 |
} |
| 987 |
if ret.ClusterName == "" { |
| 988 |
t.Errorf("expected cluster name; got: %q", ret.ClusterName) |
| 989 |
} |
| 990 |
|
| 991 |
got := w.String() |
| 992 |
pattern := `^LOGGER \d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} GET http://.*/ \[status:200, request:\d+\.\d{3}s\]\n` |
| 993 |
matched, err := regexp.MatchString(pattern, got) |
| 994 |
if err != nil { |
| 995 |
t.Fatalf("expected log line to match %q; got: %v", pattern, err) |
| 996 |
} |
| 997 |
if !matched { |
| 998 |
t.Errorf("expected log line to match %q; got: %v", pattern, got) |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
func TestPerformRequestWithLoggerAndTracer(t *testing.T) { |
| 1003 |
var lw bytes.Buffer |
| 1004 |
lout := log.New(&lw, "LOGGER ", log.LstdFlags) |
| 1005 |
|
| 1006 |
var tw bytes.Buffer |
| 1007 |
tout := log.New(&tw, "TRACER ", log.LstdFlags) |
| 1008 |
|
| 1009 |
client, err := NewClient(SetInfoLog(lout), SetTraceLog(tout), SetSniff(false)) |
| 1010 |
if err != nil { |
| 1011 |
t.Fatal(err) |
| 1012 |
} |
| 1013 |
|
| 1014 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, nil) |
| 1015 |
if err != nil { |
| 1016 |
t.Fatal(err) |
| 1017 |
} |
| 1018 |
if res == nil { |
| 1019 |
t.Fatal("expected response to be != nil") |
| 1020 |
} |
| 1021 |
|
| 1022 |
ret := new(PingResult) |
| 1023 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 1024 |
t.Fatalf("expected no error on decode; got: %v", err) |
| 1025 |
} |
| 1026 |
if ret.ClusterName == "" { |
| 1027 |
t.Errorf("expected cluster name; got: %q", ret.ClusterName) |
| 1028 |
} |
| 1029 |
|
| 1030 |
lgot := lw.String() |
| 1031 |
if lgot == "" { |
| 1032 |
t.Errorf("expected logger output; got: %q", lgot) |
| 1033 |
} |
| 1034 |
|
| 1035 |
tgot := tw.String() |
| 1036 |
if tgot == "" { |
| 1037 |
t.Errorf("expected tracer output; got: %q", tgot) |
| 1038 |
} |
| 1039 |
} |
| 1040 |
func TestPerformRequestWithTracerOnError(t *testing.T) { |
| 1041 |
var tw bytes.Buffer |
| 1042 |
tout := log.New(&tw, "TRACER ", log.LstdFlags) |
| 1043 |
|
| 1044 |
client, err := NewClient(SetTraceLog(tout), SetSniff(false)) |
| 1045 |
if err != nil { |
| 1046 |
t.Fatal(err) |
| 1047 |
} |
| 1048 |
|
| 1049 |
client.PerformRequest(context.TODO(), "GET", "/no-such-index", nil, nil) |
| 1050 |
|
| 1051 |
tgot := tw.String() |
| 1052 |
if tgot == "" { |
| 1053 |
t.Errorf("expected tracer output; got: %q", tgot) |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
type customLogger struct { |
| 1058 |
out bytes.Buffer |
| 1059 |
} |
| 1060 |
|
| 1061 |
func (l *customLogger) Printf(format string, v ...interface{}) { |
| 1062 |
l.out.WriteString(fmt.Sprintf(format, v...) + "\n") |
| 1063 |
} |
| 1064 |
|
| 1065 |
func TestPerformRequestWithCustomLogger(t *testing.T) { |
| 1066 |
logger := &customLogger{} |
| 1067 |
|
| 1068 |
client, err := NewClient(SetInfoLog(logger), SetSniff(false)) |
| 1069 |
if err != nil { |
| 1070 |
t.Fatal(err) |
| 1071 |
} |
| 1072 |
|
| 1073 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, nil) |
| 1074 |
if err != nil { |
| 1075 |
t.Fatal(err) |
| 1076 |
} |
| 1077 |
if res == nil { |
| 1078 |
t.Fatal("expected response to be != nil") |
| 1079 |
} |
| 1080 |
|
| 1081 |
ret := new(PingResult) |
| 1082 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 1083 |
t.Fatalf("expected no error on decode; got: %v", err) |
| 1084 |
} |
| 1085 |
if ret.ClusterName == "" { |
| 1086 |
t.Errorf("expected cluster name; got: %q", ret.ClusterName) |
| 1087 |
} |
| 1088 |
|
| 1089 |
got := logger.out.String() |
| 1090 |
pattern := `^GET http://.*/ \[status:200, request:\d+\.\d{3}s\]\n` |
| 1091 |
matched, err := regexp.MatchString(pattern, got) |
| 1092 |
if err != nil { |
| 1093 |
t.Fatalf("expected log line to match %q; got: %v", pattern, err) |
| 1094 |
} |
| 1095 |
if !matched { |
| 1096 |
t.Errorf("expected log line to match %q; got: %v", pattern, got) |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
// failingTransport will run a fail callback if it sees a given URL path prefix. |
| 1101 |
type failingTransport struct { |
| 1102 |
path string // path prefix to look for |
| 1103 |
fail func(*http.Request) (*http.Response, error) // call when path prefix is found |
| 1104 |
next http.RoundTripper // next round-tripper (use http.DefaultTransport if nil) |
| 1105 |
} |
| 1106 |
|
| 1107 |
// RoundTrip implements a failing transport. |
| 1108 |
func (tr *failingTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| 1109 |
if strings.HasPrefix(r.URL.Path, tr.path) && tr.fail != nil { |
| 1110 |
return tr.fail(r) |
| 1111 |
} |
| 1112 |
if tr.next != nil { |
| 1113 |
return tr.next.RoundTrip(r) |
| 1114 |
} |
| 1115 |
return http.DefaultTransport.RoundTrip(r) |
| 1116 |
} |
| 1117 |
|
| 1118 |
func TestPerformRequestRetryOnHttpError(t *testing.T) { |
| 1119 |
var numFailedReqs int |
| 1120 |
fail := func(r *http.Request) (*http.Response, error) { |
| 1121 |
numFailedReqs += 1 |
| 1122 |
//return &http.Response{Request: r, StatusCode: 400}, nil |
| 1123 |
return nil, errors.New("request failed") |
| 1124 |
} |
| 1125 |
|
| 1126 |
// Run against a failing endpoint and see if PerformRequest |
| 1127 |
// retries correctly. |
| 1128 |
tr := &failingTransport{path: "/fail", fail: fail} |
| 1129 |
httpClient := &http.Client{Transport: tr} |
| 1130 |
|
| 1131 |
client, err := NewClient(SetHttpClient(httpClient), SetMaxRetries(5), SetHealthcheck(false)) |
| 1132 |
if err != nil { |
| 1133 |
t.Fatal(err) |
| 1134 |
} |
| 1135 |
|
| 1136 |
res, err := client.PerformRequest(context.TODO(), "GET", "/fail", nil, nil) |
| 1137 |
if err == nil { |
| 1138 |
t.Fatal("expected error") |
| 1139 |
} |
| 1140 |
if res != nil { |
| 1141 |
t.Fatal("expected no response") |
| 1142 |
} |
| 1143 |
// Connection should be marked as dead after it failed |
| 1144 |
if numFailedReqs != 5 { |
| 1145 |
t.Errorf("expected %d failed requests; got: %d", 5, numFailedReqs) |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
func TestPerformRequestNoRetryOnValidButUnsuccessfulHttpStatus(t *testing.T) { |
| 1150 |
var numFailedReqs int |
| 1151 |
fail := func(r *http.Request) (*http.Response, error) { |
| 1152 |
numFailedReqs += 1 |
| 1153 |
return &http.Response{Request: r, StatusCode: 500}, nil |
| 1154 |
} |
| 1155 |
|
| 1156 |
// Run against a failing endpoint and see if PerformRequest |
| 1157 |
// retries correctly. |
| 1158 |
tr := &failingTransport{path: "/fail", fail: fail} |
| 1159 |
httpClient := &http.Client{Transport: tr} |
| 1160 |
|
| 1161 |
client, err := NewClient(SetHttpClient(httpClient), SetMaxRetries(5), SetHealthcheck(false)) |
| 1162 |
if err != nil { |
| 1163 |
t.Fatal(err) |
| 1164 |
} |
| 1165 |
|
| 1166 |
res, err := client.PerformRequest(context.TODO(), "GET", "/fail", nil, nil) |
| 1167 |
if err == nil { |
| 1168 |
t.Fatal("expected error") |
| 1169 |
} |
| 1170 |
if res == nil { |
| 1171 |
t.Fatal("expected response, got nil") |
| 1172 |
} |
| 1173 |
if want, got := 500, res.StatusCode; want != got { |
| 1174 |
t.Fatalf("expected status code = %d, got %d", want, got) |
| 1175 |
} |
| 1176 |
// Retry should not have triggered additional requests because |
| 1177 |
if numFailedReqs != 1 { |
| 1178 |
t.Errorf("expected %d failed requests; got: %d", 1, numFailedReqs) |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
// failingBody will return an error when json.Marshal is called on it. |
| 1183 |
type failingBody struct{} |
| 1184 |
|
| 1185 |
// MarshalJSON implements the json.Marshaler interface and always returns an error. |
| 1186 |
func (fb failingBody) MarshalJSON() ([]byte, error) { |
| 1187 |
return nil, errors.New("failing to marshal") |
| 1188 |
} |
| 1189 |
|
| 1190 |
func TestPerformRequestWithSetBodyError(t *testing.T) { |
| 1191 |
client, err := NewClient() |
| 1192 |
if err != nil { |
| 1193 |
t.Fatal(err) |
| 1194 |
} |
| 1195 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, failingBody{}) |
| 1196 |
if err == nil { |
| 1197 |
t.Fatal("expected error") |
| 1198 |
} |
| 1199 |
if res != nil { |
| 1200 |
t.Fatal("expected no response") |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
// sleepingTransport will sleep before doing a request. |
| 1205 |
type sleepingTransport struct { |
| 1206 |
timeout time.Duration |
| 1207 |
} |
| 1208 |
|
| 1209 |
// RoundTrip implements a "sleepy" transport. |
| 1210 |
func (tr *sleepingTransport) RoundTrip(r *http.Request) (*http.Response, error) { |
| 1211 |
time.Sleep(tr.timeout) |
| 1212 |
return http.DefaultTransport.RoundTrip(r) |
| 1213 |
} |
| 1214 |
|
| 1215 |
func TestPerformRequestWithCancel(t *testing.T) { |
| 1216 |
tr := &sleepingTransport{timeout: 3 * time.Second} |
| 1217 |
httpClient := &http.Client{Transport: tr} |
| 1218 |
|
| 1219 |
client, err := NewSimpleClient(SetHttpClient(httpClient), SetMaxRetries(0)) |
| 1220 |
if err != nil { |
| 1221 |
t.Fatal(err) |
| 1222 |
} |
| 1223 |
|
| 1224 |
type result struct { |
| 1225 |
res *Response |
| 1226 |
err error |
| 1227 |
} |
| 1228 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1229 |
|
| 1230 |
resc := make(chan result, 1) |
| 1231 |
go func() { |
| 1232 |
res, err := client.PerformRequest(ctx, "GET", "/", nil, nil) |
| 1233 |
resc <- result{res: res, err: err} |
| 1234 |
}() |
| 1235 |
select { |
| 1236 |
case <-time.After(1 * time.Second): |
| 1237 |
cancel() |
| 1238 |
case res := <-resc: |
| 1239 |
t.Fatalf("expected response before cancel, got %v", res) |
| 1240 |
case <-ctx.Done(): |
| 1241 |
t.Fatalf("expected no early termination, got ctx.Done(): %v", ctx.Err()) |
| 1242 |
} |
| 1243 |
err = ctx.Err() |
| 1244 |
if err != context.Canceled { |
| 1245 |
t.Fatalf("expected error context.Canceled, got: %v", err) |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
func TestPerformRequestWithTimeout(t *testing.T) { |
| 1250 |
tr := &sleepingTransport{timeout: 3 * time.Second} |
| 1251 |
httpClient := &http.Client{Transport: tr} |
| 1252 |
|
| 1253 |
client, err := NewSimpleClient(SetHttpClient(httpClient), SetMaxRetries(0)) |
| 1254 |
if err != nil { |
| 1255 |
t.Fatal(err) |
| 1256 |
} |
| 1257 |
|
| 1258 |
type result struct { |
| 1259 |
res *Response |
| 1260 |
err error |
| 1261 |
} |
| 1262 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 1263 |
defer cancel() |
| 1264 |
|
| 1265 |
resc := make(chan result, 1) |
| 1266 |
go func() { |
| 1267 |
res, err := client.PerformRequest(ctx, "GET", "/", nil, nil) |
| 1268 |
resc <- result{res: res, err: err} |
| 1269 |
}() |
| 1270 |
select { |
| 1271 |
case res := <-resc: |
| 1272 |
t.Fatalf("expected timeout before response, got %v", res) |
| 1273 |
case <-ctx.Done(): |
| 1274 |
err := ctx.Err() |
| 1275 |
if err != context.DeadlineExceeded { |
| 1276 |
t.Fatalf("expected error context.DeadlineExceeded, got: %v", err) |
| 1277 |
} |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
// -- Compression -- |
| 1282 |
|
| 1283 |
// Notice that the trace log does always print "Accept-Encoding: gzip" |
| 1284 |
// regardless of whether compression is enabled or not. This is because |
| 1285 |
// of the underlying "httputil.DumpRequestOut". |
| 1286 |
// |
| 1287 |
// Use a real HTTP proxy/recorder to convince yourself that |
| 1288 |
// "Accept-Encoding: gzip" is NOT sent when DisableCompression |
| 1289 |
// is set to true. |
| 1290 |
// |
| 1291 |
// See also: |
| 1292 |
// https://groups.google.com/forum/#!topic/golang-nuts/ms8QNCzew8Q |
| 1293 |
|
| 1294 |
func TestPerformRequestWithCompressionEnabled(t *testing.T) { |
| 1295 |
testPerformRequestWithCompression(t, &http.Client{ |
| 1296 |
Transport: &http.Transport{ |
| 1297 |
DisableCompression: true, |
| 1298 |
}, |
| 1299 |
}) |
| 1300 |
} |
| 1301 |
|
| 1302 |
func TestPerformRequestWithCompressionDisabled(t *testing.T) { |
| 1303 |
testPerformRequestWithCompression(t, &http.Client{ |
| 1304 |
Transport: &http.Transport{ |
| 1305 |
DisableCompression: false, |
| 1306 |
}, |
| 1307 |
}) |
| 1308 |
} |
| 1309 |
|
| 1310 |
func testPerformRequestWithCompression(t *testing.T, hc *http.Client) { |
| 1311 |
client, err := NewClient(SetHttpClient(hc), SetSniff(false)) |
| 1312 |
if err != nil { |
| 1313 |
t.Fatal(err) |
| 1314 |
} |
| 1315 |
res, err := client.PerformRequest(context.TODO(), "GET", "/", nil, nil) |
| 1316 |
if err != nil { |
| 1317 |
t.Fatal(err) |
| 1318 |
} |
| 1319 |
if res == nil { |
| 1320 |
t.Fatal("expected response to be != nil") |
| 1321 |
} |
| 1322 |
|
| 1323 |
ret := new(PingResult) |
| 1324 |
if err := json.Unmarshal(res.Body, ret); err != nil { |
| 1325 |
t.Fatalf("expected no error on decode; got: %v", err) |
| 1326 |
} |
| 1327 |
if ret.ClusterName == "" { |
| 1328 |
t.Errorf("expected cluster name; got: %q", ret.ClusterName) |
| 1329 |
} |
| 1330 |
} |