Skip to content

Commit

Permalink
Add support for log file compression (natefinch#43)
Browse files Browse the repository at this point in the history
* Check test file content, not just length.

It is insufficient to just check the length of test files,
especially given that many of the tests result in multiple files
that have the same content/length. Instead, actually check that
the file content is what it is expected to be. Vary the content
that is being written so that the test failures become apparent.

This also fixes a case where the length of the wrong value is
checked following a write (it happens to work since the length
of the value checked is the same as that written).

* Make timeFromName actually return a time.

Simplify the timeFromName parsing (we only need to slice once,
not twice) and actually parse the extracted time in the
timeFromName function rather than returning an abitrary string
that may or may not be a time. Also conver the timeFromName
tests into table driven tests.

* Add support for compressing log files.

Rather than scanning for old log files (under lock) when a rotation
occurs, a goroutine is started when we first open or create a log
file. Post-rotation compression (if enabled) and removal of stale
log files is now designated to this goroutine.

Scanning, removal and compression are run in the same goroutine in
order to minimise background disk I/O, with removals being processed
prior to compression in order to free up disk space.

This results in a small change in existing behaviour - previously
only logs would be removed when the first rotation occurs, whereas
now logs will potentially be removed when logging first starts.

* Rework file ownership test.

Previously the test only verified that the code called Chown
but failed to verify what it actually called Chown on. This
reworks the code so that we have a fake file system that tracks
file ownership.

This also simplifies upcoming additional tests.

* Clone file owner and mode on compressed log.

Clone the log file owner and the log file mode to the compressed
log file. Add tests to ensure that this is handled correctly.
  • Loading branch information
4a6f656c authored and xukuan committed Mar 5, 2020
1 parent 1e8e6dc commit eb5aaed
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 108 deletions.
131 changes: 116 additions & 15 deletions linux_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"syscall"
"testing"
"time"
)

func TestMaintainMode(t *testing.T) {
Expand Down Expand Up @@ -46,9 +47,9 @@ func TestMaintainMode(t *testing.T) {
}

func TestMaintainOwner(t *testing.T) {
fakeC := fakeChown{}
os_Chown = fakeC.Set
os_Stat = fakeStat
fakeFS := newFakeFS()
os_Chown = fakeFS.Chown
os_Stat = fakeFS.Stat
defer func() {
os_Chown = os.Chown
os_Stat = os.Stat
Expand All @@ -59,7 +60,45 @@ func TestMaintainOwner(t *testing.T) {

filename := logFile(dir)

f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
isNil(err, t)
f.Close()

l := &Logger{
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
}
defer l.Close()
b := []byte("boo!")
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)

newFakeTime()

err = l.Rotate()
isNil(err, t)

equals(555, fakeFS.files[filename].uid, t)
equals(666, fakeFS.files[filename].gid, t)
}

func TestCompressMaintainMode(t *testing.T) {
currentTime = fakeTime

dir := makeTempDir("TestCompressMaintainMode", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

mode := os.FileMode(0600)
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, mode)
isNil(err, t)
f.Close()

l := &Logger{
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
Expand All @@ -75,27 +114,89 @@ func TestMaintainOwner(t *testing.T) {
err = l.Rotate()
isNil(err, t)

equals(555, fakeC.uid, t)
equals(666, fakeC.gid, t)
// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)

// a compressed version of the log file should now exist with the correct
// mode.
filename2 := backupFile(dir)
info, err := os.Stat(filename)
isNil(err, t)
info2, err := os.Stat(filename2+compressSuffix)
isNil(err, t)
equals(mode, info.Mode(), t)
equals(mode, info2.Mode(), t)
}

func TestCompressMaintainOwner(t *testing.T) {
fakeFS := newFakeFS()
os_Chown = fakeFS.Chown
os_Stat = fakeFS.Stat
defer func() {
os_Chown = os.Chown
os_Stat = os.Stat
}()
currentTime = fakeTime
dir := makeTempDir("TestCompressMaintainOwner", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
isNil(err, t)
f.Close()

l := &Logger{
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
}
defer l.Close()
b := []byte("boo!")
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)

newFakeTime()

err = l.Rotate()
isNil(err, t)

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)

// a compressed version of the log file should now exist with the correct
// owner.
filename2 := backupFile(dir)
equals(555, fakeFS.files[filename2+compressSuffix].uid, t)
equals(666, fakeFS.files[filename2+compressSuffix].gid, t)
}

type fakeFile struct {
uid int
gid int
}

type fakeFS struct {
files map[string]fakeFile
}

type fakeChown struct {
name string
uid int
gid int
func newFakeFS() *fakeFS {
return &fakeFS{files: make(map[string]fakeFile)}
}

func (f *fakeChown) Set(name string, uid, gid int) error {
f.name = name
f.uid = uid
f.gid = gid
func (fs *fakeFS) Chown(name string, uid, gid int) error {
fs.files[name] = fakeFile{uid: uid, gid: gid}
return nil
}

func fakeStat(name string) (os.FileInfo, error) {
func (fs *fakeFS) Stat(name string) (os.FileInfo, error) {
info, err := os.Stat(name)
if err != nil {
return info, err
return nil, err
}
stat := info.Sys().(*syscall.Stat_t)
stat.Uid = 555
Expand Down

0 comments on commit eb5aaed

Please sign in to comment.