Dashboards: Add Dashboard Schema validation (2) (#103844)
* Activate schema validation and align underlying systems * update to save as v0 if not the right schema version * Resolve merge conflicts * Move RequireApiErrorStatus to tests package * Add mutation tests * Fix lint * Only do min version check if dashboard is v1 * Fix lint and disable provisioning test * Revert provisioning changes * Revert more tests and add schema test * Reran gen * SQL Dashboard save * Adjust APIVERSION * Fixed mutation test * Add logging on downgrade --------- Co-authored-by: Marco de Abreu <18629099+marcoabreu@users.noreply.github.com> Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>
This commit is contained in:
co-authored by
Marco de Abreu
Stephanie Hingtgen
parent
07a225649d
commit
c47ab101d1
@@ -7,7 +7,9 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
dashboardv0alpha1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||
dashboardv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1alpha1"
|
||||
dashboardv2alpha1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2alpha1"
|
||||
folders "github.com/grafana/grafana/pkg/apis/folder/v1"
|
||||
"github.com/grafana/grafana/pkg/apiserver/rest"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
@@ -219,19 +221,88 @@ func runDashboardValidationTests(t *testing.T, ctx TestContext) {
|
||||
t.Run("Dashboard schema validations", func(t *testing.T) {
|
||||
// Test invalid dashboard schema
|
||||
t.Run("reject dashboard with invalid schema", func(t *testing.T) {
|
||||
dashObj := &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": dashboardv1.DashboardResourceInfo.GroupVersion().String(),
|
||||
"kind": dashboardv1.DashboardResourceInfo.GroupVersionKind().Kind,
|
||||
"metadata": map[string]interface{}{
|
||||
"generateName": "test-",
|
||||
testCases := []struct {
|
||||
name string
|
||||
resourceInfo utils.ResourceInfo
|
||||
expectSpecErr bool
|
||||
testObject *unstructured.Unstructured
|
||||
}{
|
||||
{
|
||||
name: "v0alpha1 dashboard with wrong spec should not throw on v0",
|
||||
resourceInfo: dashboardv0alpha1.DashboardResourceInfo,
|
||||
expectSpecErr: false,
|
||||
testObject: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": dashboardv0alpha1.DashboardResourceInfo.TypeMeta().APIVersion,
|
||||
"kind": "Dashboard",
|
||||
"metadata": map[string]interface{}{
|
||||
"generateName": "test-",
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"title": "Dashboard Title",
|
||||
"schemaVersion": 41,
|
||||
"editable": "elephant",
|
||||
"time": 9000,
|
||||
"uid": strings.Repeat("a", 100),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v1 dashboard with wrong spec should throw on v1",
|
||||
resourceInfo: dashboardv1.DashboardResourceInfo,
|
||||
expectSpecErr: true,
|
||||
testObject: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": dashboardv1.DashboardResourceInfo.TypeMeta().APIVersion,
|
||||
"kind": "Dashboard",
|
||||
"metadata": map[string]interface{}{
|
||||
"generateName": "test-",
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"title": "Dashboard Title",
|
||||
"schemaVersion": 41,
|
||||
"editable": "elephant",
|
||||
"time": 9000,
|
||||
"uid": strings.Repeat("a", 100),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v2alpha1 dashboard with correct spec should not throw on v2",
|
||||
resourceInfo: dashboardv2alpha1.DashboardResourceInfo,
|
||||
expectSpecErr: false,
|
||||
testObject: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": dashboardv2alpha1.DashboardResourceInfo.TypeMeta().APIVersion,
|
||||
"kind": "Dashboard",
|
||||
"metadata": map[string]interface{}{
|
||||
"generateName": "test-",
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"title": "Dashboard Title",
|
||||
"description": "valid description",
|
||||
},
|
||||
},
|
||||
},
|
||||
// Missing spec
|
||||
},
|
||||
}
|
||||
|
||||
_, err := adminClient.Resource.Create(context.Background(), dashObj, v1.CreateOptions{})
|
||||
require.Error(t, err)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
resourceClient := getResourceClient(t, ctx.Helper, ctx.AdminUser, tc.resourceInfo.GroupVersionResource())
|
||||
createdDashboard, err := resourceClient.Resource.Create(context.Background(), tc.testObject, v1.CreateOptions{})
|
||||
if tc.expectSpecErr {
|
||||
ctx.Helper.RequireApiErrorStatus(err, v1.StatusReasonInvalid, http.StatusUnprocessableEntity)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, createdDashboard)
|
||||
err = resourceClient.Resource.Delete(context.Background(), createdDashboard.GetName(), v1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -683,20 +754,12 @@ func createTestContext(t *testing.T, helper *apis.K8sTestHelper, orgUsers apis.O
|
||||
|
||||
// getDashboardGVR returns the dashboard GroupVersionResource
|
||||
func getDashboardGVR() schema.GroupVersionResource {
|
||||
return schema.GroupVersionResource{
|
||||
Group: dashboardv1.DashboardResourceInfo.GroupVersion().Group,
|
||||
Version: dashboardv1.DashboardResourceInfo.GroupVersion().Version,
|
||||
Resource: dashboardv1.DashboardResourceInfo.GetName(),
|
||||
}
|
||||
return dashboardv1.DashboardResourceInfo.GroupVersionResource()
|
||||
}
|
||||
|
||||
// getFolderGVR returns the folder GroupVersionResource
|
||||
func getFolderGVR() schema.GroupVersionResource {
|
||||
return schema.GroupVersionResource{
|
||||
Group: folders.FolderResourceInfo.GroupVersion().Group,
|
||||
Version: folders.FolderResourceInfo.GroupVersion().Version,
|
||||
Resource: folders.FolderResourceInfo.GetName(),
|
||||
}
|
||||
return folders.FolderResourceInfo.GroupVersionResource()
|
||||
}
|
||||
|
||||
// Get a resource client for the specified user
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"k8s.io/client-go/rest"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
"github.com/grafana/grafana/pkg/server"
|
||||
@@ -894,3 +895,22 @@ func (c *K8sTestHelper) DeleteServiceAccount(user User, orgID int64, saID int64)
|
||||
|
||||
require.Equal(c.t, http.StatusOK, resp.Response.StatusCode, "failed to delete service account, body: %s", string(resp.Body))
|
||||
}
|
||||
|
||||
// Ensures that the passed error is an APIStatus error and fails the test if it is not.
|
||||
func (c *K8sTestHelper) RequireApiErrorStatus(err error, reason metav1.StatusReason, httpCode int) metav1.Status {
|
||||
require.Error(c.t, err)
|
||||
status, ok := utils.ExtractApiErrorStatus(err)
|
||||
if !ok {
|
||||
c.t.Fatalf("Expected error to be an APIStatus, but got %T", err)
|
||||
}
|
||||
|
||||
if reason != metav1.StatusReasonUnknown {
|
||||
require.Equal(c.t, status.Reason, reason)
|
||||
}
|
||||
|
||||
if httpCode != 0 {
|
||||
require.Equal(c.t, status.Code, int32(httpCode))
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
@@ -3294,7 +3294,6 @@
|
||||
],
|
||||
"properties": {
|
||||
"annotations": {
|
||||
"description": "Title of dashboard.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"default": {},
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
gh "github.com/google/go-github/v70/github"
|
||||
ghmock "github.com/migueleliasweb/go-github-mock/src/mock"
|
||||
@@ -23,6 +24,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
"github.com/grafana/grafana/pkg/infra/usagestats"
|
||||
"github.com/grafana/grafana/pkg/tests/apis"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func TestIntegrationProvisioning_CreatingAndGetting(t *testing.T) {
|
||||
@@ -119,6 +121,88 @@ func TestIntegrationProvisioning_CreatingAndGetting(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationProvisioning_FailInvalidSchema(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
t.Skip("Reenable this test once we enforce schema validation for provisioning")
|
||||
|
||||
helper := runGrafana(t)
|
||||
ctx := context.Background()
|
||||
|
||||
const repo = "invalid-schema-tmp"
|
||||
// Set up the repository and the file to import.
|
||||
helper.CopyToProvisioningPath(t, "testdata/invalid-dashboard-schema.json", "invalid-dashboard-schema.json")
|
||||
|
||||
localTmp := helper.RenderObject(t, "testdata/local-write.json.tmpl", map[string]any{
|
||||
"Name": repo,
|
||||
"SyncEnabled": true,
|
||||
})
|
||||
_, err := helper.Repositories.Resource.Create(ctx, localTmp, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Make sure the repo can read and validate the file
|
||||
_, err = helper.Repositories.Resource.Get(ctx, repo, metav1.GetOptions{}, "files", "invalid-dashboard-schema.json")
|
||||
status := helper.RequireApiErrorStatus(err, metav1.StatusReasonBadRequest, http.StatusBadRequest)
|
||||
require.Equal(t, status.Message, "Dry run failed: Dashboard.dashboard.grafana.app \"invalid-schema-uid\" is invalid: [spec.panels.0.repeatDirection: Invalid value: conflicting values \"h\" and \"this is not an allowed value\", spec.panels.0.repeatDirection: Invalid value: conflicting values \"v\" and \"this is not an allowed value\"]")
|
||||
|
||||
const invalidSchemaUid = "invalid-schema-uid"
|
||||
_, err = helper.Dashboards.Resource.Get(ctx, invalidSchemaUid, metav1.GetOptions{})
|
||||
require.Error(t, err, "invalid dashboard shouldn't exist")
|
||||
require.True(t, apierrors.IsNotFound(err))
|
||||
|
||||
var jobObj *unstructured.Unstructured
|
||||
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
|
||||
result := helper.AdminREST.Post().
|
||||
Namespace("default").
|
||||
Resource("repositories").
|
||||
Name(repo).
|
||||
SubResource("jobs").
|
||||
Body(asJSON(&provisioning.JobSpec{
|
||||
Action: provisioning.JobActionPull,
|
||||
Pull: &provisioning.SyncJobOptions{},
|
||||
})).
|
||||
SetHeader("Content-Type", "application/json").
|
||||
Do(t.Context())
|
||||
require.NoError(collect, result.Error())
|
||||
job, err := result.Get()
|
||||
require.NoError(collect, err)
|
||||
var ok bool
|
||||
jobObj, ok = job.(*unstructured.Unstructured)
|
||||
require.True(collect, ok, "expecting unstructured object, but got %T", job)
|
||||
}, time.Second*10, time.Millisecond*10, "Expected to be able to start a sync job")
|
||||
|
||||
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
|
||||
//helper.TriggerJobProcessing(t)
|
||||
result, err := helper.Repositories.Resource.Get(ctx, repo, metav1.GetOptions{},
|
||||
"jobs", string(jobObj.GetUID()))
|
||||
|
||||
if apierrors.IsNotFound(err) {
|
||||
assert.Fail(collect, "job '%s' not found yet yet", jobObj.GetName())
|
||||
return // continue trying
|
||||
}
|
||||
|
||||
// Can fail fast here -- the jobs are immutable
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
|
||||
job := &provisioning.Job{}
|
||||
err = runtime.DefaultUnstructuredConverter.FromUnstructured(result.Object, job)
|
||||
require.NoError(t, err, "should convert to Job object")
|
||||
|
||||
require.Equal(t, provisioning.JobStateError, job.Status.State)
|
||||
require.Equal(t, job.Status.Message, "completed with errors")
|
||||
require.Equal(t, job.Status.Errors[0], "Dashboard.dashboard.grafana.app \"invalid-schema-uid\" is invalid: [spec.panels.0.repeatDirection: Invalid value: conflicting values \"h\" and \"this is not an allowed value\", spec.panels.0.repeatDirection: Invalid value: conflicting values \"v\" and \"this is not an allowed value\"]")
|
||||
}, time.Second*10, time.Millisecond*10, "Expected provisioning job to conclude with the status failed")
|
||||
|
||||
_, err = helper.Dashboards.Resource.Get(ctx, invalidSchemaUid, metav1.GetOptions{})
|
||||
require.Error(t, err, "invalid dashboard shouldn't have been created")
|
||||
require.True(t, apierrors.IsNotFound(err))
|
||||
|
||||
err = helper.Repositories.Resource.Delete(ctx, repo, metav1.DeleteOptions{}, "files", "invalid-dashboard-schema.json")
|
||||
require.NoError(t, err, "should delete the resource file")
|
||||
}
|
||||
|
||||
func TestIntegrationProvisioning_CreatingGitHubRepository(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"title": "Provisioning test - invalid schema",
|
||||
"uid": "invalid-schema-uid",
|
||||
"schemaVersion": 41,
|
||||
"tags": [
|
||||
"tag"
|
||||
],
|
||||
"revision": "this-is-not-a-number",
|
||||
"panels": [
|
||||
{
|
||||
"gridPos": {
|
||||
"h": 3,
|
||||
"w": 12,
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"id": 34,
|
||||
"options": {
|
||||
"content": "# All panels\n\nThis dashboard was created to quickly check accessiblity issues on a lot of panels at the same time ",
|
||||
"mode": "markdown"
|
||||
},
|
||||
"pluginVersion": "8.1.0-pre",
|
||||
"transparent": true,
|
||||
"type": "text",
|
||||
"repeat": "something",
|
||||
"repeatDirection": "this is not an allowed value"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user