Files
grafana/apps/dashboard/pkg/migration/schemaversion/v33_test.go
T
Ivan Ortega Alba 93c14c52da Migrations: Compare backend and frontend outputs to ensure feature parity (#106851)
* wip: trying to understand how to get the ds info from migrator

* add datasource info provider

* Use DS service to fetch DS data

* add more tests cases to match with migrator cases

* Add snapshots

* Non-existing DS

* Add different DS for snapshots

* fix import

* Fix tests: guard against double initialization

* don't use full datasource package in test

* min version should be 35

* fix test

* fix conversion test

* Dashboards: Support schemaVersion v35 migration in backend

* Dashboards: Support schemaVersion v34 migration in backend

* Dashboards: Support schemaVersion v33 migration in backend

* Apply suggestions from code review

Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>

* Apply feedback

* Remove unused parameters

* Refactor to follow Go patterns

* Update logic

* Only write final migration result as output

* Compare backend and frontend results

* Improve snapshots to cover all possible use cases

* Linter

* wip make it consistent v33

* apply feedback

* Return default when the ref cannot be found

* Update apps/dashboard/pkg/migration/schemaversion/v33.go

Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>

* apply feedback

* Use same mocks backend/frontend

* restore migrations

* update snapshots

* Adapt migration tests to use min versions

* Ensure v40-v41 works

* Ensure v39-v40 works

* Simplify the naming of the files

* adjust jest to new input convention

* Ensure every migration v36-v41 works

* Improve v38 naming

* Ensure v36 migrates correctly

* Skip v36 refs migrations on rows

* Treat rows as frontend and ensure same results for v36

* Ensure v34 runs with the same logic than the frontend

* Leave empty stadistics as valid option

* ensure v33 is working as the frontend

* Update tests

* Undo frontend changes for legend handling

* Remove filtering by version in the frontend

* linter

* Clean up v33 input JSON

---------

Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>
Co-authored-by: Haris Rozajac <haris.rozajac12@gmail.com>
Co-authored-by: Stephanie Hingtgen <stephanie.hingtgen@grafana.com>
2025-07-03 12:23:51 +02:00

421 lines
9.9 KiB
Go

package schemaversion_test
import (
"testing"
"github.com/grafana/grafana/apps/dashboard/pkg/migration/schemaversion"
"github.com/grafana/grafana/apps/dashboard/pkg/migration/testutil"
)
func TestV33(t *testing.T) {
// Pass the mock provider to V33
migration := schemaversion.V33(testutil.GetTestProvider())
tests := []migrationTestCase{
{
name: "dashboard with no panels",
input: map[string]interface{}{
"schemaVersion": 32,
},
expected: map[string]interface{}{
"schemaVersion": 33,
},
},
{
name: "panel with default datasource should return null",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "default",
"targets": []interface{}{
map[string]interface{}{
"datasource": "default",
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": nil,
"targets": []interface{}{
map[string]interface{}{
"datasource": "default",
},
},
},
},
},
},
{
name: "panel with null datasource should return null",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": nil,
"targets": []interface{}{
map[string]interface{}{
"datasource": nil,
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": nil,
"targets": []interface{}{
map[string]interface{}{
"datasource": nil,
},
},
},
},
},
},
{
name: "panel with existing datasource reference should be preserved",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "existing-uid",
"type": "existing-type",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "target-uid",
"type": "target-type",
},
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "existing-uid",
"type": "existing-type",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "target-uid",
"type": "target-type",
},
},
},
},
},
},
},
{
name: "panel with datasource by name should be migrated",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "Existing Target Name",
"targets": []interface{}{
map[string]interface{}{
"datasource": "Existing Target Name",
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
},
},
},
},
},
},
{
name: "panel with datasource by UID should be migrated",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "existing-target-uid",
"targets": []interface{}{
map[string]interface{}{
"datasource": "existing-target-uid",
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
},
},
},
},
},
},
{
name: "panel with unknown datasource should preserve as UID",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "unknown-datasource",
"targets": []interface{}{
map[string]interface{}{
"datasource": "unknown-datasource",
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "unknown-datasource",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "unknown-datasource",
},
},
},
},
},
},
},
{
name: "panel with mixed datasources",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "Existing Target Name",
"targets": []interface{}{
map[string]interface{}{
"datasource": "default",
},
map[string]interface{}{
"datasource": "existing-target-uid",
},
map[string]interface{}{
"datasource": "unknown-ds",
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": "default",
},
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
},
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "unknown-ds",
},
},
},
},
},
},
},
{
name: "panel without targets should not fail",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "Existing Target Name",
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
},
},
},
},
{
name: "nested panels in collapsed rows should be migrated",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"type": "row",
"collapsed": true,
"datasource": "Existing Target Name",
"panels": []interface{}{
map[string]interface{}{
"datasource": "default",
"targets": []interface{}{
map[string]interface{}{
"datasource": "existing-target-uid",
},
},
},
map[string]interface{}{
"datasource": "unknown-ds",
"targets": []interface{}{
map[string]interface{}{
"datasource": "Existing Target Name",
},
},
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"type": "row",
"collapsed": true,
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
"panels": []interface{}{
map[string]interface{}{
"datasource": nil,
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
},
},
},
map[string]interface{}{
"datasource": map[string]interface{}{
"uid": "unknown-ds",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
},
},
},
},
},
},
},
},
{
name: "targets with lowercase default keyword should not be updated",
input: map[string]interface{}{
"schemaVersion": 32,
"panels": []interface{}{
map[string]interface{}{
"datasource": "Existing Target Name",
"targets": []interface{}{
map[string]interface{}{
"datasource": "default",
},
map[string]interface{}{
"datasource": nil,
},
},
},
},
},
expected: map[string]interface{}{
"schemaVersion": 33,
"panels": []interface{}{
map[string]interface{}{
"datasource": map[string]interface{}{
"type": "elasticsearch",
"uid": "existing-target-uid",
"apiVersion": "v2",
},
"targets": []interface{}{
map[string]interface{}{
"datasource": "default",
},
map[string]interface{}{
"datasource": nil,
},
},
},
},
},
},
}
runMigrationTests(t, tests, migration)
}