[MM-13828] Initialize tests in each package with a new temp folder with all test resources (#10261)

* [MM-13828] Running tests from a new temp folder with all test resources

Possible fix for #10132

All packages which have a TestMain and use testlib.MainHelper will have a new current working directory which will have all the test
resources copied.

Note: default.json is copied as config.json as well to make sure tests don't have any impact due to changes in config by devs

* [MM-13828] Added TestMain to remaining packages to use testlib.MainHelper

This makes sure tests from all packages run with same test resources, setup in a new temp folder for each package

* Updated Jenkins file to not not config/default.json

This makes sure CI has same config files as a dev's machine

* [MM-13828] Changes requested from code review

Added accessor methods to testlib.MainHelper for accessing members
Fixed some broken tests due to change in cwd while tests run
Some other code refactoring and improvements

* [MM-13828] Added new factory method with options for creating test main helper and some code refactoring

testlib.NewMainHelperWithOptions supports options to turn on/off test dependencies and environment setup
Some other code refactoring

* Exporting members of testlib.MainHelper to make enterprise tests work

* Fixed gofmt error

* [MM-13828] removed unwanted dependency on plugins directory while setting up test resources

* [MM-13828] Fixed some tests failing due to them being running from temp folder

* [MM-13828] Some code changes suggested in PR review

* Fixed gofmt error
This commit is contained in:
Sandeep Sukhani
2019-02-19 09:20:11 -05:00
committed by Joram Wilander
parent f4249b5456
commit 29060acb45
33 changed files with 630 additions and 114 deletions
+8 -3
View File
@@ -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)
}
-8
View File
@@ -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
+11 -8
View File
@@ -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)
}
}
+6 -1
View File
@@ -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)
+1 -1
View File
@@ -874,7 +874,7 @@ func TestErrorString(t *testing.T) {
package main
import (
"github.com/pkg/errors"
"errors"
"github.com/mattermost/mattermost-server/plugin"
)
+3 -3
View File
@@ -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()
+1 -1
View File
@@ -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 {
+8 -2
View File
@@ -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)
}
-4
View File
@@ -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"))
}
+35 -25
View File
@@ -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)
+11 -5
View File
@@ -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)
}
}
+4 -2
View File
@@ -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()
})
+7 -5
View File
@@ -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)
}
}
+6 -1
View File
@@ -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)
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+9
View File
@@ -0,0 +1,9 @@
package sqlstore
func InitTest() {
initStores()
}
func TearDownTest() {
tearDownStores()
}
+23
View File
@@ -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()
}
-26
View File
@@ -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()
}
+102 -16
View File
@@ -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
}
+158
View File
@@ -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
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+19
View File
@@ -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)
}
+6 -1
View File
@@ -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)
+3 -2
View File
@@ -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 {