cluster_state_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 TestClusterState(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. // Get cluster state
  13. res, err := client.ClusterState().Index("_all").Metric("_all").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.ClusterName == "" {
  21. t.Fatalf("expected a cluster name; got: %q", res.ClusterName)
  22. }
  23. }
  24. func TestClusterStateURLs(t *testing.T) {
  25. tests := []struct {
  26. Service *ClusterStateService
  27. ExpectedPath string
  28. ExpectedParams url.Values
  29. }{
  30. {
  31. Service: &ClusterStateService{
  32. indices: []string{},
  33. metrics: []string{},
  34. },
  35. ExpectedPath: "/_cluster/state/_all/_all",
  36. },
  37. {
  38. Service: &ClusterStateService{
  39. indices: []string{"twitter"},
  40. metrics: []string{},
  41. },
  42. ExpectedPath: "/_cluster/state/_all/twitter",
  43. },
  44. {
  45. Service: &ClusterStateService{
  46. indices: []string{"twitter", "gplus"},
  47. metrics: []string{},
  48. },
  49. ExpectedPath: "/_cluster/state/_all/twitter%2Cgplus",
  50. },
  51. {
  52. Service: &ClusterStateService{
  53. indices: []string{},
  54. metrics: []string{"nodes"},
  55. },
  56. ExpectedPath: "/_cluster/state/nodes/_all",
  57. },
  58. {
  59. Service: &ClusterStateService{
  60. indices: []string{"twitter"},
  61. metrics: []string{"nodes"},
  62. },
  63. ExpectedPath: "/_cluster/state/nodes/twitter",
  64. },
  65. {
  66. Service: &ClusterStateService{
  67. indices: []string{"twitter"},
  68. metrics: []string{"nodes"},
  69. masterTimeout: "1s",
  70. },
  71. ExpectedPath: "/_cluster/state/nodes/twitter",
  72. ExpectedParams: url.Values{"master_timeout": []string{"1s"}},
  73. },
  74. }
  75. for _, test := range tests {
  76. gotPath, gotParams, err := test.Service.buildURL()
  77. if err != nil {
  78. t.Fatalf("expected no error; got: %v", err)
  79. }
  80. if gotPath != test.ExpectedPath {
  81. t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath)
  82. }
  83. if gotParams.Encode() != test.ExpectedParams.Encode() {
  84. t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams)
  85. }
  86. }
  87. }