diff --git a/go/lib/log/BUILD.bazel b/go/lib/log/BUILD.bazel index 91a3ade81e..d47981dff9 100644 --- a/go/lib/log/BUILD.bazel +++ b/go/lib/log/BUILD.bazel @@ -34,8 +34,10 @@ go_test( embed = [":go_default_library"], deps = [ "//go/lib/log/mock_log:go_default_library", + "//go/lib/xtest:go_default_library", "@com_github_golang_mock//gomock:go_default_library", "@com_github_inconshreveable_log15//:go_default_library", "@com_github_smartystreets_goconvey//convey:go_default_library", + "@com_github_stretchr_testify//assert:go_default_library", ], ) diff --git a/go/lib/log/log.go b/go/lib/log/log.go index cf6a88a396..ff3aea389d 100644 --- a/go/lib/log/log.go +++ b/go/lib/log/log.go @@ -57,6 +57,10 @@ var ( func SetupLogFile(name string, logDir string, logLevel string, logSize int, logAge int, logBackups int, logFlush int, compress bool) error { + if err := os.MkdirAll(logDir, os.ModePerm); err != nil { + return common.NewBasicError("Unable create log directory:", err) + } + logLvl, err := log15.LvlFromString(changeTraceToDebug(logLevel)) if err != nil { return common.NewBasicError("Unable to parse log.level flag:", err) @@ -141,6 +145,7 @@ func setHandlers() { log15.Root().SetHandler(handler) } +// LogPanicAndExit catches panics and logs them. func LogPanicAndExit() { if msg := recover(); msg != nil { log15.Crit("Panic", "msg", msg, "stack", string(debug.Stack())) @@ -150,6 +155,7 @@ func LogPanicAndExit() { } } +// Flush writes the logs to the underlying buffer. func Flush() { if logBuf != nil { logBuf.Flush() diff --git a/go/lib/log/log_test.go b/go/lib/log/log_test.go index d575112a7c..1591e1e3de 100644 --- a/go/lib/log/log_test.go +++ b/go/lib/log/log_test.go @@ -15,52 +15,82 @@ package log_test import ( + "path/filepath" "testing" "github.com/golang/mock/gomock" "github.com/inconshreveable/log15" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" "github.com/scionproto/scion/go/lib/log" "github.com/scionproto/scion/go/lib/log/mock_log" + "github.com/scionproto/scion/go/lib/xtest" ) +func TestSetupLogConsole(t *testing.T) { + tmpDir, cleanF := xtest.MustTempDir("", "test-folder") + defer cleanF() + + tests := map[string]struct { + dir string + assertErr assert.ErrorAssertionFunc + }{ + "cannot create, errors": { + dir: "/sys/aka/doesnt/exist", + assertErr: assert.Error, + }, + + "can create, nil": { + dir: filepath.Join(tmpDir, "new"), + assertErr: assert.NoError, + }, + } + + for td, tc := range tests { + t.Run(td, func(t *testing.T) { + err := log.SetupLogFile("test", tc.dir, "debug", 0, 0, 0, 0, false) + tc.assertErr(t, err) + }) + } +} + func TestTraceFilterHandler(t *testing.T) { - Convey("Given a base handler...", t, func() { + t.Log("Given a base handler...") + + t.Run("by default...", func(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockHandler := mock_log.NewMockHandler(ctrl) logger := log.Root() - Convey("by default...", func() { - logger.SetHandler(mockHandler) - var msgSeenByMockHandler string - mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) { - msgSeenByMockHandler = record.Msg - }) - Convey("debug messages are printed", func() { - logger.Debug("foo") - So(msgSeenByMockHandler, ShouldEqual, "foo") - }) - Convey("trace messages are printed", func() { - logger.Trace("foo") - So(msgSeenByMockHandler, ShouldEqual, log.TraceMsgPrefix+"foo") - }) - }) - Convey("if wrapped by a trace filter handler...", func() { - handler := log.FilterTraceHandler(mockHandler) - logger.SetHandler(handler) - Convey("debug messages are printed", func() { - var msgSeenByMockHandler string - mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) { - msgSeenByMockHandler = record.Msg - }) - logger.Debug("foo") - So(msgSeenByMockHandler, ShouldEqual, "foo") - }) - Convey("trace messages are not printed", func() { - logger.Trace("foo") - }) - }) + logger.SetHandler(mockHandler) + var msgSeenByMockHandler string + mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) { + msgSeenByMockHandler = record.Msg + }).AnyTimes() + t.Log("debug messages are printed") + logger.Debug("foo") + assert.Equal(t, msgSeenByMockHandler, "foo") + t.Log("trace messages are printed") + logger.Trace("foo") + assert.Equal(t, msgSeenByMockHandler, log.TraceMsgPrefix+"foo") + }) + + t.Run("if wrapped by a trace filter handler...", func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockHandler := mock_log.NewMockHandler(ctrl) + logger := log.Root() + handler := log.FilterTraceHandler(mockHandler) + logger.SetHandler(handler) + t.Log("debug messages are printed") + var msgSeenByMockHandler string + mockHandler.EXPECT().Log(gomock.Any()).Do(func(record *log15.Record) { + msgSeenByMockHandler = record.Msg + }).AnyTimes() + logger.Debug("foo") + assert.Equal(t, msgSeenByMockHandler, "foo") + t.Log("trace messages are not printed") + logger.Trace("foo") }) }