mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Introduce coremodels framework (extracted from intent-api) (#47653)
* Copy over most of coremodel from intent-api branch * Fix import paths * Fix incorrect provider name * Add root compgen file, fixup componentroot.yaml * go mod tidy * Remove compgen for now * Add dashboard coremodel * Remove datasource coremodel, for now * Tweak comments on dashboard struct model * devenv: add dashboard testing directly * Fixup dashboard schema for openness, heatmap * Update Thema to tip * Fix wire/registry references * Fix hclog version
This commit is contained in:
6
devenv/dev-dashboards/dashboards.go
Normal file
6
devenv/dev-dashboards/dashboards.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package dev_dashboards
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.json */*.json
|
||||
var DevDashboardFS embed.FS
|
||||
84
devenv/dev-dashboards/dashboards_test.go
Normal file
84
devenv/dev-dashboards/dashboards_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package dev_dashboards
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"cuelang.org/go/cue/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/coremodel/dashboard"
|
||||
"github.com/grafana/grafana/pkg/cuectx"
|
||||
)
|
||||
|
||||
func TestDevenvDashboardValidity(t *testing.T) {
|
||||
m, err := themaTestableDashboards()
|
||||
require.NoError(t, err)
|
||||
cm, err := dashboard.ProvideCoremodel(cuectx.ProvideThemaLibrary())
|
||||
require.NoError(t, err)
|
||||
|
||||
for path, b := range m {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
// The path arg here only matters for error output
|
||||
cv, err := cuectx.JSONtoCUE(path, b)
|
||||
require.NoError(t, err, "error while decoding dashboard JSON into a CUE value")
|
||||
|
||||
_, err = cm.CurrentSchema().Validate(cv)
|
||||
if err != nil {
|
||||
// Testify trims errors to short length. We want the full text
|
||||
errstr := errors.Details(err, nil)
|
||||
t.Log(errstr)
|
||||
if strings.Contains(errstr, "null") {
|
||||
t.Log("validation failure appears to involve nulls - see if scripts/stripnulls.sh has any effect?")
|
||||
}
|
||||
t.FailNow()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func themaTestableDashboards() (map[string][]byte, error) {
|
||||
m := make(map[string][]byte)
|
||||
in := DevDashboardFS
|
||||
|
||||
err := fs.WalkDir(in, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if d.IsDir() || filepath.Ext(d.Name()) != ".json" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:gosec
|
||||
f, err := in.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close() // nolint: errcheck
|
||||
|
||||
b, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
jtree := make(map[string]interface{})
|
||||
json.Unmarshal(b, &jtree)
|
||||
if oldschemav, has := jtree["schemaVersion"]; !has || !(oldschemav.(float64) > 32) {
|
||||
return nil
|
||||
}
|
||||
|
||||
m[path] = b
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
Reference in New Issue
Block a user