From 5b2721b4f14bb1fffed3b9925a535f612b78612c Mon Sep 17 00:00:00 2001 From: Andres Martinez Gotor Date: Tue, 18 Jul 2023 15:57:31 +0200 Subject: [PATCH] Chore: Add linter rule to avoid imports from Grafana core in test datasource (#71779) --- .golangci.toml | 9 +++++++++ pkg/tsdb/testdatasource/csv_data.go | 15 +++++++-------- pkg/tsdb/testdatasource/csv_data_test.go | 5 ----- pkg/tsdb/testdatasource/scenarios.go | 5 ++++- pkg/tsdb/testdatasource/scenarios_test.go | 1 + pkg/tsdb/testdatasource/testdata.go | 2 ++ 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.golangci.toml b/.golangci.toml index 9ca625a834b..987f81349fa 100644 --- a/.golangci.toml +++ b/.golangci.toml @@ -23,6 +23,15 @@ deny = [ {pkg = "github.com/gofrs/uuid", desc = "Use github.com/google/uuid instead, which we already depend on."}, ] +[linters-settings.depguard.rules.coreplugins] +deny = [ + {pkg = "github.com/grafana/grafana/pkg/", desc = "Core plugins are not allowed to depend on Grafana core packages"}, +] +files = [ + "**/pkg/tsdb/testdatasource/*", + "**/pkg/tsdb/testdatasource/**/*", +] + [linters-settings.gocritic] enabled-checks = ["ruleguard"] [linters-settings.gocritic.settings.ruleguard] diff --git a/pkg/tsdb/testdatasource/csv_data.go b/pkg/tsdb/testdatasource/csv_data.go index 6616e9d3042..6fd0bddbbe2 100644 --- a/pkg/tsdb/testdatasource/csv_data.go +++ b/pkg/tsdb/testdatasource/csv_data.go @@ -15,31 +15,30 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/data" - "github.com/grafana/grafana/pkg/components/simplejson" ) func (s *Service) handleCsvContentScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { resp := backend.NewQueryDataResponse() for _, q := range req.Queries { - model, err := simplejson.NewJson(q.JSON) + model, err := getModel(q.JSON) if err != nil { return nil, fmt.Errorf("failed to parse query json: %v", err) } - csvContent := model.Get("csvContent").MustString() + csvContent := model.CSVContent if len(csvContent) == 0 { return backend.NewQueryDataResponse(), nil } - alias := model.Get("alias").MustString("") + alias := model.Alias frame, err := LoadCsvContent(strings.NewReader(csvContent), alias) if err != nil { return nil, err } - dropPercent := model.Get("dropPercent").MustFloat64(0) + dropPercent := model.DropPercent if dropPercent > 0 { frame, err = dropValues(frame, dropPercent) if err != nil { @@ -59,12 +58,12 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD resp := backend.NewQueryDataResponse() for _, q := range req.Queries { - model, err := simplejson.NewJson(q.JSON) + model, err := getModel(q.JSON) if err != nil { return nil, fmt.Errorf("failed to parse query json %v", err) } - fileName := model.Get("csvFileName").MustString() + fileName := model.CSVFileName if len(fileName) == 0 { continue @@ -76,7 +75,7 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD return nil, err } - dropPercent := model.Get("dropPercent").MustFloat64(0) + dropPercent := model.DropPercent if dropPercent > 0 { frame, err = dropValues(frame, dropPercent) if err != nil { diff --git a/pkg/tsdb/testdatasource/csv_data_test.go b/pkg/tsdb/testdatasource/csv_data_test.go index 99660ef95cb..1f69c7aacb8 100644 --- a/pkg/tsdb/testdatasource/csv_data_test.go +++ b/pkg/tsdb/testdatasource/csv_data_test.go @@ -8,15 +8,10 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/experimental" - "github.com/grafana/grafana/pkg/setting" "github.com/stretchr/testify/require" ) func TestCSVFileScenario(t *testing.T) { - cfg := setting.NewCfg() - cfg.DataPath = t.TempDir() - cfg.StaticRootPath = "../../../public" - s := &Service{} t.Run("loadCsvFile", func(t *testing.T) { diff --git a/pkg/tsdb/testdatasource/scenarios.go b/pkg/tsdb/testdatasource/scenarios.go index 74903ad77e3..8c7d11a26ce 100644 --- a/pkg/tsdb/testdatasource/scenarios.go +++ b/pkg/tsdb/testdatasource/scenarios.go @@ -249,7 +249,10 @@ type JSONModel struct { Alias string `json:"alias"` // Cannot specify a type for csvWave since legacy queries // does not follow the same format as the new ones (and there is no migration). - CSVWave interface{} `json:"csvWave"` + CSVWave interface{} `json:"csvWave"` + CSVContent string `json:"csvContent"` + CSVFileName string `json:"csvFileName"` + DropPercent float64 `json:"dropPercent"` } type pulseWave struct { diff --git a/pkg/tsdb/testdatasource/scenarios_test.go b/pkg/tsdb/testdatasource/scenarios_test.go index d0553f04e93..ed43bc8bcb8 100644 --- a/pkg/tsdb/testdatasource/scenarios_test.go +++ b/pkg/tsdb/testdatasource/scenarios_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + // nolint:depguard // Lint exception can be removed once we move testdata to a separate module "github.com/grafana/grafana/pkg/tsdb/legacydata" ) diff --git a/pkg/tsdb/testdatasource/testdata.go b/pkg/tsdb/testdatasource/testdata.go index 338703a24fd..f6792f636ea 100644 --- a/pkg/tsdb/testdatasource/testdata.go +++ b/pkg/tsdb/testdatasource/testdata.go @@ -9,6 +9,8 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter" "github.com/grafana/grafana-plugin-sdk-go/data" + + // nolint:depguard // Lint exception can be removed once we move testdata to a separate module "github.com/grafana/grafana/pkg/tsdb/testdatasource/sims" )