2022-02-15 10:26:03 -08:00
|
|
|
package extract
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2022-07-08 01:59:24 +04:00
|
|
|
"sort"
|
|
|
|
|
"strings"
|
2022-02-15 10:26:03 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2022-07-08 01:59:24 +04:00
|
|
|
type dsLookup struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *dsLookup) ByRef(ref *DataSourceRef) *DataSourceRef {
|
|
|
|
|
if ref == nil || ref.UID == "" {
|
|
|
|
|
return &DataSourceRef{
|
|
|
|
|
UID: "default.uid",
|
|
|
|
|
Type: "default.type",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ref.UID == "default" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return ref
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *dsLookup) ByType(dsType string) []DataSourceRef {
|
|
|
|
|
if dsType == "sqlite-datasource" {
|
|
|
|
|
return []DataSourceRef{
|
|
|
|
|
{
|
|
|
|
|
UID: "sqlite-1",
|
|
|
|
|
Type: "sqlite-datasource",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
UID: "sqlite-2",
|
|
|
|
|
Type: "sqlite-datasource",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return make([]DataSourceRef, 0)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 10:26:03 -08:00
|
|
|
func TestReadDashboard(t *testing.T) {
|
|
|
|
|
inputs := []string{
|
2022-03-30 09:50:32 -07:00
|
|
|
"check-string-datasource-id",
|
|
|
|
|
"all-panels",
|
|
|
|
|
"panel-graph/graph-shared-tooltips",
|
2022-07-08 01:59:24 +04:00
|
|
|
"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",
|
2022-07-23 05:10:53 +04:00
|
|
|
"mixed-datasource-with-variable",
|
|
|
|
|
"special-datasource-types",
|
|
|
|
|
"panels-without-datasources",
|
2022-02-15 10:26:03 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-30 09:50:32 -07:00
|
|
|
devdash := "../../../../devenv/dev-dashboards/"
|
|
|
|
|
|
2022-02-15 10:26:03 -08:00
|
|
|
for _, input := range inputs {
|
|
|
|
|
// nolint:gosec
|
|
|
|
|
// We can ignore the gosec G304 warning because this is a test with hardcoded input values
|
2022-03-30 09:50:32 -07:00
|
|
|
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")
|
|
|
|
|
}
|
2022-02-15 10:26:03 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2022-07-08 01:59:24 +04:00
|
|
|
dash, err := ReadDashboard(f, &dsLookup{})
|
|
|
|
|
sortDatasources(dash)
|
|
|
|
|
|
2022-04-28 19:29:09 +03:00
|
|
|
require.NoError(t, err)
|
2022-02-15 10:26:03 -08:00
|
|
|
out, err := json.MarshalIndent(dash, "", " ")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
update := false
|
2022-04-12 14:15:16 +02:00
|
|
|
savedPath := filepath.Join("testdata/", input+"-info.json")
|
2022-02-15 10:26:03 -08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-08 01:59:24 +04:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|