-
Notifications
You must be signed in to change notification settings - Fork 45
/
logger_test.go
128 lines (100 loc) · 3.82 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package utils
import (
"reflect"
"testing"
"github.com/edaniels/golog"
"go.uber.org/zap"
"go.viam.com/test"
)
// InvalidLogger fulfills the ZapCompatibleLogger interface without a Named() or Sublogger() method, used to test
// that utils.Sublogger() should fail without either of these methods.
type InvalidLogger struct {
name string
}
func (m *InvalidLogger) Desugar() *zap.Logger {
return zap.NewNop()
}
func (m *InvalidLogger) Debug(args ...interface{}) {
}
func (m *InvalidLogger) Debugf(template string, args ...interface{}) {
}
func (m *InvalidLogger) Debugw(msg string, keysAndValues ...interface{}) {
}
func (m *InvalidLogger) Info(args ...interface{}) {
}
func (m *InvalidLogger) Infof(template string, args ...interface{}) {
}
func (m *InvalidLogger) Infow(msg string, keysAndValues ...interface{}) {
}
func (m *InvalidLogger) Warn(args ...interface{}) {
}
func (m *InvalidLogger) Warnf(template string, args ...interface{}) {
}
func (m *InvalidLogger) Warnw(msg string, keysAndValues ...interface{}) {
}
func (m *InvalidLogger) Error(args ...interface{}) {
}
func (m *InvalidLogger) Errorf(template string, args ...interface{}) {
}
func (m *InvalidLogger) Errorw(msg string, keysAndValues ...interface{}) {
}
func (m *InvalidLogger) Fatal(args ...interface{}) {
}
func (m *InvalidLogger) Fatalf(template string, args ...interface{}) {
}
func (m *InvalidLogger) Fatalw(msg string, keysAndValues ...interface{}) {
}
// MockLogger fulfills the ZapCompatibleLogger interface by extending InvalidLogger with a Sublogger() method. This type
// is used to simulate calling utils.Sublogger() on an RDK logger.
type MockLogger struct {
InvalidLogger
Name string
}
func (m *MockLogger) Sublogger(subname string) ZapCompatibleLogger {
return &MockLogger{Name: m.Name + "." + subname}
}
func (m *MockLogger) WithFields(args ...interface{}) {
m.Name = "WithFields called"
}
func TestSubloggerWithZapLogger(t *testing.T) {
logger := golog.NewTestLogger(t)
sublogger := Sublogger(logger, "sub")
test.That(t, sublogger, test.ShouldNotBeNil)
test.That(t, sublogger, test.ShouldNotEqual, logger)
test.That(t, reflect.TypeOf(sublogger), test.ShouldEqual, reflect.TypeOf(logger))
}
func TestSubloggerWithMockRDKLogger(t *testing.T) {
logger := &MockLogger{Name: "main"}
sublogger := Sublogger(logger, "sub")
test.That(t, sublogger, test.ShouldNotBeNil)
test.That(t, sublogger, test.ShouldNotEqual, logger)
test.That(t, reflect.TypeOf(sublogger), test.ShouldEqual, reflect.TypeOf(logger))
test.That(t, sublogger.(*MockLogger).Name, test.ShouldEqual, "main.sub")
}
func TestSubloggerWithInvalidLogger(t *testing.T) {
logger := &InvalidLogger{name: "main"}
sublogger := Sublogger(logger, "sub")
// Sublogger returns logger (itself) if creating a sublogger fails, which we expect
test.That(t, sublogger, test.ShouldEqual, logger)
}
func TestLogWithZapLogger(t *testing.T) {
logger := golog.NewTestLogger(t)
loggerWith := AddFieldsToLogger(logger, "key", "value")
test.That(t, loggerWith, test.ShouldNotBeNil)
test.That(t, loggerWith, test.ShouldEqual, logger)
test.That(t, reflect.TypeOf(loggerWith), test.ShouldEqual, reflect.TypeOf(logger))
}
func TestLogWithMockRDKLogger(t *testing.T) {
logger := &MockLogger{Name: "main"}
loggerWith := AddFieldsToLogger(logger, "key", "value")
test.That(t, loggerWith, test.ShouldNotBeNil)
test.That(t, loggerWith, test.ShouldEqual, logger) // MockLogger modifies the logger in place
test.That(t, reflect.TypeOf(loggerWith), test.ShouldEqual, reflect.TypeOf(logger))
test.That(t, loggerWith.(*MockLogger).Name, test.ShouldEqual, "WithFields called")
}
func TestLogWithInvalidLogger(t *testing.T) {
logger := &InvalidLogger{name: "main"}
loggerWith := AddFieldsToLogger(logger, "key", "value")
// With returns logger (itself) if adding fields fails, which we expect
test.That(t, loggerWith, test.ShouldEqual, logger)
}