Chore: Update test database initialization (#81673)

* streamline initialization of test databases, support on-disk sqlite test db

* clean up test databases

* introduce testsuite helper

* use testsuite everywhere we use a test db

* update documentation

* improve error handling

* disable entity integration test until we can figure out locking error
This commit is contained in:
Dan Cech
2024-02-09 09:35:39 -05:00
committed by GitHub
parent de4acb27ce
commit 790e1feb93
104 changed files with 801 additions and 189 deletions
+9 -1
View File
@@ -10,6 +10,8 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/session"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/grafana/grafana/pkg/setting"
)
type DB interface {
@@ -51,10 +53,16 @@ type DB interface {
type Session = sqlstore.DBSession
type InitTestDBOpt = sqlstore.InitTestDBOpt
var SetupTestDB = sqlstore.SetupTestDB
var InitTestDB = sqlstore.InitTestDB
var InitTestDBwithCfg = sqlstore.InitTestDBWithCfg
var CleanupTestDB = sqlstore.CleanupTestDB
var ProvideService = sqlstore.ProvideService
func InitTestDBwithCfg(t sqlutil.ITestDB, opts ...InitTestDBOpt) (*sqlstore.SQLStore, *setting.Cfg) {
store := InitTestDB(t, opts...)
return store, store.Cfg
}
func IsTestDbSQLite() bool {
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); !present || db == "sqlite" {
return true
@@ -12,12 +12,17 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
const (
pngImageBase64 = "iVBORw0KGgoNAANSUhEUgAAAC4AAAAmCAYAAAC76qlaAAAABHNCSVQICAgIfAhkiAAAABl0RVh0U29mdHdhcmUAZ25vbWUtc2NyZWVuc2hvdO8Dvz4AAABFSURBVFiF7c5BDQAhEACx4/x7XjzwGELSKuiamfke9N8OnBKvidfEa+I18Zp4TbwmXhOvidfEa+I18Zp4TbwmXhOvidc2lcsESD1LGnUAAAAASUVORK5CYII="
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
type fsTestCase struct {
name string
skip *bool
+5
View File
@@ -10,8 +10,13 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func createTestableKVStore(t *testing.T) KVStore {
t.Helper()
@@ -13,8 +13,13 @@ import (
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore db.DB) CacheStorage {
t.Helper()
+5
View File
@@ -11,8 +11,13 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func createTestableServerLock(t *testing.T) *ServerLockService {
t.Helper()
@@ -25,8 +25,13 @@ import (
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
func TestMain(m *testing.M) {
testsuite.Run(m)
}
// This is to ensure that the interface contract is held by the implementation
func Test_InterfaceContractValidity(t *testing.T) {
newUsageStats := func() usagestats.Service {
@@ -14,9 +14,15 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/stats/statsimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/grafana/grafana/pkg/util"
)
// run tests with cleanup
func TestMain(m *testing.M) {
testsuite.Run(m)
}
func TestConcurrentUsersMetrics(t *testing.T) {
sqlStore, cfg := db.InitTestDBwithCfg(t)
statsService := statsimpl.ProvideService(&setting.Cfg{}, sqlStore)