-
Notifications
You must be signed in to change notification settings - Fork 2
/
zax_test.go
189 lines (171 loc) · 4.49 KB
/
zax_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package zax
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
type Logger struct {
logger *zap.Logger
recorded *observer.ObservedLogs
t *testing.T
}
func NewLogger(t *testing.T) *Logger {
core, recorded := observer.New(zapcore.DebugLevel)
logger := &Logger{
logger: zap.New(core),
recorded: recorded,
t: t,
}
return logger
}
func (l *Logger) GetZapLogger() *zap.Logger {
return l.logger
}
func (l *Logger) GetRecordedLogs() []observer.LoggedEntry {
return l.recorded.All()
}
func (l *Logger) AssertLogEntryExist(t assert.TestingT, key, value string) bool {
for _, log := range l.recorded.All() {
for _, r := range log.Context {
if r.Key == key && r.String == value {
return true
}
}
}
if key == "" && value == "" {
return true
}
return assert.Fail(t, fmt.Sprintf("log entry does not exist with, %s = %s", key, value))
}
func (l *Logger) AssertLogEntryKeyExist(t assert.TestingT, key string) bool {
for _, log := range l.recorded.All() {
for _, r := range log.Context {
if r.Key == key {
return true
}
}
}
return assert.Fail(t, fmt.Sprintf("log entry does not exist with key = %s ", key))
}
const (
traceIDKey = "trace_id"
spanIDKey = "span_id"
testTraceID = "test-trace-id-3333"
)
func TestSet(t *testing.T) {
testLog := NewLogger(t)
testTraceID2 := "test-trace-id-new"
ctx := context.Background()
tests := map[string]struct {
context context.Context
expectedLoggerKey string
expectedLoggerValue string
}{
"context for zax filed is empty": {
context: Set(ctx, nil),
expectedLoggerKey: "",
expectedLoggerValue: "",
},
"context with trace-id": {
context: Set(ctx, []zap.Field{zap.String(traceIDKey, testTraceID)}),
expectedLoggerKey: traceIDKey,
expectedLoggerValue: testTraceID,
},
"context with trace-id with new value(to check it will be updated)": {
context: Set(ctx, []zap.Field{zap.String(traceIDKey, testTraceID2)}),
expectedLoggerKey: traceIDKey,
expectedLoggerValue: testTraceID2,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := tc.context
logger := testLog.logger.With(Get(ctx)...)
logger.Info("just a test record")
assert.NotNil(t, logger)
testLog.AssertLogEntryExist(t, tc.expectedLoggerKey, tc.expectedLoggerValue)
})
}
}
func TestAppend(t *testing.T) {
testLog := NewLogger(t)
ctx := context.Background()
ctx = Set(ctx, []zap.Field{zap.String(traceIDKey, testTraceID)})
tests := map[string]struct {
context context.Context
expectedFieldNumber int
}{
"context for zax filed is empty": {
context: Append(ctx, nil),
expectedFieldNumber: 1,
},
"context with appending span-id": {
context: Append(ctx, []zap.Field{zap.String(spanIDKey, testTraceID)}),
expectedFieldNumber: 2,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := tc.context
logger := testLog.logger.With(Get(ctx)...)
logger.Info("just a test record")
assert.NotNil(t, logger)
assert.Equal(t, tc.expectedFieldNumber, len(Get(ctx)))
})
}
}
func TestGet(t *testing.T) {
testLog := NewLogger(t)
traceIDKey := traceIDKey
ctx := context.Background()
tests := map[string]struct {
context context.Context
expectedLoggerKey *string
}{
"context empty": {
context: context.TODO(),
expectedLoggerKey: nil,
},
"context with trace-id field": {
context: Set(ctx, []zap.Field{zap.String(traceIDKey, testTraceID)}),
expectedLoggerKey: &traceIDKey,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := tc.context
testLog.logger.With(Get(ctx)...).Info("just a test record")
if tc.expectedLoggerKey != nil {
testLog.AssertLogEntryKeyExist(t, *tc.expectedLoggerKey)
}
})
}
}
func TestGetField(t *testing.T) {
traceIDKey := traceIDKey
ctx := context.Background()
tests := map[string]struct {
context context.Context
expectedValue string
}{
"context empty": {
context: context.TODO(),
expectedValue: "",
},
"context with trace-id field": {
context: Set(ctx, []zap.Field{zap.String(traceIDKey, testTraceID)}),
expectedValue: testTraceID,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := tc.context
field := GetField(ctx, traceIDKey)
assert.Equal(t, tc.expectedValue, field.String)
})
}
}