Fix flaky TestGetMattermostLog (#36927)

* Fix flaky TestGetMattermostLog

ReconfigureLogger can create mattermost.log as soon as file logging is
enabled, racing the test's missing-file assertion. Flush and remove any
auto-created log before that check. Apply the same MM-62438 teardown
pattern to the path-validation subtest (disable file target, flush, then
remove temp dirs) and flush after pointing FileLocation outside the root.

Tests-only change. Verified with `go test -run '^TestGetMattermostLog$' -race
-count=100` locally.

Co-authored-by: mattermost-code <matty-code@mattermost.com>

* Fix govet shadow in TestGetMattermostLog

Reuse the existing err variable when removing the auto-created log file
so golangci-lint passes.

Co-authored-by: mattermost-code <matty-code@mattermost.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: mattermost-code <matty-code@mattermost.com>
This commit is contained in:
cursor[bot]
2026-06-11 16:17:36 -04:00
committed by GitHub
co-authored by mattermost-code Cursor Agent
parent ebf41be704
commit 2db07164df
+16 -1
View File
@@ -58,6 +58,14 @@ func TestGetMattermostLog(t *testing.T) {
logLocation := config.GetLogFileLocation(dir)
// ReconfigureLogger may create mattermost.log as soon as file logging is enabled.
// Flush and remove it so the next GetLogFile exercises the missing-file path.
th.Service.Logger().Flush()
err = os.Remove(logLocation)
if err != nil && !os.IsNotExist(err) {
require.NoError(t, err)
}
// There is no mattermost.log file yet, so this fails
fileData, err = th.Service.GetLogFile(th.Context)
assert.Nil(t, fileData)
@@ -80,6 +88,12 @@ func TestGetMattermostLog(t *testing.T) {
outsideDir, err := os.MkdirTemp("", "outside")
require.NoError(t, err)
t.Cleanup(func() {
// MM-62438: stop the file target before removing temp dirs.
th.Service.UpdateConfig(func(cfg *model.Config) {
*cfg.LogSettings.EnableFile = false
*cfg.LogSettings.FileLocation = dir
})
th.Service.Logger().Flush()
err = os.RemoveAll(outsideDir)
require.NoError(t, err)
})
@@ -89,10 +103,11 @@ func TestGetMattermostLog(t *testing.T) {
err = os.WriteFile(outsideLogLocation, []byte("secret data"), 0644)
require.NoError(t, err)
// Set FileLocation to the outside directory (MM_LOG_PATH is still set to 'dir')
// Set FileLocation to the outside directory (log root override is still 'dir')
th.Service.UpdateConfig(func(cfg *model.Config) {
*cfg.LogSettings.FileLocation = outsideDir
})
th.Service.Logger().Flush()
// Should be blocked by path validation
fileData, err = th.Service.GetLogFile(th.Context)