From 81c58cbecb15b5041c2c1d250503f8298fe4d090 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 15 Mar 2023 16:50:16 -0300 Subject: [PATCH 1/4] override TEST_DATABASE_(MYSQL|POSTGRESQL)_DSN for CI This should allow correct use of the generated database instead of forcing tests to run in a single database. --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 34f742f052..f029db7bc5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -41,7 +41,8 @@ jobs: --ulimit nofile=8096:8096 \ --env-file=dotenv/test.env \ --env MM_SQLSETTINGS_DRIVERNAME="${{ inputs.drivername }}" \ - --env MM_SQLSETTINGS_DATASOURCE="${{ inputs.datasource }}" \ + --env TEST_DATABASE_MYSQL_DSN="${{ inputs.datasource }}" \ + --env TEST_DATABASE_POSTGRESQL_DSN="${{ inputs.datasource }}" \ -v ~/work/mattermost-server:/mattermost-server \ -w /mattermost-server/mattermost-server/server \ $BUILD_IMAGE \ From 828b5e584204ff781aa27889f268840c2bd1e016 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 15 Mar 2023 17:15:33 -0300 Subject: [PATCH 2/4] log when TEST_DATABASE_(MYSQL|POSTGRESQL)_DSN is unset during tests --- server/channels/store/sqlstore/store.go | 2 +- server/channels/store/storetest/settings.go | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/server/channels/store/sqlstore/store.go b/server/channels/store/sqlstore/store.go index 0e3d05d5a1..a1d7380b38 100644 --- a/server/channels/store/sqlstore/store.go +++ b/server/channels/store/sqlstore/store.go @@ -237,7 +237,7 @@ func SetupConnection(connType string, dataSource string, settings *model.SqlSett } for i := 0; i < DBPingAttempts; i++ { - mlog.Info("Pinging SQL", mlog.String("database", connType)) + mlog.Info("Pinging SQL", mlog.String("database", connType), mlog.String("dataSource", dataSource)) ctx, cancel := context.WithTimeout(context.Background(), DBPingTimeoutSecs*time.Second) defer cancel() err = db.PingContext(ctx) diff --git a/server/channels/store/storetest/settings.go b/server/channels/store/storetest/settings.go index f08b038eaa..d44f3c50e0 100644 --- a/server/channels/store/storetest/settings.go +++ b/server/channels/store/storetest/settings.go @@ -17,6 +17,7 @@ import ( "github.com/pkg/errors" "github.com/mattermost/mattermost-server/v6/model" + "github.com/mattermost/mattermost-server/v6/server/platform/shared/mlog" ) const ( @@ -50,7 +51,11 @@ func log(message string) { // MySQLSettings returns the database settings to connect to the MySQL unittesting database. // The database name is generated randomly and must be created before use. func MySQLSettings(withReplica bool) *model.SqlSettings { - dsn := getEnv("TEST_DATABASE_MYSQL_DSN", defaultMysqlDSN) + dsn := os.Getenv("TEST_DATABASE_MYSQL_DSN") + if dsn == "" { + dsn = defaultMysqlDSN + mlog.Info("No TEST_DATABASE_MYSQL_DSN override, using default", mlog.String("default_dsn", dsn)) + } cfg, err := mysql.ParseDSN(dsn) if err != nil { panic("failed to parse dsn " + dsn + ": " + err.Error()) @@ -70,7 +75,12 @@ func MySQLSettings(withReplica bool) *model.SqlSettings { // PostgresSQLSettings returns the database settings to connect to the PostgreSQL unittesting database. // The database name is generated randomly and must be created before use. func PostgreSQLSettings() *model.SqlSettings { - dsn := getEnv("TEST_DATABASE_POSTGRESQL_DSN", defaultPostgresqlDSN) + dsn := os.Getenv("TEST_DATABASE_POSTGRESQL_DSN") + if dsn == "" { + dsn = defaultPostgresqlDSN + mlog.Info("No TEST_DATABASE_POSTGRESQL_DSN override, using default", mlog.String("default_dsn", dsn)) + } + dsnURL, err := url.Parse(dsn) if err != nil { panic("failed to parse dsn " + dsn + ": " + err.Error()) From affc64da791670965d6459ec6ad0cf0c5866505e Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 15 Mar 2023 17:17:38 -0300 Subject: [PATCH 3/4] use MM_SQLSETTINGS_DRIVERNAME within playbooks --- server/playbooks/server/main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/playbooks/server/main_test.go b/server/playbooks/server/main_test.go index 0c4747cc11..3f13f5485e 100644 --- a/server/playbooks/server/main_test.go +++ b/server/playbooks/server/main_test.go @@ -103,7 +103,7 @@ func Setup(t *testing.T) *TestEnvironment { os.Unsetenv("MM_SERVICESETTINGS_LISTENADDRESS") // Environment Settings - driverName := getEnvWithDefault("TEST_DATABASE_DRIVERNAME", "postgres") + driverName := getEnvWithDefault("MM_SQLSETTINGS_DRIVERNAME", "postgres") sqlSettings := storetest.MakeSqlSettings(driverName, false) // Create a test memory store and modify configuration appropriately From 724ad8e5597820c0feae4ea885e4d6205e0efdc9 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 15 Mar 2023 17:55:09 -0300 Subject: [PATCH 4/4] Keep setting MM_SQLSETTINGS_DATASOURCE, but unset for tests Boards requires something to connect to when starting up, even for tests that mock the store. So keep setting this, but unset as necessary when pointing at a generated database. --- .github/workflows/test.yml | 1 + server/channels/store/storetest/settings.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f029db7bc5..fdfaa6f774 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -41,6 +41,7 @@ jobs: --ulimit nofile=8096:8096 \ --env-file=dotenv/test.env \ --env MM_SQLSETTINGS_DRIVERNAME="${{ inputs.drivername }}" \ + --env MM_SQLSETTINGS_DATASOURCE="${{ inputs.datasource }}" \ --env TEST_DATABASE_MYSQL_DSN="${{ inputs.datasource }}" \ --env TEST_DATABASE_POSTGRESQL_DSN="${{ inputs.datasource }}" \ -v ~/work/mattermost-server:/mattermost-server \ diff --git a/server/channels/store/storetest/settings.go b/server/channels/store/storetest/settings.go index d44f3c50e0..27d196e5dd 100644 --- a/server/channels/store/storetest/settings.go +++ b/server/channels/store/storetest/settings.go @@ -55,7 +55,15 @@ func MySQLSettings(withReplica bool) *model.SqlSettings { if dsn == "" { dsn = defaultMysqlDSN mlog.Info("No TEST_DATABASE_MYSQL_DSN override, using default", mlog.String("default_dsn", dsn)) + } else { + mlog.Info("Using TEST_DATABASE_MYSQL_DSN override", mlog.String("dsn", dsn)) } + + if os.Getenv("MM_SQLSETTINGS_DATASOURCE") != "" { + os.Unsetenv("MM_SQLSETTINGS_DATASOURCE") + mlog.Info("Clearing MM_SQLSETTINGS_DATASOURCE to use test dsn exclusively") + } + cfg, err := mysql.ParseDSN(dsn) if err != nil { panic("failed to parse dsn " + dsn + ": " + err.Error()) @@ -79,6 +87,13 @@ func PostgreSQLSettings() *model.SqlSettings { if dsn == "" { dsn = defaultPostgresqlDSN mlog.Info("No TEST_DATABASE_POSTGRESQL_DSN override, using default", mlog.String("default_dsn", dsn)) + } else { + mlog.Info("Using TEST_DATABASE_POSTGRESQL_DSN override", mlog.String("dsn", dsn)) + } + + if os.Getenv("MM_SQLSETTINGS_DATASOURCE") != "" { + os.Unsetenv("MM_SQLSETTINGS_DATASOURCE") + mlog.Info("Clearing MM_SQLSETTINGS_DATASOURCE to use test dsn exclusively") } dsnURL, err := url.Parse(dsn)