Files
Roberto Jiménez Sánchez ea02e2e081 Provisioning: refactor dry-run and run logic to be stricter and more concise (#103357)
* Separate DryRun into separate method

* Fix linting

* Remove errors

* Remove checks in dualwriter

* Fix unit tests

* Add TODOs

* Dry Run as non-critical error

* Add TODOs

* Address TODO

* Fix tests

* Fix linting

* Deprecate dashboard name from path completely

* Use MissingName error also in parser

* Return 206 for non-critical errors

* Remove TODOs for previous dry-run
2025-04-04 13:31:48 +03:00

76 lines
2.4 KiB
Go

package resources
import (
"context"
"testing"
"github.com/stretchr/testify/require"
dashboardV0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
dashboardV1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1alpha1"
"github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
)
func TestParser(t *testing.T) {
clients := NewMockResourceClients(t)
clients.On("ForKind", dashboardV0.DashboardResourceInfo.GroupVersionKind()).
Return(nil, dashboardV0.DashboardResourceInfo.GroupVersionResource(), nil).Maybe()
clients.On("ForKind", dashboardV1.DashboardResourceInfo.GroupVersionKind()).
Return(nil, dashboardV1.DashboardResourceInfo.GroupVersionResource(), nil).Maybe()
parser := &Parser{
repo: v0alpha1.ResourceRepositoryInfo{
Type: v0alpha1.LocalRepositoryType,
Namespace: "xxx",
Name: "repo",
},
clients: clients,
}
t.Run("invalid input", func(t *testing.T) {
_, err := parser.Parse(context.Background(), &repository.FileInfo{
Data: []byte("hello"), // not a real resource
})
require.Error(t, err)
require.Equal(t, "classic resource must be JSON", err.Error())
})
t.Run("dashboard parsing (with and without name)", func(t *testing.T) {
dash, err := parser.Parse(context.Background(), &repository.FileInfo{
Data: []byte(`apiVersion: dashboard.grafana.app/v0alpha1
kind: Dashboard
metadata:
name: test-v0
spec:
title: Test dashboard
`),
})
require.NoError(t, err)
require.Equal(t, "test-v0", dash.Obj.GetName())
require.Equal(t, "dashboard.grafana.app", dash.GVK.Group)
require.Equal(t, "v0alpha1", dash.GVK.Version)
require.Equal(t, "dashboard.grafana.app", dash.GVR.Group)
require.Equal(t, "v0alpha1", dash.GVR.Version)
// Now try again without a name
_, err = parser.Parse(context.Background(), &repository.FileInfo{
Data: []byte(`apiVersion: dashboard.grafana.app/v1alpha1
kind: Dashboard
spec:
title: Test dashboard
`),
})
require.EqualError(t, err, "name.metadata.name: Required value: missing name in resource")
// Read the name from classic grafana format
dash, err = parser.Parse(context.Background(), &repository.FileInfo{
Data: []byte(`{ "uid": "test", "schemaVersion": 30, "panels": [], "tags": [] }`),
})
require.NoError(t, err)
require.Equal(t, v0alpha1.ClassicDashboard, dash.Classic)
require.Equal(t, "test", dash.Obj.GetName())
})
}