elastic: simplify unit test (#55168)

This commit is contained in:
Gábor Farkas 2022-09-14 14:59:35 +02:00 committed by GitHub
parent 57bc472755
commit 6dcc8534f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,41 +19,81 @@ import (
) )
func TestClient_ExecuteMultisearch(t *testing.T) { func TestClient_ExecuteMultisearch(t *testing.T) {
version, err := semver.NewVersion("8.0.0") t.Run("Given a fake http client and a client with response", func(t *testing.T) {
require.NoError(t, err) version, err := semver.NewVersion("8.0.0")
httpClientScenario(t, "Given a fake http client and a v7.0 client with response", &DatasourceInfo{ require.NoError(t, err)
Database: "[metrics-]YYYY.MM.DD", ds := DatasourceInfo{
ESVersion: version, Database: "[metrics-]YYYY.MM.DD",
TimeField: "@timestamp", ESVersion: version,
Interval: "Daily", TimeField: "@timestamp",
MaxConcurrentShardRequests: 6, Interval: "Daily",
IncludeFrozen: true, MaxConcurrentShardRequests: 6,
XPack: true, IncludeFrozen: true,
}, func(sc *scenarioContext) { XPack: true,
sc.responseBody = `{ }
var request *http.Request
var requestBody *bytes.Buffer
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
request = r
buf, err := io.ReadAll(r.Body)
require.NoError(t, err)
requestBody = bytes.NewBuffer(buf)
rw.Header().Set("Content-Type", "application/x-ndjson")
_, err = rw.Write([]byte(
`{
"responses": [ "responses": [
{ {
"hits": { "hits": [], "max_score": 0, "total": { "value": 4656, "relation": "eq"} }, "hits": { "hits": [], "max_score": 0, "total": { "value": 4656, "relation": "eq"} },
"status": 200 "status": 200
} }
] ]
}` }`))
require.NoError(t, err)
rw.WriteHeader(200)
}))
ds.URL = ts.URL
ms, err := createMultisearchForTest(t, sc.client) from := time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC)
to := time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC)
timeRange := backend.TimeRange{
From: from,
To: to,
}
c, err := NewClient(context.Background(), httpclient.NewProvider(), &ds, timeRange)
require.NoError(t, err) require.NoError(t, err)
res, err := sc.client.ExecuteMultisearch(ms) require.NotNil(t, c)
currentNewDatasourceHTTPClient := newDatasourceHttpClient
newDatasourceHttpClient = func(httpClientProvider httpclient.Provider, ds *DatasourceInfo) (*http.Client, error) {
return ts.Client(), nil
}
t.Cleanup(func() {
ts.Close()
newDatasourceHttpClient = currentNewDatasourceHTTPClient
})
ms, err := createMultisearchForTest(t, c)
require.NoError(t, err)
res, err := c.ExecuteMultisearch(ms)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, sc.request) require.NotNil(t, request)
assert.Equal(t, http.MethodPost, sc.request.Method) assert.Equal(t, http.MethodPost, request.Method)
assert.Equal(t, "/_msearch", sc.request.URL.Path) assert.Equal(t, "/_msearch", request.URL.Path)
assert.Equal(t, "max_concurrent_shard_requests=6&ignore_throttled=false", sc.request.URL.RawQuery) assert.Equal(t, "max_concurrent_shard_requests=6&ignore_throttled=false", request.URL.RawQuery)
require.NotNil(t, sc.requestBody) require.NotNil(t, requestBody)
headerBytes, err := sc.requestBody.ReadBytes('\n') headerBytes, err := requestBody.ReadBytes('\n')
require.NoError(t, err) require.NoError(t, err)
bodyBytes := sc.requestBody.Bytes() bodyBytes := requestBody.Bytes()
jHeader, err := simplejson.NewJson(headerBytes) jHeader, err := simplejson.NewJson(headerBytes)
require.NoError(t, err) require.NoError(t, err)
@ -90,62 +130,3 @@ func createMultisearchForTest(t *testing.T, c Client) (*MultiSearchRequest, erro
}) })
return msb.Build() return msb.Build()
} }
type scenarioContext struct {
client Client
request *http.Request
requestBody *bytes.Buffer
responseStatus int
responseBody string
}
type scenarioFunc func(*scenarioContext)
func httpClientScenario(t *testing.T, desc string, ds *DatasourceInfo, fn scenarioFunc) {
t.Helper()
t.Run(desc, func(t *testing.T) {
sc := &scenarioContext{
responseStatus: 200,
responseBody: `{ "responses": [] }`,
}
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
sc.request = r
buf, err := io.ReadAll(r.Body)
require.NoError(t, err)
sc.requestBody = bytes.NewBuffer(buf)
rw.Header().Set("Content-Type", "application/x-ndjson")
_, err = rw.Write([]byte(sc.responseBody))
require.NoError(t, err)
rw.WriteHeader(sc.responseStatus)
}))
ds.URL = ts.URL
from := time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC)
to := time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC)
timeRange := backend.TimeRange{
From: from,
To: to,
}
c, err := NewClient(context.Background(), httpclient.NewProvider(), ds, timeRange)
require.NoError(t, err)
require.NotNil(t, c)
sc.client = c
currentNewDatasourceHTTPClient := newDatasourceHttpClient
newDatasourceHttpClient = func(httpClientProvider httpclient.Provider, ds *DatasourceInfo) (*http.Client, error) {
return ts.Client(), nil
}
t.Cleanup(func() {
ts.Close()
newDatasourceHttpClient = currentNewDatasourceHTTPClient
})
fn(sc)
})
}