cluster_health_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2012-2015 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. "net/url"
  7. "testing"
  8. )
  9. func TestClusterHealth(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. // Get cluster health
  12. res, err := client.ClusterHealth().Index(testIndexName).Do()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if res == nil {
  17. t.Fatalf("expected res to be != nil; got: %v", res)
  18. }
  19. if res.Status != "green" && res.Status != "red" && res.Status != "yellow" {
  20. t.Fatalf("expected status \"green\", \"red\", or \"yellow\"; got: %q", res.Status)
  21. }
  22. }
  23. func TestClusterHealthURLs(t *testing.T) {
  24. tests := []struct {
  25. Service *ClusterHealthService
  26. ExpectedPath string
  27. ExpectedParams url.Values
  28. }{
  29. {
  30. Service: &ClusterHealthService{
  31. indices: []string{},
  32. },
  33. ExpectedPath: "/_cluster/health/",
  34. },
  35. {
  36. Service: &ClusterHealthService{
  37. indices: []string{"twitter"},
  38. },
  39. ExpectedPath: "/_cluster/health/twitter",
  40. },
  41. {
  42. Service: &ClusterHealthService{
  43. indices: []string{"twitter", "gplus"},
  44. },
  45. ExpectedPath: "/_cluster/health/twitter%2Cgplus",
  46. },
  47. {
  48. Service: &ClusterHealthService{
  49. indices: []string{"twitter"},
  50. waitForStatus: "yellow",
  51. },
  52. ExpectedPath: "/_cluster/health/twitter",
  53. ExpectedParams: url.Values{"wait_for_status": []string{"yellow"}},
  54. },
  55. }
  56. for _, test := range tests {
  57. gotPath, gotParams, err := test.Service.buildURL()
  58. if err != nil {
  59. t.Fatalf("expected no error; got: %v", err)
  60. }
  61. if gotPath != test.ExpectedPath {
  62. t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath)
  63. }
  64. if gotParams.Encode() != test.ExpectedParams.Encode() {
  65. t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams)
  66. }
  67. }
  68. }