Skip to content

Commit

Permalink
Eliminate MM_SERVER_PATH (mattermost#24968)
Browse files Browse the repository at this point in the history
  • Loading branch information
lieut-data authored May 10, 2024
1 parent daf8448 commit 630bd40
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 91 deletions.
11 changes: 0 additions & 11 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ ifeq ($(BUILD_NUMBER),)
BUILD_NUMBER := dev
endif

# Ensure developer invocation and tests are anchored.
MM_SERVER_PATH ?= $(ROOT)

# Go test sum configuration
GOTESTSUM_FORMAT ?= testname
GOTESTSUM_JUNITFILE ?= report.xml
Expand Down Expand Up @@ -425,7 +422,6 @@ else
@echo Running only TE tests
endif

test-server-race: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-server-race: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-server-race: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-server-race: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
Expand All @@ -444,7 +440,6 @@ ifneq ($(IS_CI),true)
endif
endif

test-server: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-server: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-server: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-server: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
Expand All @@ -459,15 +454,13 @@ ifneq ($(IS_CI),true)
endif
endif

test-server-ee: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-server-ee: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-server-ee: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-server-ee: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
test-server-ee: check-prereqs-enterprise start-docker gotestsum ## Runs EE tests.
@echo Running only EE tests
$(GOBIN)/gotestsum --packages="$(EE_PACKAGES)" -- $(GOFLAGS) -timeout=20m

test-server-quick: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-server-quick: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-server-quick: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-server-quick: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
Expand Down Expand Up @@ -516,31 +509,27 @@ inject-test-data: # add test data to the local instance.
@echo Login with a regular account username=user-1 password=SampleUs@r-1
@echo ========================================================================

test-mmctl-unit: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-mmctl-unit: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-mmctl-unit: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-mmctl-unit: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
test-mmctl-unit: gotestsum
@echo Running mmctl unit tests
$(GOBIN)/gotestsum --packages="$(MMCTL_PACKAGES)" -- -tags 'unit $(MMCTL_BUILD_TAGS)' $(MMCTL_TESTFLAGS)

test-mmctl-e2e: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-mmctl-e2e: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-mmctl-e2e: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-mmctl-e2e: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
test-mmctl-e2e: gotestsum start-docker
@echo Running mmctl e2e tests
$(GOBIN)/gotestsum --packages="$(MMCTL_PACKAGES)" -- -tags 'e2e $(MMCTL_BUILD_TAGS)' $(MMCTL_TESTFLAGS)

test-mmctl: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-mmctl: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-mmctl: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-mmctl: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
test-mmctl: gotestsum start-docker
@echo Running all mmctl tests
$(GOBIN)/gotestsum --packages="$(MMCTL_PACKAGES)" -- -tags 'unit e2e $(MMCTL_BUILD_TAGS)' $(MMCTL_TESTFLAGS)

test-mmctl-coverage: export MM_SERVER_PATH := $(MM_SERVER_PATH)
test-mmctl-coverage: export GOTESTSUM_FORMAT := $(GOTESTSUM_FORMAT)
test-mmctl-coverage: export GOTESTSUM_JUNITFILE := $(GOTESTSUM_JUNITFILE)
test-mmctl-coverage: export GOTESTSUM_JSONFILE := $(GOTESTSUM_JSONFILE)
Expand Down
5 changes: 2 additions & 3 deletions server/channels/api4/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/plugin/utils"
"github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests"
"github.com/mattermost/mattermost/server/v8/channels/testlib"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
)
Expand Down Expand Up @@ -1990,9 +1991,7 @@ func TestPluginWebSocketSession(t *testing.T) {
pluginID := "com.mattermost.websocket_session_test"

// Compile plugin
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_websocket_session", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_websocket_session", "main.go")
pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
require.NotEmpty(t, pluginCode)
Expand Down
32 changes: 10 additions & 22 deletions server/channels/app/plugin_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import (
"github.com/mattermost/mattermost/server/public/plugin/utils"
"github.com/mattermost/mattermost/server/public/shared/i18n"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
"github.com/mattermost/mattermost/server/v8/channels/app/plugin_api_tests"
"github.com/mattermost/mattermost/server/v8/einterfaces/mocks"
"github.com/mattermost/mattermost/server/v8/tests"
)

func getDefaultPluginSettingsSchema() string {
Expand Down Expand Up @@ -802,9 +803,7 @@ func TestPluginAPILoadPluginConfiguration(t *testing.T) {
cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson
})

testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_load_configuration_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_load_configuration_plugin", "main.go")

err = pluginAPIHookTest(t, th, fullPath, "testloadpluginconfig", `{"id": "testloadpluginconfig", "server": {"executable": "backend.exe"}, "settings_schema": {
"settings": [
Expand Down Expand Up @@ -837,9 +836,7 @@ func TestPluginAPILoadPluginConfigurationDefaults(t *testing.T) {
cfg.PluginSettings.Plugins["testloadpluginconfig"] = pluginJson
})

testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_load_configuration_defaults_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_load_configuration_defaults_plugin", "main.go")

err = pluginAPIHookTest(t, th, fullPath, "testloadpluginconfig", `{
"settings": [
Expand Down Expand Up @@ -927,8 +924,7 @@ func TestPluginAPIInstallPlugin(t *testing.T) {
defer th.TearDown()
api := th.SetupPluginAPI()

path, _ := fileutils.FindDir("tests")
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(tests.GetPackagePath(), "testplugin.tar.gz"))
require.NoError(t, err)

_, appErr := api.InstallPlugin(bytes.NewReader(tarData), true)
Expand Down Expand Up @@ -1004,8 +1000,7 @@ func TestInstallPlugin(t *testing.T) {
defer th.TearDown()

// start an http server to serve plugin's tarball to the test.
path, _ := fileutils.FindDir("tests")
ts := httptest.NewServer(http.FileServer(http.Dir(path)))
ts := httptest.NewServer(http.FileServer(http.Dir(tests.GetPackagePath())))
defer ts.Close()

th.App.UpdateConfig(func(cfg *model.Config) {
Expand Down Expand Up @@ -1203,8 +1198,7 @@ func pluginAPIHookTest(t *testing.T, th *TestHelper, fileName string, id string,

func TestBasicAPIPlugins(t *testing.T) {
defaultSchema := getDefaultPluginSettingsSchema()
testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot read find app folder")
testFolder := plugin_api_tests.GetPackagePath()
dirs, err := os.ReadDir(testFolder)
require.NoError(t, err, "Cannot read test folder %v", testFolder)
for _, dir := range dirs {
Expand Down Expand Up @@ -1789,9 +1783,7 @@ func TestPluginHTTPConnHijack(t *testing.T) {
th := Setup(t)
defer th.TearDown()

testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_http_hijack_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_http_hijack_plugin", "main.go")

pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
Expand Down Expand Up @@ -1824,9 +1816,7 @@ func TestPluginHTTPUpgradeWebSocket(t *testing.T) {
th := Setup(t)
defer th.TearDown()

testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_http_upgrade_websocket_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_http_upgrade_websocket_plugin", "main.go")

pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
Expand Down Expand Up @@ -2373,9 +2363,7 @@ func TestPluginServeMetrics(t *testing.T) {
cfg.MetricsSettings.ListenAddress = prevAddress
})

testFolder, found := fileutils.FindDir("channels/app/plugin_api_tests")
require.True(t, found, "Cannot find tests folder")
fullPath := path.Join(testFolder, "manual.test_serve_metrics_plugin", "main.go")
fullPath := path.Join(plugin_api_tests.GetPackagePath(), "manual.test_serve_metrics_plugin", "main.go")

pluginCode, err := os.ReadFile(fullPath)
require.NoError(t, err)
Expand Down
18 changes: 18 additions & 0 deletions server/channels/app/plugin_api_tests/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package plugin_api_tests

import (
"path/filepath"
"runtime"
)

// GetPackagePath returns the filepath to this package for use in tests that need to read data here.
func GetPackagePath() string {
// Find the path to this file
_, filename, _, _ := runtime.Caller(0)

// Return the containing directory
return filepath.Dir(filename)
}
2 changes: 1 addition & 1 deletion server/channels/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func NewServer(options ...Option) (*Server, error) {

templatesDir, ok := templates.GetTemplateDirectory()
if !ok {
return nil, errors.New("Failed find server templates in \"templates\" directory or MM_SERVER_PATH")
return nil, errors.New("Failed find server templates in \"templates\" directory")
}
htmlTemplateWatcher, errorsChan, err2 := templates.NewWithWatcher(templatesDir)
if err2 != nil {
Expand Down
15 changes: 3 additions & 12 deletions server/channels/testlib/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/mattermost/mattermost/server/v8/channels/store/searchlayer"
"github.com/mattermost/mattermost/server/v8/channels/store/sqlstore"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
"github.com/mattermost/mattermost/server/v8/channels/testlib/testdata"
"github.com/mattermost/mattermost/server/v8/channels/utils"
"github.com/mattermost/mattermost/server/v8/platform/services/searchengine"
)
Expand Down Expand Up @@ -195,25 +196,15 @@ func (h *MainHelper) PreloadMigrations() {
var buf []byte
var err error

basePath := os.Getenv("MM_SERVER_PATH")
if basePath == "" {
_, errFile := os.Stat("mattermost-server/server")
if os.IsNotExist(errFile) {
basePath = "mattermost/server"
} else {
basePath = "mattermost-server/server"
}
}
relPath := "channels/testlib/testdata"
switch *h.Settings.DriverName {
case model.DatabaseDriverPostgres:
finalPath := filepath.Join(basePath, relPath, "postgres_migration_warmup.sql")
finalPath := filepath.Join(testdata.GetPackagePath(), "postgres_migration_warmup.sql")
buf, err = os.ReadFile(finalPath)
if err != nil {
panic(fmt.Errorf("cannot read file: %v", err))
}
case model.DatabaseDriverMysql:
finalPath := filepath.Join(basePath, relPath, "mysql_migration_warmup.sql")
finalPath := filepath.Join(testdata.GetPackagePath(), "mysql_migration_warmup.sql")
buf, err = os.ReadFile(finalPath)
if err != nil {
panic(fmt.Errorf("cannot read file: %v", err))
Expand Down
13 changes: 11 additions & 2 deletions server/channels/testlib/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/utils"
"github.com/mattermost/mattermost/server/v8/channels/testlib/testdata"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
"github.com/mattermost/mattermost/server/v8/platform/shared/filestore"
)
Expand All @@ -38,7 +39,11 @@ type testResourceDetails struct {
}

func findFile(path string) string {
return fileutils.FindPath(path, fileutils.CommonBaseSearchPaths(), func(fileInfo os.FileInfo) bool {
// Use the testdata path to search from the root of the monorepo.
searchPaths := fileutils.CommonBaseSearchPaths()
searchPaths = append(searchPaths, filepath.Join(testdata.GetPackagePath(), "../../../"))

return fileutils.FindPath(path, searchPaths, func(fileInfo os.FileInfo) bool {
return !fileInfo.IsDir()
})
}
Expand All @@ -53,7 +58,11 @@ func findDir(dir string) (string, bool) {
return path.Dir(srcPath), true
}

found := fileutils.FindPath(dir, fileutils.CommonBaseSearchPaths(), func(fileInfo os.FileInfo) bool {
// Use the testdata path to search from the root of the monorepo.
searchPaths := fileutils.CommonBaseSearchPaths()
searchPaths = append(searchPaths, filepath.Join(testdata.GetPackagePath(), "../../../"))

found := fileutils.FindPath(dir, searchPaths, func(fileInfo os.FileInfo) bool {
return fileInfo.IsDir()
})
if found == "" {
Expand Down
18 changes: 18 additions & 0 deletions server/channels/testlib/testdata/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package testdata

import (
"path/filepath"
"runtime"
)

// GetPackagePath returns the filepath to this package for in tests that need to read data here.
func GetPackagePath() string {
// Find the path to this file
_, filename, _, _ := runtime.Caller(0)

// Return the containing directory
return filepath.Dir(filename)
}
5 changes: 0 additions & 5 deletions server/channels/utils/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ func CommonBaseSearchPaths() []string {
"../../../..",
}

// this enables the server to be used in tests from a different repository
if mmPath := os.Getenv("MM_SERVER_PATH"); mmPath != "" {
paths = append(paths, mmPath)
}

return paths
}

Expand Down
5 changes: 0 additions & 5 deletions server/channels/utils/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package utils

import (
"fmt"
"os"
"path/filepath"

"github.com/mattermost/mattermost/server/public/shared/i18n"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
Expand All @@ -16,9 +14,6 @@ import (
// loaded already and assigns english while loading server config.
func TranslationsPreInit() error {
translationsDir := "i18n"
if mattermostPath := os.Getenv("MM_SERVER_PATH"); mattermostPath != "" {
translationsDir = filepath.Join(mattermostPath, "i18n")
}

i18nDirectory, found := fileutils.FindDirRelBinary(translationsDir)
if !found {
Expand Down
10 changes: 4 additions & 6 deletions server/cmd/mmctl/commands/export_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
"github.com/mattermost/mattermost/server/v8/tests"

"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/utils"
Expand All @@ -19,9 +20,8 @@ import (

func (s *MmctlE2ETestSuite) TestExportListCmdF() {
s.SetupTestHelper()
serverPath := os.Getenv("MM_SERVER_PATH")
importName := "import_test.zip"
importFilePath := filepath.Join(serverPath, "tests", importName)
importFilePath := filepath.Join(tests.GetPackagePath(), importName)
exportPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory,
*s.th.App.Config().ExportSettings.Directory))
s.Require().Nil(err)
Expand Down Expand Up @@ -72,9 +72,8 @@ func (s *MmctlE2ETestSuite) TestExportListCmdF() {

func (s *MmctlE2ETestSuite) TestExportDeleteCmdF() {
s.SetupTestHelper()
serverPath := os.Getenv("MM_SERVER_PATH")
importName := "import_test.zip"
importFilePath := filepath.Join(serverPath, "tests", importName)
importFilePath := filepath.Join(tests.GetPackagePath(), importName)
exportPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory,
*s.th.App.Config().ExportSettings.Directory))
s.Require().Nil(err)
Expand Down Expand Up @@ -179,9 +178,8 @@ func (s *MmctlE2ETestSuite) TestExportCreateCmdF() {

func (s *MmctlE2ETestSuite) TestExportDownloadCmdF() {
s.SetupTestHelper()
serverPath := os.Getenv("MM_SERVER_PATH")
importName := "import_test.zip"
importFilePath := filepath.Join(serverPath, "tests", importName)
importFilePath := filepath.Join(tests.GetPackagePath(), importName)
exportPath, err := filepath.Abs(filepath.Join(*s.th.App.Config().FileSettings.Directory,
*s.th.App.Config().ExportSettings.Directory))
s.Require().Nil(err)
Expand Down
4 changes: 2 additions & 2 deletions server/cmd/mmctl/commands/extract_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (

"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
"github.com/mattermost/mattermost/server/v8/tests"

"github.com/mattermost/mattermost/server/public/model"
"github.com/spf13/cobra"
)

func (s *MmctlE2ETestSuite) TestExtractRunCmdF() {
s.SetupTestHelper().InitBasic()
serverPath := os.Getenv("MM_SERVER_PATH")
docName := "sample-doc.pdf"
docFilePath := filepath.Join(serverPath, "tests", docName)
docFilePath := filepath.Join(tests.GetPackagePath(), docName)

s.Run("no permissions", func() {
printer.Clean()
Expand Down
Loading

0 comments on commit 630bd40

Please sign in to comment.