Chore: Rewrite elasticsearch client test to standard library (#30093)

This commit is contained in:
Emil Hessman 2021-01-07 09:35:56 +01:00 committed by GitHub
parent 2b9387210b
commit b2b3a603e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,27 +11,23 @@ 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() {
Convey("NewClient", func() {
Convey("When no version set should return error", func() {
ds := &models.DataSource{ ds := &models.DataSource{
JsonData: simplejson.NewFromAny(make(map[string]interface{})), 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,
@ -39,10 +35,10 @@ func TestClient(t *testing.T) {
} }
_, 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,
@ -51,10 +47,10 @@ func TestClient(t *testing.T) {
} }
_, 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{}{ JsonData: simplejson.NewFromAny(map[string]interface{}{
"esVersion": 2, "esVersion": 2,
@ -63,11 +59,11 @@ func TestClient(t *testing.T) {
} }
c, err := NewClient(context.Background(), ds, nil) c, err := NewClient(context.Background(), ds, nil)
So(err, ShouldBeNil) require.NoError(t, err)
So(c.GetVersion(), ShouldEqual, 2) assert.Equal(t, 2, c.GetVersion())
}) })
Convey("When version 5 should return v5 client", func() { t.Run("When version 5 should return v5 client", 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,
@ -76,11 +72,11 @@ func TestClient(t *testing.T) {
} }
c, err := NewClient(context.Background(), ds, nil) c, err := NewClient(context.Background(), ds, nil)
So(err, ShouldBeNil) require.NoError(t, err)
So(c.GetVersion(), ShouldEqual, 5) assert.Equal(t, 5, c.GetVersion())
}) })
Convey("When version 56 should return v5.6 client", func() { t.Run("When version 56 should return v5.6 client", func(t *testing.T) {
ds := &models.DataSource{ ds := &models.DataSource{
JsonData: simplejson.NewFromAny(map[string]interface{}{ JsonData: simplejson.NewFromAny(map[string]interface{}{
"esVersion": 56, "esVersion": 56,
@ -89,11 +85,11 @@ func TestClient(t *testing.T) {
} }
c, err := NewClient(context.Background(), ds, nil) c, err := NewClient(context.Background(), ds, nil)
So(err, ShouldBeNil) require.NoError(t, err)
So(c.GetVersion(), ShouldEqual, 56) assert.Equal(t, 56, c.GetVersion())
}) })
Convey("When version 60 should return v6.0 client", func() { t.Run("When version 60 should return v6.0 client", func(t *testing.T) {
ds := &models.DataSource{ ds := &models.DataSource{
JsonData: simplejson.NewFromAny(map[string]interface{}{ JsonData: simplejson.NewFromAny(map[string]interface{}{
"esVersion": 60, "esVersion": 60,
@ -102,11 +98,11 @@ func TestClient(t *testing.T) {
} }
c, err := NewClient(context.Background(), ds, nil) c, err := NewClient(context.Background(), ds, nil)
So(err, ShouldBeNil) require.NoError(t, err)
So(c.GetVersion(), ShouldEqual, 60) assert.Equal(t, 60, c.GetVersion())
}) })
Convey("When version 70 should return v7.0 client", func() { t.Run("When version 70 should return v7.0 client", func(t *testing.T) {
ds := &models.DataSource{ ds := &models.DataSource{
JsonData: simplejson.NewFromAny(map[string]interface{}{ JsonData: simplejson.NewFromAny(map[string]interface{}{
"esVersion": 70, "esVersion": 70,
@ -115,11 +111,12 @@ func TestClient(t *testing.T) {
} }
c, err := NewClient(context.Background(), ds, nil) c, err := NewClient(context.Background(), ds, nil)
So(err, ShouldBeNil) require.NoError(t, err)
So(c.GetVersion(), ShouldEqual, 70) 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{ httpClientScenario(t, "Given a fake http client and a v2.x 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{}{
@ -137,48 +134,37 @@ 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)
So(err, ShouldBeNil) require.NoError(t, err)
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, "count") assert.Equal(t, "count", 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.x client with response", &models.DataSource{ httpClientScenario(t, "Given a fake http client and a v5.x client with response", &models.DataSource{
@ -199,48 +185,38 @@ 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)
So(err, ShouldBeNil) require.NoError(t, err)
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{
@ -261,48 +237,38 @@ 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)
So(err, ShouldBeNil) require.NoError(t, err)
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{
@ -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)
So(err, ShouldBeNil) require.NoError(t, err)
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)
}) })