mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Rewrite elasticsearch client test to standard library (#30093)
This commit is contained in:
parent
2b9387210b
commit
b2b3a603e8
@ -11,310 +11,276 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/tsdb"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:goconst
|
func TestNewClient(t *testing.T) {
|
||||||
func TestClient(t *testing.T) {
|
t.Run("When no version set should return error", func(t *testing.T) {
|
||||||
Convey("Test elasticsearch client", t, func() {
|
ds := &models.DataSource{
|
||||||
Convey("NewClient", func() {
|
JsonData: simplejson.NewFromAny(make(map[string]interface{})),
|
||||||
Convey("When no version set should return error", func() {
|
}
|
||||||
ds := &models.DataSource{
|
|
||||||
JsonData: simplejson.NewFromAny(make(map[string]interface{})),
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := NewClient(context.Background(), ds, nil)
|
_, err := NewClient(context.Background(), ds, nil)
|
||||||
So(err, ShouldNotBeNil)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When no time field name set should return error", func() {
|
t.Run("When no time field name set should return error", func(t *testing.T) {
|
||||||
ds := &models.DataSource{
|
ds := &models.DataSource{
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"esVersion": 5,
|
"esVersion": 5,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := NewClient(context.Background(), ds, nil)
|
_, err := NewClient(context.Background(), ds, nil)
|
||||||
So(err, ShouldNotBeNil)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When unsupported version set should return error", func() {
|
t.Run("When unsupported version set should return error", func(t *testing.T) {
|
||||||
ds := &models.DataSource{
|
ds := &models.DataSource{
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"esVersion": 6,
|
"esVersion": 6,
|
||||||
"timeField": "@timestamp",
|
"timeField": "@timestamp",
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := NewClient(context.Background(), ds, nil)
|
_, err := NewClient(context.Background(), ds, nil)
|
||||||
So(err, ShouldNotBeNil)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When version 2 should return v2 client", func() {
|
t.Run("When version 2 should return v2 client", func(t *testing.T) {
|
||||||
ds := &models.DataSource{
|
ds := &models.DataSource{
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
|
||||||
"esVersion": 2,
|
|
||||||
"timeField": "@timestamp",
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := NewClient(context.Background(), 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(context.Background(), 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(context.Background(), ds, nil)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(c.GetVersion(), ShouldEqual, 56)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("When version 60 should return v6.0 client", func() {
|
|
||||||
ds := &models.DataSource{
|
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
|
||||||
"esVersion": 60,
|
|
||||||
"timeField": "@timestamp",
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := NewClient(context.Background(), ds, nil)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(c.GetVersion(), ShouldEqual, 60)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("When version 70 should return v7.0 client", func() {
|
|
||||||
ds := &models.DataSource{
|
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
|
||||||
"esVersion": 70,
|
|
||||||
"timeField": "@timestamp",
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := NewClient(context.Background(), ds, nil)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(c.GetVersion(), ShouldEqual, 70)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
httpClientScenario(t, "Given a fake http client and a v2.x client with response", &models.DataSource{
|
|
||||||
Database: "[metrics-]YYYY.MM.DD",
|
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"esVersion": 2,
|
"esVersion": 2,
|
||||||
"timeField": "@timestamp",
|
"timeField": "@timestamp",
|
||||||
"interval": "Daily",
|
|
||||||
}),
|
}),
|
||||||
}, func(sc *scenarioContext) {
|
}
|
||||||
sc.responseBody = `{
|
|
||||||
|
c, err := NewClient(context.Background(), ds, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 2, c.GetVersion())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When version 5 should return v5 client", func(t *testing.T) {
|
||||||
|
ds := &models.DataSource{
|
||||||
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"esVersion": 5,
|
||||||
|
"timeField": "@timestamp",
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := NewClient(context.Background(), ds, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 5, c.GetVersion())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When version 56 should return v5.6 client", func(t *testing.T) {
|
||||||
|
ds := &models.DataSource{
|
||||||
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"esVersion": 56,
|
||||||
|
"timeField": "@timestamp",
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := NewClient(context.Background(), ds, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 56, c.GetVersion())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When version 60 should return v6.0 client", func(t *testing.T) {
|
||||||
|
ds := &models.DataSource{
|
||||||
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"esVersion": 60,
|
||||||
|
"timeField": "@timestamp",
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := NewClient(context.Background(), ds, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 60, c.GetVersion())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When version 70 should return v7.0 client", func(t *testing.T) {
|
||||||
|
ds := &models.DataSource{
|
||||||
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"esVersion": 70,
|
||||||
|
"timeField": "@timestamp",
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := NewClient(context.Background(), ds, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 70, c.GetVersion())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClient_ExecuteMultisearch(t *testing.T) {
|
||||||
|
httpClientScenario(t, "Given a fake http client and a v2.x client with response", &models.DataSource{
|
||||||
|
Database: "[metrics-]YYYY.MM.DD",
|
||||||
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"esVersion": 2,
|
||||||
|
"timeField": "@timestamp",
|
||||||
|
"interval": "Daily",
|
||||||
|
}),
|
||||||
|
}, func(sc *scenarioContext) {
|
||||||
|
sc.responseBody = `{
|
||||||
"responses": [
|
"responses": [
|
||||||
{
|
{
|
||||||
"hits": { "hits": [], "max_score": 0, "total": 4656 },
|
"hits": { "hits": [], "max_score": 0, "total": 4656 },
|
||||||
"status": 200
|
"status": 200
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
Convey("When executing multi search", func() {
|
ms, err := createMultisearchForTest(t, sc.client)
|
||||||
ms, err := createMultisearchForTest(sc.client)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
res, err := sc.client.ExecuteMultisearch(ms)
|
||||||
res, err := sc.client.ExecuteMultisearch(ms)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("Should send correct request and payload", func() {
|
require.NotNil(t, sc.request)
|
||||||
So(sc.request, ShouldNotBeNil)
|
assert.Equal(t, http.MethodPost, sc.request.Method)
|
||||||
So(sc.request.Method, ShouldEqual, http.MethodPost)
|
assert.Equal(t, "/_msearch", sc.request.URL.Path)
|
||||||
So(sc.request.URL.Path, ShouldEqual, "/_msearch")
|
|
||||||
|
|
||||||
So(sc.requestBody, ShouldNotBeNil)
|
require.NotNil(t, sc.requestBody)
|
||||||
|
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
||||||
|
require.NoError(t, err)
|
||||||
|
bodyBytes := sc.requestBody.Bytes()
|
||||||
|
|
||||||
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
jHeader, err := simplejson.NewJson(headerBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
bodyBytes := sc.requestBody.Bytes()
|
|
||||||
|
|
||||||
jHeader, err := simplejson.NewJson(headerBytes)
|
jBody, err := simplejson.NewJson(bodyBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
jBody, err := simplejson.NewJson(bodyBytes)
|
assert.Equal(t, "metrics-2018.05.15", jHeader.Get("index").MustString())
|
||||||
So(err, ShouldBeNil)
|
assert.True(t, jHeader.Get("ignore_unavailable").MustBool(false))
|
||||||
|
assert.Equal(t, "count", jHeader.Get("search_type").MustString())
|
||||||
|
assert.Empty(t, jHeader.Get("max_concurrent_shard_requests"))
|
||||||
|
|
||||||
So(jHeader.Get("index").MustString(), ShouldEqual, "metrics-2018.05.15")
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
So(jHeader.Get("ignore_unavailable").MustBool(false), ShouldEqual, true)
|
|
||||||
So(jHeader.Get("search_type").MustString(), ShouldEqual, "count")
|
|
||||||
So(jHeader.Get("max_concurrent_shard_requests").MustInt(10), ShouldEqual, 10)
|
|
||||||
|
|
||||||
Convey("and replace $__interval variable", func() {
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString(), ShouldEqual, "15000*@hostname")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and replace $__interval_ms variable", func() {
|
assert.Equal(t, 200, res.Status)
|
||||||
So(jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString(), ShouldEqual, "15s")
|
require.Len(t, res.Responses, 1)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should parse response", func() {
|
httpClientScenario(t, "Given a fake http client and a v5.x client with response", &models.DataSource{
|
||||||
So(res.Status, ShouldEqual, 200)
|
Database: "[metrics-]YYYY.MM.DD",
|
||||||
So(res.Responses, ShouldHaveLength, 1)
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
})
|
"esVersion": 5,
|
||||||
})
|
"maxConcurrentShardRequests": 100,
|
||||||
})
|
"timeField": "@timestamp",
|
||||||
|
"interval": "Daily",
|
||||||
httpClientScenario(t, "Given a fake http client and a v5.x client with response", &models.DataSource{
|
}),
|
||||||
Database: "[metrics-]YYYY.MM.DD",
|
}, func(sc *scenarioContext) {
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
sc.responseBody = `{
|
||||||
"esVersion": 5,
|
|
||||||
"maxConcurrentShardRequests": 100,
|
|
||||||
"timeField": "@timestamp",
|
|
||||||
"interval": "Daily",
|
|
||||||
}),
|
|
||||||
}, func(sc *scenarioContext) {
|
|
||||||
sc.responseBody = `{
|
|
||||||
"responses": [
|
"responses": [
|
||||||
{
|
{
|
||||||
"hits": { "hits": [], "max_score": 0, "total": 4656 },
|
"hits": { "hits": [], "max_score": 0, "total": 4656 },
|
||||||
"status": 200
|
"status": 200
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
Convey("When executing multi search", func() {
|
ms, err := createMultisearchForTest(t, sc.client)
|
||||||
ms, err := createMultisearchForTest(sc.client)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
res, err := sc.client.ExecuteMultisearch(ms)
|
||||||
res, err := sc.client.ExecuteMultisearch(ms)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("Should send correct request and payload", func() {
|
require.NotNil(t, sc.request)
|
||||||
So(sc.request, ShouldNotBeNil)
|
assert.Equal(t, http.MethodPost, sc.request.Method)
|
||||||
So(sc.request.Method, ShouldEqual, http.MethodPost)
|
assert.Equal(t, "/_msearch", sc.request.URL.Path)
|
||||||
So(sc.request.URL.Path, ShouldEqual, "/_msearch")
|
|
||||||
|
|
||||||
So(sc.requestBody, ShouldNotBeNil)
|
require.NotNil(t, sc.requestBody)
|
||||||
|
|
||||||
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
bodyBytes := sc.requestBody.Bytes()
|
bodyBytes := sc.requestBody.Bytes()
|
||||||
|
|
||||||
jHeader, err := simplejson.NewJson(headerBytes)
|
jHeader, err := simplejson.NewJson(headerBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
jBody, err := simplejson.NewJson(bodyBytes)
|
jBody, err := simplejson.NewJson(bodyBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
So(jHeader.Get("index").MustString(), ShouldEqual, "metrics-2018.05.15")
|
assert.Equal(t, "metrics-2018.05.15", jHeader.Get("index").MustString())
|
||||||
So(jHeader.Get("ignore_unavailable").MustBool(false), ShouldEqual, true)
|
assert.True(t, jHeader.Get("ignore_unavailable").MustBool(false))
|
||||||
So(jHeader.Get("search_type").MustString(), ShouldEqual, "query_then_fetch")
|
assert.Equal(t, "query_then_fetch", jHeader.Get("search_type").MustString())
|
||||||
So(jHeader.Get("max_concurrent_shard_requests").MustInt(10), ShouldEqual, 10)
|
assert.Empty(t, jHeader.Get("max_concurrent_shard_requests"))
|
||||||
|
|
||||||
Convey("and replace $__interval variable", func() {
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString(), ShouldEqual, "15000*@hostname")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and replace $__interval_ms variable", func() {
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString(), ShouldEqual, "15s")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should parse response", func() {
|
assert.Equal(t, 200, res.Status)
|
||||||
So(res.Status, ShouldEqual, 200)
|
require.Len(t, res.Responses, 1)
|
||||||
So(res.Responses, ShouldHaveLength, 1)
|
})
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
httpClientScenario(t, "Given a fake http client and a v5.6 client with response", &models.DataSource{
|
httpClientScenario(t, "Given a fake http client and a v5.6 client with response", &models.DataSource{
|
||||||
Database: "[metrics-]YYYY.MM.DD",
|
Database: "[metrics-]YYYY.MM.DD",
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"esVersion": 56,
|
"esVersion": 56,
|
||||||
"maxConcurrentShardRequests": 100,
|
"maxConcurrentShardRequests": 100,
|
||||||
"timeField": "@timestamp",
|
"timeField": "@timestamp",
|
||||||
"interval": "Daily",
|
"interval": "Daily",
|
||||||
}),
|
}),
|
||||||
}, func(sc *scenarioContext) {
|
}, func(sc *scenarioContext) {
|
||||||
sc.responseBody = `{
|
sc.responseBody = `{
|
||||||
"responses": [
|
"responses": [
|
||||||
{
|
{
|
||||||
"hits": { "hits": [], "max_score": 0, "total": 4656 },
|
"hits": { "hits": [], "max_score": 0, "total": 4656 },
|
||||||
"status": 200
|
"status": 200
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
Convey("When executing multi search", func() {
|
ms, err := createMultisearchForTest(t, sc.client)
|
||||||
ms, err := createMultisearchForTest(sc.client)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
res, err := sc.client.ExecuteMultisearch(ms)
|
||||||
res, err := sc.client.ExecuteMultisearch(ms)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("Should send correct request and payload", func() {
|
require.NotNil(t, sc.request)
|
||||||
So(sc.request, ShouldNotBeNil)
|
assert.Equal(t, http.MethodPost, sc.request.Method)
|
||||||
So(sc.request.Method, ShouldEqual, http.MethodPost)
|
assert.Equal(t, "/_msearch", sc.request.URL.Path)
|
||||||
So(sc.request.URL.Path, ShouldEqual, "/_msearch")
|
|
||||||
|
|
||||||
So(sc.requestBody, ShouldNotBeNil)
|
require.NotNil(t, sc.requestBody)
|
||||||
|
|
||||||
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
bodyBytes := sc.requestBody.Bytes()
|
bodyBytes := sc.requestBody.Bytes()
|
||||||
|
|
||||||
jHeader, err := simplejson.NewJson(headerBytes)
|
jHeader, err := simplejson.NewJson(headerBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
jBody, err := simplejson.NewJson(bodyBytes)
|
jBody, err := simplejson.NewJson(bodyBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
So(jHeader.Get("index").MustString(), ShouldEqual, "metrics-2018.05.15")
|
assert.Equal(t, "metrics-2018.05.15", jHeader.Get("index").MustString())
|
||||||
So(jHeader.Get("ignore_unavailable").MustBool(false), ShouldEqual, true)
|
assert.True(t, jHeader.Get("ignore_unavailable").MustBool(false))
|
||||||
So(jHeader.Get("search_type").MustString(), ShouldEqual, "query_then_fetch")
|
assert.Equal(t, "query_then_fetch", jHeader.Get("search_type").MustString())
|
||||||
So(jHeader.Get("max_concurrent_shard_requests").MustInt(), ShouldEqual, 100)
|
assert.Equal(t, 100, jHeader.Get("max_concurrent_shard_requests").MustInt())
|
||||||
|
|
||||||
Convey("and replace $__interval variable", func() {
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString(), ShouldEqual, "15000*@hostname")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and replace $__interval_ms variable", func() {
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString(), ShouldEqual, "15s")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should parse response", func() {
|
assert.Equal(t, 200, res.Status)
|
||||||
So(res.Status, ShouldEqual, 200)
|
require.Len(t, res.Responses, 1)
|
||||||
So(res.Responses, ShouldHaveLength, 1)
|
})
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
httpClientScenario(t, "Given a fake http client and a v7.0 client with response", &models.DataSource{
|
httpClientScenario(t, "Given a fake http client and a v7.0 client with response", &models.DataSource{
|
||||||
Database: "[metrics-]YYYY.MM.DD",
|
Database: "[metrics-]YYYY.MM.DD",
|
||||||
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
JsonData: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"esVersion": 70,
|
"esVersion": 70,
|
||||||
"maxConcurrentShardRequests": 6,
|
"maxConcurrentShardRequests": 6,
|
||||||
"timeField": "@timestamp",
|
"timeField": "@timestamp",
|
||||||
"interval": "Daily",
|
"interval": "Daily",
|
||||||
}),
|
}),
|
||||||
}, func(sc *scenarioContext) {
|
}, func(sc *scenarioContext) {
|
||||||
sc.responseBody = `{
|
sc.responseBody = `{
|
||||||
"responses": [
|
"responses": [
|
||||||
{
|
{
|
||||||
"hits": { "hits": [], "max_score": 0, "total": { "value": 4656, "relation": "eq"} },
|
"hits": { "hits": [], "max_score": 0, "total": { "value": 4656, "relation": "eq"} },
|
||||||
@ -323,53 +289,45 @@ func TestClient(t *testing.T) {
|
|||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
Convey("When executing multi search", func() {
|
ms, err := createMultisearchForTest(t, sc.client)
|
||||||
ms, err := createMultisearchForTest(sc.client)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
res, err := sc.client.ExecuteMultisearch(ms)
|
||||||
res, err := sc.client.ExecuteMultisearch(ms)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("Should send correct request and payload", func() {
|
require.NotNil(t, sc.request)
|
||||||
So(sc.request, ShouldNotBeNil)
|
assert.Equal(t, http.MethodPost, sc.request.Method)
|
||||||
So(sc.request.Method, ShouldEqual, http.MethodPost)
|
assert.Equal(t, "/_msearch", sc.request.URL.Path)
|
||||||
So(sc.request.URL.Path, ShouldEqual, "/_msearch")
|
assert.Equal(t, "max_concurrent_shard_requests=6", sc.request.URL.RawQuery)
|
||||||
So(sc.request.URL.RawQuery, ShouldEqual, "max_concurrent_shard_requests=6")
|
|
||||||
|
|
||||||
So(sc.requestBody, ShouldNotBeNil)
|
require.NotNil(t, sc.requestBody)
|
||||||
|
|
||||||
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
headerBytes, err := sc.requestBody.ReadBytes('\n')
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
bodyBytes := sc.requestBody.Bytes()
|
bodyBytes := sc.requestBody.Bytes()
|
||||||
|
|
||||||
jHeader, err := simplejson.NewJson(headerBytes)
|
jHeader, err := simplejson.NewJson(headerBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
jBody, err := simplejson.NewJson(bodyBytes)
|
jBody, err := simplejson.NewJson(bodyBytes)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
So(jHeader.Get("index").MustString(), ShouldEqual, "metrics-2018.05.15")
|
assert.Equal(t, "metrics-2018.05.15", jHeader.Get("index").MustString())
|
||||||
So(jHeader.Get("ignore_unavailable").MustBool(false), ShouldEqual, true)
|
assert.True(t, jHeader.Get("ignore_unavailable").MustBool(false))
|
||||||
So(jHeader.Get("search_type").MustString(), ShouldEqual, "query_then_fetch")
|
assert.Equal(t, "query_then_fetch", jHeader.Get("search_type").MustString())
|
||||||
|
assert.Empty(t, jHeader.Get("max_concurrent_shard_requests"))
|
||||||
|
|
||||||
Convey("and replace $__interval variable", func() {
|
assert.Equal(t, "15000*@hostname", jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "aggs", "1", "avg", "script").MustString(), ShouldEqual, "15000*@hostname")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("and replace $__interval_ms variable", func() {
|
assert.Equal(t, "15s", jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString())
|
||||||
So(jBody.GetPath("aggs", "2", "date_histogram", "interval").MustString(), ShouldEqual, "15s")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should parse response", func() {
|
assert.Equal(t, 200, res.Status)
|
||||||
So(res.Status, ShouldEqual, 200)
|
require.Len(t, res.Responses, 1)
|
||||||
So(res.Responses, ShouldHaveLength, 1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMultisearchForTest(c Client) (*MultiSearchRequest, error) {
|
func createMultisearchForTest(t *testing.T, c Client) (*MultiSearchRequest, error) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
msb := c.MultiSearch()
|
msb := c.MultiSearch()
|
||||||
s := msb.Search(tsdb.Interval{Value: 15 * time.Second, Text: "15s"})
|
s := msb.Search(tsdb.Interval{Value: 15 * time.Second, Text: "15s"})
|
||||||
s.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) {
|
s.Agg().DateHistogram("2", "@timestamp", func(a *DateHistogramAgg, ab AggBuilder) {
|
||||||
@ -395,7 +353,7 @@ type scenarioFunc func(*scenarioContext)
|
|||||||
func httpClientScenario(t *testing.T, desc string, ds *models.DataSource, fn scenarioFunc) {
|
func httpClientScenario(t *testing.T, desc string, ds *models.DataSource, fn scenarioFunc) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
Convey(desc, func() {
|
t.Run(desc, func(t *testing.T) {
|
||||||
sc := &scenarioContext{
|
sc := &scenarioContext{
|
||||||
responseStatus: 200,
|
responseStatus: 200,
|
||||||
responseBody: `{ "responses": [] }`,
|
responseBody: `{ "responses": [] }`,
|
||||||
@ -403,13 +361,13 @@ func httpClientScenario(t *testing.T, desc string, ds *models.DataSource, fn sce
|
|||||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
sc.request = r
|
sc.request = r
|
||||||
buf, err := ioutil.ReadAll(r.Body)
|
buf, err := ioutil.ReadAll(r.Body)
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
sc.requestBody = bytes.NewBuffer(buf)
|
sc.requestBody = bytes.NewBuffer(buf)
|
||||||
|
|
||||||
rw.Header().Set("Content-Type", "application/json")
|
rw.Header().Set("Content-Type", "application/json")
|
||||||
_, err = rw.Write([]byte(sc.responseBody))
|
_, err = rw.Write([]byte(sc.responseBody))
|
||||||
require.Nil(t, err)
|
require.NoError(t, err)
|
||||||
rw.WriteHeader(sc.responseStatus)
|
rw.WriteHeader(sc.responseStatus)
|
||||||
}))
|
}))
|
||||||
ds.Url = ts.URL
|
ds.Url = ts.URL
|
||||||
@ -421,20 +379,20 @@ func httpClientScenario(t *testing.T, desc string, ds *models.DataSource, fn sce
|
|||||||
timeRange := tsdb.NewTimeRange(fromStr, toStr)
|
timeRange := tsdb.NewTimeRange(fromStr, toStr)
|
||||||
|
|
||||||
c, err := NewClient(context.Background(), ds, timeRange)
|
c, err := NewClient(context.Background(), ds, timeRange)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
So(c, ShouldNotBeNil)
|
require.NotNil(t, c)
|
||||||
sc.client = c
|
sc.client = c
|
||||||
|
|
||||||
currentNewDatasourceHttpClient := newDatasourceHttpClient
|
currentNewDatasourceHTTPClient := newDatasourceHttpClient
|
||||||
|
|
||||||
newDatasourceHttpClient = func(ds *models.DataSource) (*http.Client, error) {
|
newDatasourceHttpClient = func(ds *models.DataSource) (*http.Client, error) {
|
||||||
return ts.Client(), nil
|
return ts.Client(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
t.Cleanup(func() {
|
||||||
ts.Close()
|
ts.Close()
|
||||||
newDatasourceHttpClient = currentNewDatasourceHttpClient
|
newDatasourceHttpClient = currentNewDatasourceHTTPClient
|
||||||
}()
|
})
|
||||||
|
|
||||||
fn(sc)
|
fn(sc)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user