Use tmpfs containers for api/api4 tests, move and speed up CLI tests (#7606)

* use tmpfs containers for api/api4, move and speed up cli tests

* minor optimizations

* add missing files, fix pre-existing race condition

* add . to TestMain check

* add requested log message
Этот коммит содержится в:
Chris
2017-10-12 12:24:54 -07:00
коммит произвёл Christopher Speller
родитель 86a0e16035
Коммит 917e4789c2
10 изменённых файлов: 508 добавлений и 434 удалений

48
api4/api_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,48 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"flag"
"os"
"testing"
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/store/storetest"
"github.com/mattermost/mattermost-server/utils"
)
func TestMain(m *testing.M) {
flag.Parse()
// 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 != "." {
utils.TranslationsPreInit()
utils.LoadConfig("config.json")
l4g.Info("-test.run used, not creating temporary containers")
os.Exit(m.Run())
}
utils.TranslationsPreInit()
utils.LoadConfig("config.json")
utils.InitTranslations(utils.Cfg.LocalizationSettings)
status := 0
container, settings, err := storetest.NewMySQLContainer()
if err != nil {
panic(err)
}
UseTestStore(container, settings)
defer func() {
StopTestStore()
os.Exit(status)
}()
status = m.Run()
}

Просмотреть файл

@@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"os"
"reflect"
@@ -21,6 +22,8 @@ import (
"github.com/mattermost/mattermost-server/app"
"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/wsapi"
@@ -45,13 +48,43 @@ type TestHelper struct {
SystemAdminUser *model.User
}
type persistentTestStore struct {
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 setupTestHelper(enterprise bool) *TestHelper {
utils.TranslationsPreInit()
if utils.T == nil {
utils.TranslationsPreInit()
}
utils.LoadConfig("config.json")
utils.InitTranslations(utils.Cfg.LocalizationSettings)
var options []app.Option
if testStore != nil {
options = append(options, app.StoreOverride(testStore))
}
th := &TestHelper{
App: app.New(),
App: app.New(options...),
}
*utils.Cfg.TeamSettings.MaxUsersPerTeam = 50
@@ -142,10 +175,18 @@ func (me *TestHelper) TearDown() {
me.App.Shutdown()
utils.EnableDebugLogForTest()
if err := recover(); err != nil {
StopTestStore()
panic(err)
}
}
func (me *TestHelper) InitBasic() *TestHelper {
me.waitForConnectivity()
me.TeamAdminUser = me.CreateUser()
me.App.UpdateUserRoles(me.TeamAdminUser.Id, model.ROLE_SYSTEM_USER.Id)
me.LoginTeamAdmin()
me.BasicTeam = me.CreateTeam()
me.BasicChannel = me.CreatePublicChannel()
@@ -169,6 +210,8 @@ func (me *TestHelper) InitBasic() *TestHelper {
}
func (me *TestHelper) InitSystemAdmin() *TestHelper {
me.waitForConnectivity()
me.SystemAdminUser = me.CreateUser()
me.App.UpdateUserRoles(me.SystemAdminUser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_ADMIN.Id)
me.LoginSystemAdmin()
@@ -176,6 +219,17 @@ func (me *TestHelper) InitSystemAdmin() *TestHelper {
return me
}
func (me *TestHelper) waitForConnectivity() {
for i := 0; i < 1000; i++ {
_, err := net.Dial("tcp", "localhost"+*utils.Cfg.ServiceSettings.ListenAddress)
if err == nil {
return
}
time.Sleep(time.Millisecond * 20)
}
panic("unable to connect")
}
func (me *TestHelper) CreateClient() *model.Client4 {
return model.NewAPIv4Client("http://localhost" + *utils.Cfg.ServiceSettings.ListenAddress)
}