From e01d1e1f5b39677e8817bda7cc946b3c8fcf9a4a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 3 May 2022 17:38:54 +0100 Subject: [PATCH 1/2] Skip tests that require a database if we can't connect to one (#2413) * Skip tests that require a database if we can't connect to one * Add `DENDRITE_SKIP_DB_TESTS` environment variable to bring @kegsay joy * Call it `DENDRITE_TEST_SKIP_NODB` intead --- test/db.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test/db.go b/test/db.go index 6412feaa6..fecae5d48 100644 --- a/test/db.go +++ b/test/db.go @@ -33,10 +33,19 @@ var DBTypeSQLite DBType = 1 var DBTypePostgres DBType = 2 var Quiet = false +var Required = os.Getenv("DENDRITE_TEST_SKIP_NODB") == "" -func createLocalDB(dbName string) { +func fatalError(t *testing.T, format string, args ...interface{}) { + if Required { + t.Fatalf(format, args...) + } else { + t.Skipf(format, args...) + } +} + +func createLocalDB(t *testing.T, dbName string) { if !Quiet { - fmt.Println("Note: tests require a postgres install accessible to the current user") + t.Log("Note: tests require a postgres install accessible to the current user") } createDB := exec.Command("createdb", dbName) if !Quiet { @@ -44,15 +53,15 @@ func createLocalDB(dbName string) { createDB.Stderr = os.Stderr } err := createDB.Run() - if err != nil && !Quiet { - fmt.Println("createLocalDB returned error:", err) + if err != nil { + fatalError(t, "createLocalDB returned error: %s", err) } } func createRemoteDB(t *testing.T, dbName, user, connStr string) { db, err := sql.Open("postgres", connStr+" dbname=postgres") if err != nil { - t.Fatalf("failed to open postgres conn with connstr=%s : %s", connStr, err) + fatalError(t, "failed to open postgres conn with connstr=%s : %s", connStr, err) } _, err = db.Exec(fmt.Sprintf(`CREATE DATABASE %s;`, dbName)) if err != nil { @@ -133,7 +142,7 @@ func PrepareDBConnectionString(t *testing.T, dbType DBType) (connStr string, clo hash := sha256.Sum256([]byte(wd)) dbName := fmt.Sprintf("dendrite_test_%s", hex.EncodeToString(hash[:16])) if postgresDB == "" { // local server, use createdb - createLocalDB(dbName) + createLocalDB(t, dbName) } else { // remote server, shell into the postgres user and CREATE DATABASE createRemoteDB(t, dbName, user, connStr) } From b0a9e85c4a02f39880682d9d682f9cc7af13a93c Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 3 May 2022 17:40:56 +0100 Subject: [PATCH 2/2] Fix bug in database global setup --- setup/base/base.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup/base/base.go b/setup/base/base.go index 9b227b70b..3641ad780 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -201,7 +201,8 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, options ...Base if cfg.Global.DatabaseOptions.ConnectionString.IsSQLite() { logrus.Panic("Using a global database connection pool is not supported with SQLite databases") } - if db, err = sqlutil.Open(&cfg.Global.DatabaseOptions, sqlutil.NewDummyWriter()); err != nil { + writer = sqlutil.NewDummyWriter() + if db, err = sqlutil.Open(&cfg.Global.DatabaseOptions, writer); err != nil { logrus.WithError(err).Panic("Failed to set up global database connections") } logrus.Debug("Using global database connection pool")