| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- package es
- import (
- "net/http"
- "testing"
- "github.com/grafana/grafana/pkg/components/simplejson"
- "github.com/grafana/grafana/pkg/models"
- . "github.com/smartystreets/goconvey/convey"
- )
- func TestClient(t *testing.T) {
- Convey("Test elasticsearch client", t, func() {
- Convey("NewClient", func() {
- Convey("When no version set should return error", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(make(map[string]interface{})),
- }
- _, err := NewClient(nil, ds, nil)
- So(err, ShouldNotBeNil)
- })
- Convey("When no time field name set should return error", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 5,
- }),
- }
- _, err := NewClient(nil, ds, nil)
- So(err, ShouldNotBeNil)
- })
- Convey("When unspported version set should return error", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 6,
- "timeField": "@timestamp",
- }),
- }
- _, err := NewClient(nil, ds, nil)
- So(err, ShouldNotBeNil)
- })
- Convey("When version 2 should return v2 client", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 2,
- "timeField": "@timestamp",
- }),
- }
- c, err := NewClient(nil, ds, nil)
- So(err, ShouldBeNil)
- So(c.GetVersion(), ShouldEqual, 2)
- })
- Convey("When version 5 should return v5 client", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 5,
- "timeField": "@timestamp",
- }),
- }
- c, err := NewClient(nil, ds, nil)
- So(err, ShouldBeNil)
- So(c.GetVersion(), ShouldEqual, 5)
- })
- Convey("When version 56 should return v5.6 client", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 56,
- "timeField": "@timestamp",
- }),
- }
- c, err := NewClient(nil, ds, nil)
- So(err, ShouldBeNil)
- So(c.GetVersion(), ShouldEqual, 56)
- })
- })
- Convey("v2", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 2,
- }),
- }
- c, err := newV2Client(newFakeBaseClient(ds, []string{"test-*"}))
- So(err, ShouldBeNil)
- So(c, ShouldNotBeNil)
- Convey("When creating multisearch requests should have correct headers", func() {
- multiRequests := c.createMultiSearchRequests([]*SearchRequest{
- {Index: "test-*"},
- })
- So(multiRequests, ShouldHaveLength, 1)
- header := multiRequests[0].header
- So(header, ShouldHaveLength, 3)
- So(header["index"], ShouldEqual, "test-*")
- So(header["ignore_unavailable"], ShouldEqual, true)
- So(header["search_type"], ShouldEqual, "count")
- })
- })
- Convey("v5", func() {
- ds := &models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 5,
- }),
- }
- c, err := newV5Client(newFakeBaseClient(ds, []string{"test-*"}))
- So(err, ShouldBeNil)
- So(c, ShouldNotBeNil)
- Convey("When creating multisearch requests should have correct headers", func() {
- multiRequests := c.createMultiSearchRequests([]*SearchRequest{
- {Index: "test-*"},
- })
- So(multiRequests, ShouldHaveLength, 1)
- header := multiRequests[0].header
- So(header, ShouldHaveLength, 3)
- So(header["index"], ShouldEqual, "test-*")
- So(header["ignore_unavailable"], ShouldEqual, true)
- So(header["search_type"], ShouldEqual, "query_then_fetch")
- })
- })
- Convey("v5.6", func() {
- Convey("With default settings", func() {
- ds := models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 56,
- }),
- }
- c, err := newV56Client(newFakeBaseClient(&ds, []string{"test-*"}))
- So(err, ShouldBeNil)
- So(c, ShouldNotBeNil)
- Convey("When creating multisearch requests should have correct headers", func() {
- multiRequests := c.createMultiSearchRequests([]*SearchRequest{
- {Index: "test-*"},
- })
- So(multiRequests, ShouldHaveLength, 1)
- header := multiRequests[0].header
- So(header, ShouldHaveLength, 4)
- So(header["index"], ShouldEqual, "test-*")
- So(header["ignore_unavailable"], ShouldEqual, true)
- So(header["search_type"], ShouldEqual, "query_then_fetch")
- So(header["max_concurrent_shard_requests"], ShouldEqual, 256)
- })
- })
- Convey("With custom settings", func() {
- ds := models.DataSource{
- JsonData: simplejson.NewFromAny(map[string]interface{}{
- "esVersion": 56,
- "maxConcurrentShardRequests": 100,
- }),
- }
- c, err := newV56Client(newFakeBaseClient(&ds, []string{"test-*"}))
- So(err, ShouldBeNil)
- So(c, ShouldNotBeNil)
- Convey("When creating multisearch requests should have correct headers", func() {
- multiRequests := c.createMultiSearchRequests([]*SearchRequest{
- {Index: "test-*"},
- })
- So(multiRequests, ShouldHaveLength, 1)
- header := multiRequests[0].header
- So(header, ShouldHaveLength, 4)
- So(header["index"], ShouldEqual, "test-*")
- So(header["ignore_unavailable"], ShouldEqual, true)
- So(header["search_type"], ShouldEqual, "query_then_fetch")
- So(header["max_concurrent_shard_requests"], ShouldEqual, 100)
- })
- })
- })
- })
- }
- type fakeBaseClient struct {
- *baseClientImpl
- ds *models.DataSource
- }
- func newFakeBaseClient(ds *models.DataSource, indices []string) baseClient {
- return &fakeBaseClient{
- baseClientImpl: &baseClientImpl{
- ds: ds,
- indices: indices,
- },
- ds: ds,
- }
- }
- func (c *fakeBaseClient) executeBatchRequest(uriPath string, requests []*multiRequest) (*http.Response, error) {
- return nil, nil
- }
- func (c *fakeBaseClient) executeRequest(method, uriPath string, body []byte) (*http.Response, error) {
- return nil, nil
- }
- func (c *fakeBaseClient) executeMultisearch(searchRequests []*SearchRequest) ([]*SearchResponse, error) {
- return nil, nil
- }
|