summaryrefslogtreecommitdiffstats
path: root/api4/apitestlib.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-10-12 12:24:54 -0700
committerChristopher Speller <crspeller@gmail.com>2017-10-12 12:24:54 -0700
commit917e4789c2fde00bcae0f0ccc82b3c3815e1d38a (patch)
tree115270abbda7c7991fbfc419aff465b29fec1f88 /api4/apitestlib.go
parent86a0e16035fa94487c606d925fd856164481a60f (diff)
downloadchat-917e4789c2fde00bcae0f0ccc82b3c3815e1d38a.tar.gz
chat-917e4789c2fde00bcae0f0ccc82b3c3815e1d38a.tar.bz2
chat-917e4789c2fde00bcae0f0ccc82b3c3815e1d38a.zip
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
Diffstat (limited to 'api4/apitestlib.go')
-rw-r--r--api4/apitestlib.go58
1 files changed, 56 insertions, 2 deletions
diff --git a/api4/apitestlib.go b/api4/apitestlib.go
index d1da7bfd8..80a44c13f 100644
--- a/api4/apitestlib.go
+++ b/api4/apitestlib.go
@@ -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)
}