Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cluster_health_test.go 3.3KB

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