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:
Sofia Papagiannaki
2023-03-17 17:31:03 +02:00
committed by GitHub
parent 7261c6f7cd
commit f5cb8c660e
2 changed files with 28 additions and 5 deletions

View File

@ -84,8 +84,8 @@ func ProvideService(cfg *setting.Cfg, cacheService *localcache.CacheService, mig
return s, nil
}
func ProvideServiceForTests(migrations registry.DatabaseMigrator) (*SQLStore, error) {
return initTestDB(migrations, InitTestDBOpt{EnsureDefaultOrgAndUser: true})
func ProvideServiceForTests(cfg *setting.Cfg, migrations registry.DatabaseMigrator) (*SQLStore, error) {
return initTestDB(cfg, migrations, InitTestDBOpt{EnsureDefaultOrgAndUser: true})
}
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.
func InitTestDBWithMigration(t ITestDB, migration registry.DatabaseMigrator, opts ...InitTestDBOpt) *SQLStore {
t.Helper()
store, err := initTestDB(migration, opts...)
store, err := initTestDB(setting.NewCfg(), migration, opts...)
if err != nil {
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.
func InitTestDB(t ITestDB, opts ...InitTestDBOpt) *SQLStore {
t.Helper()
store, err := initTestDB(&migrations.OSSMigrations{}, opts...)
store, err := initTestDB(setting.NewCfg(), &migrations.OSSMigrations{}, opts...)
if err != nil {
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
}
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()
defer testSQLStoreMutex.Unlock()
@ -559,10 +560,12 @@ func initTestDB(migration registry.DatabaseMigrator, opts ...InitTestDBOpt) (*SQ
}
return false
}
sec, err := cfg.Raw.NewSection("database")
if err != nil {
return nil, err
}
if _, err := sec.NewKey("type", dbType); err != nil {
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
engine, err := xorm.NewEngine(dbType, sec.Key("connection_string").String())
if err != nil {

View File

@ -53,6 +53,11 @@ func StartGrafanaEnv(t *testing.T, grafDir, cfgPath string) (string, *server.Tes
require.NoError(t, err)
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() {
// When the server runs, it will also build and initialize the service graph
if err := env.Server.Run(); err != nil {