[MM-57380] Fix running Support Packet tests locally (#26540)

This commit is contained in:
Ben Schumacher 2024-04-04 15:38:45 +02:00 committed by GitHub
parent 1e0de8f559
commit 6de38842f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,6 +14,7 @@ import (
"github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/app/platform" "github.com/mattermost/mattermost/server/v8/channels/app/platform"
"github.com/mattermost/mattermost/server/v8/config"
fmocks "github.com/mattermost/mattermost/server/v8/platform/shared/filestore/mocks" fmocks "github.com/mattermost/mattermost/server/v8/platform/shared/filestore/mocks"
) )
@ -90,10 +91,24 @@ func TestGenerateSupportPacket(t *testing.T) {
th := Setup(t) th := Setup(t)
defer th.TearDown() defer th.TearDown()
d1 := []byte("hello\ngo\n") dir, err := os.MkdirTemp("", "")
err := os.WriteFile("mattermost.log", d1, 0777)
require.NoError(t, err) require.NoError(t, err)
err = os.WriteFile("notifications.log", d1, 0777) t.Cleanup(func() {
err = os.RemoveAll(dir)
assert.NoError(t, err)
})
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.LogSettings.FileLocation = dir
})
logLocation := config.GetLogFileLocation(dir)
notificationsLogLocation := config.GetNotificationsLogFileLocation(dir)
d1 := []byte("hello\ngo\n")
err = os.WriteFile(logLocation, d1, 0777)
require.NoError(t, err)
err = os.WriteFile(notificationsLogLocation, d1, 0777)
require.NoError(t, err) require.NoError(t, err)
fileDatas := th.App.GenerateSupportPacket(th.Context) fileDatas := th.App.GenerateSupportPacket(th.Context)
@ -117,9 +132,9 @@ func TestGenerateSupportPacket(t *testing.T) {
assert.ElementsMatch(t, testFiles, rFileNames) assert.ElementsMatch(t, testFiles, rFileNames)
// Remove these two files and ensure that warning.txt file is generated // Remove these two files and ensure that warning.txt file is generated
err = os.Remove("notifications.log") err = os.Remove(logLocation)
require.NoError(t, err) require.NoError(t, err)
err = os.Remove("mattermost.log") err = os.Remove(notificationsLogLocation)
require.NoError(t, err) require.NoError(t, err)
fileDatas = th.App.GenerateSupportPacket(th.Context) fileDatas = th.App.GenerateSupportPacket(th.Context)
testFiles = []string{ testFiles = []string{
@ -145,7 +160,7 @@ func TestGetNotificationsLog(t *testing.T) {
th := Setup(t) th := Setup(t)
defer th.TearDown() defer th.TearDown()
// Disable notifications file to get an error // Disable notifications file setting in config so we should get an warning
th.App.UpdateConfig(func(cfg *model.Config) { th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.NotificationLogSettings.EnableFile = false *cfg.NotificationLogSettings.EnableFile = false
}) })
@ -154,29 +169,36 @@ func TestGetNotificationsLog(t *testing.T) {
assert.Nil(t, fileData) assert.Nil(t, fileData)
assert.ErrorContains(t, err, "Unable to retrieve notifications.log because LogSettings: EnableFile is set to false") assert.ErrorContains(t, err, "Unable to retrieve notifications.log because LogSettings: EnableFile is set to false")
// Enable notifications file but delete any notifications file to get an error trying to read the file dir, err := os.MkdirTemp("", "")
th.App.UpdateConfig(func(cfg *model.Config) { require.NoError(t, err)
*cfg.NotificationLogSettings.EnableFile = true t.Cleanup(func() {
err = os.RemoveAll(dir)
assert.NoError(t, err)
}) })
// If any previous notifications.log file, lets delete it // Enable notifications file but point to an empty directory to get an error trying to read the file
os.Remove("notifications.log") th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.NotificationLogSettings.EnableFile = true
*cfg.LogSettings.FileLocation = dir
})
logLocation := config.GetNotificationsLogFileLocation(dir)
// There is no notifications.log file yet, so this fails
fileData, err = th.App.getNotificationsLog(th.Context) fileData, err = th.App.getNotificationsLog(th.Context)
assert.Nil(t, fileData) assert.Nil(t, fileData)
assert.ErrorContains(t, err, "failed read notifcation log file at path") assert.ErrorContains(t, err, "failed read notifcation log file at path "+logLocation)
// Happy path where we have file and no error // Happy path where we have file and no error
d1 := []byte("hello\ngo\n") d1 := []byte("hello\ngo\n")
err = os.WriteFile("notifications.log", d1, 0777) err = os.WriteFile(logLocation, d1, 0777)
defer os.Remove("notifications.log")
require.NoError(t, err) require.NoError(t, err)
fileData, err = th.App.getNotificationsLog(th.Context) fileData, err = th.App.getNotificationsLog(th.Context)
assert.NoError(t, err)
require.NotNil(t, fileData) require.NotNil(t, fileData)
assert.Equal(t, "notifications.log", fileData.Filename) assert.Equal(t, "notifications.log", fileData.Filename)
assert.Positive(t, len(fileData.Body)) assert.Positive(t, len(fileData.Body))
assert.NoError(t, err)
} }
func TestGetMattermostLog(t *testing.T) { func TestGetMattermostLog(t *testing.T) {
@ -192,29 +214,36 @@ func TestGetMattermostLog(t *testing.T) {
assert.Nil(t, fileData) assert.Nil(t, fileData)
assert.ErrorContains(t, err, "Unable to retrieve mattermost.log because LogSettings: EnableFile is set to false") assert.ErrorContains(t, err, "Unable to retrieve mattermost.log because LogSettings: EnableFile is set to false")
// We enable the setting but delete any mattermost log file dir, err := os.MkdirTemp("", "")
th.App.UpdateConfig(func(cfg *model.Config) { require.NoError(t, err)
*cfg.LogSettings.EnableFile = true t.Cleanup(func() {
err = os.RemoveAll(dir)
assert.NoError(t, err)
}) })
// If any previous mattermost.log file, lets delete it // Enable log file but point to an empty directory to get an error trying to read the file
os.Remove("mattermost.log") th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.LogSettings.EnableFile = true
*cfg.LogSettings.FileLocation = dir
})
logLocation := config.GetLogFileLocation(dir)
// There is no mattermost.log file yet, so this fails
fileData, err = th.App.getMattermostLog(th.Context) fileData, err = th.App.getMattermostLog(th.Context)
assert.Nil(t, fileData) assert.Nil(t, fileData)
assert.ErrorContains(t, err, "failed read mattermost log file at path mattermost.log") assert.ErrorContains(t, err, "failed read mattermost log file at path "+logLocation)
// Happy path where we get a log file and no warning // Happy path where we get a log file and no warning
d1 := []byte("hello\ngo\n") d1 := []byte("hello\ngo\n")
err = os.WriteFile("mattermost.log", d1, 0777) err = os.WriteFile(logLocation, d1, 0777)
defer os.Remove("mattermost.log")
require.NoError(t, err) require.NoError(t, err)
fileData, err = th.App.getMattermostLog(th.Context) fileData, err = th.App.getMattermostLog(th.Context)
require.NoError(t, err)
require.NotNil(t, fileData) require.NotNil(t, fileData)
assert.Equal(t, "mattermost.log", fileData.Filename) assert.Equal(t, "mattermost.log", fileData.Filename)
assert.Positive(t, len(fileData.Body)) assert.Positive(t, len(fileData.Body))
assert.NoError(t, err)
} }
func TestCreateSanitizedConfigFile(t *testing.T) { func TestCreateSanitizedConfigFile(t *testing.T) {