[MM-31790] Support Packet Generation BACKEND (#16667)
* init commit * clean up the code * make mocks * fix translations * mocks and lint fixes * add tests * little fixes * Update i18n/en.json Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> * Update i18n/en.json Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> * Update i18n/en.json Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> * Update i18n/en.json Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> * Update i18n/en.json Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> * Address Comments * fix i18n * update api endpoint * add enable file and file level for conditional show of banner * Address Comments * Make it more clear about returns * Create zip file utility function * update en.json * address comments * write tests * check for data in test * remove warning string * Correct expected and actual * set database through environment variables * reset environment variable at end of test Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
Этот коммит содержится в:
@@ -21,6 +21,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/config"
|
||||
@@ -161,6 +162,184 @@ func TestStartServerTLSSuccess(t *testing.T) {
|
||||
require.NoError(t, serverErr)
|
||||
}
|
||||
|
||||
func TestDatabaseTypeAndMattermostVersion(t *testing.T) {
|
||||
sqlDrivernameEnvironment := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
|
||||
defer os.Setenv("MM_SQLSETTINGS_DRIVERNAME", sqlDrivernameEnvironment)
|
||||
|
||||
os.Setenv("MM_SQLSETTINGS_DRIVERNAME", "postgres")
|
||||
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
databaseType, mattermostVersion := th.Server.DatabaseTypeAndMattermostVersion()
|
||||
assert.Equal(t, "postgres", databaseType)
|
||||
assert.Equal(t, "5.31.0", mattermostVersion)
|
||||
|
||||
os.Setenv("MM_SQLSETTINGS_DRIVERNAME", "mysql")
|
||||
|
||||
th2 := Setup(t)
|
||||
defer th2.TearDown()
|
||||
|
||||
databaseType, mattermostVersion = th2.Server.DatabaseTypeAndMattermostVersion()
|
||||
assert.Equal(t, "mysql", databaseType)
|
||||
assert.Equal(t, "5.31.0", mattermostVersion)
|
||||
}
|
||||
|
||||
func TestGenerateSupportPacket(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
d1 := []byte("hello\ngo\n")
|
||||
err := ioutil.WriteFile("mattermost.log", d1, 0777)
|
||||
require.Nil(t, err)
|
||||
err = ioutil.WriteFile("notifications.log", d1, 0777)
|
||||
require.Nil(t, err)
|
||||
|
||||
fileDatas := th.App.GenerateSupportPacket()
|
||||
testFiles := []string{"support_packet.yaml", "plugins.json", "sanitized_config.json", "mattermost.log", "notifications.log"}
|
||||
for i, fileData := range fileDatas {
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, testFiles[i], fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
}
|
||||
|
||||
// Remove these two files and ensure that warning.txt file is generated
|
||||
err = os.Remove("notifications.log")
|
||||
require.Nil(t, err)
|
||||
err = os.Remove("mattermost.log")
|
||||
require.Nil(t, err)
|
||||
fileDatas = th.App.GenerateSupportPacket()
|
||||
testFiles = []string{"support_packet.yaml", "plugins.json", "sanitized_config.json", "warning.txt"}
|
||||
for i, fileData := range fileDatas {
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, testFiles[i], fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNotificationsLog(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
// Disable notifications file to get an error
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.NotificationLogSettings.EnableFile = false
|
||||
})
|
||||
|
||||
fileData, warning := th.App.getNotificationsLog()
|
||||
assert.Nil(t, fileData)
|
||||
assert.Equal(t, warning, "Unable to retrieve notifications.log because LogSettings: EnableFile is false in config.json")
|
||||
|
||||
// Enable notifications file but delete any notifications file to get an error trying to read the file
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.NotificationLogSettings.EnableFile = true
|
||||
})
|
||||
|
||||
// If any previous notifications.log file, lets delete it
|
||||
os.Remove("notifications.log")
|
||||
|
||||
fileData, warning = th.App.getNotificationsLog()
|
||||
assert.Nil(t, fileData)
|
||||
assert.Contains(t, warning, "ioutil.ReadFile(notificationsLog) Error:")
|
||||
|
||||
// Happy path where we have file and no warning
|
||||
d1 := []byte("hello\ngo\n")
|
||||
err := ioutil.WriteFile("notifications.log", d1, 0777)
|
||||
defer os.Remove("notifications.log")
|
||||
require.Nil(t, err)
|
||||
|
||||
fileData, warning = th.App.getNotificationsLog()
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, "notifications.log", fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
assert.Empty(t, warning)
|
||||
}
|
||||
|
||||
func TestGetMattermostLog(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
// disable mattermost log file setting in config so we should get an warning
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.LogSettings.EnableFile = false
|
||||
})
|
||||
|
||||
fileData, warning := th.App.getMattermostLog()
|
||||
assert.Nil(t, fileData)
|
||||
assert.Equal(t, "Unable to retrieve mattermost.log because LogSettings: EnableFile is false in config.json", warning)
|
||||
|
||||
// We enable the setting but delete any mattermost log file
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.LogSettings.EnableFile = true
|
||||
})
|
||||
|
||||
// If any previous mattermost.log file, lets delete it
|
||||
os.Remove("mattermost.log")
|
||||
|
||||
fileData, warning = th.App.getMattermostLog()
|
||||
assert.Nil(t, fileData)
|
||||
assert.Contains(t, warning, "ioutil.ReadFile(mattermostLog) Error:")
|
||||
|
||||
// Happy path where we get a log file and no warning
|
||||
d1 := []byte("hello\ngo\n")
|
||||
err := ioutil.WriteFile("mattermost.log", d1, 0777)
|
||||
defer os.Remove("mattermost.log")
|
||||
require.Nil(t, err)
|
||||
|
||||
fileData, warning = th.App.getMattermostLog()
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, "mattermost.log", fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
assert.Empty(t, warning)
|
||||
}
|
||||
|
||||
func TestCreateSanitizedConfigFile(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
// Happy path where we have a sanitized config file with no warning
|
||||
fileData, warning := th.App.createSanitizedConfigFile()
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, "sanitized_config.json", fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
assert.Empty(t, warning)
|
||||
}
|
||||
|
||||
func TestCreatePluginsFile(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
// Happy path where we have a plugins file with no warning
|
||||
fileData, warning := th.App.createPluginsFile()
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, "plugins.json", fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
assert.Empty(t, warning)
|
||||
|
||||
// Turn off plugins so we can get an error
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.PluginSettings.Enable = false
|
||||
})
|
||||
|
||||
// Plugins off in settings so no fileData and we get a warning instead
|
||||
fileData, warning = th.App.createPluginsFile()
|
||||
assert.Nil(t, fileData)
|
||||
assert.Contains(t, warning, "c.App.GetPlugins() Error:")
|
||||
}
|
||||
|
||||
func TestGenerateSupportPacketYaml(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
// Happy path where we have a support packet yaml file without any warnings
|
||||
fileData, warning := th.App.generateSupportPacketYaml()
|
||||
require.NotNil(t, fileData)
|
||||
assert.Equal(t, "support_packet.yaml", fileData.Filename)
|
||||
assert.Positive(t, len(fileData.Body))
|
||||
assert.Empty(t, warning)
|
||||
|
||||
}
|
||||
|
||||
func TestStartServerTLSVersion(t *testing.T) {
|
||||
s, err := NewServer()
|
||||
require.NoError(t, err)
|
||||
|
||||
Ссылка в новой задаче
Block a user