snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
264
server/channels/app/platform/service_test.go
Normal file
264
server/channels/app/platform/service_test.go
Normal file
@@ -0,0 +1,264 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package platform
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/markdown"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
|
||||
"github.com/mattermost/mattermost/server/v8/config"
|
||||
"github.com/mattermost/mattermost/server/v8/einterfaces/mocks"
|
||||
)
|
||||
|
||||
// A copy of validTestLicense from channels/utils/license_test.go
|
||||
var validTestLicense = []byte("eyJpZCI6InpvZ3c2NW44Z2lmajVkbHJoYThtYnUxcGl3IiwiaXNzdWVkX2F0IjoxNjg0Nzg3MzcxODY5LCJzdGFydHNfYXQiOjE2ODQ3ODczNzE4NjksImV4cGlyZXNfYXQiOjIwMDA0MDY1MzgwMDAsInNrdV9uYW1lIjoiUHJvZmVzc2lvbmFsIiwic2t1X3Nob3J0X25hbWUiOiJwcm9mZXNzaW9uYWwiLCJjdXN0b21lciI6eyJpZCI6InA5dW4zNjlhNjdnaW1qNHlkNmk2aWIzOXdoIiwibmFtZSI6Ik1hdHRlcm1vc3QiLCJlbWFpbCI6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c7htHAxrnT/hV42xu+G24OMrAIsQtX4NjFSX6jvehIMRL5II3RPXYhHKUd2wruQ5ITEh1htFb5DgOJW3tvBdMmXt09nXjLRS1UYJ7ZsX3mU0uQndt7qfMriGAkk71veYuUJgztB3MsV7lRWB+8ZTp6WJ7RH+uWnuDspiA8B85mLnyuoCDokYksF2uIb+CtPGBTUB6qSOgxBBJxu5qftQXISCDAWY4O8lCrN3p5HCA/zf/rSRRNtet06QFobbjUDI4B7ZEAescKBKoHpP6nZPhg4KmhnkUi/o04ox")
|
||||
|
||||
func TestSetLicenseOnStart(t *testing.T) {
|
||||
oldValue := model.BuildEnterpriseReady
|
||||
defer func() { model.BuildEnterpriseReady = oldValue }()
|
||||
model.BuildEnterpriseReady = "true"
|
||||
|
||||
cfg := model.Config{}
|
||||
cfg.SetDefaults()
|
||||
|
||||
f, err := os.CreateTemp("", "TestSetLicenseOnStart")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(f.Name())
|
||||
require.NoError(t, os.WriteFile(f.Name(), validTestLicense, 0777))
|
||||
|
||||
*cfg.ServiceSettings.LicenseFileLocation = f.Name()
|
||||
|
||||
driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
|
||||
if driverName == "" {
|
||||
driverName = model.DatabaseDriverPostgres
|
||||
}
|
||||
cfg.SqlSettings = *storetest.MakeSqlSettings(driverName)
|
||||
|
||||
configStore := config.NewTestMemoryStore()
|
||||
_, _, err = configStore.Set(&cfg)
|
||||
require.NoError(t, err)
|
||||
// It should not panic when ps.LoadLicense gets called from platform.New
|
||||
_, err = New(
|
||||
ServiceConfig{},
|
||||
ConfigStore(configStore),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestReadReplicaDisabledBasedOnLicense(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
cfg := model.Config{}
|
||||
cfg.SetDefaults()
|
||||
driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
|
||||
if driverName == "" {
|
||||
driverName = model.DatabaseDriverPostgres
|
||||
}
|
||||
cfg.SqlSettings = *storetest.MakeSqlSettings(driverName)
|
||||
cfg.SqlSettings.DataSourceReplicas = []string{*cfg.SqlSettings.DataSource}
|
||||
cfg.SqlSettings.DataSourceSearchReplicas = []string{*cfg.SqlSettings.DataSource}
|
||||
|
||||
t.Run("Read Replicas with no License", func(t *testing.T) {
|
||||
configStore := config.NewTestMemoryStore()
|
||||
_, _, err := configStore.Set(&cfg)
|
||||
require.NoError(t, err)
|
||||
ps, err := New(
|
||||
ServiceConfig{},
|
||||
ConfigStore(configStore),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Same(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetReplica())
|
||||
require.Len(t, ps.Config().SqlSettings.DataSourceReplicas, 1)
|
||||
})
|
||||
|
||||
t.Run("Read Replicas With License", func(t *testing.T) {
|
||||
configStore := config.NewTestMemoryStore()
|
||||
_, _, err := configStore.Set(&cfg)
|
||||
require.NoError(t, err)
|
||||
ps, err := New(
|
||||
ServiceConfig{},
|
||||
ConfigStore(configStore),
|
||||
func(ps *PlatformService) error {
|
||||
ps.licenseValue.Store(model.NewTestLicense())
|
||||
return nil
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.NotSame(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetReplica())
|
||||
require.Len(t, ps.Config().SqlSettings.DataSourceReplicas, 1)
|
||||
})
|
||||
|
||||
t.Run("Search Replicas with no License", func(t *testing.T) {
|
||||
configStore := config.NewTestMemoryStore()
|
||||
_, _, err := configStore.Set(&cfg)
|
||||
require.NoError(t, err)
|
||||
ps, err := New(
|
||||
ServiceConfig{},
|
||||
ConfigStore(configStore),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Same(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetSearchReplicaX())
|
||||
require.Len(t, ps.Config().SqlSettings.DataSourceSearchReplicas, 1)
|
||||
})
|
||||
|
||||
t.Run("Search Replicas With License", func(t *testing.T) {
|
||||
configStore := config.NewTestMemoryStore()
|
||||
_, _, err := configStore.Set(&cfg)
|
||||
require.NoError(t, err)
|
||||
ps, err := New(
|
||||
ServiceConfig{},
|
||||
ConfigStore(configStore),
|
||||
func(ps *PlatformService) error {
|
||||
ps.licenseValue.Store(model.NewTestLicense())
|
||||
return nil
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.NotSame(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetSearchReplicaX())
|
||||
require.Len(t, ps.Config().SqlSettings.DataSourceSearchReplicas, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMetrics(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
t.Run("ensure the metrics server is not started by default", func(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t)
|
||||
|
||||
require.Nil(t, th.Service.metrics)
|
||||
})
|
||||
|
||||
t.Run("ensure the metrics server is started", func(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t, StartMetrics())
|
||||
|
||||
// there is no config listener for the metrics
|
||||
// we handle it on config save step
|
||||
cfg := th.Service.Config().Clone()
|
||||
cfg.MetricsSettings.Enable = new(true)
|
||||
_, _, appErr := th.Service.SaveConfig(cfg, false)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
require.NotNil(t, th.Service.metrics)
|
||||
metricsAddr := strings.Replace(th.Service.metrics.listenAddr, "[::]", "http://localhost", 1)
|
||||
metricsAddr = strings.Replace(metricsAddr, "127.0.0.1", "http://localhost", 1)
|
||||
|
||||
resp, err := http.Get(metricsAddr)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
cfg.MetricsSettings.Enable = new(false)
|
||||
_, _, appErr = th.Service.SaveConfig(cfg, false)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
_, err = http.Get(metricsAddr)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("ensure the metrics server is started with advanced metrics", func(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t, StartMetrics())
|
||||
|
||||
mockMetricsImpl := &mocks.MetricsInterface{}
|
||||
mockMetricsImpl.On("Register").Return()
|
||||
|
||||
th.Service.metricsIFace = mockMetricsImpl
|
||||
err := th.Service.resetMetrics()
|
||||
require.NoError(t, err)
|
||||
|
||||
mockMetricsImpl.AssertExpectations(t)
|
||||
})
|
||||
|
||||
t.Run("ensure advanced metrics have database metrics", func(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
mockMetricsImpl := &mocks.MetricsInterface{}
|
||||
mockMetricsImpl.On("Register").Return()
|
||||
mockMetricsImpl.On("ObserveStoreMethodDuration", mock.Anything, mock.Anything, mock.Anything).Return()
|
||||
mockMetricsImpl.On("RegisterDBCollector", mock.AnythingOfType("*sql.DB"), "master")
|
||||
|
||||
th := Setup(t, StartMetrics(), func(ps *PlatformService) error {
|
||||
ps.metricsIFace = mockMetricsImpl
|
||||
return nil
|
||||
})
|
||||
|
||||
_ = th.CreateUserOrGuest(t, false)
|
||||
|
||||
th.Shutdown(t)
|
||||
mockMetricsImpl.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
|
||||
func TestShutdown(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
t.Run("should shutdown gracefully", func(t *testing.T) {
|
||||
th := Setup(t)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// we create plenty of go routines to make sure we wait for all of them
|
||||
// to finish before shutting down
|
||||
for range 1000 {
|
||||
th.Service.Go(func() {
|
||||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(20)))
|
||||
})
|
||||
}
|
||||
|
||||
th.Shutdown(t)
|
||||
|
||||
// assert that there are no more go routines running
|
||||
require.Zero(t, atomic.LoadInt32(&th.Service.goroutineCount))
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetTelemetryId(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
t.Run("ensure client config is regenerated after setting the telemetry id", func(t *testing.T) {
|
||||
th := Setup(t)
|
||||
|
||||
clientConfig := th.Service.LimitedClientConfig()
|
||||
require.Empty(t, clientConfig["DiagnosticId"])
|
||||
|
||||
id := model.NewId()
|
||||
th.Service.SetTelemetryId(id)
|
||||
|
||||
clientConfig = th.Service.LimitedClientConfig()
|
||||
require.Equal(t, clientConfig["DiagnosticId"], id)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDatabaseTypeAndMattermostVersion(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t)
|
||||
|
||||
databaseType, schemaVersion, err := th.Service.DatabaseTypeAndSchemaVersion()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "postgres", databaseType)
|
||||
|
||||
// It's hard to check whether the schema version is correct or not.
|
||||
// So, we just check if it's greater than 1.
|
||||
assert.GreaterOrEqual(t, schemaVersion, strconv.Itoa(1))
|
||||
}
|
||||
|
||||
func TestNewSyncsMarkdownMaxLenWithMaxPostSize(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t)
|
||||
|
||||
// markdown.SetMaxPostRunes is package-global; parallel tests (including
|
||||
// markdown's own tests) can overwrite it between Setup and this check.
|
||||
require.Eventually(t, func() bool {
|
||||
maxPostSize := th.Service.MaxPostSize()
|
||||
return markdown.MaxLen() == 4*maxPostSize
|
||||
}, 5*time.Second, 10*time.Millisecond)
|
||||
}
|
||||
Reference in New Issue
Block a user