2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-10-07 12:37:31 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2023-03-08 15:20:04 +00:00
|
|
|
"os"
|
2019-10-07 12:37:31 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/logutils"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-10-07 12:37:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testFileName = "Nomad.log"
|
|
|
|
testDuration = 2 * time.Second
|
|
|
|
testBytes = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogFile_timeRotation(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-10-07 12:37:31 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-10-07 12:37:31 +00:00
|
|
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
logFile := logFile{
|
|
|
|
logFilter: filt,
|
|
|
|
fileName: testFileName,
|
|
|
|
logPath: tempDir,
|
|
|
|
duration: testDuration,
|
|
|
|
}
|
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
logFile.Write([]byte("Second File"))
|
|
|
|
want := 2
|
2023-03-08 15:20:04 +00:00
|
|
|
if got, _ := os.ReadDir(tempDir); len(got) != want {
|
2019-10-07 12:37:31 +00:00
|
|
|
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogFile_openNew(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-10-07 12:37:31 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-10-07 12:37:31 +00:00
|
|
|
|
2021-10-27 00:57:07 +00:00
|
|
|
filt := LevelFilter()
|
|
|
|
filt.MinLevel = logutils.LogLevel("INFO")
|
|
|
|
logFile := logFile{
|
|
|
|
logFilter: filt,
|
|
|
|
fileName: testFileName,
|
|
|
|
logPath: tempDir,
|
|
|
|
MaxBytes: testBytes,
|
|
|
|
duration: 24 * time.Hour,
|
|
|
|
}
|
2019-10-07 12:37:31 +00:00
|
|
|
require.NoError(logFile.openNew())
|
|
|
|
|
2023-03-08 15:20:04 +00:00
|
|
|
_, err := os.ReadFile(logFile.FileInfo.Name())
|
2019-10-07 12:37:31 +00:00
|
|
|
require.NoError(err)
|
2021-08-23 15:27:34 +00:00
|
|
|
|
|
|
|
require.Equal(logFile.FileInfo.Name(), filepath.Join(tempDir, testFileName))
|
2021-10-27 00:57:07 +00:00
|
|
|
|
|
|
|
// Check if create time and bytes written are kept when opening the active
|
|
|
|
// log file again.
|
|
|
|
bytesWritten, err := logFile.Write([]byte("test"))
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
require.NoError(logFile.openNew())
|
|
|
|
|
|
|
|
timeDelta := time.Now().Sub(logFile.LastCreated)
|
|
|
|
require.GreaterOrEqual(timeDelta, 2*time.Second)
|
|
|
|
require.Equal(logFile.BytesWritten, int64(bytesWritten))
|
2019-10-07 12:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogFile_byteRotation(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-10-07 12:37:31 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-10-07 12:37:31 +00:00
|
|
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
filt.MinLevel = logutils.LogLevel("INFO")
|
|
|
|
logFile := logFile{
|
|
|
|
logFilter: filt,
|
|
|
|
fileName: testFileName,
|
|
|
|
logPath: tempDir,
|
|
|
|
MaxBytes: testBytes,
|
|
|
|
duration: 24 * time.Hour,
|
|
|
|
}
|
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
logFile.Write([]byte("Second File"))
|
|
|
|
want := 2
|
2023-03-08 15:20:04 +00:00
|
|
|
tempFiles, _ := os.ReadDir(tempDir)
|
2019-10-07 12:37:31 +00:00
|
|
|
require.Equal(want, len(tempFiles))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogFile_logLevelFiltering(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-10-07 12:37:31 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-10-07 12:37:31 +00:00
|
|
|
filt := LevelFilter()
|
|
|
|
logFile := logFile{
|
|
|
|
logFilter: filt,
|
|
|
|
fileName: testFileName,
|
|
|
|
logPath: tempDir,
|
|
|
|
MaxBytes: testBytes,
|
|
|
|
duration: testDuration,
|
|
|
|
}
|
|
|
|
logFile.Write([]byte("[INFO] This is an info message"))
|
|
|
|
logFile.Write([]byte("[DEBUG] This is a debug message"))
|
|
|
|
logFile.Write([]byte("[ERR] This is an error message"))
|
|
|
|
want := 2
|
2023-03-08 15:20:04 +00:00
|
|
|
tempFiles, _ := os.ReadDir(tempDir)
|
2019-10-07 12:37:31 +00:00
|
|
|
require.Equal(want, len(tempFiles))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogFile_deleteArchives(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-10-07 12:37:31 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-10-07 12:37:31 +00:00
|
|
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
filt.MinLevel = logutils.LogLevel("INFO")
|
|
|
|
logFile := logFile{
|
|
|
|
logFilter: filt,
|
|
|
|
fileName: testFileName,
|
|
|
|
logPath: tempDir,
|
|
|
|
MaxBytes: testBytes,
|
|
|
|
duration: 24 * time.Hour,
|
|
|
|
MaxFiles: 1,
|
|
|
|
}
|
|
|
|
logFile.Write([]byte("[INFO] Hello World"))
|
|
|
|
logFile.Write([]byte("[INFO] Second File"))
|
|
|
|
logFile.Write([]byte("[INFO] Third File"))
|
|
|
|
want := 2
|
2023-03-08 15:20:04 +00:00
|
|
|
tempFiles, _ := os.ReadDir(tempDir)
|
2019-10-07 12:37:31 +00:00
|
|
|
|
|
|
|
require.Equal(want, len(tempFiles))
|
|
|
|
|
|
|
|
for _, tempFile := range tempFiles {
|
|
|
|
var bytes []byte
|
|
|
|
var err error
|
|
|
|
path := filepath.Join(tempDir, tempFile.Name())
|
2023-03-08 15:20:04 +00:00
|
|
|
if bytes, err = os.ReadFile(path); err != nil {
|
2019-10-07 12:37:31 +00:00
|
|
|
t.Errorf(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contents := string(bytes)
|
|
|
|
|
2019-10-10 11:33:08 +00:00
|
|
|
require.NotEqual("[INFO] Hello World", contents, "oldest log should have been deleted")
|
2019-10-07 12:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogFile_deleteArchivesDisabled(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-10-07 12:37:31 +00:00
|
|
|
|
|
|
|
require := require.New(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
tempDir := t.TempDir()
|
2019-10-07 12:37:31 +00:00
|
|
|
|
|
|
|
filt := LevelFilter()
|
|
|
|
filt.MinLevel = logutils.LogLevel("INFO")
|
|
|
|
logFile := logFile{
|
|
|
|
logFilter: filt,
|
|
|
|
fileName: testFileName,
|
|
|
|
logPath: tempDir,
|
|
|
|
MaxBytes: testBytes,
|
|
|
|
duration: 24 * time.Hour,
|
|
|
|
MaxFiles: 0,
|
|
|
|
}
|
|
|
|
logFile.Write([]byte("[INFO] Hello World"))
|
|
|
|
logFile.Write([]byte("[INFO] Second File"))
|
|
|
|
logFile.Write([]byte("[INFO] Third File"))
|
|
|
|
want := 3
|
2023-03-08 15:20:04 +00:00
|
|
|
tempFiles, _ := os.ReadDir(tempDir)
|
2019-10-07 12:37:31 +00:00
|
|
|
require.Equal(want, len(tempFiles))
|
|
|
|
}
|