2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2018-08-29 20:56:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2020-11-23 19:57:06 +00:00
|
|
|
"os"
|
2019-07-19 21:36:34 +00:00
|
|
|
"path/filepath"
|
2020-11-23 19:57:06 +00:00
|
|
|
"sort"
|
2018-08-29 20:56:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-23 19:57:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-29 20:56:58 +00:00
|
|
|
|
2020-11-23 19:57:06 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2018-08-29 20:56:58 +00:00
|
|
|
)
|
|
|
|
|
2020-11-23 19:57:06 +00:00
|
|
|
func TestLogFile_Rotation_MaxDuration(t *testing.T) {
|
|
|
|
tempDir := testutil.TempDir(t, "")
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := LogFile{
|
2020-11-23 19:57:06 +00:00
|
|
|
fileName: "consul.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-11-23 19:57:06 +00:00
|
|
|
duration: 50 * time.Millisecond,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
2020-11-23 19:57:06 +00:00
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Hello World"))
|
2020-11-23 19:57:06 +00:00
|
|
|
time.Sleep(3 * logFile.duration)
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Second File"))
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +00:00
|
|
|
logFile := LogFile{
|
|
|
|
fileName: "consul.log",
|
|
|
|
logPath: testutil.TempDir(t, ""),
|
|
|
|
duration: defaultRotateDuration,
|
2019-02-15 22:01:48 +00:00
|
|
|
}
|
2020-11-23 19:57:06 +00:00
|
|
|
err := logFile.openNew()
|
|
|
|
require.NoError(t, err)
|
2019-02-15 22:01:48 +00:00
|
|
|
|
2020-11-23 19:57:06 +00:00
|
|
|
msg := "[INFO] Something"
|
|
|
|
_, err = logFile.Write([]byte(msg))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(logFile.FileInfo.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(content), msg)
|
2019-02-15 22:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +00:00
|
|
|
fileName: "somefile.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +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-11-23 19:57:06 +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-11-23 19:57:06 +00:00
|
|
|
fileName: "consul.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +00:00
|
|
|
|
|
|
|
logFiles := listDir(t, tempDir)
|
|
|
|
sort.Strings(logFiles)
|
|
|
|
require.Len(t, logFiles, 2)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(filepath.Join(tempDir, logFiles[0]))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(content), "Second File")
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(filepath.Join(tempDir, logFiles[1]))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(content), "Third File")
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +00:00
|
|
|
fileName: "somename.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +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-11-23 19:57:06 +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-11-23 19:57:06 +00:00
|
|
|
fileName: "consul.log",
|
2020-01-28 23:50:41 +00:00
|
|
|
logPath: tempDir,
|
2020-11-23 19:57:06 +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-11-23 19:57:06 +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
|
|
|
}
|