diff --git a/api4/main_test.go b/api4/main_test.go index 0829a8eeba..f93699a5ed 100644 --- a/api4/main_test.go +++ b/api4/main_test.go @@ -12,9 +12,14 @@ import ( var mainHelper *testlib.MainHelper func TestMain(m *testing.M) { - mainHelper = testlib.NewMainHelper() - defer mainHelper.Close() - UseTestStore(mainHelper.Store) + var options = testlib.HelperOptions{ + EnableStore: true, + EnableResources: true, + } + mainHelper = testlib.NewMainHelperWithOptions(&options) + defer mainHelper.Close() + + UseTestStore(mainHelper.GetStore()) mainHelper.Main(m) } diff --git a/app/app_test.go b/app/app_test.go index 107a241a83..dc0b7919bb 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -45,10 +45,6 @@ func TestDoAdvancedPermissionsMigration(t *testing.T) { th := Setup(t) defer th.TearDown() - if mainHelper.SqlSupplier == nil { - t.Skip("This test requires a TestStore to be run.") - } - th.ResetRoleMigration() th.App.DoAdvancedPermissionsMigration() @@ -424,10 +420,6 @@ func TestDoEmojisPermissionsMigration(t *testing.T) { th := Setup(t) defer th.TearDown() - if mainHelper.SqlSupplier == nil { - t.Skip("This test requires a TestStore to be run.") - } - // Add a license and change the policy config. restrictCustomEmojiCreation := *th.App.Config().ServiceSettings.DEPRECATED_DO_NOT_USE_RestrictCustomEmojiCreation diff --git a/app/helper_test.go b/app/helper_test.go index f508335ee9..f09ba6bd98 100644 --- a/app/helper_test.go +++ b/app/helper_test.go @@ -34,7 +34,8 @@ type TestHelper struct { } func setupTestHelper(enterprise bool, tb testing.TB) *TestHelper { - mainHelper.Store.DropAllTables() + store := mainHelper.GetStore() + store.DropAllTables() permConfig, err := os.Open(fileutils.FindConfigFile("config.json")) if err != nil { @@ -52,7 +53,7 @@ func setupTestHelper(enterprise bool, tb testing.TB) *TestHelper { } options := []Option{Config(tempConfig.Name(), false)} - options = append(options, StoreOverride(mainHelper.Store)) + options = append(options, StoreOverride(store)) options = append(options, SetLogger(mlog.NewTestingLogger(tb))) s, err := NewServer(options...) @@ -427,25 +428,27 @@ func (me *TestHelper) TearDown() { } func (me *TestHelper) ResetRoleMigration() { - if _, err := mainHelper.SqlSupplier.GetMaster().Exec("DELETE from Roles"); err != nil { + sqlSupplier := mainHelper.GetSqlSupplier() + if _, err := sqlSupplier.GetMaster().Exec("DELETE from Roles"); err != nil { panic(err) } - mainHelper.ClusterInterface.SendClearRoleCacheMessage() + mainHelper.GetClusterInterface().SendClearRoleCacheMessage() - if _, err := mainHelper.SqlSupplier.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": ADVANCED_PERMISSIONS_MIGRATION_KEY}); err != nil { + if _, err := sqlSupplier.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": ADVANCED_PERMISSIONS_MIGRATION_KEY}); err != nil { panic(err) } } func (me *TestHelper) ResetEmojisMigration() { - if _, err := mainHelper.SqlSupplier.GetMaster().Exec("UPDATE Roles SET Permissions=REPLACE(Permissions, ', manage_emojis', '') WHERE builtin=True"); err != nil { + sqlSupplier := mainHelper.GetSqlSupplier() + if _, err := sqlSupplier.GetMaster().Exec("UPDATE Roles SET Permissions=REPLACE(Permissions, ', manage_emojis', '') WHERE builtin=True"); err != nil { panic(err) } - mainHelper.ClusterInterface.SendClearRoleCacheMessage() + mainHelper.GetClusterInterface().SendClearRoleCacheMessage() - if _, err := mainHelper.SqlSupplier.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": EMOJIS_PERMISSIONS_MIGRATION_KEY}); err != nil { + if _, err := sqlSupplier.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": EMOJIS_PERMISSIONS_MIGRATION_KEY}); err != nil { panic(err) } } diff --git a/app/main_test.go b/app/main_test.go index b12c72d462..8b9c838df6 100644 --- a/app/main_test.go +++ b/app/main_test.go @@ -12,7 +12,12 @@ import ( var mainHelper *testlib.MainHelper func TestMain(m *testing.M) { - mainHelper = testlib.NewMainHelper() + var options = testlib.HelperOptions{ + EnableStore: true, + EnableResources: true, + } + + mainHelper = testlib.NewMainHelperWithOptions(&options) defer mainHelper.Close() mainHelper.Main(m) diff --git a/app/plugin_hooks_test.go b/app/plugin_hooks_test.go index cf666f260d..ec2a529b40 100644 --- a/app/plugin_hooks_test.go +++ b/app/plugin_hooks_test.go @@ -874,7 +874,7 @@ func TestErrorString(t *testing.T) { package main import ( - "github.com/pkg/errors" + "errors" "github.com/mattermost/mattermost-server/plugin" ) diff --git a/app/slackimport_test.go b/app/slackimport_test.go index ce80c004d7..80b33b95e3 100644 --- a/app/slackimport_test.go +++ b/app/slackimport_test.go @@ -112,7 +112,7 @@ func TestSlackConvertChannelMentions(t *testing.T) { } func TestSlackParseChannels(t *testing.T) { - file, err := os.Open("../tests/slack-import-test-channels.json") + file, err := os.Open("tests/slack-import-test-channels.json") require.NoError(t, err) defer file.Close() @@ -122,7 +122,7 @@ func TestSlackParseChannels(t *testing.T) { } func TestSlackParseUsers(t *testing.T) { - file, err := os.Open("../tests/slack-import-test-users.json") + file, err := os.Open("tests/slack-import-test-users.json") require.NoError(t, err) defer file.Close() @@ -132,7 +132,7 @@ func TestSlackParseUsers(t *testing.T) { } func TestSlackParsePosts(t *testing.T) { - file, err := os.Open("../tests/slack-import-test-posts.json") + file, err := os.Open("tests/slack-import-test-posts.json") require.NoError(t, err) defer file.Close() diff --git a/cmd/mattermost/commands/cmdtestlib.go b/cmd/mattermost/commands/cmdtestlib.go index 69d04adfbe..7abc8d57c4 100644 --- a/cmd/mattermost/commands/cmdtestlib.go +++ b/cmd/mattermost/commands/cmdtestlib.go @@ -80,7 +80,7 @@ func (h *testHelper) ConfigPath() string { // SetConfig replaces the configuration passed to a running command. func (h *testHelper) SetConfig(config *model.Config) { - config.SqlSettings = *mainHelper.Settings + config.SqlSettings = *mainHelper.GetSqlSettings() h.config = config if err := ioutil.WriteFile(h.configFilePath, []byte(config.ToJson()), 0600); err != nil { diff --git a/cmd/mattermost/commands/main_test.go b/cmd/mattermost/commands/main_test.go index 4f622eef99..d3507b723c 100644 --- a/cmd/mattermost/commands/main_test.go +++ b/cmd/mattermost/commands/main_test.go @@ -22,9 +22,15 @@ func TestMain(m *testing.M) { return } - mainHelper = testlib.NewMainHelper() + var options = testlib.HelperOptions{ + EnableStore: true, + EnableResources: true, + } + + mainHelper = testlib.NewMainHelperWithOptions(&options) defer mainHelper.Close() - api4.UseTestStore(mainHelper.Store) + + api4.UseTestStore(mainHelper.GetStore()) mainHelper.Main(m) } diff --git a/cmd/mattermost/commands/plugin_test.go b/cmd/mattermost/commands/plugin_test.go index 70c127baeb..4f79c9154b 100644 --- a/cmd/mattermost/commands/plugin_test.go +++ b/cmd/mattermost/commands/plugin_test.go @@ -26,8 +26,6 @@ func TestPlugin(t *testing.T) { path, _ := fileutils.FindDir("tests") - os.Chdir(filepath.Join("..", "..", "..")) - th.CheckCommand(t, "plugin", "add", filepath.Join(path, "testplugin.tar.gz")) th.CheckCommand(t, "plugin", "enable", "testplugin") @@ -45,6 +43,4 @@ func TestPlugin(t *testing.T) { th.CheckCommand(t, "plugin", "list") th.CheckCommand(t, "plugin", "delete", "testplugin") - - os.Chdir(filepath.Join("cmd", "mattermost", "commands")) } diff --git a/config/database_test.go b/config/database_test.go index ee00f143d9..b596090c35 100644 --- a/config/database_test.go +++ b/config/database_test.go @@ -28,7 +28,7 @@ func setupConfigDatabase(t *testing.T, cfg *model.Config) (string, func()) { cfgData, err := config.MarshalConfig(cfg) require.NoError(t, err) - db := sqlx.NewDb(mainHelper.SqlSupplier.GetMaster().Db, *mainHelper.Settings.DriverName) + db := sqlx.NewDb(mainHelper.GetSqlSupplier().GetMaster().Db, *mainHelper.GetSqlSettings().DriverName) err = config.InitializeConfigurationsTable(db) require.NoError(t, err) @@ -50,7 +50,7 @@ func getActualDatabaseConfig(t *testing.T) *model.Config { t.Helper() var actualCfgData []byte - db := sqlx.NewDb(mainHelper.SqlSupplier.GetMaster().Db, *mainHelper.Settings.DriverName) + db := sqlx.NewDb(mainHelper.GetSqlSupplier().GetMaster().Db, *mainHelper.GetSqlSettings().DriverName) err := db.Get(&actualCfgData, "SELECT Value FROM Configurations WHERE Active") require.NoError(t, err) @@ -79,8 +79,10 @@ func assertDatabaseNotEqualsConfig(t *testing.T, expectedCfg *model.Config) { } func TestDatabaseStoreNew(t *testing.T) { + sqlSettings := mainHelper.GetSqlSettings() + t.Run("no existing configuration - initialization required", func(t *testing.T) { - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -91,7 +93,7 @@ func TestDatabaseStoreNew(t *testing.T) { _, tearDown := setupConfigDatabase(t, testConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -103,7 +105,7 @@ func TestDatabaseStoreNew(t *testing.T) { _, tearDown := setupConfigDatabase(t, minimalConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -126,7 +128,8 @@ func TestDatabaseStoreGet(t *testing.T) { _, tearDown := setupConfigDatabase(t, testConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + sqlSettings := mainHelper.GetSqlSettings() + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -150,7 +153,8 @@ func TestDatabaseStoreGetEnivironmentOverrides(t *testing.T) { _, tearDown := setupConfigDatabase(t, testConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + sqlSettings := mainHelper.GetSqlSettings() + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -159,7 +163,7 @@ func TestDatabaseStoreGetEnivironmentOverrides(t *testing.T) { os.Setenv("MM_SERVICESETTINGS_SITEURL", "http://override") - ds, err = config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err = config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -168,13 +172,15 @@ func TestDatabaseStoreGetEnivironmentOverrides(t *testing.T) { } func TestDatabaseStoreSet(t *testing.T) { + sqlSettings := mainHelper.GetSqlSettings() + t.Run("set same pointer value", func(t *testing.T) { t.Skip("not yet implemented") _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -188,7 +194,7 @@ func TestDatabaseStoreSet(t *testing.T) { _, tearDown := setupConfigDatabase(t, minimalConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -207,7 +213,7 @@ func TestDatabaseStoreSet(t *testing.T) { _, tearDown := setupConfigDatabase(t, ldapConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -227,7 +233,7 @@ func TestDatabaseStoreSet(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -246,7 +252,7 @@ func TestDatabaseStoreSet(t *testing.T) { _, tearDown := setupConfigDatabase(t, readOnlyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -267,11 +273,11 @@ func TestDatabaseStoreSet(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() - db := sqlx.NewDb(mainHelper.SqlSupplier.GetMaster().Db, *mainHelper.Settings.DriverName) + db := sqlx.NewDb(mainHelper.GetSqlSupplier().GetMaster().Db, *sqlSettings.DriverName) _, err = db.Exec("DROP TABLE Configurations") require.NoError(t, err) @@ -289,7 +295,7 @@ func TestDatabaseStoreSet(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -316,11 +322,13 @@ func TestDatabaseStoreSet(t *testing.T) { } func TestDatabaseStoreLoad(t *testing.T) { + sqlSettings := mainHelper.GetSqlSettings() + t.Run("active configuration no longer exists", func(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -335,7 +343,7 @@ func TestDatabaseStoreLoad(t *testing.T) { _, tearDown := setupConfigDatabase(t, minimalConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -351,14 +359,14 @@ func TestDatabaseStoreLoad(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() cfgData, err := config.MarshalConfig(invalidConfig) require.NoError(t, err) - db := sqlx.NewDb(mainHelper.SqlSupplier.GetMaster().Db, *mainHelper.Settings.DriverName) + db := sqlx.NewDb(mainHelper.GetSqlSupplier().GetMaster().Db, *sqlSettings.DriverName) truncateTables(t) id := model.NewId() _, err = db.NamedExec("INSERT INTO Configurations (Id, Value, CreateAt, Active) VALUES(:Id, :Value, :CreateAt, TRUE)", map[string]interface{}{ @@ -378,7 +386,7 @@ func TestDatabaseStoreLoad(t *testing.T) { _, tearDown := setupConfigDatabase(t, fixesRequiredConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -392,7 +400,7 @@ func TestDatabaseStoreLoad(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -417,7 +425,8 @@ func TestDatabaseStoreSave(t *testing.T) { _, tearDown := setupConfigDatabase(t, minimalConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + sqlSettings := mainHelper.GetSqlSettings() + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() @@ -455,14 +464,15 @@ func TestDatabaseStoreString(t *testing.T) { _, tearDown := setupConfigDatabase(t, emptyConfig) defer tearDown() - ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource)) + sqlSettings := mainHelper.GetSqlSettings() + ds, err := config.NewDatabaseStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource)) require.NoError(t, err) defer ds.Close() actualStringURL, err := url.Parse(ds.String()) require.NoError(t, err) - assert.Equal(t, *mainHelper.Settings.DriverName, actualStringURL.Scheme) + assert.Equal(t, *sqlSettings.DriverName, actualStringURL.Scheme) actualUsername := actualStringURL.User.Username() actualPassword, _ := actualStringURL.User.Password() assert.NotEmpty(t, actualUsername) diff --git a/config/main_test.go b/config/main_test.go index 28bbd9f61b..1c1510ed07 100644 --- a/config/main_test.go +++ b/config/main_test.go @@ -12,7 +12,11 @@ import ( var mainHelper *testlib.MainHelper func TestMain(m *testing.M) { - mainHelper = testlib.NewMainHelper() + var options = testlib.HelperOptions{ + EnableStore: true, + } + + mainHelper = testlib.NewMainHelperWithOptions(&options) defer mainHelper.Close() mainHelper.Main(m) @@ -21,10 +25,12 @@ func TestMain(m *testing.M) { // truncateTables clears tables used by the config package for reuse in other tests func truncateTables(t *testing.T) { t.Helper() + sqlSetting := mainHelper.GetSqlSettings() + sqlSupplier := mainHelper.GetSqlSupplier() - switch *mainHelper.Settings.DriverName { + switch *sqlSetting.DriverName { case model.DATABASE_DRIVER_MYSQL: - _, err := mainHelper.SqlSupplier.GetMaster().Db.Exec("TRUNCATE TABLE Configurations") + _, err := sqlSupplier.GetMaster().Db.Exec("TRUNCATE TABLE Configurations") if err != nil { if driverErr, ok := err.(*mysql.MySQLError); ok { // Ignore if the Configurations table does not exist. @@ -36,10 +42,10 @@ func truncateTables(t *testing.T) { require.NoError(t, err) case model.DATABASE_DRIVER_POSTGRES: - _, err := mainHelper.SqlSupplier.GetMaster().Db.Exec("TRUNCATE TABLE Configurations") + _, err := sqlSupplier.GetMaster().Db.Exec("TRUNCATE TABLE Configurations") require.NoError(t, err) default: - t.Fatalf("unsupported driver name: %s", *mainHelper.Settings.DriverName) + t.Fatalf("unsupported driver name: %s", *sqlSetting.DriverName) } } diff --git a/config/store_test.go b/config/store_test.go index 9a534480fc..8b46c6f481 100644 --- a/config/store_test.go +++ b/config/store_test.go @@ -9,14 +9,16 @@ import ( ) func TestNewStore(t *testing.T) { + sqlSettings := mainHelper.GetSqlSettings() + t.Run("database dsn", func(t *testing.T) { - ds, err := config.NewStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource), false) + ds, err := config.NewStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource), false) require.NoError(t, err) ds.Close() }) t.Run("database dsn, watch ignored", func(t *testing.T) { - ds, err := config.NewStore(fmt.Sprintf("%s://%s", *mainHelper.Settings.DriverName, *mainHelper.Settings.DataSource), true) + ds, err := config.NewStore(fmt.Sprintf("%s://%s", *sqlSettings.DriverName, *sqlSettings.DataSource), true) require.NoError(t, err) ds.Close() }) diff --git a/migrations/helper_test.go b/migrations/helper_test.go index a782fbd6cf..574b2a6edc 100644 --- a/migrations/helper_test.go +++ b/migrations/helper_test.go @@ -32,7 +32,8 @@ type TestHelper struct { } func setupTestHelper(enterprise bool) *TestHelper { - mainHelper.Store.DropAllTables() + store := mainHelper.GetStore() + store.DropAllTables() permConfig, err := os.Open(fileutils.FindConfigFile("config.json")) if err != nil { @@ -50,7 +51,7 @@ func setupTestHelper(enterprise bool) *TestHelper { } options := []app.Option{app.Config(tempConfig.Name(), false)} - options = append(options, app.StoreOverride(mainHelper.Store)) + options = append(options, app.StoreOverride(store)) s, err := app.NewServer(options...) if err != nil { @@ -268,13 +269,14 @@ func (me *TestHelper) TearDown() { } func (me *TestHelper) ResetRoleMigration() { - if _, err := mainHelper.SqlSupplier.GetMaster().Exec("DELETE from Roles"); err != nil { + sqlSupplier := mainHelper.GetSqlSupplier() + if _, err := sqlSupplier.GetMaster().Exec("DELETE from Roles"); err != nil { panic(err) } - mainHelper.ClusterInterface.SendClearRoleCacheMessage() + mainHelper.GetClusterInterface().SendClearRoleCacheMessage() - if _, err := mainHelper.SqlSupplier.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": app.ADVANCED_PERMISSIONS_MIGRATION_KEY}); err != nil { + if _, err := sqlSupplier.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": app.ADVANCED_PERMISSIONS_MIGRATION_KEY}); err != nil { panic(err) } } diff --git a/migrations/main_test.go b/migrations/main_test.go index 89d1c31072..7cf8da0bce 100644 --- a/migrations/main_test.go +++ b/migrations/main_test.go @@ -12,7 +12,12 @@ import ( var mainHelper *testlib.MainHelper func TestMain(m *testing.M) { - mainHelper = testlib.NewMainHelper() + var options = testlib.HelperOptions{ + EnableStore: true, + EnableResources: true, + } + + mainHelper = testlib.NewMainHelperWithOptions(&options) defer mainHelper.Close() mainHelper.Main(m) diff --git a/mlog/main_test.go b/mlog/main_test.go new file mode 100644 index 0000000000..f8175de560 --- /dev/null +++ b/mlog/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package mlog_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/model/main_test.go b/model/main_test.go new file mode 100644 index 0000000000..d42ebf9cc6 --- /dev/null +++ b/model/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/plugin/main_test.go b/plugin/main_test.go new file mode 100644 index 0000000000..41018d6f2d --- /dev/null +++ b/plugin/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package plugin_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/services/filesstore/main_test.go b/services/filesstore/main_test.go new file mode 100644 index 0000000000..fc01b7513f --- /dev/null +++ b/services/filesstore/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package filesstore_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/services/httpservice/main_test.go b/services/httpservice/main_test.go new file mode 100644 index 0000000000..13dbf83a40 --- /dev/null +++ b/services/httpservice/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package httpservice_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/services/imageproxy/main_test.go b/services/imageproxy/main_test.go new file mode 100644 index 0000000000..343f9026c2 --- /dev/null +++ b/services/imageproxy/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package imageproxy_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/services/mailservice/main_test.go b/services/mailservice/main_test.go new file mode 100644 index 0000000000..900e97dce8 --- /dev/null +++ b/services/mailservice/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package mailservice_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/services/mfa/main_test.go b/services/mfa/main_test.go new file mode 100644 index 0000000000..40e2c9e542 --- /dev/null +++ b/services/mfa/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package mfa_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/store/sqlstore/init_test.go b/store/sqlstore/init_test.go new file mode 100644 index 0000000000..fe85e3b298 --- /dev/null +++ b/store/sqlstore/init_test.go @@ -0,0 +1,9 @@ +package sqlstore + +func InitTest() { + initStores() +} + +func TearDownTest() { + tearDownStores() +} diff --git a/store/sqlstore/main_test.go b/store/sqlstore/main_test.go new file mode 100644 index 0000000000..57fb09694d --- /dev/null +++ b/store/sqlstore/main_test.go @@ -0,0 +1,23 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package sqlstore_test + +import ( + "github.com/mattermost/mattermost-server/store/sqlstore" + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + sqlstore.InitTest() + + mainHelper.Main(m) + sqlstore.TearDownTest() +} diff --git a/store/sqlstore/store_test.go b/store/sqlstore/store_test.go index 9ac04bf2e7..850c7b740f 100644 --- a/store/sqlstore/store_test.go +++ b/store/sqlstore/store_test.go @@ -4,15 +4,12 @@ package sqlstore import ( - "os" "sync" "testing" - "github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store/storetest" - "github.com/mattermost/mattermost-server/utils" ) type storeType struct { @@ -99,26 +96,3 @@ func tearDownStores() { wg.Wait() }) } - -func TestMain(m *testing.M) { - // Setup a global logger to catch tests logging outside of app context - // The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen. - mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{ - EnableConsole: true, - ConsoleJson: true, - ConsoleLevel: "error", - EnableFile: false, - })) - - utils.TranslationsPreInit() - - status := 0 - - initStores() - defer func() { - tearDownStores() - os.Exit(status) - }() - - status = m.Run() -} diff --git a/testlib/helper.go b/testlib/helper.go index 760cca058c..4a0036678e 100644 --- a/testlib/helper.go +++ b/testlib/helper.go @@ -5,6 +5,7 @@ package testlib import ( "flag" + "fmt" "os" "testing" @@ -22,10 +23,24 @@ type MainHelper struct { SqlSupplier *sqlstore.SqlSupplier ClusterInterface *FakeClusterInterface - status int + status int + testResourcePath string +} + +type HelperOptions struct { + EnableStore bool + EnableResources bool } func NewMainHelper() *MainHelper { + return NewMainHelperWithOptions(&HelperOptions{ + EnableStore: true, + EnableResources: true, + }) +} + +func NewMainHelperWithOptions(options *HelperOptions) *MainHelper { + var mainHelper MainHelper flag.Parse() // Setup a global logger to catch tests logging outside of app context @@ -40,35 +55,106 @@ func NewMainHelper() *MainHelper { utils.TranslationsPreInit() + if options != nil { + if options.EnableStore { + mainHelper.setupStore() + } + + if options.EnableResources { + mainHelper.setupResources() + } + } + + return &mainHelper +} + +func (h *MainHelper) Main(m *testing.M) { + if h.testResourcePath != "" { + prevDir, err := os.Getwd() + if err != nil { + panic("Failed to get current working directory: " + err.Error()) + } + + err = os.Chdir(h.testResourcePath) + if err != nil { + panic(fmt.Sprintf("Failed to set current working directory to %s: %s", h.testResourcePath, err.Error())) + } + + defer func() { + err := os.Chdir(prevDir) + if err != nil { + panic(fmt.Sprintf("Failed to restore current working directory to %s: %s", prevDir, err.Error())) + } + }() + } + + h.status = m.Run() +} + +func (h *MainHelper) setupStore() { driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME") if driverName == "" { driverName = model.DATABASE_DRIVER_MYSQL } - settings := storetest.MakeSqlSettings(driverName) + h.Settings = storetest.MakeSqlSettings(driverName) - clusterInterface := &FakeClusterInterface{} - sqlSupplier := sqlstore.NewSqlSupplier(*settings, nil) - testStore := &TestStore{ - store.NewLayeredStore(sqlSupplier, nil, clusterInterface), - } - - return &MainHelper{ - Settings: settings, - Store: testStore, - SqlSupplier: sqlSupplier, - ClusterInterface: clusterInterface, + h.ClusterInterface = &FakeClusterInterface{} + h.SqlSupplier = sqlstore.NewSqlSupplier(*h.Settings, nil) + h.Store = &TestStore{ + store.NewLayeredStore(h.SqlSupplier, nil, h.ClusterInterface), } } -func (h *MainHelper) Main(m *testing.M) { - h.status = m.Run() +func (h *MainHelper) setupResources() { + var err error + h.testResourcePath, err = SetupTestResources() + if err != nil { + panic("failed to setup test resources: " + err.Error()) + } } func (h *MainHelper) Close() error { - storetest.CleanupSqlSettings(h.Settings) + if h.Settings != nil { + storetest.CleanupSqlSettings(h.Settings) + } + if h.testResourcePath != "" { + os.RemoveAll(h.testResourcePath) + } os.Exit(h.status) return nil } + +func (h *MainHelper) GetSqlSettings() *model.SqlSettings { + if h.Settings == nil { + panic("MainHelper not initialized with database access.") + } + + return h.Settings +} + +func (h *MainHelper) GetStore() store.Store { + if h.Store == nil { + panic("MainHelper not initialized with store.") + } + + return h.Store +} + +func (h *MainHelper) GetSqlSupplier() *sqlstore.SqlSupplier { + if h.SqlSupplier == nil { + panic("MainHelper not initialized with sql supplier.") + } + + return h.SqlSupplier +} + +func (h *MainHelper) GetClusterInterface() *FakeClusterInterface { + if h.ClusterInterface == nil { + panic("MainHelper not initialized with sql supplier.") + } + + return h.ClusterInterface +} diff --git a/testlib/resources.go b/testlib/resources.go new file mode 100644 index 0000000000..4258d380d2 --- /dev/null +++ b/testlib/resources.go @@ -0,0 +1,158 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package testlib + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + + "github.com/mattermost/mattermost-server/model" + "github.com/pkg/errors" + + "github.com/mattermost/mattermost-server/utils" + "github.com/mattermost/mattermost-server/utils/fileutils" +) + +const ( + resourceTypeFile = iota + resourceTypeFolder +) + +const ( + actionCopy = iota + actionSymlink +) + +type testResourceDetails struct { + src string + dest string + resType int8 + action int8 +} + +func getTestResourcesToSetup() []testResourceDetails { + var srcPath string + var found bool + + var testResourcesToSetup = []testResourceDetails{ + {"config/timezones.json", "config/timezones.json", resourceTypeFile, actionCopy}, + {"i18n", "i18n", resourceTypeFolder, actionSymlink}, + {"templates", "templates", resourceTypeFolder, actionSymlink}, + {"tests", "tests", resourceTypeFolder, actionSymlink}, + {"fonts", "fonts", resourceTypeFolder, actionSymlink}, + {"utils/policies-roles-mapping.json", "utils/policies-roles-mapping.json", resourceTypeFile, actionSymlink}, + } + + // Finding resources and setting full path to source to be used for further processing + for i, testResource := range testResourcesToSetup { + if testResource.resType == resourceTypeFile { + srcPath = fileutils.FindFile(testResource.src) + if srcPath == "" { + panic(fmt.Sprintf("Failed to find file %s", testResource.src)) + } + + testResourcesToSetup[i].src = srcPath + } else if testResource.resType == resourceTypeFolder { + srcPath, found = fileutils.FindDir(testResource.src) + if found == false { + panic(fmt.Sprintf("Failed to find folder %s", testResource.src)) + } + + testResourcesToSetup[i].src = srcPath + } else { + panic(fmt.Sprintf("Invalid resource type: %d", testResource.resType)) + } + } + + return testResourcesToSetup +} + +func SetupTestResources() (string, error) { + testResourcesToSetup := getTestResourcesToSetup() + + tempDir, err := ioutil.TempDir("", "testlib") + if err != nil { + return "", errors.Wrap(err, "failed to create temporary directory") + } + + pluginsDir := path.Join(tempDir, "plugins") + err = os.Mkdir(pluginsDir, 0700) + if err != nil { + return "", errors.Wrapf(err, "failed to create plugins directory %s", pluginsDir) + } + + err = setupConfig(path.Join(tempDir, "config")) + if err != nil { + return "", errors.Wrap(err, "failed to setup config") + } + + var resourceDestInTemp string + + // Setting up test resources in temp. + // Action in each resource tells whether it needs to be copied or just symlinked + for _, testResource := range testResourcesToSetup { + resourceDestInTemp = filepath.Join(tempDir, testResource.dest) + + if testResource.action == actionCopy { + if testResource.resType == resourceTypeFile { + err = utils.CopyFile(testResource.src, resourceDestInTemp) + if err != nil { + return "", errors.Wrapf(err, "failed to copy file %s to %s", testResource.src, resourceDestInTemp) + } + } else if testResource.resType == resourceTypeFolder { + err = utils.CopyDir(testResource.src, resourceDestInTemp) + if err != nil { + return "", errors.Wrapf(err, "failed to copy folder %s to %s", testResource.src, resourceDestInTemp) + } + } + } else if testResource.action == actionSymlink { + destDir := path.Dir(resourceDestInTemp) + if destDir != "." { + err = os.MkdirAll(destDir, os.ModePerm) + if err != nil { + return "", errors.Wrapf(err, "failed to make dir %s", destDir) + } + } + + err = os.Symlink(testResource.src, resourceDestInTemp) + if err != nil { + return "", errors.Wrapf(err, "failed to symlink %s to %s", testResource.src, resourceDestInTemp) + } + } else { + return "", errors.Wrapf(err, "Invalid action: %d", testResource.action) + } + + } + + return tempDir, nil +} + +func setupConfig(configDir string) error { + var err error + var config model.Config + + config.SetDefaults() + + err = os.Mkdir(configDir, 0700) + if err != nil { + return errors.Wrapf(err, "failed to create config directory %s", configDir) + } + + defaultJson := path.Join(configDir, "default.json") + err = ioutil.WriteFile(defaultJson, []byte(config.ToJson()), 0644) + if err != nil { + return errors.Wrapf(err, "failed to write config to %s", defaultJson) + } + + configJson := path.Join(configDir, "config.json") + err = utils.CopyFile(defaultJson, configJson) + if err != nil { + return errors.Wrapf(err, "failed to copy file %s to %s", defaultJson, configJson) + } + + return nil +} diff --git a/utils/fileutils/main_test.go b/utils/fileutils/main_test.go new file mode 100644 index 0000000000..e8bc05ab9d --- /dev/null +++ b/utils/fileutils/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package fileutils_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/utils/jsonutils/main_test.go b/utils/jsonutils/main_test.go new file mode 100644 index 0000000000..f518ecf9ac --- /dev/null +++ b/utils/jsonutils/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package jsonutils_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/utils/main_test.go b/utils/main_test.go new file mode 100644 index 0000000000..c1f9c14847 --- /dev/null +++ b/utils/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/utils/markdown/main_test.go b/utils/markdown/main_test.go new file mode 100644 index 0000000000..fb276d87d8 --- /dev/null +++ b/utils/markdown/main_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package markdown_test + +import ( + "testing" + + "github.com/mattermost/mattermost-server/testlib" +) + +var mainHelper *testlib.MainHelper + +func TestMain(m *testing.M) { + mainHelper = testlib.NewMainHelperWithOptions(nil) + defer mainHelper.Close() + + mainHelper.Main(m) +} diff --git a/web/main_test.go b/web/main_test.go index 49b9f82a9a..1898ce40ea 100644 --- a/web/main_test.go +++ b/web/main_test.go @@ -12,7 +12,12 @@ import ( var mainHelper *testlib.MainHelper func TestMain(m *testing.M) { - mainHelper = testlib.NewMainHelper() + var options = testlib.HelperOptions{ + EnableStore: true, + EnableResources: true, + } + + mainHelper = testlib.NewMainHelperWithOptions(&options) defer mainHelper.Close() mainHelper.Main(m) diff --git a/web/web_test.go b/web/web_test.go index 9e5005ef47..c1a4c3538f 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -30,7 +30,8 @@ type TestHelper struct { } func Setup() *TestHelper { - mainHelper.Store.DropAllTables() + store := mainHelper.GetStore() + store.DropAllTables() permConfig, err := os.Open(fileutils.FindConfigFile("config.json")) if err != nil { @@ -48,7 +49,7 @@ func Setup() *TestHelper { } options := []app.Option{app.Config(tempConfig.Name(), false)} - options = append(options, app.StoreOverride(mainHelper.Store)) + options = append(options, app.StoreOverride(store)) s, err := app.NewServer(options...) if err != nil {