mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 18:13:09 +08:00
SQLStore: Fix setting query tries for integration tests (#64944)
* SQLStore: Pass testinfra database configuration to the test database * Add test * Bypass gocyclo check for initTestDB
This commit is contained in:

committed by
GitHub

parent
7261c6f7cd
commit
f5cb8c660e
@ -84,8 +84,8 @@ func ProvideService(cfg *setting.Cfg, cacheService *localcache.CacheService, mig
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProvideServiceForTests(migrations registry.DatabaseMigrator) (*SQLStore, error) {
|
func ProvideServiceForTests(cfg *setting.Cfg, migrations registry.DatabaseMigrator) (*SQLStore, error) {
|
||||||
return initTestDB(migrations, InitTestDBOpt{EnsureDefaultOrgAndUser: true})
|
return initTestDB(cfg, migrations, InitTestDBOpt{EnsureDefaultOrgAndUser: true})
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, engine *xorm.Engine,
|
func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, engine *xorm.Engine,
|
||||||
@ -503,7 +503,7 @@ var featuresEnabledDuringTests = []string{
|
|||||||
// InitTestDBWithMigration initializes the test DB given custom migrations.
|
// InitTestDBWithMigration initializes the test DB given custom migrations.
|
||||||
func InitTestDBWithMigration(t ITestDB, migration registry.DatabaseMigrator, opts ...InitTestDBOpt) *SQLStore {
|
func InitTestDBWithMigration(t ITestDB, migration registry.DatabaseMigrator, opts ...InitTestDBOpt) *SQLStore {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
store, err := initTestDB(migration, opts...)
|
store, err := initTestDB(setting.NewCfg(), migration, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to initialize sql store: %s", err)
|
t.Fatalf("failed to initialize sql store: %s", err)
|
||||||
}
|
}
|
||||||
@ -513,7 +513,7 @@ func InitTestDBWithMigration(t ITestDB, migration registry.DatabaseMigrator, opt
|
|||||||
// InitTestDB initializes the test DB.
|
// InitTestDB initializes the test DB.
|
||||||
func InitTestDB(t ITestDB, opts ...InitTestDBOpt) *SQLStore {
|
func InitTestDB(t ITestDB, opts ...InitTestDBOpt) *SQLStore {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
store, err := initTestDB(&migrations.OSSMigrations{}, opts...)
|
store, err := initTestDB(setting.NewCfg(), &migrations.OSSMigrations{}, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to initialize sql store: %s", err)
|
t.Fatalf("failed to initialize sql store: %s", err)
|
||||||
}
|
}
|
||||||
@ -525,7 +525,8 @@ func InitTestDBWithCfg(t ITestDB, opts ...InitTestDBOpt) (*SQLStore, *setting.Cf
|
|||||||
return store, store.Cfg
|
return store, store.Cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTestDB(migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQLStore, error) {
|
//nolint:gocyclo
|
||||||
|
func initTestDB(testCfg *setting.Cfg, migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQLStore, error) {
|
||||||
testSQLStoreMutex.Lock()
|
testSQLStoreMutex.Lock()
|
||||||
defer testSQLStoreMutex.Unlock()
|
defer testSQLStoreMutex.Unlock()
|
||||||
|
|
||||||
@ -559,10 +560,12 @@ func initTestDB(migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQ
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
sec, err := cfg.Raw.NewSection("database")
|
sec, err := cfg.Raw.NewSection("database")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := sec.NewKey("type", dbType); err != nil {
|
if _, err := sec.NewKey("type", dbType); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -589,6 +592,21 @@ func initTestDB(migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if testCfg.Raw.HasSection("database") {
|
||||||
|
testSec, err := testCfg.Raw.GetSection("database")
|
||||||
|
if err == nil {
|
||||||
|
// copy from testCfg to the Cfg keys that do not exist
|
||||||
|
for _, k := range testSec.Keys() {
|
||||||
|
if sec.HasKey(k.Name()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := sec.NewKey(k.Name(), k.Value()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// need to get engine to clean db before we init
|
// need to get engine to clean db before we init
|
||||||
engine, err := xorm.NewEngine(dbType, sec.Key("connection_string").String())
|
engine, err := xorm.NewEngine(dbType, sec.Key("connection_string").String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -53,6 +53,11 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, env.SQLStore.Sync())
|
require.NoError(t, env.SQLStore.Sync())
|
||||||
|
|
||||||
|
require.NotNil(t, env.SQLStore.Cfg)
|
||||||
|
dbSec, err := env.SQLStore.Cfg.Raw.GetSection("database")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Greater(t, dbSec.Key("query_retries").MustInt(), 0)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// When the server runs, it will also build and initialize the service graph
|
// When the server runs, it will also build and initialize the service graph
|
||||||
if err := env.Server.Run(); err != nil {
|
if err := env.Server.Run(); err != nil {
|
||||||
|
Reference in New Issue
Block a user