diff --git a/command/agent/log_file.go b/command/agent/log_file.go index 4e0833df9..39ffd43d1 100644 --- a/command/agent/log_file.go +++ b/command/agent/log_file.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strconv" "strings" "sync" @@ -104,6 +105,11 @@ func (l *logFile) pruneFiles() error { if err != nil { return err } + + // Stort the strings as filepath.Glob does not publicly guarantee that files + // are sorted, so here we add an extra defensive sort. + sort.Strings(matches) + // Prune if there are more files stored than the configured max stale := len(matches) - l.MaxFiles for i := 0; i < stale; i++ { diff --git a/command/agent/log_file_test.go b/command/agent/log_file_test.go index dde394276..12777784d 100644 --- a/command/agent/log_file_test.go +++ b/command/agent/log_file_test.go @@ -140,7 +140,7 @@ func TestLogFile_deleteArchives(t *testing.T) { } contents := string(bytes) - require.NotEqual("[INFO] Hellow World", contents, "oldest log should have been deleted") + require.NotEqual("[INFO] Hello World", contents, "oldest log should have been deleted") } }