@@ -6,11 +6,19 @@ package logr_test
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "runtime"
10
+ "strings"
11
+ "testing"
9
12
10
13
"github.com/go-logr/logr"
11
14
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
15
+ "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
16
+ "github.com/stretchr/testify/assert"
17
+ "github.com/stretchr/testify/require"
18
+ "github.com/stretchr/testify/suite"
12
19
"google.golang.org/grpc"
13
20
"k8s.io/klog/v2"
21
+ "k8s.io/klog/v2/ktesting"
14
22
)
15
23
16
24
// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
@@ -25,7 +33,7 @@ const (
25
33
// This code is simple enough to be copied and not imported.
26
34
func InterceptorLogger (l logr.Logger ) logging.Logger {
27
35
return logging .LoggerFunc (func (_ context.Context , lvl logging.Level , msg string , fields ... any ) {
28
- l : = l .WithValues (fields ... )
36
+ l = l .WithValues (fields ... )
29
37
switch lvl {
30
38
case logging .LevelDebug :
31
39
l .V (debugVerbosity ).Info (msg )
@@ -76,3 +84,48 @@ func ExampleInterceptorLogger() {
76
84
)
77
85
// Output:
78
86
}
87
+
88
+ type logrExampleTestSuite struct {
89
+ * testpb.InterceptorTestSuite
90
+ logBuffer * ktesting.BufferTL
91
+ }
92
+
93
+ func TestSuite (t * testing.T ) {
94
+ if strings .HasPrefix (runtime .Version (), "go1.7" ) {
95
+ t .Skipf ("Skipping due to json.RawMessage incompatibility with go1.7" )
96
+ return
97
+ }
98
+
99
+ buffer := & ktesting.BufferTL {}
100
+ cfg := ktesting .NewConfig ()
101
+ logger := InterceptorLogger (ktesting .NewLogger (buffer , cfg ))
102
+
103
+ s := & logrExampleTestSuite {
104
+ InterceptorTestSuite : & testpb.InterceptorTestSuite {
105
+ TestService : & testpb.TestPingService {},
106
+ },
107
+ logBuffer : buffer ,
108
+ }
109
+
110
+ s .InterceptorTestSuite .ServerOpts = []grpc.ServerOption {
111
+ grpc .StreamInterceptor (logging .StreamServerInterceptor (logger )),
112
+ grpc .UnaryInterceptor (logging .UnaryServerInterceptor (logger )),
113
+ }
114
+
115
+ suite .Run (t , s )
116
+ }
117
+
118
+ func (s * logrExampleTestSuite ) TestPing () {
119
+ ctx := context .Background ()
120
+ _ , err := s .Client .Ping (ctx , testpb .GoodPing )
121
+ assert .NoError (s .T (), err , "there must be not be an on a successful call" )
122
+ logStr := s .logBuffer .String ()
123
+ require .Contains (s .T (), logStr , "started call" )
124
+ require .Contains (s .T (), logStr , "protocol=\" grpc\" " )
125
+ require .Contains (s .T (), logStr , "grpc.component=\" server\" " )
126
+ require .Contains (s .T (), logStr , "grpc.service=\" testing.testpb.v1.TestService\" " )
127
+ require .Contains (s .T (), logStr , "grpc.method=\" Ping\" " )
128
+ require .Contains (s .T (), logStr , "grpc.method_type=\" unary\" " )
129
+ require .Contains (s .T (), logStr , "start_time=" )
130
+ require .Contains (s .T (), logStr , "grpc.time_ms=" )
131
+ }
0 commit comments