Skip to content

Commit

Permalink
client: fix deletion of archive log files
Browse files Browse the repository at this point in the history
  • Loading branch information
h3yduck committed Jul 20, 2021
1 parent 179e88c commit bbaa508
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
IMPROVEMENTS:
* dispatch jobs: Added optional idempotency token to `WriteOptions` which prevents Nomad from creating new dispatched jobs for retried requests. [[GH-10806](https://github.com/hashicorp/nomad/pull/10806)]

BUG FIXES:
* client: Fixed deletion of archive log files based on `log_rotate_max_files`. [[GH-10913](https://github.com/hashicorp/nomad/issues/10913)]

## 1.1.2 (June 22, 2021)

IMPROVEMENTS:
Expand Down
3 changes: 3 additions & 0 deletions command/agent/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func (l *logFile) Write(b []byte) (int, error) {
defer l.acquire.Unlock()
//Create a new file if we have no file to write to
if l.FileInfo == nil {
if err := l.pruneFiles(); err != nil {
return 0, err
}
if err := l.openNew(); err != nil {
return 0, err
}
Expand Down
44 changes: 44 additions & 0 deletions command/agent/log_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,50 @@ func TestLogFile_deleteArchives(t *testing.T) {
}
}

func TestLogFile_deleteArchivesOnStart(t *testing.T) {
t.Parallel()
require := require.New(t)

tempDir, err := ioutil.TempDir("", "LogWriterDeleteArchivesOnStartTest")
require.NoError(err)
defer os.Remove(tempDir)

startNewAgentAndWrite := func(log string) {
filt := LevelFilter()
filt.MinLevel = "INFO"
firstRun := logFile{
logFilter: filt,
fileName: testFileName,
logPath: tempDir,
MaxBytes: 1024,
duration: 24 * time.Hour,
MaxFiles: 1,
}
firstRun.Write([]byte(log))
}

startNewAgentAndWrite("[INFO] Hello World")
startNewAgentAndWrite("[INFO] Second File")
startNewAgentAndWrite("[INFO] Third File")

want := 2
tempFiles, _ := ioutil.ReadDir(tempDir)
require.Equal(want, len(tempFiles))

for _, tempFile := range tempFiles {
var bytes []byte
var err error
path := filepath.Join(tempDir, tempFile.Name())
if bytes, err = ioutil.ReadFile(path); err != nil {
t.Errorf(err.Error())
return
}
contents := string(bytes)

require.NotEqual("[INFO] Hello World", contents, "oldest log should have been deleted")
}
}

func TestLogFile_deleteArchivesDisabled(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit bbaa508

Please sign in to comment.