2e0dc835cf
* Add test for devenv resources * Refactor validation tests for grokkability * Devenv dashboards error-tracking script * Refactor to use cueerrors.Details() * Further test refinement * Close major elements of dashboard schema * Centralize dashboard validation tests General dashboard validation testing belongs in the load package. * Better names for error context on glue CUE code * Fixup validate-resource Do only one of base or dist, and fix copied docs. * Skip the devenv test * Remove test for validateResources * Fix shellcheck * Backend linter Co-authored-by: sam boyer <sdboyer@grafana.com>
69 lines
2.3 KiB
Go
69 lines
2.3 KiB
Go
package commands
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/grafana/grafana/pkg/schema/load"
|
|
"github.com/laher/mergefs"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var defaultBaseLoadPaths = load.GetDefaultLoadPaths()
|
|
|
|
func TestValidateScuemataBasics(t *testing.T) {
|
|
t.Run("Testing scuemata validity with valid cue schemas", func(t *testing.T) {
|
|
var baseLoadPaths = load.BaseLoadPaths{
|
|
BaseCueFS: defaultBaseLoadPaths.BaseCueFS,
|
|
DistPluginCueFS: defaultBaseLoadPaths.DistPluginCueFS,
|
|
}
|
|
|
|
err := validateScuemata(baseLoadPaths, load.BaseDashboardFamily)
|
|
require.NoError(t, err, "error while loading base dashboard scuemata")
|
|
|
|
err = validateScuemata(baseLoadPaths, load.DistDashboardFamily)
|
|
require.NoError(t, err, "error while loading dist dashboard scuemata")
|
|
})
|
|
|
|
t.Run("Testing scuemata validity with invalid cue schemas - family missing", func(t *testing.T) {
|
|
genCue, err := os.ReadFile("testdata/missing_family_gen.cue")
|
|
require.NoError(t, err)
|
|
|
|
filesystem := fstest.MapFS{
|
|
"cue/data/gen.cue": &fstest.MapFile{Data: genCue},
|
|
}
|
|
mergedFS := mergefs.Merge(filesystem, defaultBaseLoadPaths.BaseCueFS)
|
|
|
|
var baseLoadPaths = load.BaseLoadPaths{
|
|
BaseCueFS: mergedFS,
|
|
DistPluginCueFS: defaultBaseLoadPaths.DistPluginCueFS,
|
|
}
|
|
|
|
err = validateScuemata(baseLoadPaths, load.BaseDashboardFamily)
|
|
assert.EqualError(t, err, "error while loading dashboard scuemata, err: dashboard schema family did not exist at expected path in expected file")
|
|
})
|
|
|
|
t.Run("Testing scuemata validity with invalid cue schemas - panel missing ", func(t *testing.T) {
|
|
genCue, err := os.ReadFile("testdata/missing_panel_gen.cue")
|
|
require.NoError(t, err)
|
|
|
|
filesystem := fstest.MapFS{
|
|
"cue/data/gen.cue": &fstest.MapFile{Data: genCue},
|
|
}
|
|
mergedFS := mergefs.Merge(filesystem, defaultBaseLoadPaths.BaseCueFS)
|
|
|
|
var baseLoadPaths = load.BaseLoadPaths{
|
|
BaseCueFS: mergedFS,
|
|
DistPluginCueFS: defaultBaseLoadPaths.DistPluginCueFS,
|
|
}
|
|
|
|
err = validateScuemata(baseLoadPaths, load.BaseDashboardFamily)
|
|
require.NoError(t, err, "error while loading base dashboard scuemata")
|
|
|
|
err = validateScuemata(baseLoadPaths, load.DistDashboardFamily)
|
|
assert.EqualError(t, err, "all schema should be valid with respect to basic CUE rules, Family.lineages.0.0: field #Panel not allowed")
|
|
})
|
|
}
|