diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index 55a43a0529f..4d4bd3dc50c 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -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 { diff --git a/pkg/tests/testinfra/testinfra.go b/pkg/tests/testinfra/testinfra.go index e6a8e6c1d96..b4302fc482a 100644 --- a/pkg/tests/testinfra/testinfra.go +++ b/pkg/tests/testinfra/testinfra.go @@ -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 {