| 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 |
"context" |
| 9 |
"net/url" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestClusterState(t *testing.T) { |
| 14 |
client := setupTestClientAndCreateIndex(t) |
| 15 |
|
| 16 |
// Get cluster state |
| 17 |
res, err := client.ClusterState().Index("_all").Metric("_all").Pretty(true).Do(context.TODO()) |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
if res == nil { |
| 22 |
t.Fatalf("expected res to be != nil; got: %v", res) |
| 23 |
} |
| 24 |
if res.ClusterName == "" { |
| 25 |
t.Fatalf("expected a cluster name; got: %q", res.ClusterName) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func TestClusterStateURLs(t *testing.T) { |
| 30 |
tests := []struct { |
| 31 |
Service *ClusterStateService |
| 32 |
ExpectedPath string |
| 33 |
ExpectedParams url.Values |
| 34 |
}{ |
| 35 |
{ |
| 36 |
Service: &ClusterStateService{ |
| 37 |
indices: []string{}, |
| 38 |
metrics: []string{}, |
| 39 |
}, |
| 40 |
ExpectedPath: "/_cluster/state/_all/_all", |
| 41 |
}, |
| 42 |
{ |
| 43 |
Service: &ClusterStateService{ |
| 44 |
indices: []string{"twitter"}, |
| 45 |
metrics: []string{}, |
| 46 |
}, |
| 47 |
ExpectedPath: "/_cluster/state/_all/twitter", |
| 48 |
}, |
| 49 |
{ |
| 50 |
Service: &ClusterStateService{ |
| 51 |
indices: []string{"twitter", "gplus"}, |
| 52 |
metrics: []string{}, |
| 53 |
}, |
| 54 |
ExpectedPath: "/_cluster/state/_all/twitter%2Cgplus", |
| 55 |
}, |
| 56 |
{ |
| 57 |
Service: &ClusterStateService{ |
| 58 |
indices: []string{}, |
| 59 |
metrics: []string{"nodes"}, |
| 60 |
}, |
| 61 |
ExpectedPath: "/_cluster/state/nodes/_all", |
| 62 |
}, |
| 63 |
{ |
| 64 |
Service: &ClusterStateService{ |
| 65 |
indices: []string{"twitter"}, |
| 66 |
metrics: []string{"nodes"}, |
| 67 |
}, |
| 68 |
ExpectedPath: "/_cluster/state/nodes/twitter", |
| 69 |
}, |
| 70 |
{ |
| 71 |
Service: &ClusterStateService{ |
| 72 |
indices: []string{"twitter"}, |
| 73 |
metrics: []string{"nodes"}, |
| 74 |
masterTimeout: "1s", |
| 75 |
}, |
| 76 |
ExpectedPath: "/_cluster/state/nodes/twitter", |
| 77 |
ExpectedParams: url.Values{"master_timeout": []string{"1s"}}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for _, test := range tests { |
| 82 |
gotPath, gotParams, err := test.Service.buildURL() |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("expected no error; got: %v", err) |
| 85 |
} |
| 86 |
if gotPath != test.ExpectedPath { |
| 87 |
t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath) |
| 88 |
} |
| 89 |
if gotParams.Encode() != test.ExpectedParams.Encode() { |
| 90 |
t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams) |
| 91 |
} |
| 92 |
} |
| 93 |
} |