mattermost/server/config/migrate_test.go
Jesse Hallam e3fbf8711f
MM-68149: Upgrade to Go 1.26.2 (#36418)
* MM-68149: upgrade to Go 1.26.2

Update go directive in go.mod and .go-version.

* MM-68149: replace pointer helpers with Go 1.26 new()

Go 1.26 extends the built-in new() to accept an initial value expression,
making typed-pointer helpers like model.NewPointer(x), bToP(x), and boolPtr(x)
redundant. Replace every call site with new(x) and remove the now-unused
helper functions and their //go:fix inline directives.

* MM-68149: apply go fix for reflect API and format-string changes

- reflect.Ptr → reflect.Pointer (renamed in Go 1.18, deprecated alias removed in 1.26)
- reflect range-over-struct: for i := 0; i < t.NumField(); i++ → for field := range t.Fields()
  and the equivalent for Methods() and interface types
- Fix format-string concatenation and variadic-arg mismatches flagged by go vet

* MM-68149: update JPEG fixtures and test infrastructure for Go 1.26 encoder

Go 1.26 ships a new image/jpeg encoder that produces slightly different output.
Regenerate all JPEG fixture files and switch the comparison helpers from
byte-equality to pixel-level comparison with a small per-channel tolerance,
so minor encoder drift across patch versions is handled automatically.

Add -update-fixtures flag to make it easy to regenerate fixtures after future
major Go upgrades. Document the update procedure in tests/README.md.

* MM-68149: CI check that go fix ./... produces no changes

* Fix real bugs flagged by CodeRabbit review

- group.go: set newGroup.MemberCount not group.MemberCount (member count
  was populated on the wrong variable and lost before publish/return)
- file_test.go: guard compareImage(GetFilePreview) on the preview slice
  length, not the thumbnail slice length (copy-paste error)
- config_test.go: remove duplicate MinimumLength assignment

* fixup! Fix real bugs flagged by CodeRabbit review
2026-05-12 15:59:12 +00:00

156 lines
4 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package config
import (
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
type cleanUpFn func(store *Store)
func TestMigrate(t *testing.T) {
if testing.Short() {
t.Skip("skipping migration test in short mode")
}
files := []string{
"IdpCertificateFile",
"PublicCertificateFile",
"PrivateKeyFile",
"internal.crt",
"internal2.crt",
}
filesData := make([]string, len(files))
for i := range files {
// Generate random data for each file, ensuring that stale data from a past test
// won't generate a false positive.
filesData[i] = model.NewId()
}
setup := func(t *testing.T) {
os.Clearenv()
t.Helper()
tempDir := t.TempDir()
t.Chdir(tempDir)
truncateTables(t)
}
setupSource := func(t *testing.T, source *Store) cleanUpFn {
t.Helper()
cfg := source.Get()
originalCfg := cfg.Clone()
cfg.ServiceSettings.SiteURL = new("http://example.com")
cfg.SamlSettings.IdpCertificateFile = &files[0]
cfg.SamlSettings.PublicCertificateFile = &files[1]
cfg.SamlSettings.PrivateKeyFile = &files[2]
cfg.PluginSettings.SignaturePublicKeyFiles = []string{
files[3],
files[4],
}
cfg.SqlSettings.DataSourceReplicas = []string{
"postgres://mmuser:password@replicahost:5432/mattermost",
}
cfg.SqlSettings.DataSourceSearchReplicas = []string{
"postgres://mmuser:password@searchreplicahost:5432/mattermost",
}
_, _, err := source.Set(cfg)
require.NoError(t, err)
for i, file := range files {
err = source.SetFile(file, []byte(filesData[i]))
require.NoError(t, err)
}
return func(store *Store) {
_, _, err := store.Set(originalCfg)
require.NoError(t, err)
}
}
assertDestination := func(t *testing.T, destination *Store, source *Store) {
t.Helper()
for i, file := range files {
hasFile, err := destination.HasFile(file)
require.NoError(t, err)
require.Truef(t, hasFile, "destination missing file %s", file)
actualData, err := destination.GetFile(file)
require.NoError(t, err)
assert.Equalf(t, []byte(filesData[i]), actualData, "destination has wrong contents for file %s", file)
}
assert.Equal(t, source.Get(), destination.Get())
}
t.Run("database to file", func(t *testing.T) {
setup(t)
pwd, err := os.Getwd()
require.NoError(t, err)
sqlSettings := mainHelper.GetSQLSettings()
destinationDSN := path.Join(pwd, "config-custom.json")
sourceDSN := getDsn(*sqlSettings.DriverName, *sqlSettings.DataSource)
sourcedb, err := NewDatabaseStore(sourceDSN)
require.NoError(t, err)
source, err := NewStoreFromBacking(sourcedb, nil, false)
require.NoError(t, err)
defer source.Close()
cleanUp := setupSource(t, source)
err = Migrate(sourceDSN, destinationDSN)
require.NoError(t, err)
destinationfile, err := NewFileStore(destinationDSN, false)
require.NoError(t, err)
destination, err := NewStoreFromBacking(destinationfile, nil, false)
require.NoError(t, err)
defer destination.Close()
defer cleanUp(destination)
assertDestination(t, destination, source)
})
t.Run("file to database", func(t *testing.T) {
setup(t)
pwd, err := os.Getwd()
require.NoError(t, err)
sqlSettings := mainHelper.GetSQLSettings()
sourceDSN := path.Join(pwd, "config-custom.json")
destinationDSN := getDsn(*sqlSettings.DriverName, *sqlSettings.DataSource)
sourcefile, err := NewFileStore(sourceDSN, true)
require.NoError(t, err)
source, err := NewStoreFromBacking(sourcefile, nil, false)
require.NoError(t, err)
defer source.Close()
cleanUp := setupSource(t, source)
err = Migrate(sourceDSN, destinationDSN)
require.NoError(t, err)
destinationdb, err := NewDatabaseStore(destinationDSN)
require.NoError(t, err)
destination, err := NewStoreFromBacking(destinationdb, nil, false)
require.NoError(t, err)
defer destination.Close()
defer cleanUp(destination)
assertDestination(t, destination, source)
})
}