2018-08-29 20:56:58 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-04-11 16:04:28 +00:00
|
|
|
"github.com/hashicorp/logutils"
|
2018-08-29 20:56:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testFileName = "Consul.log"
|
|
|
|
testDuration = 2 * time.Second
|
|
|
|
testBytes = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogFile_timeRotation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterTime")
|
|
|
|
defer os.Remove(tempDir)
|
2019-04-11 16:04:28 +00:00
|
|
|
filt := LevelFilter()
|
|
|
|
logFile := LogFile{logFilter: filt, fileName: testFileName, logPath: tempDir, duration: testDuration}
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
logFile.Write([]byte("Second File"))
|
|
|
|
want := 2
|
|
|
|
if got, _ := ioutil.ReadDir(tempDir); len(got) != want {
|
|
|
|
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:01:48 +00:00
|
|
|
func TestLogFile_openNew(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterOpen")
|
|
|
|
defer os.Remove(tempDir)
|
|
|
|
logFile := LogFile{fileName: testFileName, logPath: tempDir, duration: testDuration}
|
|
|
|
if err := logFile.openNew(); err != nil {
|
|
|
|
t.Errorf("Expected open file %s, got an error (%s)", testFileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ioutil.ReadFile(logFile.FileInfo.Name()); err != nil {
|
|
|
|
t.Errorf("Expected readable file %s, got an error (%s)", logFile.FileInfo.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
func TestLogFile_byteRotation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterBytes")
|
|
|
|
defer os.Remove(tempDir)
|
2019-04-11 16:04:28 +00:00
|
|
|
filt := LevelFilter()
|
|
|
|
filt.MinLevel = logutils.LogLevel("INFO")
|
|
|
|
logFile := LogFile{logFilter: filt, fileName: testFileName, logPath: tempDir, MaxBytes: testBytes, duration: 24 * time.Hour}
|
2018-08-29 20:56:58 +00:00
|
|
|
logFile.Write([]byte("Hello World"))
|
|
|
|
logFile.Write([]byte("Second File"))
|
|
|
|
want := 2
|
|
|
|
tempFiles, _ := ioutil.ReadDir(tempDir)
|
|
|
|
if got := tempFiles; len(got) != want {
|
|
|
|
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 16:04:28 +00:00
|
|
|
|
|
|
|
func TestLogFile_logLevelFiltering(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tempDir := testutil.TempDir(t, "LogWriterTime")
|
|
|
|
defer os.Remove(tempDir)
|
|
|
|
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
|
|
|
|
if got, _ := ioutil.ReadDir(tempDir); len(got) != want {
|
|
|
|
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
|
|
|
|
}
|
|
|
|
}
|