client_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package es
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestClient(t *testing.T) {
  10. Convey("Test elasticsearch client", t, func() {
  11. Convey("NewClient", func() {
  12. Convey("When no version set should return error", func() {
  13. ds := &models.DataSource{
  14. JsonData: simplejson.NewFromAny(make(map[string]interface{})),
  15. }
  16. _, err := NewClient(nil, ds, nil)
  17. So(err, ShouldNotBeNil)
  18. })
  19. Convey("When no time field name set should return error", func() {
  20. ds := &models.DataSource{
  21. JsonData: simplejson.NewFromAny(map[string]interface{}{
  22. "esVersion": 5,
  23. }),
  24. }
  25. _, err := NewClient(nil, ds, nil)
  26. So(err, ShouldNotBeNil)
  27. })
  28. Convey("When unspported version set should return error", func() {
  29. ds := &models.DataSource{
  30. JsonData: simplejson.NewFromAny(map[string]interface{}{
  31. "esVersion": 6,
  32. "timeField": "@timestamp",
  33. }),
  34. }
  35. _, err := NewClient(nil, ds, nil)
  36. So(err, ShouldNotBeNil)
  37. })
  38. Convey("When version 2 should return v2 client", func() {
  39. ds := &models.DataSource{
  40. JsonData: simplejson.NewFromAny(map[string]interface{}{
  41. "esVersion": 2,
  42. "timeField": "@timestamp",
  43. }),
  44. }
  45. c, err := NewClient(nil, ds, nil)
  46. So(err, ShouldBeNil)
  47. So(c.GetVersion(), ShouldEqual, 2)
  48. })
  49. Convey("When version 5 should return v5 client", func() {
  50. ds := &models.DataSource{
  51. JsonData: simplejson.NewFromAny(map[string]interface{}{
  52. "esVersion": 5,
  53. "timeField": "@timestamp",
  54. }),
  55. }
  56. c, err := NewClient(nil, ds, nil)
  57. So(err, ShouldBeNil)
  58. So(c.GetVersion(), ShouldEqual, 5)
  59. })
  60. Convey("When version 56 should return v5.6 client", func() {
  61. ds := &models.DataSource{
  62. JsonData: simplejson.NewFromAny(map[string]interface{}{
  63. "esVersion": 56,
  64. "timeField": "@timestamp",
  65. }),
  66. }
  67. c, err := NewClient(nil, ds, nil)
  68. So(err, ShouldBeNil)
  69. So(c.GetVersion(), ShouldEqual, 56)
  70. })
  71. })
  72. Convey("v2", func() {
  73. ds := &models.DataSource{
  74. JsonData: simplejson.NewFromAny(map[string]interface{}{
  75. "esVersion": 2,
  76. }),
  77. }
  78. c, err := newV2Client(newFakeBaseClient(ds, []string{"test-*"}))
  79. So(err, ShouldBeNil)
  80. So(c, ShouldNotBeNil)
  81. Convey("When creating multisearch requests should have correct headers", func() {
  82. multiRequests := c.createMultiSearchRequests([]*SearchRequest{
  83. {Index: "test-*"},
  84. })
  85. So(multiRequests, ShouldHaveLength, 1)
  86. header := multiRequests[0].header
  87. So(header, ShouldHaveLength, 3)
  88. So(header["index"], ShouldEqual, "test-*")
  89. So(header["ignore_unavailable"], ShouldEqual, true)
  90. So(header["search_type"], ShouldEqual, "count")
  91. })
  92. })
  93. Convey("v5", func() {
  94. ds := &models.DataSource{
  95. JsonData: simplejson.NewFromAny(map[string]interface{}{
  96. "esVersion": 5,
  97. }),
  98. }
  99. c, err := newV5Client(newFakeBaseClient(ds, []string{"test-*"}))
  100. So(err, ShouldBeNil)
  101. So(c, ShouldNotBeNil)
  102. Convey("When creating multisearch requests should have correct headers", func() {
  103. multiRequests := c.createMultiSearchRequests([]*SearchRequest{
  104. {Index: "test-*"},
  105. })
  106. So(multiRequests, ShouldHaveLength, 1)
  107. header := multiRequests[0].header
  108. So(header, ShouldHaveLength, 3)
  109. So(header["index"], ShouldEqual, "test-*")
  110. So(header["ignore_unavailable"], ShouldEqual, true)
  111. So(header["search_type"], ShouldEqual, "query_then_fetch")
  112. })
  113. })
  114. Convey("v5.6", func() {
  115. Convey("With default settings", func() {
  116. ds := models.DataSource{
  117. JsonData: simplejson.NewFromAny(map[string]interface{}{
  118. "esVersion": 56,
  119. }),
  120. }
  121. c, err := newV56Client(newFakeBaseClient(&ds, []string{"test-*"}))
  122. So(err, ShouldBeNil)
  123. So(c, ShouldNotBeNil)
  124. Convey("When creating multisearch requests should have correct headers", func() {
  125. multiRequests := c.createMultiSearchRequests([]*SearchRequest{
  126. {Index: "test-*"},
  127. })
  128. So(multiRequests, ShouldHaveLength, 1)
  129. header := multiRequests[0].header
  130. So(header, ShouldHaveLength, 4)
  131. So(header["index"], ShouldEqual, "test-*")
  132. So(header["ignore_unavailable"], ShouldEqual, true)
  133. So(header["search_type"], ShouldEqual, "query_then_fetch")
  134. So(header["max_concurrent_shard_requests"], ShouldEqual, 256)
  135. })
  136. })
  137. Convey("With custom settings", func() {
  138. ds := models.DataSource{
  139. JsonData: simplejson.NewFromAny(map[string]interface{}{
  140. "esVersion": 56,
  141. "maxConcurrentShardRequests": 100,
  142. }),
  143. }
  144. c, err := newV56Client(newFakeBaseClient(&ds, []string{"test-*"}))
  145. So(err, ShouldBeNil)
  146. So(c, ShouldNotBeNil)
  147. Convey("When creating multisearch requests should have correct headers", func() {
  148. multiRequests := c.createMultiSearchRequests([]*SearchRequest{
  149. {Index: "test-*"},
  150. })
  151. So(multiRequests, ShouldHaveLength, 1)
  152. header := multiRequests[0].header
  153. So(header, ShouldHaveLength, 4)
  154. So(header["index"], ShouldEqual, "test-*")
  155. So(header["ignore_unavailable"], ShouldEqual, true)
  156. So(header["search_type"], ShouldEqual, "query_then_fetch")
  157. So(header["max_concurrent_shard_requests"], ShouldEqual, 100)
  158. })
  159. })
  160. })
  161. })
  162. }
  163. type fakeBaseClient struct {
  164. *baseClientImpl
  165. ds *models.DataSource
  166. }
  167. func newFakeBaseClient(ds *models.DataSource, indices []string) baseClient {
  168. return &fakeBaseClient{
  169. baseClientImpl: &baseClientImpl{
  170. ds: ds,
  171. indices: indices,
  172. },
  173. ds: ds,
  174. }
  175. }
  176. func (c *fakeBaseClient) executeBatchRequest(uriPath string, requests []*multiRequest) (*http.Response, error) {
  177. return nil, nil
  178. }
  179. func (c *fakeBaseClient) executeRequest(method, uriPath string, body []byte) (*http.Response, error) {
  180. return nil, nil
  181. }
  182. func (c *fakeBaseClient) executeMultisearch(searchRequests []*SearchRequest) ([]*SearchResponse, error) {
  183. return nil, nil
  184. }