[v9.4.x] SQLStore: Fix setting query retries for integration tests (#64972)

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

(cherry picked from commit f5cb8c660e)

Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2023-03-17 18:05:30 +02:00
committed by GitHub
co-authored by Sofia Papagiannaki
parent c5f4ef6c3e
commit 7e5e3d78d0
2 changed files with 28 additions and 5 deletions
+23 -5
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,
@@ -504,7 +504,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)
}
@@ -514,7 +514,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)
}
@@ -526,7 +526,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()
@@ -560,10 +561,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
}
@@ -590,6 +593,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 {