cluster_stats_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 TestClusterStats(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. // Get cluster stats
  13. res, err := client.ClusterStats().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. if res.Nodes == nil {
  24. t.Fatalf("expected nodes; got: %v", res.Nodes)
  25. }
  26. if res.Nodes.Count == nil {
  27. t.Fatalf("expected nodes count; got: %v", res.Nodes.Count)
  28. }
  29. }
  30. func TestClusterStatsURLs(t *testing.T) {
  31. fFlag := false
  32. tFlag := true
  33. tests := []struct {
  34. Service *ClusterStatsService
  35. ExpectedPath string
  36. ExpectedParams url.Values
  37. }{
  38. {
  39. Service: &ClusterStatsService{
  40. nodeId: []string{},
  41. },
  42. ExpectedPath: "/_cluster/stats",
  43. },
  44. {
  45. Service: &ClusterStatsService{
  46. nodeId: []string{"node1"},
  47. },
  48. ExpectedPath: "/_cluster/stats/nodes/node1",
  49. },
  50. {
  51. Service: &ClusterStatsService{
  52. nodeId: []string{"node1", "node2"},
  53. },
  54. ExpectedPath: "/_cluster/stats/nodes/node1%2Cnode2",
  55. },
  56. {
  57. Service: &ClusterStatsService{
  58. nodeId: []string{},
  59. flatSettings: &tFlag,
  60. },
  61. ExpectedPath: "/_cluster/stats",
  62. ExpectedParams: url.Values{"flat_settings": []string{"true"}},
  63. },
  64. {
  65. Service: &ClusterStatsService{
  66. nodeId: []string{"node1"},
  67. flatSettings: &fFlag,
  68. },
  69. ExpectedPath: "/_cluster/stats/nodes/node1",
  70. ExpectedParams: url.Values{"flat_settings": []string{"false"}},
  71. },
  72. }
  73. for _, test := range tests {
  74. gotPath, gotParams, err := test.Service.buildURL()
  75. if err != nil {
  76. t.Fatalf("expected no error; got: %v", err)
  77. }
  78. if gotPath != test.ExpectedPath {
  79. t.Errorf("expected URL path = %q; got: %q", test.ExpectedPath, gotPath)
  80. }
  81. if gotParams.Encode() != test.ExpectedParams.Encode() {
  82. t.Errorf("expected URL params = %v; got: %v", test.ExpectedParams, gotParams)
  83. }
  84. }
  85. }