| 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 TestClusterStats(t *testing.T) { |
| 14 |
client := setupTestClientAndCreateIndex(t) |
| 15 |
|
| 16 |
// Get cluster stats |
| 17 |
res, err := client.ClusterStats().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 |
if res.Nodes == nil { |
| 28 |
t.Fatalf("expected nodes; got: %v", res.Nodes) |
| 29 |
} |
| 30 |
if res.Nodes.Count == nil { |
| 31 |
t.Fatalf("expected nodes count; got: %v", res.Nodes.Count) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestClusterStatsURLs(t *testing.T) { |
| 36 |
fFlag := false |
| 37 |
tFlag := true |
| 38 |
|
| 39 |
tests := []struct { |
| 40 |
Service *ClusterStatsService |
| 41 |
ExpectedPath string |
| 42 |
ExpectedParams url.Values |
| 43 |
}{ |
| 44 |
{ |
| 45 |
Service: &ClusterStatsService{ |
| 46 |
nodeId: []string{}, |
| 47 |
}, |
| 48 |
ExpectedPath: "/_cluster/stats", |
| 49 |
}, |
| 50 |
{ |
| 51 |
Service: &ClusterStatsService{ |
| 52 |
nodeId: []string{"node1"}, |
| 53 |
}, |
| 54 |
ExpectedPath: "/_cluster/stats/nodes/node1", |
| 55 |
}, |
| 56 |
{ |
| 57 |
Service: &ClusterStatsService{ |
| 58 |
nodeId: []string{"node1", "node2"}, |
| 59 |
}, |
| 60 |
ExpectedPath: "/_cluster/stats/nodes/node1%2Cnode2", |
| 61 |
}, |
| 62 |
{ |
| 63 |
Service: &ClusterStatsService{ |
| 64 |
nodeId: []string{}, |
| 65 |
flatSettings: &tFlag, |
| 66 |
}, |
| 67 |
ExpectedPath: "/_cluster/stats", |
| 68 |
ExpectedParams: url.Values{"flat_settings": []string{"true"}}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
Service: &ClusterStatsService{ |
| 72 |
nodeId: []string{"node1"}, |
| 73 |
flatSettings: &fFlag, |
| 74 |
}, |
| 75 |
ExpectedPath: "/_cluster/stats/nodes/node1", |
| 76 |
ExpectedParams: url.Values{"flat_settings": []string{"false"}}, |
| 77 |
}, |
| 78 |
} |
| 79 |
|
| 80 |
for _, test := range tests { |
| 81 |
gotPath, gotParams, err := test.Service.buildURL() |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("expected no error; got: %v", err) |
| 84 |
} |
| 85 |
if gotPath != test.ExpectedPath { |
| 86 |
t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath) |
| 87 |
} |
| 88 |
if gotParams.Encode() != test.ExpectedParams.Encode() { |
| 89 |
t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams) |
| 90 |
} |
| 91 |
} |
| 92 |
} |