Chore: Embed CSV files with the test data source (#71435)

This commit is contained in:
Andres Martinez Gotor 2023-07-13 09:34:40 +02:00 committed by GitHub
parent 51c94bccd7
commit 9990d5025d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 27 additions and 51 deletions

1
.github/CODEOWNERS vendored
View File

@ -444,7 +444,6 @@ lerna.json @grafana/frontend-ops
/public/robots.txt @grafana/frontend-ops /public/robots.txt @grafana/frontend-ops
/public/sass/ @grafana/grafana-frontend-platform /public/sass/ @grafana/grafana-frontend-platform
/public/test/ @grafana/grafana-frontend-platform /public/test/ @grafana/grafana-frontend-platform
/public/testdata/ @grafana/grafana-frontend-platform
/public/views/ @grafana/grafana-frontend-platform /public/views/ @grafana/grafana-frontend-platform
/public/app/features/explore/Logs/ @grafana/observability-logs /public/app/features/explore/Logs/ @grafana/observability-logs

View File

@ -63,7 +63,7 @@ func TestCallResource(t *testing.T) {
cfg.Azure = &azsettings.AzureSettings{} cfg.Azure = &azsettings.AzureSettings{}
coreRegistry := coreplugin.ProvideCoreRegistry(nil, &cloudwatch.CloudWatchService{}, nil, nil, nil, nil, coreRegistry := coreplugin.ProvideCoreRegistry(nil, &cloudwatch.CloudWatchService{}, nil, nil, nil, nil,
nil, nil, nil, nil, testdatasource.ProvideService(cfg), nil, nil, nil, nil, nil, nil) nil, nil, nil, nil, testdatasource.ProvideService(), nil, nil, nil, nil, nil, nil)
pCfg, err := config.ProvideConfig(setting.ProvideProvider(cfg), cfg, featuremgmt.WithFeatures()) pCfg, err := config.ProvideConfig(setting.ProvideProvider(cfg), cfg, featuremgmt.WithFeatures())
require.NoError(t, err) require.NoError(t, err)
reg := registry.ProvideService() reg := registry.ProvideService()

View File

@ -103,7 +103,7 @@ func TestIntegrationPluginManager(t *testing.T) {
otsdb := opentsdb.ProvideService(hcp) otsdb := opentsdb.ProvideService(hcp)
pr := prometheus.ProvideService(hcp, cfg, features, tracer) pr := prometheus.ProvideService(hcp, cfg, features, tracer)
tmpo := tempo.ProvideService(hcp) tmpo := tempo.ProvideService(hcp)
td := testdatasource.ProvideService(cfg) td := testdatasource.ProvideService()
pg := postgres.ProvideService(cfg) pg := postgres.ProvideService(cfg)
my := mysql.ProvideService(cfg, hcp) my := mysql.ProvideService(cfg, hcp)
ms := mssql.ProvideService(cfg) ms := mssql.ProvideService(cfg)

View File

@ -59,7 +59,6 @@ var (
Disk: &StorageLocalDiskConfig{ Disk: &StorageLocalDiskConfig{
Path: publicRoot, Path: publicRoot,
Roots: []string{ Roots: []string{
"/testdata/",
"/img/icons/", "/img/icons/",
"/img/bg/", "/img/bg/",
"/gazetteer/", "/gazetteer/",
@ -75,12 +74,12 @@ func TestListFiles(t *testing.T) {
store := newStandardStorageService(db.InitTestDB(t), roots, func(orgId int64) []storageRuntime { store := newStandardStorageService(db.InitTestDB(t), roots, func(orgId int64) []storageRuntime {
return make([]storageRuntime, 0) return make([]storageRuntime, 0)
}, allowAllAuthService, cfg, nil) }, allowAllAuthService, cfg, nil)
frame, err := store.List(context.Background(), dummyUser, "public/testdata") frame, err := store.List(context.Background(), dummyUser, "public/maps")
require.NoError(t, err) require.NoError(t, err)
experimental.CheckGoldenJSONFrame(t, "testdata", "public_testdata.golden", frame.Frame, true) experimental.CheckGoldenJSONFrame(t, "testdata", "public_testdata.golden", frame.Frame, true)
file, err := store.Read(context.Background(), dummyUser, "public/testdata/js_libraries.csv") file, err := store.Read(context.Background(), dummyUser, "public/maps/countries.geojson")
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, file) require.NotNil(t, file)
@ -96,7 +95,7 @@ func TestListFilesWithoutPermissions(t *testing.T) {
store := newStandardStorageService(db.InitTestDB(t), roots, func(orgId int64) []storageRuntime { store := newStandardStorageService(db.InitTestDB(t), roots, func(orgId int64) []storageRuntime {
return make([]storageRuntime, 0) return make([]storageRuntime, 0)
}, denyAllAuthService, cfg, nil) }, denyAllAuthService, cfg, nil)
frame, err := store.List(context.Background(), dummyUser, "public/testdata") frame, err := store.List(context.Background(), dummyUser, "public/maps")
require.NoError(t, err) require.NoError(t, err)
rowLen, err := frame.RowLen() rowLen, err := frame.RowLen()
require.NoError(t, err) require.NoError(t, err)

View File

@ -11,20 +11,15 @@
// } // }
// } // }
// Name: // Name:
// Dimensions: 3 Fields by 7 Rows // Dimensions: 3 Fields by 2 Rows
// +--------------------------+-------------------------+---------------+ // +--------------------+----------------------+---------------+
// | Name: name | Name: mediaType | Name: size | // | Name: name | Name: mediaType | Name: size |
// | Labels: | Labels: | Labels: | // | Labels: | Labels: | Labels: |
// | Type: []string | Type: []string | Type: []int64 | // | Type: []string | Type: []string | Type: []int64 |
// +--------------------------+-------------------------+---------------+ // +--------------------+----------------------+---------------+
// | browser_marketshare.csv | text/csv; charset=utf-8 | 355 | // | countries.geojson | application/geo+json | 255943 |
// | flight_info_by_state.csv | text/csv; charset=utf-8 | 681 | // | usa-states.geojson | application/geo+json | 89263 |
// | gdp_per_capita.csv | text/csv; charset=utf-8 | 4116 | // +--------------------+----------------------+---------------+
// | js_libraries.csv | text/csv; charset=utf-8 | 179 |
// | ohlc_dogecoin.csv | text/csv; charset=utf-8 | 191804 |
// | population_by_state.csv | text/csv; charset=utf-8 | 138 |
// | weight_height.csv | text/csv; charset=utf-8 | 418121 |
// +--------------------------+-------------------------+---------------+
// //
// //
// 🌟 This was machine generated. Do not edit. 🌟 // 🌟 This was machine generated. Do not edit. 🌟
@ -73,31 +68,16 @@
"data": { "data": {
"values": [ "values": [
[ [
"browser_marketshare.csv", "countries.geojson",
"flight_info_by_state.csv", "usa-states.geojson"
"gdp_per_capita.csv",
"js_libraries.csv",
"ohlc_dogecoin.csv",
"population_by_state.csv",
"weight_height.csv"
], ],
[ [
"text/csv; charset=utf-8", "application/geo+json",
"text/csv; charset=utf-8", "application/geo+json"
"text/csv; charset=utf-8",
"text/csv; charset=utf-8",
"text/csv; charset=utf-8",
"text/csv; charset=utf-8",
"text/csv; charset=utf-8"
], ],
[ [
355, 255943,
681, 89263
4116,
179,
191804,
138,
418121
] ]
] ]
} }

View File

@ -2,11 +2,11 @@ package testdatasource
import ( import (
"context" "context"
"embed"
"encoding/csv" "encoding/csv"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
@ -92,6 +92,9 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD
return resp, nil return resp, nil
} }
//go:embed data/*.csv
var embeddedCsvFiles embed.FS
func (s *Service) loadCsvFile(fileName string) (*data.Frame, error) { func (s *Service) loadCsvFile(fileName string) (*data.Frame, error) {
validFileName := regexp.MustCompile(`^\w+\.csv$`) validFileName := regexp.MustCompile(`^\w+\.csv$`)
@ -100,11 +103,9 @@ func (s *Service) loadCsvFile(fileName string) (*data.Frame, error) {
} }
csvFilepath := filepath.Clean(filepath.Join("/", fileName)) csvFilepath := filepath.Clean(filepath.Join("/", fileName))
filePath := filepath.Join(s.cfg.StaticRootPath, "testdata", csvFilepath) filePath := filepath.Join("data", csvFilepath)
// Can ignore gosec G304 here, because we check the file pattern above fileReader, err := embeddedCsvFiles.Open(filePath)
// nolint:gosec
fileReader, err := os.Open(filePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed open file: %v", err) return nil, fmt.Errorf("failed open file: %v", err)
} }

View File

@ -10,11 +10,10 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/testdatasource/sims" "github.com/grafana/grafana/pkg/tsdb/testdatasource/sims"
) )
func ProvideService(cfg *setting.Cfg) *Service { func ProvideService() *Service {
s := &Service{ s := &Service{
queryMux: datasource.NewQueryTypeMux(), queryMux: datasource.NewQueryTypeMux(),
scenarios: map[string]*Scenario{}, scenarios: map[string]*Scenario{},
@ -30,7 +29,6 @@ func ProvideService(cfg *setting.Cfg) *Service {
data.NewField("Value", nil, make([]float64, 1)), data.NewField("Value", nil, make([]float64, 1)),
), ),
logger: log.New("tsdb.testdata"), logger: log.New("tsdb.testdata"),
cfg: cfg,
} }
var err error var err error
@ -46,7 +44,6 @@ func ProvideService(cfg *setting.Cfg) *Service {
} }
type Service struct { type Service struct {
cfg *setting.Cfg
logger log.Logger logger log.Logger
scenarios map[string]*Scenario scenarios map[string]*Scenario
frame *data.Frame frame *data.Frame