Browse Source

logging: improve tests

Standardize naming
Use stricter assertions and reduce boilerplate to make the intent of the tests more obvious.

Also explicitly sort the filenames so that the correct files are pruned,
and so that the tests can not flake.
pull/9012/head
Daniel Nephin 4 years ago
parent
commit
752e3ec0b1
  1. 2
      logging/logfile.go
  2. 141
      logging/logfile_test.go

2
logging/logfile.go

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -107,6 +108,7 @@ func (l *LogFile) pruneFiles() error {
return nil return nil
} }
sort.Strings(matches)
last := len(matches) - l.MaxFiles last := len(matches) - l.MaxFiles
return removeFiles(matches[:last]) return removeFiles(matches[:last])
} }

141
logging/logfile_test.go

@ -2,135 +2,122 @@ package logging
import ( import (
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"sort"
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/sdk/testutil" "github.com/stretchr/testify/require"
)
const ( "github.com/hashicorp/consul/sdk/testutil"
testFileName = "Consul.log"
testDuration = 50 * time.Millisecond
testBytes = 10
) )
func TestLogFile_timeRotation(t *testing.T) { func TestLogFile_Rotation_MaxDuration(t *testing.T) {
tempDir := testutil.TempDir(t, "LogWriterTime") tempDir := testutil.TempDir(t, "")
logFile := LogFile{ logFile := LogFile{
fileName: testFileName, fileName: "consul.log",
logPath: tempDir, logPath: tempDir,
duration: testDuration, duration: 50 * time.Millisecond,
} }
logFile.Write([]byte("Hello World")) logFile.Write([]byte("Hello World"))
time.Sleep(3 * testDuration) time.Sleep(3 * logFile.duration)
logFile.Write([]byte("Second File")) logFile.Write([]byte("Second File"))
want := 2 require.Len(t, listDir(t, tempDir), 2)
if got, _ := ioutil.ReadDir(tempDir); len(got) != want {
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
}
} }
func TestLogFile_openNew(t *testing.T) { func TestLogFile_openNew(t *testing.T) {
tempDir := testutil.TempDir(t, "LogWriterOpen") logFile := LogFile{
logFile := LogFile{fileName: testFileName, logPath: tempDir, duration: testDuration} fileName: "consul.log",
if err := logFile.openNew(); err != nil { logPath: testutil.TempDir(t, ""),
t.Errorf("Expected open file %s, got an error (%s)", testFileName, err) duration: defaultRotateDuration,
} }
err := logFile.openNew()
require.NoError(t, err)
if _, err := ioutil.ReadFile(logFile.FileInfo.Name()); err != nil { msg := "[INFO] Something"
t.Errorf("Expected readable file %s, got an error (%s)", logFile.FileInfo.Name(), err) _, 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)
} }
func TestLogFile_byteRotation(t *testing.T) { func TestLogFile_Rotation_MaxBytes(t *testing.T) {
tempDir := testutil.TempDir(t, "LogWriterBytes") tempDir := testutil.TempDir(t, "LogWriterBytes")
logFile := LogFile{ logFile := LogFile{
fileName: testFileName, fileName: "somefile.log",
logPath: tempDir, logPath: tempDir,
MaxBytes: testBytes, MaxBytes: 10,
duration: 24 * time.Hour, duration: defaultRotateDuration,
} }
logFile.Write([]byte("Hello World")) logFile.Write([]byte("Hello World"))
logFile.Write([]byte("Second File")) logFile.Write([]byte("Second File"))
want := 2 require.Len(t, listDir(t, tempDir), 2)
tempFiles, _ := ioutil.ReadDir(tempDir)
if got := tempFiles; len(got) != want {
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
}
} }
func TestLogFile_deleteArchives(t *testing.T) { func TestLogFile_PruneFiles(t *testing.T) {
tempDir := testutil.TempDir(t, "LogWriteDeleteArchives") tempDir := testutil.TempDir(t, t.Name())
logFile := LogFile{ logFile := LogFile{
fileName: testFileName, fileName: "consul.log",
logPath: tempDir, logPath: tempDir,
MaxBytes: testBytes, MaxBytes: 10,
duration: 24 * time.Hour, duration: defaultRotateDuration,
MaxFiles: 1, MaxFiles: 1,
} }
logFile.Write([]byte("[INFO] Hello World")) logFile.Write([]byte("[INFO] Hello World"))
logFile.Write([]byte("[INFO] Second File")) logFile.Write([]byte("[INFO] Second File"))
logFile.Write([]byte("[INFO] Third File")) logFile.Write([]byte("[INFO] Third File"))
want := 2
tempFiles, _ := ioutil.ReadDir(tempDir) logFiles := listDir(t, tempDir)
if got := tempFiles; len(got) != want { sort.Strings(logFiles)
t.Errorf("Expected %d files, got %v file(s)", want, len(got)) require.Len(t, logFiles, 2)
return
} content, err := ioutil.ReadFile(filepath.Join(tempDir, logFiles[0]))
for _, tempFile := range tempFiles { require.NoError(t, err)
var bytes []byte require.Contains(t, string(content), "Second File")
var err error
path := filepath.Join(tempDir, tempFile.Name()) content, err = ioutil.ReadFile(filepath.Join(tempDir, logFiles[1]))
if bytes, err = ioutil.ReadFile(path); err != nil { require.NoError(t, err)
t.Errorf(err.Error()) require.Contains(t, string(content), "Third File")
return
}
contents := string(bytes)
if contents == "[INFO] Hello World" {
t.Errorf("Should have deleted the eldest log file")
return
}
}
} }
func TestLogFile_deleteArchivesDisabled(t *testing.T) { func TestLogFile_PruneFiles_Disabled(t *testing.T) {
tempDir := testutil.TempDir(t, t.Name()) tempDir := testutil.TempDir(t, t.Name())
logFile := LogFile{ logFile := LogFile{
fileName: testFileName, fileName: "somename.log",
logPath: tempDir, logPath: tempDir,
MaxBytes: testBytes, MaxBytes: 10,
duration: 24 * time.Hour, duration: defaultRotateDuration,
MaxFiles: 0, MaxFiles: 0,
} }
logFile.Write([]byte("[INFO] Hello World")) logFile.Write([]byte("[INFO] Hello World"))
logFile.Write([]byte("[INFO] Second File")) logFile.Write([]byte("[INFO] Second File"))
logFile.Write([]byte("[INFO] Third File")) logFile.Write([]byte("[INFO] Third File"))
want := 3 require.Len(t, listDir(t, tempDir), 3)
tempFiles, _ := ioutil.ReadDir(tempDir)
if got := tempFiles; len(got) != want {
t.Errorf("Expected %d files, got %v file(s)", want, len(got))
return
}
} }
func TestLogFile_rotationDisabled(t *testing.T) { func TestLogFile_FileRotation_Disabled(t *testing.T) {
tempDir := testutil.TempDir(t, t.Name()) tempDir := testutil.TempDir(t, t.Name())
logFile := LogFile{ logFile := LogFile{
fileName: testFileName, fileName: "consul.log",
logPath: tempDir, logPath: tempDir,
MaxBytes: testBytes, MaxBytes: 10,
duration: 24 * time.Hour,
MaxFiles: -1, MaxFiles: -1,
} }
logFile.Write([]byte("[INFO] Hello World")) logFile.Write([]byte("[INFO] Hello World"))
logFile.Write([]byte("[INFO] Second File")) logFile.Write([]byte("[INFO] Second File"))
logFile.Write([]byte("[INFO] Third File")) logFile.Write([]byte("[INFO] Third File"))
want := 1 require.Len(t, listDir(t, tempDir), 1)
tempFiles, _ := ioutil.ReadDir(tempDir) }
if got := tempFiles; len(got) != want {
t.Errorf("Expected %d files, got %v file(s)", want, len(got)) func listDir(t *testing.T, name string) []string {
return t.Helper()
} fh, err := os.Open(name)
require.NoError(t, err)
files, err := fh.Readdirnames(100)
require.NoError(t, err)
return files
} }

Loading…
Cancel
Save