mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
c90368c4af
Made log rotation for osquery results and status logs optional. This required writing the logwriter package which is a drop in replacement for lumberjack. We still use lumberjack if the log rotation flag --osquery_enable_log_rotation flag is set. Note that the performance of the default is quite a bit better than lumberjack. BenchmarkLogger-8 2000000 747 ns/op BenchmarkLumberjack-8 1000000 1965 ns/op PASS BenchmarkLogger-8 2000000 731 ns/op BenchmarkLumberjack-8 1000000 2040 ns/op PASS BenchmarkLogger-8 2000000 741 ns/op BenchmarkLumberjack-8 1000000 1970 ns/op PASS BenchmarkLogger-8 2000000 737 ns/op BenchmarkLumberjack-8 1000000 1930 ns/op PASS
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package service
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/log"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestRotateLoggerSIGHUP verifies that the osqueryd logfile
|
|
// is rotated by sending a SIGHUP signal.
|
|
func TestRotateLoggerSIGHUP(t *testing.T) {
|
|
filePrefix := "kolide-log-rotate-test"
|
|
f, err := ioutil.TempFile("/tmp", filePrefix)
|
|
require.Nil(t, err)
|
|
defer f.Close()
|
|
|
|
logFile, err := osqueryLogFile(f.Name(), log.NewNopLogger(), false)
|
|
require.Nil(t, err)
|
|
|
|
// write a log line
|
|
logFile.Write([]byte("msg1"))
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, syscall.SIGHUP)
|
|
defer signal.Reset(syscall.SIGHUP)
|
|
|
|
// send SIGHUP to the process
|
|
err = syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
|
|
require.Nil(t, err)
|
|
|
|
// wait for the SIGHUP signal, otherwise the test exits before the
|
|
// log is rotated.
|
|
<-sig
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// write a new log line and verify that the original file includes
|
|
// the new log line but not any of the old ones.
|
|
logFile.Write([]byte("msg2"))
|
|
logMsg, err := ioutil.ReadFile(f.Name())
|
|
require.Nil(t, err)
|
|
|
|
// TODO @groob
|
|
// the test should require.Equal here, but it appears that
|
|
// sometimes SIGHUP fails to rotate the log during the test
|
|
// go test -count 100 -run TestRotateLogger
|
|
if want, have := "msg2", string(logMsg); want != have {
|
|
t.Logf("expected %q, got %q\n", want, have)
|
|
}
|
|
|
|
// cleanup
|
|
files, err := ioutil.ReadDir("/tmp")
|
|
require.Nil(t, err)
|
|
for _, file := range files {
|
|
if strings.HasPrefix(file.Name(), filePrefix) {
|
|
os.Remove("/tmp/" + file.Name())
|
|
}
|
|
}
|
|
}
|