2021-01-16 15:56:44 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2021-03-23 14:30:43 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-16 15:56:44 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockedWriter struct {
|
2023-04-06 18:31:47 +00:00
|
|
|
wantErr bool
|
|
|
|
writeCalls int
|
2021-01-16 15:56:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 18:31:47 +00:00
|
|
|
func (c *mockedWriter) Write(p []byte) (int, error) {
|
|
|
|
c.writeCalls++
|
2021-01-16 15:56:44 +00:00
|
|
|
|
|
|
|
if c.wantErr {
|
|
|
|
return -1, errors.New("Expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a new writer is only used if it actually works.
|
2023-04-06 18:31:47 +00:00
|
|
|
func TestResilientMultiWriter_Errors(t *testing.T) {
|
2021-01-16 15:56:44 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2023-04-06 18:31:47 +00:00
|
|
|
writers []*mockedWriter
|
2021-01-16 15:56:44 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-03-23 14:30:43 +00:00
|
|
|
name: "All valid writers",
|
2023-04-06 18:31:47 +00:00
|
|
|
writers: []*mockedWriter{
|
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-04-06 18:31:47 +00:00
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-23 14:30:43 +00:00
|
|
|
name: "All invalid writers",
|
2023-04-06 18:31:47 +00:00
|
|
|
writers: []*mockedWriter{
|
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-04-06 18:31:47 +00:00
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-23 14:30:43 +00:00
|
|
|
name: "First invalid writer",
|
2023-04-06 18:31:47 +00:00
|
|
|
writers: []*mockedWriter{
|
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-04-06 18:31:47 +00:00
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-23 14:30:43 +00:00
|
|
|
name: "First valid writer",
|
2023-04-06 18:31:47 +00:00
|
|
|
writers: []*mockedWriter{
|
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-04-06 18:31:47 +00:00
|
|
|
{
|
2021-01-16 15:56:44 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:31:47 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
writers := []io.Writer{}
|
|
|
|
for _, w := range test.writers {
|
|
|
|
writers = append(writers, w)
|
|
|
|
}
|
|
|
|
multiWriter := resilientMultiWriter{zerolog.InfoLevel, writers, nil}
|
|
|
|
|
|
|
|
logger := zerolog.New(multiWriter).With().Timestamp().Logger()
|
|
|
|
logger.Info().Msg("Test msg")
|
|
|
|
|
|
|
|
for _, w := range test.writers {
|
|
|
|
// Expect each writer to be written to regardless of the previous writers returning an error
|
|
|
|
assert.Equal(t, 1, w.writeCalls)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockedManagementWriter struct {
|
|
|
|
WriteCalls int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockedManagementWriter) Write(p []byte) (int, error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockedManagementWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) {
|
|
|
|
c.WriteCalls++
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that management writer receives write calls of all levels except Disabled
|
|
|
|
func TestResilientMultiWriter_Management(t *testing.T) {
|
|
|
|
for _, level := range []zerolog.Level{
|
|
|
|
zerolog.DebugLevel,
|
|
|
|
zerolog.InfoLevel,
|
|
|
|
zerolog.WarnLevel,
|
|
|
|
zerolog.ErrorLevel,
|
|
|
|
zerolog.FatalLevel,
|
|
|
|
zerolog.PanicLevel,
|
|
|
|
} {
|
|
|
|
t.Run(level.String(), func(t *testing.T) {
|
|
|
|
managementWriter := mockedManagementWriter{}
|
|
|
|
multiWriter := resilientMultiWriter{level, []io.Writer{&mockedWriter{}}, &managementWriter}
|
2021-01-16 15:56:44 +00:00
|
|
|
|
2023-04-06 18:31:47 +00:00
|
|
|
logger := zerolog.New(multiWriter).With().Timestamp().Logger()
|
|
|
|
logger.Info().Msg("Test msg")
|
2021-01-16 15:56:44 +00:00
|
|
|
|
2023-04-06 18:31:47 +00:00
|
|
|
// Always write to management
|
|
|
|
assert.Equal(t, 1, managementWriter.WriteCalls)
|
|
|
|
})
|
2021-01-16 15:56:44 +00:00
|
|
|
}
|
2021-03-23 14:30:43 +00:00
|
|
|
}
|