nodes_stats_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "testing"
  8. )
  9. func TestNodesStats(t *testing.T) {
  10. client, err := NewClient()
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. info, err := client.NodesStats().Human(true).Do(context.TODO())
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. if info == nil {
  19. t.Fatal("expected nodes stats")
  20. }
  21. if info.ClusterName == "" {
  22. t.Errorf("expected cluster name; got: %q", info.ClusterName)
  23. }
  24. if len(info.Nodes) == 0 {
  25. t.Errorf("expected some nodes; got: %d", len(info.Nodes))
  26. }
  27. for id, node := range info.Nodes {
  28. if id == "" {
  29. t.Errorf("expected node id; got: %q", id)
  30. }
  31. if node == nil {
  32. t.Fatalf("expected node info; got: %v", node)
  33. }
  34. if len(node.Name) == 0 {
  35. t.Errorf("expected node name; got: %q", node.Name)
  36. }
  37. if node.Timestamp == 0 {
  38. t.Errorf("expected timestamp; got: %q", node.Timestamp)
  39. }
  40. }
  41. }
  42. func TestNodesStatsBuildURL(t *testing.T) {
  43. tests := []struct {
  44. NodeIds []string
  45. Metrics []string
  46. IndexMetrics []string
  47. Expected string
  48. }{
  49. {
  50. NodeIds: nil,
  51. Metrics: nil,
  52. IndexMetrics: nil,
  53. Expected: "/_nodes/stats",
  54. },
  55. {
  56. NodeIds: []string{"node1"},
  57. Metrics: nil,
  58. IndexMetrics: nil,
  59. Expected: "/_nodes/node1/stats",
  60. },
  61. {
  62. NodeIds: []string{"node1", "node2"},
  63. Metrics: nil,
  64. IndexMetrics: nil,
  65. Expected: "/_nodes/node1%2Cnode2/stats",
  66. },
  67. {
  68. NodeIds: nil,
  69. Metrics: []string{"indices"},
  70. IndexMetrics: nil,
  71. Expected: "/_nodes/stats/indices",
  72. },
  73. {
  74. NodeIds: nil,
  75. Metrics: []string{"indices", "jvm"},
  76. IndexMetrics: nil,
  77. Expected: "/_nodes/stats/indices%2Cjvm",
  78. },
  79. {
  80. NodeIds: []string{"node1"},
  81. Metrics: []string{"indices", "jvm"},
  82. IndexMetrics: nil,
  83. Expected: "/_nodes/node1/stats/indices%2Cjvm",
  84. },
  85. {
  86. NodeIds: nil,
  87. Metrics: nil,
  88. IndexMetrics: []string{"fielddata"},
  89. Expected: "/_nodes/stats/_all/fielddata",
  90. },
  91. {
  92. NodeIds: []string{"node1"},
  93. Metrics: nil,
  94. IndexMetrics: []string{"fielddata"},
  95. Expected: "/_nodes/node1/stats/_all/fielddata",
  96. },
  97. {
  98. NodeIds: nil,
  99. Metrics: []string{"indices"},
  100. IndexMetrics: []string{"fielddata"},
  101. Expected: "/_nodes/stats/indices/fielddata",
  102. },
  103. {
  104. NodeIds: []string{"node1"},
  105. Metrics: []string{"indices"},
  106. IndexMetrics: []string{"fielddata"},
  107. Expected: "/_nodes/node1/stats/indices/fielddata",
  108. },
  109. {
  110. NodeIds: []string{"node1", "node2"},
  111. Metrics: []string{"indices", "jvm"},
  112. IndexMetrics: []string{"fielddata", "docs"},
  113. Expected: "/_nodes/node1%2Cnode2/stats/indices%2Cjvm/fielddata%2Cdocs",
  114. },
  115. }
  116. client, err := NewClient()
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. for i, tt := range tests {
  121. svc := client.NodesStats().NodeId(tt.NodeIds...).Metric(tt.Metrics...).IndexMetric(tt.IndexMetrics...)
  122. path, _, err := svc.buildURL()
  123. if err != nil {
  124. t.Errorf("#%d: expected no error, got %v", i, err)
  125. } else {
  126. if want, have := tt.Expected, path; want != have {
  127. t.Errorf("#%d: expected %q, got %q", i, want, have)
  128. }
  129. }
  130. }
  131. }