forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_interceptors_test.go
212 lines (180 loc) · 8.88 KB
/
client_interceptors_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package grpc_logrus_test
import (
"io"
"testing"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
)
func customClientCodeToLevel(c codes.Code) logrus.Level {
if c == codes.Unauthenticated {
// Make this a special case for tests, and an error.
return logrus.ErrorLevel
}
level := grpc_logrus.DefaultClientCodeToLevel(c)
return level
}
func TestLogrusClientSuite(t *testing.T) {
opts := []grpc_logrus.Option{
grpc_logrus.WithLevels(customClientCodeToLevel),
}
b := newLogrusBaseSuite(t)
b.logger.Level = logrus.DebugLevel // a lot of our stuff is on debug level by default
b.InterceptorTestSuite.ClientOpts = []grpc.DialOption{
grpc.WithUnaryInterceptor(grpc_logrus.UnaryClientInterceptor(logrus.NewEntry(b.logger), opts...)),
grpc.WithStreamInterceptor(grpc_logrus.StreamClientInterceptor(logrus.NewEntry(b.logger), opts...)),
}
suite.Run(t, &logrusClientSuite{b})
}
type logrusClientSuite struct {
*logrusBaseSuite
}
func (s *logrusClientSuite) TestPing() {
_, err := s.Client.Ping(s.SimpleCtx(), goodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
msgs := s.getOutputJSONs()
require.Len(s.T(), msgs, 1, "one log statement should be logged")
assert.Equal(s.T(), msgs[0]["grpc.service"], "mwitkow.testproto.TestService", "all lines must contain the correct service name")
assert.Equal(s.T(), msgs[0]["grpc.method"], "Ping", "all lines must contain the correct method name")
assert.Equal(s.T(), msgs[0]["msg"], "finished client unary call", "handler's message must contain the correct message")
assert.Equal(s.T(), msgs[0]["span.kind"], "client", "all lines must contain the kind of call (client)")
assert.Equal(s.T(), msgs[0]["level"], "debug", "OK codes must be logged on debug level.")
assert.Contains(s.T(), msgs[0], "grpc.time_ms", "interceptor log statement should contain execution time (duration in ms)")
}
func (s *logrusClientSuite) TestPingList() {
stream, err := s.Client.PingList(s.SimpleCtx(), goodPing)
require.NoError(s.T(), err, "should not fail on establishing the stream")
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
require.NoError(s.T(), err, "reading stream should not fail")
}
msgs := s.getOutputJSONs()
require.Len(s.T(), msgs, 1, "one log statement should be logged")
assert.Equal(s.T(), msgs[0]["grpc.service"], "mwitkow.testproto.TestService", "all lines must contain the correct service name")
assert.Equal(s.T(), msgs[0]["grpc.method"], "PingList", "all lines must contain the correct method name")
assert.Equal(s.T(), msgs[0]["msg"], "finished client streaming call", "handler's message must contain the correct message")
assert.Equal(s.T(), msgs[0]["span.kind"], "client", "all lines must contain the kind of call (client)")
assert.Equal(s.T(), msgs[0]["level"], "debug", "OK codes must be logged on debug level.")
assert.Contains(s.T(), msgs[0], "grpc.time_ms", "interceptor log statement should contain execution time (duration in ms)")
}
func (s *logrusClientSuite) TestPingError_WithCustomLevels() {
for _, tcase := range []struct {
code codes.Code
level logrus.Level
msg string
}{
{
code: codes.Internal,
level: logrus.WarnLevel,
msg: "Internal must remap to ErrorLevel in DefaultClientCodeToLevel",
},
{
code: codes.NotFound,
level: logrus.DebugLevel,
msg: "NotFound must remap to InfoLevel in DefaultClientCodeToLevel",
},
{
code: codes.FailedPrecondition,
level: logrus.DebugLevel,
msg: "FailedPrecondition must remap to WarnLevel in DefaultClientCodeToLevel",
},
{
code: codes.Unauthenticated,
level: logrus.ErrorLevel,
msg: "Unauthenticated is overwritten to ErrorLevel with customClientCodeToLevel override, which probably didn't work",
},
} {
s.SetupTest()
_, err := s.Client.PingError(
s.SimpleCtx(),
&pb_testproto.PingRequest{Value: "something", ErrorCodeReturned: uint32(tcase.code)})
assert.Error(s.T(), err, "each call here must return an error")
msgs := s.getOutputJSONs()
require.Len(s.T(), msgs, 1, "only a single log message is printed")
assert.Equal(s.T(), msgs[0]["grpc.service"], "mwitkow.testproto.TestService", "all lines must contain the correct service name")
assert.Equal(s.T(), msgs[0]["grpc.method"], "PingError", "all lines must contain the correct method name")
assert.Equal(s.T(), msgs[0]["grpc.code"], tcase.code.String(), "all lines must contain a grpc code")
assert.Equal(s.T(), msgs[0]["level"], tcase.level.String(), tcase.msg)
}
}
func TestLogrusClientOverrideSuite(t *testing.T) {
opts := []grpc_logrus.Option{
grpc_logrus.WithDurationField(grpc_logrus.DurationToDurationField),
}
b := newLogrusBaseSuite(t)
b.logger.Level = logrus.DebugLevel // a lot of our stuff is on debug level by default
b.InterceptorTestSuite.ClientOpts = []grpc.DialOption{
grpc.WithUnaryInterceptor(grpc_logrus.UnaryClientInterceptor(logrus.NewEntry(b.logger), opts...)),
grpc.WithStreamInterceptor(grpc_logrus.StreamClientInterceptor(logrus.NewEntry(b.logger), opts...)),
}
suite.Run(t, &logrusClientOverrideSuite{b})
}
type logrusClientOverrideSuite struct {
*logrusBaseSuite
}
func (s *logrusClientOverrideSuite) TestPing_HasOverrides() {
_, err := s.Client.Ping(s.SimpleCtx(), goodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
msgs := s.getOutputJSONs()
require.Len(s.T(), msgs, 1, "one log statement should be logged")
assert.Equal(s.T(), msgs[0]["grpc.service"], "mwitkow.testproto.TestService", "all lines must contain the correct service name")
assert.Equal(s.T(), msgs[0]["grpc.method"], "Ping", "all lines must contain the correct method name")
assert.Equal(s.T(), msgs[0]["msg"], "finished client unary call", "handler's message must contain the correct message")
assert.NotContains(s.T(), msgs[0], "grpc.time_ms", "message must not contain default duration")
assert.Contains(s.T(), msgs[0], "grpc.duration", "message must contain overridden duration")
}
func (s *logrusClientOverrideSuite) TestPingList_HasOverrides() {
stream, err := s.Client.PingList(s.SimpleCtx(), goodPing)
require.NoError(s.T(), err, "should not fail on establishing the stream")
for {
_, err := stream.Recv()
if err == io.EOF {
break
}
require.NoError(s.T(), err, "reading stream should not fail")
}
msgs := s.getOutputJSONs()
require.Len(s.T(), msgs, 1, "one log statement should be logged")
assert.Equal(s.T(), msgs[0]["grpc.service"], "mwitkow.testproto.TestService", "all lines must contain the correct service name")
assert.Equal(s.T(), msgs[0]["grpc.method"], "PingList", "all lines must contain the correct method name")
assert.Equal(s.T(), msgs[0]["msg"], "finished client streaming call", "log message must be correct")
assert.Equal(s.T(), msgs[0]["span.kind"], "client", "all lines must contain the kind of call (client)")
assert.Equal(s.T(), msgs[0]["level"], "debug", "OK codes must be logged on debug level.")
assert.NotContains(s.T(), msgs[0], "grpc.time_ms", "message must not contain default duration")
assert.Contains(s.T(), msgs[0], "grpc.duration", "message must contain overridden duration")
}
func TestZapLoggingClientMessageProducerSuite(t *testing.T) {
opts := []grpc_logrus.Option{
grpc_logrus.WithMessageProducer(StubMessageProducer),
}
b := newLogrusBaseSuite(t)
b.logger.Level = logrus.DebugLevel // a lot of our stuff is on debug level by default
b.InterceptorTestSuite.ClientOpts = []grpc.DialOption{
grpc.WithUnaryInterceptor(grpc_logrus.UnaryClientInterceptor(logrus.NewEntry(b.logger), opts...)),
grpc.WithStreamInterceptor(grpc_logrus.StreamClientInterceptor(logrus.NewEntry(b.logger), opts...)),
}
suite.Run(t, &logrusClientMessageProducerSuite{b})
}
type logrusClientMessageProducerSuite struct {
*logrusBaseSuite
}
func (s *logrusClientMessageProducerSuite) TestPing_HasOverriddenMessageProducer() {
_, err := s.Client.Ping(s.SimpleCtx(), goodPing)
assert.NoError(s.T(), err, "there must be not be an on a successful call")
msgs := s.getOutputJSONs()
require.Len(s.T(), msgs, 1, "one log statement should be logged")
assert.Equal(s.T(), msgs[0]["grpc.service"], "mwitkow.testproto.TestService", "all lines must contain the correct service name")
assert.Equal(s.T(), msgs[0]["grpc.method"], "Ping", "all lines must contain the correct method name")
assert.Equal(s.T(), msgs[0]["msg"], "custom message", "handler's message must contain the correct message")
assert.Equal(s.T(), msgs[0]["span.kind"], "client", "all lines must contain the kind of call (client)")
assert.Equal(s.T(), msgs[0]["level"], "debug", "OK codes must be logged on debug level.")
assert.Contains(s.T(), msgs[0], "grpc.time_ms", "interceptor log statement should contain execution time (duration in ms)")
}