Dockerized build updated tests (#9943)
* testlib: introduce and leverage This doesn't yet factor out the individual test helpers: many packages still rely on `api4` directly to do this, but now wire up the test store setup through this package. `app` and `store`, in particular, don't use `testlib` because of circular dependencies at the moment. * cmd: command_test.go: use api4 testlib * cmd: plugin_test.go: remove dependence on test-config.json * cmd: config_test.go use configured database settings * ensure test-(te|ee) exit with status code * test-server: run all tests, deprecating test-te/test-ee * cmd/mattermost/commands: fix unit tests Instead of relying on (and modifying) a config.json found in the current path, explicitly create a temporary one from defaults for each test. This was likely the source of various bugs over time, but specifically allows us to override the SqlSettings to point at the configured test database for all tests simultaneously. * wrap run/check into a test helper It was insufficient to set a config for each invocation of CheckCommand or RunCommand: some tests relied on the config having changed in a subsequent assertion. Instead, create a new test helper embedding api4.TestHelper. This has the nice advantage of cleaning up all the teardown. * additional TestConfigGet granularity * customized config path to avoid default location * be explicit if the storetest initialization fails * generate safe coverprofile names in the presence of subtests * additional TestConfigShow granularity * fix permission_test.go typo * fix webhook tests * actually flag.Parse() to skip database setup on os.Execed tests * fix recent regression in #9962, not caught by unit tests
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2708ed6d1f
Коммит
d39d9a5caf
@@ -1,52 +0,0 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api4
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/mlog"
|
||||
"github.com/mattermost/mattermost-server/store/storetest"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
flag.Parse()
|
||||
|
||||
// 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()
|
||||
|
||||
// In the case where a dev just wants to run a single test, it's faster to just use the default
|
||||
// store.
|
||||
if filter := flag.Lookup("test.run").Value.String(); filter != "" && filter != "." {
|
||||
mlog.Info("-test.run used, not creating temporary containers")
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
status := 0
|
||||
|
||||
container, settings, err := storetest.NewMySQLContainer()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
UseTestStore(container, settings)
|
||||
|
||||
defer func() {
|
||||
StopTestStore()
|
||||
os.Exit(status)
|
||||
}()
|
||||
|
||||
status = m.Run()
|
||||
}
|
||||
@@ -21,8 +21,6 @@ import (
|
||||
"github.com/mattermost/mattermost-server/mlog"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/mattermost/mattermost-server/store/sqlstore"
|
||||
"github.com/mattermost/mattermost-server/store/storetest"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
"github.com/mattermost/mattermost-server/web"
|
||||
"github.com/mattermost/mattermost-server/wsapi"
|
||||
@@ -52,33 +50,18 @@ type TestHelper struct {
|
||||
tempWorkspace string
|
||||
}
|
||||
|
||||
type persistentTestStore struct {
|
||||
store.Store
|
||||
}
|
||||
// testStore tracks the active test store.
|
||||
// This is a bridge between the new testlib ownership of the test store and the existing usage
|
||||
// of the api4 test helper by many packages. In the future, this test helper would ideally belong
|
||||
// to the testlib altogether.
|
||||
var testStore store.Store
|
||||
|
||||
func (*persistentTestStore) Close() {}
|
||||
|
||||
var testStoreContainer *storetest.RunningContainer
|
||||
var testStore *persistentTestStore
|
||||
|
||||
// UseTestStore sets the container and corresponding settings to use for tests. Once the tests are
|
||||
// complete (e.g. at the end of your TestMain implementation), you should call StopTestStore.
|
||||
func UseTestStore(container *storetest.RunningContainer, settings *model.SqlSettings) {
|
||||
testStoreContainer = container
|
||||
testStore = &persistentTestStore{store.NewLayeredStore(sqlstore.NewSqlSupplier(*settings, nil), nil, nil)}
|
||||
}
|
||||
|
||||
func StopTestStore() {
|
||||
if testStoreContainer != nil {
|
||||
testStoreContainer.Stop()
|
||||
testStoreContainer = nil
|
||||
}
|
||||
func UseTestStore(store store.Store) {
|
||||
testStore = store
|
||||
}
|
||||
|
||||
func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHelper {
|
||||
if testStore != nil {
|
||||
testStore.DropAllTables()
|
||||
}
|
||||
testStore.DropAllTables()
|
||||
|
||||
permConfig, err := os.Open(utils.FindConfigFile("config.json"))
|
||||
if err != nil {
|
||||
@@ -96,9 +79,7 @@ func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHel
|
||||
}
|
||||
|
||||
options := []app.Option{app.ConfigFile(tempConfig.Name()), app.DisableConfigWatch}
|
||||
if testStore != nil {
|
||||
options = append(options, app.StoreOverride(testStore))
|
||||
}
|
||||
options = append(options, app.StoreOverride(testStore))
|
||||
|
||||
s, err := app.NewServer(options...)
|
||||
if err != nil {
|
||||
@@ -117,9 +98,7 @@ func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHel
|
||||
cfg.EmailSettings.SendEmailNotifications = true
|
||||
})
|
||||
prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress
|
||||
if testStore != nil {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
|
||||
}
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
|
||||
if updateConfig != nil {
|
||||
th.App.UpdateConfig(updateConfig)
|
||||
}
|
||||
@@ -205,7 +184,6 @@ func (me *TestHelper) TearDown() {
|
||||
utils.EnableDebugLogForTest()
|
||||
|
||||
if err := recover(); err != nil {
|
||||
StopTestStore()
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
20
api4/main_test.go
Обычный файл
20
api4/main_test.go
Обычный файл
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package api4
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/testlib"
|
||||
)
|
||||
|
||||
var mainHelper *testlib.MainHelper
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
mainHelper = testlib.NewMainHelper()
|
||||
defer mainHelper.Close()
|
||||
UseTestStore(mainHelper.Store)
|
||||
|
||||
mainHelper.Main(m)
|
||||
}
|
||||
@@ -603,7 +603,7 @@ func autocompleteUsers(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
var autocomplete *model.UserAutocomplete
|
||||
var autocomplete model.UserAutocomplete
|
||||
|
||||
if len(channelId) > 0 {
|
||||
// Applying the provided teamId here is useful for DMs and GMs which don't belong
|
||||
|
||||
Ссылка в новой задаче
Block a user