elastic: more tests (#68533)

* elastic: more tests

* fix lint error
This commit is contained in:
Gábor Farkas 2023-05-16 17:09:09 +02:00 committed by GitHub
parent f9601f8f27
commit 6189f48be7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -111,6 +111,104 @@ func TestClient_ExecuteMultisearch(t *testing.T) {
})
}
func TestClient_Index(t *testing.T) {
tt := []struct {
name string
indexInDatasource string
patternInDatasource string
indexInRequest []string
}{
{
name: "single string",
indexInDatasource: "logs-*",
patternInDatasource: "",
indexInRequest: []string{"logs-*"},
},
{
name: "daily pattern",
indexInDatasource: "[logs-]YYYY.MM.DD",
patternInDatasource: "Daily",
indexInRequest: []string{"logs-2018.05.10", "logs-2018.05.11", "logs-2018.05.12"},
},
}
for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
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": [
{
"hits": { "hits": [], "max_score": 0, "total": { "value": 4656, "relation": "eq"} },
"status": 200
}
]
}`))
require.NoError(t, err)
rw.WriteHeader(200)
}))
configuredFields := ConfiguredFields{
TimeField: "testtime",
LogMessageField: "line",
LogLevelField: "lvl",
}
ds := DatasourceInfo{
URL: ts.URL,
HTTPClient: ts.Client(),
Database: test.indexInDatasource,
ConfiguredFields: configuredFields,
Interval: test.patternInDatasource,
MaxConcurrentShardRequests: 6,
IncludeFrozen: true,
XPack: true,
}
from := time.Date(2018, 5, 10, 17, 50, 0, 0, time.UTC)
to := time.Date(2018, 5, 12, 17, 55, 0, 0, time.UTC)
timeRange := backend.TimeRange{
From: from,
To: to,
}
c, err := NewClient(context.Background(), &ds, timeRange)
require.NoError(t, err)
require.NotNil(t, c)
t.Cleanup(func() {
ts.Close()
})
ms, err := createMultisearchForTest(t, c)
require.NoError(t, err)
_, err = c.ExecuteMultisearch(ms)
require.NoError(t, err)
require.NotNil(t, request)
require.NotNil(t, requestBody)
headerBytes, err := requestBody.ReadBytes('\n')
require.NoError(t, err)
jHeader, err := simplejson.NewJson(headerBytes)
require.NoError(t, err)
assert.Equal(t, test.indexInRequest, jHeader.Get("index").MustStringArray())
})
}
}
func createMultisearchForTest(t *testing.T, c Client) (*MultiSearchRequest, error) {
t.Helper()