2022-04-27 03:29:39 -05:00
|
|
|
package searchV2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-16 18:22:45 -05:00
|
|
|
"flag"
|
|
|
|
"path/filepath"
|
2022-04-27 03:29:39 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/services/searchV2/extract"
|
2022-04-27 03:29:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/store"
|
2022-05-16 18:22:45 -05:00
|
|
|
|
|
|
|
"github.com/blugelabs/bluge"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/experimental"
|
2022-04-27 03:29:39 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testDashboardLoader struct {
|
|
|
|
dashboards []dashboard
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
func (t *testDashboardLoader) LoadDashboards(_ context.Context, _ int64, _ string) ([]dashboard, error) {
|
2022-04-27 03:29:39 -05:00
|
|
|
return t.dashboards, nil
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
var testLogger = log.New("index-test-logger")
|
|
|
|
|
|
|
|
var testAllowAllFilter = func(uid string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDisallowAllFilter = func(uid string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var update = flag.Bool("update", false, "update golden files")
|
|
|
|
|
|
|
|
func initTestIndexFromDashes(t *testing.T, dashboards []dashboard) (*dashboardIndex, *bluge.Reader, *bluge.Writer) {
|
|
|
|
t.Helper()
|
2022-04-27 03:29:39 -05:00
|
|
|
dashboardLoader := &testDashboardLoader{
|
2022-05-16 18:22:45 -05:00
|
|
|
dashboards: dashboards,
|
2022-04-27 03:29:39 -05:00
|
|
|
}
|
|
|
|
index := newDashboardIndex(dashboardLoader, &store.MockEntityEventsService{})
|
|
|
|
require.NotNil(t, index)
|
2022-05-16 18:22:45 -05:00
|
|
|
numDashboards, err := index.buildOrgIndex(context.Background(), 1)
|
2022-04-27 03:29:39 -05:00
|
|
|
require.NoError(t, err)
|
2022-05-16 18:22:45 -05:00
|
|
|
require.Equal(t, len(dashboardLoader.dashboards), numDashboards)
|
|
|
|
reader, ok := index.getOrgReader(1)
|
|
|
|
require.True(t, ok)
|
|
|
|
writer, ok := index.getOrgWriter(1)
|
|
|
|
require.True(t, ok)
|
|
|
|
return index, reader, writer
|
|
|
|
}
|
2022-04-27 03:29:39 -05:00
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
func checkSearchResponse(t *testing.T, fileName string, reader *bluge.Reader, filter ResourceFilter, query DashboardQuery) {
|
|
|
|
t.Helper()
|
|
|
|
resp := doSearchQuery(context.Background(), testLogger, reader, filter, query)
|
|
|
|
goldenFile := filepath.Join("testdata", fileName)
|
|
|
|
err := experimental.CheckGoldenDataResponse(goldenFile, resp, *update)
|
2022-04-27 03:29:39 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
var testDashboards = []dashboard{
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
uid: "1",
|
|
|
|
info: &extract.DashboardInfo{
|
|
|
|
Title: "test",
|
2022-04-27 03:29:39 -05:00
|
|
|
},
|
2022-05-16 18:22:45 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
uid: "2",
|
|
|
|
info: &extract.DashboardInfo{
|
|
|
|
Title: "boom",
|
2022-04-27 03:29:39 -05:00
|
|
|
},
|
2022-05-16 18:22:45 -05:00
|
|
|
},
|
2022-04-27 03:29:39 -05:00
|
|
|
}
|
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
func TestDashboardIndex(t *testing.T) {
|
|
|
|
t.Run("basic-search", func(t *testing.T) {
|
|
|
|
_, reader, _ := initTestIndexFromDashes(t, testDashboards)
|
|
|
|
checkSearchResponse(t, filepath.Base(t.Name())+".txt", reader, testAllowAllFilter,
|
|
|
|
DashboardQuery{Query: "boom"},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("basic-filter", func(t *testing.T) {
|
|
|
|
_, reader, _ := initTestIndexFromDashes(t, testDashboards)
|
|
|
|
checkSearchResponse(t, filepath.Base(t.Name())+".txt", reader, testDisallowAllFilter,
|
|
|
|
DashboardQuery{Query: "boom"},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDashboardIndexUpdates(t *testing.T) {
|
|
|
|
t.Run("dashboard-delete", func(t *testing.T) {
|
|
|
|
index, reader, writer := initTestIndexFromDashes(t, testDashboards)
|
|
|
|
|
|
|
|
newReader, err := index.removeDashboard(writer, reader, "2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkSearchResponse(t, filepath.Base(t.Name())+".txt", newReader, testAllowAllFilter,
|
|
|
|
DashboardQuery{Query: "boom"},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("dashboard-create", func(t *testing.T) {
|
|
|
|
index, reader, writer := initTestIndexFromDashes(t, testDashboards)
|
|
|
|
|
|
|
|
newReader, err := index.updateDashboard(writer, reader, dashboard{
|
|
|
|
id: 3,
|
|
|
|
uid: "3",
|
|
|
|
info: &extract.DashboardInfo{
|
|
|
|
Title: "created",
|
2022-04-27 03:29:39 -05:00
|
|
|
},
|
2022-05-16 18:22:45 -05:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-04-27 03:29:39 -05:00
|
|
|
|
2022-05-16 18:22:45 -05:00
|
|
|
checkSearchResponse(t, filepath.Base(t.Name())+".txt", newReader, testAllowAllFilter,
|
|
|
|
DashboardQuery{Query: "created"},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("dashboard-update", func(t *testing.T) {
|
|
|
|
index, reader, writer := initTestIndexFromDashes(t, testDashboards)
|
|
|
|
|
|
|
|
newReader, err := index.updateDashboard(writer, reader, dashboard{
|
|
|
|
id: 2,
|
|
|
|
uid: "2",
|
|
|
|
info: &extract.DashboardInfo{
|
|
|
|
Title: "nginx",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkSearchResponse(t, filepath.Base(t.Name())+".txt", newReader, testAllowAllFilter,
|
|
|
|
DashboardQuery{Query: "nginx"},
|
|
|
|
)
|
|
|
|
})
|
2022-04-27 03:29:39 -05:00
|
|
|
}
|