Commit d9ea00dd authored by Tomasz Maczukin's avatar Tomasz Maczukin

Refactor and improve tests for SystemServiceLogHook

parent 10e4de4f
......@@ -151,11 +151,12 @@ dockerfiles:
mocks: $(MOCKERY)
rm -rf ./helpers/service/mocks
find . -type f ! -path '*vendor/*' -name 'mock_*' -delete
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./vendor/github.com/ayufan/golang-kardianos-service -output=./helpers/service/mocks -name='(Interface|Logger)'
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./vendor/github.com/ayufan/golang-kardianos-service -output=./helpers/service/mocks -name='(Interface)'
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./helpers/docker -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./helpers/certificate -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./cache -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./common -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./log -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./session -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./shells -all -inpkg
......
......@@ -664,7 +664,7 @@ func (mr *RunCommand) Execute(context *cli.Context) {
}
if mr.Syslog {
log.SetSystemLogger(svc)
log.SetSystemLogger(logrus.StandardLogger(), svc)
}
logrus.AddHook(&mr.sentryLogHook)
......
......@@ -2,17 +2,17 @@
// This comment works around https://github.com/vektra/mockery/issues/155
package mocks
package log
import mock "github.com/stretchr/testify/mock"
// Logger is an autogenerated mock type for the Logger type
type Logger struct {
// mockSystemLogger is an autogenerated mock type for the systemLogger type
type mockSystemLogger struct {
mock.Mock
}
// Error provides a mock function with given fields: v
func (_m *Logger) Error(v ...interface{}) error {
func (_m *mockSystemLogger) Error(v ...interface{}) error {
var _ca []interface{}
_ca = append(_ca, v...)
ret := _m.Called(_ca...)
......@@ -28,7 +28,7 @@ func (_m *Logger) Error(v ...interface{}) error {
}
// Errorf provides a mock function with given fields: format, a
func (_m *Logger) Errorf(format string, a ...interface{}) error {
func (_m *mockSystemLogger) Errorf(format string, a ...interface{}) error {
var _ca []interface{}
_ca = append(_ca, format)
_ca = append(_ca, a...)
......@@ -45,7 +45,7 @@ func (_m *Logger) Errorf(format string, a ...interface{}) error {
}
// Info provides a mock function with given fields: v
func (_m *Logger) Info(v ...interface{}) error {
func (_m *mockSystemLogger) Info(v ...interface{}) error {
var _ca []interface{}
_ca = append(_ca, v...)
ret := _m.Called(_ca...)
......@@ -61,7 +61,7 @@ func (_m *Logger) Info(v ...interface{}) error {
}
// Infof provides a mock function with given fields: format, a
func (_m *Logger) Infof(format string, a ...interface{}) error {
func (_m *mockSystemLogger) Infof(format string, a ...interface{}) error {
var _ca []interface{}
_ca = append(_ca, format)
_ca = append(_ca, a...)
......@@ -78,7 +78,7 @@ func (_m *Logger) Infof(format string, a ...interface{}) error {
}
// Warning provides a mock function with given fields: v
func (_m *Logger) Warning(v ...interface{}) error {
func (_m *mockSystemLogger) Warning(v ...interface{}) error {
var _ca []interface{}
_ca = append(_ca, v...)
ret := _m.Called(_ca...)
......@@ -94,7 +94,7 @@ func (_m *Logger) Warning(v ...interface{}) error {
}
// Warningf provides a mock function with given fields: format, a
func (_m *Logger) Warningf(format string, a ...interface{}) error {
func (_m *mockSystemLogger) Warningf(format string, a ...interface{}) error {
var _ca []interface{}
_ca = append(_ca, format)
_ca = append(_ca, a...)
......
// Code generated by mockery v1.0.0. DO NOT EDIT.
// This comment works around https://github.com/vektra/mockery/issues/155
package log
import mock "github.com/stretchr/testify/mock"
import service "github.com/ayufan/golang-kardianos-service"
// mockSystemService is an autogenerated mock type for the systemService type
type mockSystemService struct {
mock.Mock
}
// Install provides a mock function with given fields:
func (_m *mockSystemService) Install() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Logger provides a mock function with given fields: errs
func (_m *mockSystemService) Logger(errs chan<- error) (service.Logger, error) {
ret := _m.Called(errs)
var r0 service.Logger
if rf, ok := ret.Get(0).(func(chan<- error) service.Logger); ok {
r0 = rf(errs)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(service.Logger)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(chan<- error) error); ok {
r1 = rf(errs)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Restart provides a mock function with given fields:
func (_m *mockSystemService) Restart() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Run provides a mock function with given fields:
func (_m *mockSystemService) Run() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Start provides a mock function with given fields:
func (_m *mockSystemService) Start() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Status provides a mock function with given fields:
func (_m *mockSystemService) Status() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Stop provides a mock function with given fields:
func (_m *mockSystemService) Stop() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// String provides a mock function with given fields:
func (_m *mockSystemService) String() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// SystemLogger provides a mock function with given fields: errs
func (_m *mockSystemService) SystemLogger(errs chan<- error) (service.Logger, error) {
ret := _m.Called(errs)
var r0 service.Logger
if rf, ok := ret.Get(0).(func(chan<- error) service.Logger); ok {
r0 = rf(errs)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(service.Logger)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(chan<- error) error); ok {
r1 = rf(errs)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Uninstall provides a mock function with given fields:
func (_m *mockSystemService) Uninstall() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
......@@ -5,12 +5,20 @@ import (
"github.com/sirupsen/logrus"
)
type ServiceLogHook struct {
type systemLogger interface {
service.Logger
}
type systemService interface {
service.Service
}
type SystemServiceLogHook struct {
systemLogger
Level logrus.Level
}
func (s *ServiceLogHook) Levels() []logrus.Level {
func (s *SystemServiceLogHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
......@@ -20,7 +28,7 @@ func (s *ServiceLogHook) Levels() []logrus.Level {
}
}
func (s *ServiceLogHook) Fire(entry *logrus.Entry) error {
func (s *SystemServiceLogHook) Fire(entry *logrus.Entry) error {
if entry.Level > s.Level {
return nil
}
......@@ -42,12 +50,16 @@ func (s *ServiceLogHook) Fire(entry *logrus.Entry) error {
return nil
}
func SetSystemLogger(svc service.Service) {
func SetSystemLogger(logrusLogger *logrus.Logger, svc systemService) {
logger, err := svc.SystemLogger(nil)
if err == nil {
logrus.AddHook(&ServiceLogHook{logger, logrus.GetLevel()})
hook := new(SystemServiceLogHook)
hook.systemLogger = logger
hook.Level = logrus.GetLevel()
logrusLogger.AddHook(hook)
} else {
logrus.WithError(err).Error("Error while setting up the system logger")
logrusLogger.WithError(err).Error("Error while setting up the system logger")
}
}
package log
import (
"os"
"fmt"
"io/ioutil"
"testing"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-runner/helpers/service/mocks"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestServiceLogHook(t *testing.T) {
formatter := new(logrus.TextFormatter)
formatter.DisableColors = true
formatter.DisableTimestamp = true
logger := &logrus.Logger{
Formatter: formatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
Out: os.Stderr,
key := "key"
value := "value"
testMessage := "test message"
tests := map[logrus.Level]string{
logrus.InfoLevel: "Info",
logrus.WarnLevel: "Warning",
logrus.ErrorLevel: "Error",
// Fatal is skipped by purpose
//
// There is no way to disable or overwrite the `Exit(1)` called by logrus
// at the end of `Fatal` logger. We have our helpers.MakeFatalToPanic
// hook, but it doesn't work reliable here.
//
// We just need to assume, that if all other levels are working properly, then
// `Fatal` will also work. In the end, it's just another entry in the systemLogger
// method selector.
logrus.PanicLevel: "Error",
}
for level, sysLoggerMethod := range tests {
t.Run(fmt.Sprintf("level-%s", level), func(t *testing.T) {
defer func() {
recover()
}()
sysLogger := new(mockSystemLogger)
defer sysLogger.AssertExpectations(t)
sysService := new(mockSystemService)
defer sysService.AssertExpectations(t)
sysService.On("SystemLogger", mock.Anything).Return(sysLogger, nil).Once()
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.SetOutput(ioutil.Discard)
SetSystemLogger(logger, sysService)
sysLogger.On(sysLoggerMethod, mock.Anything).Return(nil).Once().Run(func(args mock.Arguments) {
msg := args.Get(0)
assert.Contains(t, msg, fmt.Sprintf("msg=%q %s=%s", testMessage, key, value))
})
levels := map[logrus.Level]func(args ...interface{}){
logrus.PanicLevel: logger.WithField(key, value).Panic,
logrus.ErrorLevel: logger.WithField(key, value).Error,
logrus.WarnLevel: logger.WithField(key, value).Warning,
logrus.InfoLevel: logger.WithField(key, value).Info,
logrus.DebugLevel: logger.WithField(key, value).Debug,
}
levelLogger, ok := levels[level]
require.True(t, ok, "Unknown level %v used", level)
levelLogger(testMessage)
})
}
}
func TestServiceLogHookWithSpecifiedLevel(t *testing.T) {
entry := logrus.NewEntry(logrus.New())
entry.Message = "test message"
sysLogger := new(mockSystemLogger)
defer sysLogger.AssertExpectations(t)
assertSysLoggerMethod := func(args mock.Arguments) {
msg := args.Get(0)
assert.Contains(t, msg, `msg="test message"`)
}
sysLogger.On("Error", mock.Anything).Return(nil).Once().Run(assertSysLoggerMethod)
sysLogger.On("Warning", mock.Anything).Return(nil).Once().Run(assertSysLoggerMethod)
hook := new(SystemServiceLogHook)
hook.systemLogger = sysLogger
hook.Level = logrus.WarnLevel
for _, level := range []logrus.Level{
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
} {
entry.Level = level
err := hook.Fire(entry)
assert.NoError(t, err)
}
}
func TestSetSystemLogger_ErrorOnInitialization(t *testing.T) {
logger, hook := test.NewNullLogger()
mockServiceLogger := new(mocks.Logger)
mockServiceLogger.On("Info", "level=info msg=test\n").Return(nil)
sysService := new(mockSystemService)
defer sysService.AssertExpectations(t)
sysService.On("SystemLogger", mock.Anything).Return(nil, fmt.Errorf("test error")).Once()
logHook := &ServiceLogHook{mockServiceLogger, logrus.InfoLevel}
SetSystemLogger(logger, sysService)
logger.Hooks.Add(logHook)
entry := hook.LastEntry()
require.NotNil(t, entry)
logger.Info("test")
logrusOutput, err := entry.String()
require.NoError(t, err)
mockServiceLogger.AssertExpectations(t)
assert.Contains(t, logrusOutput, "Error while setting up the system logger")
assert.Contains(t, logrusOutput, `error="test error"`)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment