Fix dashboard migration discrepancies between backend and frontend implementations (use toEqual) (#110268)
**Highlights**
* **Single-version migrations**: add `targetVersion` to migrator & model, separate outputs, enforce exact version.
* **Datasource fixes**: include `apiVersion` in tests, empty-string → `{}`, preserve `{}` refs, drop unwanted defaults.
* **Panel defaults & nesting**: only top-level panels get defaults; preserve empty `transformations` context-aware; filter repeated panels.
* **Migration parity**
* V16: collapsed rows, grid height parsing (`px`).
* V17: omit `maxPerRow` when `minSpan=1`.
* V19–V20: cleanup defaults (`targetBlank`, style).
* V23–V24: template vars + table panel consistency.
* V28: full singlestat/stat parity, mappings & color.
* V30–V36: threshold logic, empty refs, nested targets.
* **Save-model cleanup**: replicate frontend defaults/filtering, drop null IDs, metadata, unused props.
* **Testing**: unified suites, dev dashboards (v42), full unit coverage for major migrations.
Co-authored-by: Ivan Ortega [ivanortegaalba@gmail.com](mailto:ivanortegaalba@gmail.com)
Co-authored-by: Dominik Prokop [dominik.prokop@grafana.com](mailto:dominik.prokop@grafana.com)
This commit is contained in:
co-authored by
Ivan Ortega [ivanortegaalba@gmail.com](mailto:ivanortegaalba@gmail.com)
Dominik Prokop [dominik.prokop@grafana.com](mailto:dominik.prokop@grafana.com)
parent
98fd3e8fe9
commit
a72e02f88a
@@ -6,9 +6,39 @@ import (
|
||||
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
|
||||
)
|
||||
|
||||
type TestDataSourceProvider struct{}
|
||||
// DataSourceConfig defines different test configurations
|
||||
type DataSourceConfig string
|
||||
|
||||
func (m *TestDataSourceProvider) GetDataSourceInfo(_ context.Context) []schemaversion.DataSourceInfo {
|
||||
const (
|
||||
// StandardTestConfig provides datasources for standard migration tests
|
||||
StandardTestConfig DataSourceConfig = "standard"
|
||||
// DevDashboardConfig provides datasources matching dev dashboard requirements
|
||||
DevDashboardConfig DataSourceConfig = "dev-dashboard"
|
||||
)
|
||||
|
||||
// ConfigurableDataSourceProvider provides flexible datasource configurations for different test scenarios
|
||||
type ConfigurableDataSourceProvider struct {
|
||||
config DataSourceConfig
|
||||
}
|
||||
|
||||
// NewDataSourceProvider creates a provider with the specified configuration
|
||||
func NewDataSourceProvider(config DataSourceConfig) *ConfigurableDataSourceProvider {
|
||||
return &ConfigurableDataSourceProvider{config: config}
|
||||
}
|
||||
|
||||
func (p *ConfigurableDataSourceProvider) GetDataSourceInfo(_ context.Context) []schemaversion.DataSourceInfo {
|
||||
switch p.config {
|
||||
case StandardTestConfig:
|
||||
return p.getStandardTestDataSources()
|
||||
case DevDashboardConfig:
|
||||
return p.getDevDashboardDataSources()
|
||||
default:
|
||||
return p.getStandardTestDataSources()
|
||||
}
|
||||
}
|
||||
|
||||
// getStandardTestDataSources returns datasources for standard migration tests
|
||||
func (p *ConfigurableDataSourceProvider) getStandardTestDataSources() []schemaversion.DataSourceInfo {
|
||||
return []schemaversion.DataSourceInfo{
|
||||
{
|
||||
Default: true,
|
||||
@@ -22,7 +52,7 @@ func (m *TestDataSourceProvider) GetDataSourceInfo(_ context.Context) []schemave
|
||||
Default: false,
|
||||
UID: "non-default-test-ds-uid",
|
||||
Type: "loki",
|
||||
APIVersion: "1",
|
||||
APIVersion: "v1",
|
||||
Name: "Non Default Test Datasource Name",
|
||||
ID: 2,
|
||||
},
|
||||
@@ -61,7 +91,56 @@ func (m *TestDataSourceProvider) GetDataSourceInfo(_ context.Context) []schemave
|
||||
}
|
||||
}
|
||||
|
||||
// GetTestDataSourceProvider returns a singleton instance of the test provider
|
||||
func GetTestDataSourceProvider() *TestDataSourceProvider {
|
||||
return &TestDataSourceProvider{}
|
||||
// getDevDashboardDataSources returns datasources for dev dashboard tests
|
||||
func (p *ConfigurableDataSourceProvider) getDevDashboardDataSources() []schemaversion.DataSourceInfo {
|
||||
return []schemaversion.DataSourceInfo{
|
||||
{
|
||||
Default: true,
|
||||
UID: "testdata-type-uid",
|
||||
Type: "grafana-testdata-datasource",
|
||||
APIVersion: "v1",
|
||||
Name: "grafana-testdata-datasource",
|
||||
ID: 1,
|
||||
},
|
||||
{
|
||||
Default: false,
|
||||
UID: "testdata",
|
||||
Type: "grafana-testdata-datasource",
|
||||
APIVersion: "", // Frontend testdata datasource has no apiVersion
|
||||
Name: "TestData",
|
||||
ID: 2,
|
||||
},
|
||||
{
|
||||
Default: false,
|
||||
UID: "prometheus-uid",
|
||||
Type: "prometheus",
|
||||
APIVersion: "v1",
|
||||
Name: "Prometheus",
|
||||
ID: 3,
|
||||
},
|
||||
{
|
||||
Default: false,
|
||||
UID: "loki-uid",
|
||||
Type: "loki",
|
||||
APIVersion: "v1",
|
||||
Name: "Loki",
|
||||
ID: 4,
|
||||
},
|
||||
{
|
||||
Default: false,
|
||||
UID: "elasticsearch-uid",
|
||||
Type: "elasticsearch",
|
||||
APIVersion: "v1",
|
||||
Name: "Elasticsearch",
|
||||
ID: 5,
|
||||
},
|
||||
{
|
||||
Default: false,
|
||||
UID: "-- Mixed --",
|
||||
Type: "mixed",
|
||||
APIVersion: "v1",
|
||||
Name: "-- Mixed --",
|
||||
ID: 6,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConfigurableDataSourceProvider(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("standard test config", func(t *testing.T) {
|
||||
provider := NewDataSourceProvider(StandardTestConfig)
|
||||
dataSources := provider.GetDataSourceInfo(ctx)
|
||||
|
||||
require.Len(t, dataSources, 6)
|
||||
|
||||
// Verify standard test configuration
|
||||
defaultDS := dataSources[0]
|
||||
assert.True(t, defaultDS.Default)
|
||||
assert.Equal(t, "default-ds-uid", defaultDS.UID)
|
||||
assert.Equal(t, "prometheus", defaultDS.Type)
|
||||
assert.Equal(t, "v1", defaultDS.APIVersion)
|
||||
})
|
||||
|
||||
t.Run("dev dashboard config", func(t *testing.T) {
|
||||
provider := NewDataSourceProvider(DevDashboardConfig)
|
||||
dataSources := provider.GetDataSourceInfo(ctx)
|
||||
|
||||
require.Len(t, dataSources, 6)
|
||||
|
||||
// Verify dev dashboard configuration
|
||||
defaultDS := dataSources[0]
|
||||
assert.True(t, defaultDS.Default)
|
||||
assert.Equal(t, "testdata-type-uid", defaultDS.UID)
|
||||
assert.Equal(t, "grafana-testdata-datasource", defaultDS.Type)
|
||||
assert.Equal(t, "v1", defaultDS.APIVersion)
|
||||
|
||||
// Verify testdata without apiVersion
|
||||
testDataDS := dataSources[1]
|
||||
assert.False(t, testDataDS.Default)
|
||||
assert.Equal(t, "testdata", testDataDS.UID)
|
||||
assert.Equal(t, "grafana-testdata-datasource", testDataDS.Type)
|
||||
assert.Equal(t, "", testDataDS.APIVersion) // No apiVersion for frontend testdata
|
||||
})
|
||||
|
||||
t.Run("equivalent configurations", func(t *testing.T) {
|
||||
// Test that different ways of creating providers return equivalent results
|
||||
standardProvider1 := NewDataSourceProvider(StandardTestConfig)
|
||||
standardProvider2 := NewDataSourceProvider(StandardTestConfig)
|
||||
devProvider1 := NewDataSourceProvider(DevDashboardConfig)
|
||||
devProvider2 := NewDataSourceProvider(DevDashboardConfig)
|
||||
|
||||
standardDS1 := standardProvider1.GetDataSourceInfo(ctx)
|
||||
standardDS2 := standardProvider2.GetDataSourceInfo(ctx)
|
||||
devDS1 := devProvider1.GetDataSourceInfo(ctx)
|
||||
devDS2 := devProvider2.GetDataSourceInfo(ctx)
|
||||
|
||||
require.Len(t, standardDS1, 6)
|
||||
require.Len(t, standardDS2, 6)
|
||||
require.Len(t, devDS1, 6)
|
||||
require.Len(t, devDS2, 6)
|
||||
|
||||
// Verify equivalent configurations return the same data
|
||||
assert.Equal(t, standardDS1[0].UID, standardDS2[0].UID)
|
||||
assert.Equal(t, devDS1[0].UID, devDS2[0].UID)
|
||||
assert.Equal(t, "default-ds-uid", standardDS1[0].UID)
|
||||
assert.Equal(t, "testdata-type-uid", devDS1[0].UID)
|
||||
})
|
||||
|
||||
t.Run("unknown config defaults to standard", func(t *testing.T) {
|
||||
provider := NewDataSourceProvider("unknown-config")
|
||||
dataSources := provider.GetDataSourceInfo(ctx)
|
||||
|
||||
require.Len(t, dataSources, 6)
|
||||
|
||||
// Should default to standard configuration
|
||||
defaultDS := dataSources[0]
|
||||
assert.Equal(t, "default-ds-uid", defaultDS.UID)
|
||||
assert.Equal(t, "prometheus", defaultDS.Type)
|
||||
})
|
||||
}
|
||||
|
||||
// TestModernUsageExample demonstrates how to use the new configurable approach
|
||||
func TestModernUsageExample(t *testing.T) {
|
||||
// Example of modern usage in migration tests
|
||||
t.Run("modern test setup", func(t *testing.T) {
|
||||
// Create provider with specific configuration
|
||||
provider := NewDataSourceProvider(StandardTestConfig)
|
||||
|
||||
// Use in migration initialization (example)
|
||||
dataSources := provider.GetDataSourceInfo(context.Background())
|
||||
|
||||
// Verify we got the expected configuration
|
||||
require.NotEmpty(t, dataSources)
|
||||
assert.Equal(t, "prometheus", dataSources[0].Type)
|
||||
})
|
||||
|
||||
t.Run("dev dashboard test setup", func(t *testing.T) {
|
||||
// For dev dashboard tests
|
||||
provider := NewDataSourceProvider(DevDashboardConfig)
|
||||
|
||||
dataSources := provider.GetDataSourceInfo(context.Background())
|
||||
|
||||
// Verify we got the dev dashboard configuration
|
||||
require.NotEmpty(t, dataSources)
|
||||
assert.Equal(t, "grafana-testdata-datasource", dataSources[0].Type)
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,9 @@ package testutil
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func PrettyPrint(label string, i interface{}) {
|
||||
@@ -13,3 +16,32 @@ func PrettyPrint(label string, i interface{}) {
|
||||
}
|
||||
fmt.Println(label, string(b))
|
||||
}
|
||||
|
||||
// FindJSONFiles recursively finds all .json files in a directory
|
||||
func FindJSONFiles(dir string) ([]string, error) {
|
||||
var jsonFiles []string
|
||||
|
||||
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() && strings.HasSuffix(strings.ToLower(info.Name()), ".json") {
|
||||
jsonFiles = append(jsonFiles, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return jsonFiles, err
|
||||
}
|
||||
|
||||
// GetRelativeOutputPath converts an input path to a relative output path preserving directory structure
|
||||
func GetRelativeOutputPath(inputPath, inputDir string) string {
|
||||
// Get the relative path from the input directory
|
||||
relPath, err := filepath.Rel(inputDir, inputPath)
|
||||
if err != nil {
|
||||
// If we can't get relative path, just use the filename
|
||||
return filepath.Base(inputPath)
|
||||
}
|
||||
// Preserve the directory structure
|
||||
return relPath
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user