mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-21 04:53:14 -06:00
Use t.TempDir for SQLite databases, so tests don't rip out each others
databases
This commit is contained in:
parent
430932f0f1
commit
8b4bf1561b
12
test/db.go
12
test/db.go
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
|
@ -103,13 +104,10 @@ func currentUser() string {
|
||||||
// TODO: namespace for concurrent package tests
|
// TODO: namespace for concurrent package tests
|
||||||
func PrepareDBConnectionString(t *testing.T, dbType DBType) (connStr string, close func()) {
|
func PrepareDBConnectionString(t *testing.T, dbType DBType) (connStr string, close func()) {
|
||||||
if dbType == DBTypeSQLite {
|
if dbType == DBTypeSQLite {
|
||||||
// this will be made in the current working directory which namespaces concurrent package runs correctly
|
// this will be made in the t.TempDir, which is unique per test
|
||||||
dbname := "dendrite_test.db"
|
dbname := filepath.Join(t.TempDir(), "dendrite_test.db")
|
||||||
return fmt.Sprintf("file:%s", dbname), func() {
|
return fmt.Sprintf("file:%s", dbname), func() {
|
||||||
err := os.Remove(dbname)
|
t.Cleanup(func() {}) // removes the t.TempDir
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to cleanup sqlite db '%s': %s", dbname, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,7 +174,7 @@ func WithAllDatabases(t *testing.T, testFn func(t *testing.T, db DBType)) {
|
||||||
for dbName, dbType := range dbs {
|
for dbName, dbType := range dbs {
|
||||||
dbt := dbType
|
dbt := dbType
|
||||||
t.Run(dbName, func(tt *testing.T) {
|
t.Run(dbName, func(tt *testing.T) {
|
||||||
//tt.Parallel()
|
tt.Parallel()
|
||||||
testFn(tt, dbt)
|
testFn(tt, dbt)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,18 +15,14 @@
|
||||||
package testrig
|
package testrig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"path/filepath"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/setup/base"
|
"github.com/matrix-org/dendrite/setup/base"
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
"github.com/matrix-org/dendrite/test"
|
"github.com/matrix-org/dendrite/test"
|
||||||
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) {
|
func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) {
|
||||||
|
|
@ -77,27 +73,22 @@ func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, f
|
||||||
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
|
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
|
||||||
// the file system event with InMemory=true :(
|
// the file system event with InMemory=true :(
|
||||||
cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType)
|
cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType)
|
||||||
|
|
||||||
|
// Use a temp dir provided by go for tests, this will be cleanup by a call to t.CleanUp()
|
||||||
|
tempDir := t.TempDir()
|
||||||
|
cfg.FederationAPI.Database.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "federationapi.db"))
|
||||||
|
cfg.KeyServer.Database.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "keyserver.db"))
|
||||||
|
cfg.MSCs.Database.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "mscs.db"))
|
||||||
|
cfg.MediaAPI.Database.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "mediaapi.db"))
|
||||||
|
cfg.RoomServer.Database.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "roomserver.db"))
|
||||||
|
cfg.SyncAPI.Database.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "syncapi.db"))
|
||||||
|
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(filepath.Join("file://", tempDir, "userapi.db"))
|
||||||
|
|
||||||
base := base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics)
|
base := base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics)
|
||||||
return base, func() {
|
return base, func() {
|
||||||
base.ShutdownDendrite()
|
base.ShutdownDendrite()
|
||||||
base.WaitForShutdown()
|
base.WaitForShutdown()
|
||||||
// cleanup db files. This risks getting out of sync as we add more database strings :(
|
t.Cleanup(func() {}) // removes t.TempDir, where all database files are created
|
||||||
dbFiles := []config.DataSource{
|
|
||||||
cfg.FederationAPI.Database.ConnectionString,
|
|
||||||
cfg.KeyServer.Database.ConnectionString,
|
|
||||||
cfg.MSCs.Database.ConnectionString,
|
|
||||||
cfg.MediaAPI.Database.ConnectionString,
|
|
||||||
cfg.RoomServer.Database.ConnectionString,
|
|
||||||
cfg.SyncAPI.Database.ConnectionString,
|
|
||||||
cfg.UserAPI.AccountDatabase.ConnectionString,
|
|
||||||
}
|
|
||||||
for _, fileURI := range dbFiles {
|
|
||||||
path := strings.TrimPrefix(string(fileURI), "file:")
|
|
||||||
err := os.Remove(path)
|
|
||||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
||||||
t.Fatalf("failed to cleanup sqlite db '%s': %s", fileURI, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
t.Fatalf("unknown db type: %v", dbType)
|
t.Fatalf("unknown db type: %v", dbType)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue