Migrate to idiomatic error handling in support_package.go (#24382)

Этот коммит содержится в:
Ben Schumacher
2023-09-04 19:36:46 +02:00
коммит произвёл GitHub
родитель dfbe0c14e7
Коммит 01e7e96448
3 изменённых файлов: 212 добавлений и 196 удалений

Просмотреть файл

@@ -18,12 +18,12 @@ 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()
// Happy path where we have a plugins file with no err
fileData, err := th.App.createPluginsFile()
require.NotNil(t, fileData)
assert.Equal(t, "plugins.json", fileData.Filename)
assert.Positive(t, len(fileData.Body))
assert.Empty(t, warning)
assert.NoError(t, err)
// Turn off plugins so we can get an error
th.App.UpdateConfig(func(cfg *model.Config) {
@@ -31,9 +31,9 @@ func TestCreatePluginsFile(t *testing.T) {
})
// Plugins off in settings so no fileData and we get a warning instead
fileData, warning = th.App.createPluginsFile()
fileData, err = th.App.createPluginsFile()
assert.Nil(t, fileData)
assert.Contains(t, warning, "c.App.GetPlugins() Error:")
assert.ErrorContains(t, err, "failed to get plugin list for support package")
}
func TestGenerateSupportPacketYaml(t *testing.T) {
@@ -46,16 +46,17 @@ func TestGenerateSupportPacketYaml(t *testing.T) {
th.App.Srv().SetLicense(license)
// Happy path where we have a support packet yaml file without any warnings
fileData, warning := th.App.generateSupportPacketYaml()
fileData, err := 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)
assert.NoError(t, err)
var packet model.SupportPacket
require.NoError(t, yaml.Unmarshal(fileData.Body, &packet))
assert.Equal(t, 3, packet.ActiveUsers) // from InitBasic.
assert.Equal(t, licenseUsers, packet.LicenseSupportedUsers)
}
func TestGenerateSupportPacket(t *testing.T) {
th := Setup(t)
defer th.TearDown()
@@ -67,12 +68,15 @@ func TestGenerateSupportPacket(t *testing.T) {
require.NoError(t, err)
fileDatas := th.App.GenerateSupportPacket()
var rFileNames []string
testFiles := []string{"support_packet.yaml", "plugins.json", "sanitized_config.json", "mattermost.log", "notifications.log"}
for i, fileData := range fileDatas {
for _, fileData := range fileDatas {
require.NotNil(t, fileData)
assert.Equal(t, testFiles[i], fileData.Filename)
assert.Positive(t, len(fileData.Body))
rFileNames = append(rFileNames, fileData.Filename)
}
assert.ElementsMatch(t, testFiles, rFileNames)
// Remove these two files and ensure that warning.txt file is generated
err = os.Remove("notifications.log")
@@ -81,11 +85,14 @@ func TestGenerateSupportPacket(t *testing.T) {
require.NoError(t, err)
fileDatas = th.App.GenerateSupportPacket()
testFiles = []string{"support_packet.yaml", "plugins.json", "sanitized_config.json", "warning.txt"}
for i, fileData := range fileDatas {
rFileNames = nil
for _, fileData := range fileDatas {
require.NotNil(t, fileData)
assert.Equal(t, testFiles[i], fileData.Filename)
assert.Positive(t, len(fileData.Body))
rFileNames = append(rFileNames, fileData.Filename)
}
assert.ElementsMatch(t, testFiles, rFileNames)
}
func TestGetNotificationsLog(t *testing.T) {
@@ -97,9 +104,9 @@ func TestGetNotificationsLog(t *testing.T) {
*cfg.NotificationLogSettings.EnableFile = false
})
fileData, warning := th.App.getNotificationsLog()
fileData, err := th.App.getNotificationsLog()
assert.Nil(t, fileData)
assert.Equal(t, warning, "Unable to retrieve notifications.log because LogSettings: EnableFile is false in config.json")
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
th.App.UpdateConfig(func(cfg *model.Config) {
@@ -109,21 +116,21 @@ func TestGetNotificationsLog(t *testing.T) {
// If any previous notifications.log file, lets delete it
os.Remove("notifications.log")
fileData, warning = th.App.getNotificationsLog()
fileData, err = th.App.getNotificationsLog()
assert.Nil(t, fileData)
assert.Contains(t, warning, "os.ReadFile(notificationsLog) Error:")
assert.ErrorContains(t, err, "failed read notifcation log file at path")
// Happy path where we have file and no warning
// Happy path where we have file and no error
d1 := []byte("hello\ngo\n")
err := os.WriteFile("notifications.log", d1, 0777)
err = os.WriteFile("notifications.log", d1, 0777)
defer os.Remove("notifications.log")
require.NoError(t, err)
fileData, warning = th.App.getNotificationsLog()
fileData, err = th.App.getNotificationsLog()
require.NotNil(t, fileData)
assert.Equal(t, "notifications.log", fileData.Filename)
assert.Positive(t, len(fileData.Body))
assert.Empty(t, warning)
assert.NoError(t, err)
}
func TestGetMattermostLog(t *testing.T) {
@@ -135,9 +142,9 @@ func TestGetMattermostLog(t *testing.T) {
*cfg.LogSettings.EnableFile = false
})
fileData, warning := th.App.getMattermostLog()
fileData, err := th.App.getMattermostLog()
assert.Nil(t, fileData)
assert.Equal(t, "Unable to retrieve mattermost.log because LogSettings: EnableFile is false in config.json", warning)
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
th.App.UpdateConfig(func(cfg *model.Config) {
@@ -147,31 +154,31 @@ func TestGetMattermostLog(t *testing.T) {
// If any previous mattermost.log file, lets delete it
os.Remove("mattermost.log")
fileData, warning = th.App.getMattermostLog()
fileData, err = th.App.getMattermostLog()
assert.Nil(t, fileData)
assert.Contains(t, warning, "os.ReadFile(mattermostLog) Error:")
assert.ErrorContains(t, err, "failed read mattermost log file at path mattermost.log")
// Happy path where we get a log file and no warning
d1 := []byte("hello\ngo\n")
err := os.WriteFile("mattermost.log", d1, 0777)
err = os.WriteFile("mattermost.log", d1, 0777)
defer os.Remove("mattermost.log")
require.NoError(t, err)
fileData, warning = th.App.getMattermostLog()
fileData, err = th.App.getMattermostLog()
require.NotNil(t, fileData)
assert.Equal(t, "mattermost.log", fileData.Filename)
assert.Positive(t, len(fileData.Body))
assert.Empty(t, warning)
assert.NoError(t, err)
}
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()
// Happy path where we have a sanitized config file with no err
fileData, err := 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)
assert.NoError(t, err)
}