Only a handful of critical errors are present in the codebase. They all occur during server startup (in `app.StartServer()`). Currently, when one of these critical error occurs, it is simpled mentionned in the logs – then the error is discarded, and the app attempts to continue the execution (and probably fails pretty quickly in a weird way). Rather than continuing operations in an unknow state, these errors should trigger a clean exit. This commit rewrites critical startup errors to be correctly propagated, logged, and then terminate the command execution. Additionnaly, it makes the server return a proper error code to the shell.
75 строки
1.6 KiB
Go
75 строки
1.6 KiB
Go
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"testing"
|
|
|
|
l4g "github.com/alecthomas/log4go"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
"github.com/mattermost/mattermost-server/store/storetest"
|
|
"github.com/mattermost/mattermost-server/utils"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
flag.Parse()
|
|
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 != "." {
|
|
l4g.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()
|
|
}
|
|
|
|
func TestAppRace(t *testing.T) {
|
|
for i := 0; i < 10; i++ {
|
|
a, err := New()
|
|
require.NoError(t, err)
|
|
a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
|
|
serverErr := a.StartServer()
|
|
require.NoError(t, serverErr)
|
|
a.Shutdown()
|
|
}
|
|
}
|
|
|
|
func TestUpdateConfig(t *testing.T) {
|
|
th := Setup()
|
|
defer th.TearDown()
|
|
|
|
prev := *th.App.Config().ServiceSettings.SiteURL
|
|
|
|
th.App.AddConfigListener(func(old, current *model.Config) {
|
|
assert.Equal(t, prev, *old.ServiceSettings.SiteURL)
|
|
assert.Equal(t, "foo", *current.ServiceSettings.SiteURL)
|
|
})
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.ServiceSettings.SiteURL = "foo"
|
|
})
|
|
}
|