mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Embed CSV files with the test data source (#71435)
This commit is contained in:
parent
51c94bccd7
commit
9990d5025d
1
.github/CODEOWNERS
vendored
1
.github/CODEOWNERS
vendored
@ -444,7 +444,6 @@ lerna.json @grafana/frontend-ops
|
||||
/public/robots.txt @grafana/frontend-ops
|
||||
/public/sass/ @grafana/grafana-frontend-platform
|
||||
/public/test/ @grafana/grafana-frontend-platform
|
||||
/public/testdata/ @grafana/grafana-frontend-platform
|
||||
/public/views/ @grafana/grafana-frontend-platform
|
||||
|
||||
/public/app/features/explore/Logs/ @grafana/observability-logs
|
||||
|
@ -63,7 +63,7 @@ func TestCallResource(t *testing.T) {
|
||||
cfg.Azure = &azsettings.AzureSettings{}
|
||||
|
||||
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())
|
||||
require.NoError(t, err)
|
||||
reg := registry.ProvideService()
|
||||
|
@ -103,7 +103,7 @@ func TestIntegrationPluginManager(t *testing.T) {
|
||||
otsdb := opentsdb.ProvideService(hcp)
|
||||
pr := prometheus.ProvideService(hcp, cfg, features, tracer)
|
||||
tmpo := tempo.ProvideService(hcp)
|
||||
td := testdatasource.ProvideService(cfg)
|
||||
td := testdatasource.ProvideService()
|
||||
pg := postgres.ProvideService(cfg)
|
||||
my := mysql.ProvideService(cfg, hcp)
|
||||
ms := mssql.ProvideService(cfg)
|
||||
|
@ -59,7 +59,6 @@ var (
|
||||
Disk: &StorageLocalDiskConfig{
|
||||
Path: publicRoot,
|
||||
Roots: []string{
|
||||
"/testdata/",
|
||||
"/img/icons/",
|
||||
"/img/bg/",
|
||||
"/gazetteer/",
|
||||
@ -75,12 +74,12 @@ func TestListFiles(t *testing.T) {
|
||||
store := newStandardStorageService(db.InitTestDB(t), roots, func(orgId int64) []storageRuntime {
|
||||
return make([]storageRuntime, 0)
|
||||
}, 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)
|
||||
|
||||
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.NotNil(t, file)
|
||||
|
||||
@ -96,7 +95,7 @@ func TestListFilesWithoutPermissions(t *testing.T) {
|
||||
store := newStandardStorageService(db.InitTestDB(t), roots, func(orgId int64) []storageRuntime {
|
||||
return make([]storageRuntime, 0)
|
||||
}, 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)
|
||||
rowLen, err := frame.RowLen()
|
||||
require.NoError(t, err)
|
||||
|
@ -11,20 +11,15 @@
|
||||
// }
|
||||
// }
|
||||
// Name:
|
||||
// Dimensions: 3 Fields by 7 Rows
|
||||
// +--------------------------+-------------------------+---------------+
|
||||
// Dimensions: 3 Fields by 2 Rows
|
||||
// +--------------------+----------------------+---------------+
|
||||
// | Name: name | Name: mediaType | Name: size |
|
||||
// | Labels: | Labels: | Labels: |
|
||||
// | Type: []string | Type: []string | Type: []int64 |
|
||||
// +--------------------------+-------------------------+---------------+
|
||||
// | browser_marketshare.csv | text/csv; charset=utf-8 | 355 |
|
||||
// | flight_info_by_state.csv | text/csv; charset=utf-8 | 681 |
|
||||
// | 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 |
|
||||
// +--------------------------+-------------------------+---------------+
|
||||
// +--------------------+----------------------+---------------+
|
||||
// | countries.geojson | application/geo+json | 255943 |
|
||||
// | usa-states.geojson | application/geo+json | 89263 |
|
||||
// +--------------------+----------------------+---------------+
|
||||
//
|
||||
//
|
||||
// 🌟 This was machine generated. Do not edit. 🌟
|
||||
@ -73,31 +68,16 @@
|
||||
"data": {
|
||||
"values": [
|
||||
[
|
||||
"browser_marketshare.csv",
|
||||
"flight_info_by_state.csv",
|
||||
"gdp_per_capita.csv",
|
||||
"js_libraries.csv",
|
||||
"ohlc_dogecoin.csv",
|
||||
"population_by_state.csv",
|
||||
"weight_height.csv"
|
||||
"countries.geojson",
|
||||
"usa-states.geojson"
|
||||
],
|
||||
[
|
||||
"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",
|
||||
"text/csv; charset=utf-8",
|
||||
"text/csv; charset=utf-8"
|
||||
"application/geo+json",
|
||||
"application/geo+json"
|
||||
],
|
||||
[
|
||||
355,
|
||||
681,
|
||||
4116,
|
||||
179,
|
||||
191804,
|
||||
138,
|
||||
418121
|
||||
255943,
|
||||
89263
|
||||
]
|
||||
]
|
||||
}
|
||||
|
@ -2,11 +2,11 @@ package testdatasource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -92,6 +92,9 @@ func (s *Service) handleCsvFileScenario(ctx context.Context, req *backend.QueryD
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
//go:embed data/*.csv
|
||||
var embeddedCsvFiles embed.FS
|
||||
|
||||
func (s *Service) loadCsvFile(fileName string) (*data.Frame, error) {
|
||||
validFileName := regexp.MustCompile(`^\w+\.csv$`)
|
||||
|
||||
@ -100,11 +103,9 @@ func (s *Service) loadCsvFile(fileName string) (*data.Frame, error) {
|
||||
}
|
||||
|
||||
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
|
||||
// nolint:gosec
|
||||
fileReader, err := os.Open(filePath)
|
||||
fileReader, err := embeddedCsvFiles.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed open file: %v", err)
|
||||
}
|
||||
|
@ -10,11 +10,10 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/testdatasource/sims"
|
||||
)
|
||||
|
||||
func ProvideService(cfg *setting.Cfg) *Service {
|
||||
func ProvideService() *Service {
|
||||
s := &Service{
|
||||
queryMux: datasource.NewQueryTypeMux(),
|
||||
scenarios: map[string]*Scenario{},
|
||||
@ -30,7 +29,6 @@ func ProvideService(cfg *setting.Cfg) *Service {
|
||||
data.NewField("Value", nil, make([]float64, 1)),
|
||||
),
|
||||
logger: log.New("tsdb.testdata"),
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
var err error
|
||||
@ -46,7 +44,6 @@ func ProvideService(cfg *setting.Cfg) *Service {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
cfg *setting.Cfg
|
||||
logger log.Logger
|
||||
scenarios map[string]*Scenario
|
||||
frame *data.Frame
|
||||
|
Loading…
Reference in New Issue
Block a user