Debian Codegraph @

cluster_health_test.go

raw
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 TestClusterHealth(t *testing.T) {
14 client := setupTestClientAndCreateIndex(t)
15
16 // Get cluster health
17 res, err := client.ClusterHealth().Index(testIndexName).Level("shards").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.Status != "green" && res.Status != "red" && res.Status != "yellow" {
25 t.Fatalf("expected status \"green\", \"red\", or \"yellow\"; got: %q", res.Status)
26 }
27 }
28
29 func TestClusterHealthURLs(t *testing.T) {
30 tests := []struct {
31 Service *ClusterHealthService
32 ExpectedPath string
33 ExpectedParams url.Values
34 }{
35 {
36 Service: &ClusterHealthService{
37 indices: []string{},
38 },
39 ExpectedPath: "/_cluster/health",
40 },
41 {
42 Service: &ClusterHealthService{
43 indices: []string{"twitter"},
44 },
45 ExpectedPath: "/_cluster/health/twitter",
46 },
47 {
48 Service: &ClusterHealthService{
49 indices: []string{"twitter", "gplus"},
50 },
51 ExpectedPath: "/_cluster/health/twitter%2Cgplus",
52 },
53 {
54 Service: &ClusterHealthService{
55 indices: []string{"twitter"},
56 waitForStatus: "yellow",
57 },
58 ExpectedPath: "/_cluster/health/twitter",
59 ExpectedParams: url.Values{"wait_for_status": []string{"yellow"}},
60 },
61 }
62
63 for _, test := range tests {
64 gotPath, gotParams, err := test.Service.buildURL()
65 if err != nil {
66 t.Fatalf("expected no error; got: %v", err)
67 }
68 if gotPath != test.ExpectedPath {
69 t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath)
70 }
71 if gotParams.Encode() != test.ExpectedParams.Encode() {
72 t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams)
73 }
74 }
75 }
76
77 func TestClusterHealthWaitForStatus(t *testing.T) {
78 client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
79
80 // Ensure preconditions are met: A green cluster.
81 health, err := client.ClusterHealth().Do(context.TODO())
82 if err != nil {
83 t.Fatal(err)
84 }
85 if got, want := health.Status, "green"; got != want {
86 t.Skipf("precondition failed: expected cluster to be %q, not %q", want, got)
87 }
88
89 // Cluster health on an index that does not exist should never get to yellow
90 health, err = client.ClusterHealth().Index("no-such-index").WaitForStatus("yellow").Timeout("1s").Do(context.TODO())
91 if err == nil {
92 t.Fatalf("expected timeout error; got: %v", err)
93 }
94 if !IsTimeout(err) {
95 t.Fatalf("expected timeout error; got: %v", err)
96 }
97 if health != nil {
98 t.Fatalf("expected no response; got: %v", health)
99 }
100
101 // Cluster wide health
102 health, err = client.ClusterHealth().WaitForGreenStatus().Timeout("10s").Do(context.TODO())
103 if err != nil {
104 t.Fatalf("expected no error; got: %v", err)
105 }
106 if health.TimedOut != false {
107 t.Fatalf("expected no timeout; got: %v "+
108 "(does your local cluster contain unassigned shards?)", health.TimedOut)
109 }
110 if health.Status != "green" {
111 t.Fatalf("expected health = %q; got: %q", "green", health.Status)
112 }
113
114 // Cluster wide health via shortcut on client
115 err = client.WaitForGreenStatus("10s")
116 if err != nil {
117 t.Fatalf("expected no error; got: %v", err)
118 }
119 }