2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2018-08-29 20:56:58 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-22 18:53:32 +00:00
|
|
|
"os"
|
2019-07-19 21:36:34 +00:00
|
|
|
"path/filepath"
|
2020-10-22 18:53:32 +00:00
|
|
|
"sort"
|
2018-08-29 20:56:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-29 20:56:58 +00:00
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2018-08-29 20:56:58 +00:00
|
|
|
)
|
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
func TestLogFile_Rotation_MaxDuration(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
tempDir := testutil.TempDir(t, "")
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := LogFile{
|
2020-10-22 18:53:32 +00:00
|
|
|
fileName: "consul.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-10-22 18:53:32 +00:00
|
|
|
duration: 50 * time.Millisecond,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
2020-10-22 18:53:32 +00:00
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Hello World"))
|
2020-10-22 18:53:32 +00:00
|
|
|
time.Sleep(3 * logFile.duration)
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Second File"))
|
2020-10-22 18:53:32 +00:00
|
|
|
require.Len(t, listDir(t, tempDir), 2)
|
2018-08-29 20:56:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 22:01:48 +00:00
|
|
|
func TestLogFile_openNew(t *testing.T) {
|
2020-10-22 18:53:32 +00:00
|
|
|
logFile := LogFile{
|
|
|
|
fileName: "consul.log",
|
|
|
|
logPath: testutil.TempDir(t, ""),
|
|
|
|
duration: defaultRotateDuration,
|
2019-02-15 22:01:48 +00:00
|
|
|
}
|
2020-10-22 18:53:32 +00:00
|
|
|
err := logFile.openNew()
|
|
|
|
require.NoError(t, err)
|
2019-02-15 22:01:48 +00:00
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
msg := "[INFO] Something"
|
|
|
|
_, err = logFile.Write([]byte(msg))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
content, err := os.ReadFile(logFile.FileInfo.Name())
|
2020-10-22 18:53:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(content), msg)
|
2019-02-15 22:01:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:21:09 +00:00
|
|
|
func TestLogFile_renameCurrentFile(t *testing.T) {
|
|
|
|
logFile := LogFile{
|
|
|
|
fileName: "consul.log",
|
|
|
|
logPath: testutil.TempDir(t, ""),
|
|
|
|
duration: defaultRotateDuration,
|
|
|
|
}
|
|
|
|
err := logFile.openNew()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = logFile.renameCurrentFile()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = os.ReadFile(logFile.FileInfo.Name())
|
|
|
|
require.Contains(t, err.Error(), "no such file or directory")
|
|
|
|
}
|
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
func TestLogFile_Rotation_MaxBytes(t *testing.T) {
|
2018-08-29 20:56:58 +00:00
|
|
|
tempDir := testutil.TempDir(t, "LogWriterBytes")
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := LogFile{
|
2020-10-22 18:53:32 +00:00
|
|
|
fileName: "somefile.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-10-22 18:53:32 +00:00
|
|
|
MaxBytes: 10,
|
|
|
|
duration: defaultRotateDuration,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
logFile.Write([]byte("Second File"))
|
2020-10-22 18:53:32 +00:00
|
|
|
require.Len(t, listDir(t, tempDir), 2)
|
2018-08-29 20:56:58 +00:00
|
|
|
}
|
2019-04-11 16:04:28 +00:00
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
func TestLogFile_PruneFiles(t *testing.T) {
|
|
|
|
tempDir := testutil.TempDir(t, t.Name())
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := LogFile{
|
2020-10-22 18:53:32 +00:00
|
|
|
fileName: "consul.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-10-22 18:53:32 +00:00
|
|
|
MaxBytes: 10,
|
|
|
|
duration: defaultRotateDuration,
|
2020-01-28 23:50:41 +00:00
|
|
|
MaxFiles: 1,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
|
|
|
logFile.Write([]byte("[INFO] Hello World"))
|
|
|
|
logFile.Write([]byte("[INFO] Second File"))
|
|
|
|
logFile.Write([]byte("[INFO] Third File"))
|
2020-10-22 18:53:32 +00:00
|
|
|
|
|
|
|
logFiles := listDir(t, tempDir)
|
|
|
|
sort.Strings(logFiles)
|
|
|
|
require.Len(t, logFiles, 2)
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
content, err := os.ReadFile(filepath.Join(tempDir, logFiles[0]))
|
2020-10-22 18:53:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(content), "Second File")
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
content, err = os.ReadFile(filepath.Join(tempDir, logFiles[1]))
|
2020-10-22 18:53:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(content), "Third File")
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
func TestLogFile_PruneFiles_Disabled(t *testing.T) {
|
2019-12-18 21:31:22 +00:00
|
|
|
tempDir := testutil.TempDir(t, t.Name())
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := LogFile{
|
2020-10-22 18:53:32 +00:00
|
|
|
fileName: "somename.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-10-22 18:53:32 +00:00
|
|
|
MaxBytes: 10,
|
|
|
|
duration: defaultRotateDuration,
|
2020-01-28 23:50:41 +00:00
|
|
|
MaxFiles: 0,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
|
|
|
logFile.Write([]byte("[INFO] Hello World"))
|
|
|
|
logFile.Write([]byte("[INFO] Second File"))
|
|
|
|
logFile.Write([]byte("[INFO] Third File"))
|
2020-10-22 18:53:32 +00:00
|
|
|
require.Len(t, listDir(t, tempDir), 3)
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
2019-12-18 21:31:22 +00:00
|
|
|
|
2020-10-22 18:53:32 +00:00
|
|
|
func TestLogFile_FileRotation_Disabled(t *testing.T) {
|
2019-12-18 21:31:22 +00:00
|
|
|
tempDir := testutil.TempDir(t, t.Name())
|
|
|
|
logFile := LogFile{
|
2020-10-22 18:53:32 +00:00
|
|
|
fileName: "consul.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-10-22 18:53:32 +00:00
|
|
|
MaxBytes: 10,
|
2020-01-28 23:50:41 +00:00
|
|
|
MaxFiles: -1,
|
2019-12-18 21:31:22 +00:00
|
|
|
}
|
|
|
|
logFile.Write([]byte("[INFO] Hello World"))
|
|
|
|
logFile.Write([]byte("[INFO] Second File"))
|
|
|
|
logFile.Write([]byte("[INFO] Third File"))
|
2020-10-22 18:53:32 +00:00
|
|
|
require.Len(t, listDir(t, tempDir), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listDir(t *testing.T, name string) []string {
|
|
|
|
t.Helper()
|
|
|
|
fh, err := os.Open(name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
files, err := fh.Readdirnames(100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return files
|
2019-12-18 21:31:22 +00:00
|
|
|
}
|