forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_reroute_test.go
93 lines (85 loc) · 2.25 KB
/
cluster_reroute_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
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"net/url"
"testing"
)
func TestClusterRerouteURLs(t *testing.T) {
trueFlag := true
truePtr := &trueFlag
tests := []struct {
Service *ClusterRerouteService
ExpectedPath string
ExpectedParams url.Values
}{
{
Service: &ClusterRerouteService{},
ExpectedPath: "/_cluster/reroute",
},
{
Service: &ClusterRerouteService{
dryRun: truePtr,
metrics: []string{"blocks", "nodes"},
},
ExpectedPath: "/_cluster/reroute",
ExpectedParams: url.Values{
"dry_run": []string{"true"},
"metric": []string{"blocks,nodes"},
},
},
}
for _, tt := range tests {
gotPath, gotParams, err := tt.Service.buildURL()
if err != nil {
t.Fatalf("expected no error; got: %v", err)
}
if gotPath != tt.ExpectedPath {
t.Errorf("expected URL path = %q; got: %q", tt.ExpectedPath, gotPath)
}
if gotParams.Encode() != tt.ExpectedParams.Encode() {
t.Errorf("expected URL params = %v; got: %v", tt.ExpectedParams, gotParams)
}
}
}
func TestClusterReroute(t *testing.T) {
client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
// Get cluster nodes
var nodes []string
{
res, err := client.ClusterState().Do(context.Background())
if err != nil {
t.Fatal(err)
}
for node := range res.Nodes {
nodes = append(nodes, node)
}
if len(nodes) == 0 {
t.Fatal("expected at least one node in cluster")
}
}
// Perform a nop cluster reroute
res, err := client.ClusterReroute().
DryRun(true).
Add(
NewMoveAllocationCommand(testIndexName, 0, nodes[0], nodes[0]),
NewCancelAllocationCommand(testIndexName, 0, nodes[0], true),
).
Do(context.Background())
// Expect an error here: We just test if it's of a specific kind
if err == nil {
t.Fatal("expected an error, got nil")
}
if res != nil {
t.Fatalf("expected res to be != nil; got: %v", res)
}
e, ok := err.(*Error)
if !ok {
t.Fatalf("expected an error of type *elastic.Error, got %T", err)
}
if want, have := 400, e.Status; want != have {
t.Fatalf("expected Status=%d, have %d", want, have)
}
}