grafana/pkg/services/searchV2/extract/dashboard_test.go
Emil Tullstedt b287047052
Chore: Upgrade Go to 1.19.1 (#54902)
* WIP

* Set public_suffix to a pre Ruby 2.6 version

* we don't need to install python

* Stretch->Buster

* Bump versions in lib.star

* Manually update linter

Sort of messy, but the .mod-file need to contain all dependencies that
use 1.16+ features, otherwise they're assumed to be compiled with
-lang=go1.16 and cannot access generics et al.

Bingo doesn't seem to understand that, but it's possible to manually
update things to get Bingo happy.

* undo reformatting

* Various lint improvements

* More from the linter

* goimports -w ./pkg/

* Disable gocritic

* Add/modify linter exceptions

* lint + flatten nested list

Go 1.19 doesn't support nested lists, and there wasn't an obvious workaround.
https://go.dev/doc/comment#lists
2022-09-12 12:03:49 +02:00

129 lines
3.2 KiB
Go

package extract
import (
"encoding/json"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/services/searchV2/dslookup"
)
func dsLookup() dslookup.DatasourceLookup {
return dslookup.CreateDatasourceLookup([]*dslookup.DatasourceQueryResult{
{
UID: "P8045C56BDA891CB2",
Type: "cloudwatch",
Name: "cloudwatch-name",
IsDefault: false,
},
{
UID: "PD8C576611E62080A",
Type: "testdata",
Name: "gdev-testdata",
IsDefault: false,
},
{
UID: "dgd92lq7k",
Type: "frser-sqlite-datasource",
Name: "frser-sqlite-datasource-name",
IsDefault: false,
},
{
UID: "sqlite-1",
Type: "sqlite-datasource",
Name: "SQLite Grafana",
IsDefault: false,
},
{
UID: "sqlite-2",
Type: "sqlite-datasource",
Name: "SQLite Grafana2",
IsDefault: false,
},
{
UID: "default.uid",
Type: "default.type",
Name: "default.name",
IsDefault: true,
},
})
}
func TestReadDashboard(t *testing.T) {
inputs := []string{
"check-string-datasource-id",
"all-panels",
"panel-graph/graph-shared-tooltips",
"datasource-variable",
"default-datasource-variable",
"empty-datasource-variable",
"repeated-datasource-variables",
"string-datasource-variable",
"datasource-variable-no-curly-braces",
"all-selected-multi-datasource-variable",
"all-selected-single-datasource-variable",
"repeated-datasource-variables-with-default",
"mixed-datasource-with-variable",
"special-datasource-types",
"panels-without-datasources",
}
devdash := "../../../../devenv/dev-dashboards/"
for _, input := range inputs {
// nolint:gosec
// We can ignore the gosec G304 warning because this is a test with hardcoded input values
f, err := os.Open(filepath.Join(devdash, input) + ".json")
if err == nil {
input = "devdash-" + filepath.Base(input)
}
if err != nil {
// nolint:gosec
// We can ignore the gosec G304 warning because this is a test with hardcoded input values
f, err = os.Open(filepath.Join("testdata", input) + ".json")
}
require.NoError(t, err)
dash, err := ReadDashboard(f, dsLookup())
sortDatasources(dash)
require.NoError(t, err)
out, err := json.MarshalIndent(dash, "", " ")
require.NoError(t, err)
update := false
savedPath := filepath.Join("testdata/", input+"-info.json")
//nolint:gosec
saved, err := os.ReadFile(savedPath)
if err != nil {
update = true
assert.NoError(t, err)
} else if !assert.JSONEq(t, string(saved), string(out)) {
update = true
}
if update {
_ = os.WriteFile(savedPath, out, 0600)
}
}
}
// assure consistent ordering of datasources to prevent random failures of `assert.JSONEq`
func sortDatasources(dash *DashboardInfo) {
sort.Slice(dash.Datasource, func(i, j int) bool {
return strings.Compare(dash.Datasource[i].UID, dash.Datasource[j].UID) > 0
})
for panelId := range dash.Panels {
sort.Slice(dash.Panels[panelId].Datasource, func(i, j int) bool {
return strings.Compare(dash.Panels[panelId].Datasource[i].UID, dash.Panels[panelId].Datasource[j].UID) > 0
})
}
}