mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
parent
f9601f8f27
commit
6189f48be7
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user