Commit 4937f0da by Sofia Papagiannaki Committed by GitHub

SQLStore: Run tests as integration tests (#28265)

* sqlstore: Run tests as integration tests

* Truncate database instead of re-creating it on each test

* Fix test description

See https://github.com/grafana/grafana/pull/12129

* Fix lint issues

* Fix postgres dialect after review suggestion

* Rename and document functions after review suggestion

* Add periods

* Fix auto-increment value for mysql dialect

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
parent c0789606
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......@@ -128,7 +130,7 @@ func TestAlertingDataAccess(t *testing.T) {
So(alert.DashboardSlug, ShouldEqual, "dashboard-with-alerts")
})
Convey("Viewer cannot read alerts", func() {
Convey("Viewer can read alerts", func() {
viewerUser := &models.SignedInUser{OrgRole: models.ROLE_VIEWER, OrgId: 1}
alertQuery := models.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, PanelId: 1, OrgId: 1, User: viewerUser}
err2 := HandleAlertsQuery(&alertQuery)
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
......@@ -45,6 +45,7 @@ type Dialect interface {
PostInsertId(table string, sess *xorm.Session) error
CleanDB() error
TruncateDBTables() error
NoOpSql() string
IsUniqueConstraintViolation(err error) bool
......@@ -270,3 +271,7 @@ func (db *BaseDialect) CleanDB() error {
func (db *BaseDialect) NoOpSql() string {
return "SELECT 0;"
}
func (db *BaseDialect) TruncateDBTables() error {
return nil
}
package migrator
import (
"fmt"
"strconv"
"strings"
......@@ -137,6 +138,36 @@ func (db *Mysql) CleanDB() error {
return nil
}
// TruncateDBTables truncates all the tables.
// A special case is the dashboard_acl table where we keep the default permissions.
func (db *Mysql) TruncateDBTables() error {
tables, err := db.engine.DBMetas()
if err != nil {
return err
}
sess := db.engine.NewSession()
defer sess.Close()
for _, table := range tables {
switch table.Name {
case "dashboard_acl":
// keep default dashboard permissions
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %v WHERE dashboard_id != -1 AND org_id != -1;", db.Quote(table.Name))); err != nil {
return errutil.Wrapf(err, "failed to truncate table %q", table.Name)
}
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE %v AUTO_INCREMENT = 3;", db.Quote(table.Name))); err != nil {
return errutil.Wrapf(err, "failed to reset table %q", table.Name)
}
default:
if _, err := sess.Exec(fmt.Sprintf("TRUNCATE TABLE %v;", db.Quote(table.Name))); err != nil {
return errutil.Wrapf(err, "failed to truncate table %q", table.Name)
}
}
}
return nil
}
func (db *Mysql) isThisError(err error, errcode uint16) bool {
if driverErr, ok := err.(*mysql.MySQLError); ok {
if driverErr.Number == errcode {
......
......@@ -140,6 +140,37 @@ func (db *Postgres) CleanDB() error {
return nil
}
// TruncateDBTables truncates all the tables.
// A special case is the dashboard_acl table where we keep the default permissions.
func (db *Postgres) TruncateDBTables() error {
sess := db.engine.NewSession()
defer sess.Close()
for _, table := range db.engine.Tables {
switch table.Name {
case "":
continue
case "dashboard_acl":
// keep default dashboard permissions
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %v WHERE dashboard_id != -1 AND org_id != -1;", db.Quote(table.Name))); err != nil {
return errutil.Wrapf(err, "failed to truncate table %q", table.Name)
}
if _, err := sess.Exec(fmt.Sprintf("ALTER SEQUENCE %v RESTART WITH 3;", db.Quote(fmt.Sprintf("%v_id_seq", table.Name)))); err != nil {
return errutil.Wrapf(err, "failed to reset table %q", table.Name)
}
default:
if _, err := sess.Exec(fmt.Sprintf("TRUNCATE TABLE %v RESTART IDENTITY CASCADE;", db.Quote(table.Name))); err != nil {
if db.isUndefinedTable(err) {
continue
}
return errutil.Wrapf(err, "failed to truncate table %q", table.Name)
}
}
}
return nil
}
func (db *Postgres) isThisError(err error, errcode string) bool {
if driverErr, ok := err.(*pq.Error); ok {
if string(driverErr.Code) == errcode {
......@@ -157,6 +188,10 @@ func (db *Postgres) ErrorMessage(err error) string {
return ""
}
func (db *Postgres) isUndefinedTable(err error) bool {
return db.isThisError(err, "42P01")
}
func (db *Postgres) IsUniqueConstraintViolation(err error) bool {
return db.isThisError(err, "23505")
}
......
......@@ -3,6 +3,7 @@ package migrator
import (
"fmt"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/mattn/go-sqlite3"
"xorm.io/xorm"
)
......@@ -85,6 +86,39 @@ func (db *Sqlite3) CleanDB() error {
return nil
}
// TruncateDBTables deletes all data from all the tables and resets the sequences.
// A special case is the dashboard_acl table where we keep the default permissions.
func (db *Sqlite3) TruncateDBTables() error {
tables, err := db.engine.DBMetas()
if err != nil {
return err
}
sess := db.engine.NewSession()
defer sess.Close()
for _, table := range tables {
switch table.Name {
case "dashboard_acl":
// keep default dashboard permissions
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %q WHERE dashboard_id != -1 AND org_id != -1;", table.Name)); err != nil {
return errutil.Wrapf(err, "failed to truncate table %q", table.Name)
}
if _, err := sess.Exec("UPDATE sqlite_sequence SET seq = 2 WHERE name = '%s';", table.Name); err != nil {
return errutil.Wrapf(err, "failed to cleanup sqlite_sequence")
}
default:
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %s;", table.Name)); err != nil {
return errutil.Wrapf(err, "failed to truncate table %q", table.Name)
}
}
}
if _, err := sess.Exec("UPDATE sqlite_sequence SET seq = 0 WHERE name != 'dashboard_acl';"); err != nil {
return errutil.Wrapf(err, "failed to cleanup sqlite_sequence")
}
return nil
}
func (db *Sqlite3) isThisError(err error, errcode int) bool {
if driverErr, ok := err.(sqlite3.Error); ok {
if int(driverErr.ExtendedCode) == errcode {
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
// package search_test contains integration tests for search
package searchstore_test
......
// +build integration
package sqlstore
import (
......
......@@ -340,72 +340,80 @@ type ITestDB interface {
Logf(format string, args ...interface{})
}
var testSqlStore *SqlStore
// InitTestDB initializes the test DB.
func InitTestDB(t ITestDB) *SqlStore {
t.Helper()
sqlstore := &SqlStore{}
sqlstore.Bus = bus.New()
sqlstore.CacheService = localcache.New(5*time.Minute, 10*time.Minute)
sqlstore.skipEnsureDefaultOrgAndUser = true
dbType := migrator.SQLITE
// environment variable present for test db?
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
t.Logf("Using database type %q", db)
dbType = db
}
// set test db config
sqlstore.Cfg = setting.NewCfg()
sec, err := sqlstore.Cfg.Raw.NewSection("database")
if err != nil {
t.Fatalf("Failed to create section: %s", err)
}
if _, err := sec.NewKey("type", dbType); err != nil {
t.Fatalf("Failed to create key: %s", err)
}
if testSqlStore == nil {
testSqlStore = &SqlStore{}
testSqlStore.Bus = bus.New()
testSqlStore.CacheService = localcache.New(5*time.Minute, 10*time.Minute)
testSqlStore.skipEnsureDefaultOrgAndUser = true
dbType := migrator.SQLITE
// environment variable present for test db?
if db, present := os.LookupEnv("GRAFANA_TEST_DB"); present {
t.Logf("Using database type %q", db)
dbType = db
}
switch dbType {
case "mysql":
if _, err := sec.NewKey("connection_string", sqlutil.MySQLTestDB().ConnStr); err != nil {
t.Fatalf("Failed to create key: %s", err)
// set test db config
testSqlStore.Cfg = setting.NewCfg()
sec, err := testSqlStore.Cfg.Raw.NewSection("database")
if err != nil {
t.Fatalf("Failed to create section: %s", err)
}
case "postgres":
if _, err := sec.NewKey("connection_string", sqlutil.PostgresTestDB().ConnStr); err != nil {
if _, err := sec.NewKey("type", dbType); err != nil {
t.Fatalf("Failed to create key: %s", err)
}
default:
if _, err := sec.NewKey("connection_string", sqlutil.Sqlite3TestDB().ConnStr); err != nil {
t.Fatalf("Failed to create key: %s", err)
switch dbType {
case "mysql":
if _, err := sec.NewKey("connection_string", sqlutil.MySQLTestDB().ConnStr); err != nil {
t.Fatalf("Failed to create key: %s", err)
}
case "postgres":
if _, err := sec.NewKey("connection_string", sqlutil.PostgresTestDB().ConnStr); err != nil {
t.Fatalf("Failed to create key: %s", err)
}
default:
if _, err := sec.NewKey("connection_string", sqlutil.Sqlite3TestDB().ConnStr); err != nil {
t.Fatalf("Failed to create key: %s", err)
}
}
}
// need to get engine to clean db before we init
t.Logf("Creating database connection: %q", sec.Key("connection_string"))
engine, err := xorm.NewEngine(dbType, sec.Key("connection_string").String())
if err != nil {
t.Fatalf("Failed to init test database: %v", err)
}
// need to get engine to clean db before we init
t.Logf("Creating database connection: %q", sec.Key("connection_string"))
engine, err := xorm.NewEngine(dbType, sec.Key("connection_string").String())
if err != nil {
t.Fatalf("Failed to init test database: %v", err)
}
sqlstore.Dialect = migrator.NewDialect(engine)
testSqlStore.Dialect = migrator.NewDialect(engine)
// temp global var until we get rid of global vars
dialect = sqlstore.Dialect
// temp global var until we get rid of global vars
dialect = testSqlStore.Dialect
t.Logf("Cleaning DB")
if err := dialect.CleanDB(); err != nil {
t.Fatalf("Failed to clean test db %v", err)
}
t.Logf("Cleaning DB")
if err := dialect.CleanDB(); err != nil {
t.Fatalf("Failed to clean test db %v", err)
}
if err := sqlstore.Init(); err != nil {
t.Fatalf("Failed to init test database: %v", err)
if err := testSqlStore.Init(); err != nil {
t.Fatalf("Failed to init test database: %v", err)
}
testSqlStore.engine.DatabaseTZ = time.UTC
testSqlStore.engine.TZLocation = time.UTC
}
sqlstore.engine.DatabaseTZ = time.UTC
sqlstore.engine.TZLocation = time.UTC
if err := dialect.TruncateDBTables(); err != nil {
t.Fatalf("Failed to truncate test db %v", err)
}
return sqlstore
return testSqlStore
}
func IsTestDbMySql() bool {
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
// +build integration
package sqlstore
import (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment