Actually enforce dashboard schema on devenv (#53797)

* Relocate dashboards test so it runs

* Cover newer schema versions in stripnulls

* Strip nulls and remove iteration fields

* Fix fiscalYearStartMonth bounds

* Fix up and format dashboard schema

* Update thema, tidy Go dependencies
This commit is contained in:
sam boyer
2022-08-16 22:07:57 -04:00
committed by GitHub
parent 25de383540
commit c23a9d78ab
12 changed files with 86 additions and 78 deletions

View File

@@ -1,84 +0,0 @@
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) > dashboard.HandoffSchemaVersion-1) {
return nil
}
m[path] = b
return nil
})
if err != nil {
return nil, err
}
return m, nil
}

View File

@@ -25,7 +25,6 @@
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 95,
"iteration": 1652465568602,
"links": [],
"liveNow": false,
"panels": [

View File

@@ -25,7 +25,6 @@
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 85,
"iteration": 1652465607869,
"links": [],
"liveNow": false,
"panels": [

View File

@@ -49,8 +49,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -151,8 +150,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -238,8 +236,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -360,4 +357,4 @@
"uid": "KVxZKdmVz",
"version": 10,
"weekStart": ""
}
}

View File

@@ -49,8 +49,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -152,8 +151,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -259,8 +257,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -358,8 +355,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -474,4 +470,4 @@
"uid": "2xuwrgV7z",
"version": 4,
"weekStart": ""
}
}

View File

@@ -3414,8 +3414,7 @@
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
"color": "green"
},
{
"color": "red",
@@ -3489,4 +3488,4 @@
"uid": "1KxMUdE7k",
"version": 5,
"weekStart": ""
}
}