cluster_state_test.go 2.2 KB

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