Files
grafana/apps/dashboard/pkg/migration/testutil/utils.go
Ivan Ortega Alba a72e02f88a 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)
2025-09-24 12:20:25 +02:00

48 lines
1.1 KiB
Go

package testutil
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
func PrettyPrint(label string, i interface{}) {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
fmt.Println("error:", err)
return
}
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
}