focalboard/server/integrationtests/clienttestlib.go

128 lines
2.7 KiB
Go
Raw Normal View History

package integrationtests
import (
"net/http"
"os"
2021-01-06 21:55:14 +01:00
"time"
2021-01-26 23:13:46 +01:00
"github.com/mattermost/focalboard/server/client"
"github.com/mattermost/focalboard/server/server"
"github.com/mattermost/focalboard/server/services/config"
"github.com/mattermost/focalboard/server/services/mlog"
)
type TestHelper struct {
Server *server.Server
Client *client.Client
}
func getTestConfig() *config.Configuration {
dbType := os.Getenv("FB_STORE_TEST_DB_TYPE")
if dbType == "" {
dbType = "sqlite3"
}
connectionString := os.Getenv("FB_STORE_TEST_CONN_STRING")
if connectionString == "" {
connectionString = ":memory:"
}
logging := `
{
"testing": {
"type": "console",
"options": {
"out": "stdout"
},
"format": "plain",
"format_options": {
"delim": " "
},
"levels": [
{"id": 5, "name": "debug"},
{"id": 4, "name": "info"},
{"id": 3, "name": "warn"},
{"id": 2, "name": "error", "stacktrace": true},
{"id": 1, "name": "fatal", "stacktrace": true},
{"id": 0, "name": "panic", "stacktrace": true}
]
}
}`
return &config.Configuration{
ServerRoot: "http://localhost:8888",
Port: 8888,
DBType: dbType,
DBConfigString: connectionString,
DBTablePrefix: "test_",
WebPath: "./pack",
FilesDriver: "local",
FilesPath: "./files",
2021-06-18 16:01:36 +02:00
LoggingCfgJSON: logging,
}
}
func SetupTestHelper() *TestHelper {
2021-02-10 01:07:41 +01:00
sessionToken := "TESTTOKEN"
th := &TestHelper{}
logger := mlog.NewLogger()
if err := logger.Configure("", getTestConfig().LoggingCfgJSON); err != nil {
panic(err)
}
cfg := getTestConfig()
db, err := server.NewStore(cfg, logger)
if err != nil {
panic(err)
}
srv, err := server.New(cfg, sessionToken, db, logger)
if err != nil {
panic(err)
}
th.Server = srv
2021-02-10 01:07:41 +01:00
th.Client = client.NewClient(srv.Config().ServerRoot, sessionToken)
return th
}
func (th *TestHelper) InitBasic() *TestHelper {
go func() {
2021-01-18 21:59:32 +01:00
if err := th.Server.Start(); err != nil {
panic(err)
}
}()
2021-01-06 21:55:14 +01:00
for {
URL := th.Server.Config().ServerRoot
th.Server.Logger().Info("Polling server", mlog.String("url", URL))
resp, err := http.Get(URL) //nolint:gosec
2021-01-06 21:55:14 +01:00
if err != nil {
th.Server.Logger().Error("Polling failed", mlog.Err(err))
2021-01-06 21:55:14 +01:00
time.Sleep(100 * time.Millisecond)
continue
}
resp.Body.Close()
// Currently returns 404
// if resp.StatusCode != http.StatusOK {
// th.Server.Logger().Error("Not OK", mlog.Int("statusCode", resp.StatusCode))
2021-01-06 21:55:14 +01:00
// continue
// }
// Reached this point: server is up and running!
th.Server.Logger().Info("Server ping OK", mlog.Int("statusCode", resp.StatusCode))
2021-01-06 21:55:14 +01:00
break
}
return th
}
func (th *TestHelper) TearDown() {
defer func() { _ = th.Server.Logger().Shutdown() }()
err := th.Server.Shutdown()
if err != nil {
panic(err)
}
}