Chore: Add linter rule to avoid imports from Grafana core in test datasource (#71779)

This commit is contained in:
Andres Martinez Gotor 2023-07-18 15:57:31 +02:00 committed by GitHub
parent 5faf5e48ea
commit 5b2721b4f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 14 deletions

View File

@ -23,6 +23,15 @@ deny = [
{pkg = "github.com/gofrs/uuid", desc = "Use github.com/google/uuid instead, which we already depend on."}, {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] [linters-settings.gocritic]
enabled-checks = ["ruleguard"] enabled-checks = ["ruleguard"]
[linters-settings.gocritic.settings.ruleguard] [linters-settings.gocritic.settings.ruleguard]

View File

@ -15,31 +15,30 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data" "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) { func (s *Service) handleCsvContentScenario(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := simplejson.NewJson(q.JSON) model, err := getModel(q.JSON)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse query json: %v", err) return nil, fmt.Errorf("failed to parse query json: %v", err)
} }
csvContent := model.Get("csvContent").MustString() csvContent := model.CSVContent
if len(csvContent) == 0 { if len(csvContent) == 0 {
return backend.NewQueryDataResponse(), nil return backend.NewQueryDataResponse(), nil
} }
alias := model.Get("alias").MustString("") alias := model.Alias
frame, err := LoadCsvContent(strings.NewReader(csvContent), alias) frame, err := LoadCsvContent(strings.NewReader(csvContent), alias)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dropPercent := model.Get("dropPercent").MustFloat64(0) dropPercent := model.DropPercent
if dropPercent > 0 { if dropPercent > 0 {
frame, err = dropValues(frame, dropPercent) frame, err = dropValues(frame, dropPercent)
if err != nil { if err != nil {
@ -59,12 +58,12 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD
resp := backend.NewQueryDataResponse() resp := backend.NewQueryDataResponse()
for _, q := range req.Queries { for _, q := range req.Queries {
model, err := simplejson.NewJson(q.JSON) model, err := getModel(q.JSON)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse query json %v", err) return nil, fmt.Errorf("failed to parse query json %v", err)
} }
fileName := model.Get("csvFileName").MustString() fileName := model.CSVFileName
if len(fileName) == 0 { if len(fileName) == 0 {
continue continue
@ -76,7 +75,7 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD
return nil, err return nil, err
} }
dropPercent := model.Get("dropPercent").MustFloat64(0) dropPercent := model.DropPercent
if dropPercent > 0 { if dropPercent > 0 {
frame, err = dropValues(frame, dropPercent) frame, err = dropValues(frame, dropPercent)
if err != nil { if err != nil {

View File

@ -8,15 +8,10 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental" "github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestCSVFileScenario(t *testing.T) { func TestCSVFileScenario(t *testing.T) {
cfg := setting.NewCfg()
cfg.DataPath = t.TempDir()
cfg.StaticRootPath = "../../../public"
s := &Service{} s := &Service{}
t.Run("loadCsvFile", func(t *testing.T) { t.Run("loadCsvFile", func(t *testing.T) {

View File

@ -250,6 +250,9 @@ type JSONModel struct {
// Cannot specify a type for csvWave since legacy queries // Cannot specify a type for csvWave since legacy queries
// does not follow the same format as the new ones (and there is no migration). // 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 { type pulseWave struct {

View File

@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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" "github.com/grafana/grafana/pkg/tsdb/legacydata"
) )

View File

@ -9,6 +9,8 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/log" "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/backend/resource/httpadapter"
"github.com/grafana/grafana-plugin-sdk-go/data" "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" "github.com/grafana/grafana/pkg/tsdb/testdatasource/sims"
) )