Skip to content

Commit 6f07a83

Browse files
committed
add tests for examples interceptors
1 parent bfde808 commit 6f07a83

File tree

9 files changed

+400
-25
lines changed

9 files changed

+400
-25
lines changed

interceptors/logging/examples/go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@ require (
66
github.com/go-kit/log v0.2.1
77
github.com/go-logr/logr v1.2.4
88
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0
9-
github.com/phuslu/log v1.0.83
109
github.com/rs/zerolog v1.29.0
1110
github.com/sirupsen/logrus v1.9.0
11+
github.com/stretchr/testify v1.8.0
1212
go.uber.org/zap v1.24.0
1313
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
1414
google.golang.org/grpc v1.53.0
1515
k8s.io/klog/v2 v2.90.1
1616
)
1717

1818
require (
19+
github.com/davecgh/go-spew v1.1.1 // indirect
1920
github.com/go-logfmt/logfmt v0.5.1 // indirect
2021
github.com/golang/protobuf v1.5.2 // indirect
2122
github.com/mattn/go-colorable v0.1.12 // indirect
2223
github.com/mattn/go-isatty v0.0.14 // indirect
24+
github.com/pkg/errors v0.9.1 // indirect
25+
github.com/pmezard/go-difflib v1.0.0 // indirect
2326
go.uber.org/atomic v1.7.0 // indirect
2427
go.uber.org/multierr v1.6.0 // indirect
2528
golang.org/x/net v0.8.0 // indirect
2629
golang.org/x/sys v0.6.0 // indirect
2730
golang.org/x/text v0.8.0 // indirect
2831
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
2932
google.golang.org/protobuf v1.30.0 // indirect
33+
gopkg.in/yaml.v3 v3.0.1 // indirect
3034
)
3135

3236
replace github.com/grpc-ecosystem/go-grpc-middleware/v2 => ../../../

interceptors/logging/examples/go.sum

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZb
2020
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
2121
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
2222
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
23-
github.com/phuslu/log v1.0.83 h1:zfqz5tfFPLF8w0jEscpDxE2aFg1Y1kcbORDPliKdIbU=
24-
github.com/phuslu/log v1.0.83/go.mod h1:yAZh4pv6KxAsJDmJIcVSMxkMiUF7mJbpFN3vROkf0dc=
2523
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2624
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2725
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -32,9 +30,12 @@ github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6us
3230
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
3331
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
3432
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
3534
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3635
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
36+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3737
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
38+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
3839
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
3940
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
4041
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
@@ -62,8 +63,10 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
6263
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
6364
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
6465
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
66+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6567
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6668
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6769
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
70+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6871
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
6972
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=

interceptors/logging/examples/kit/example_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
package kit_test
55

66
import (
7+
"bytes"
78
"context"
89
"fmt"
10+
"runtime"
11+
"strings"
12+
"testing"
913

1014
"github.com/go-kit/log"
1115
"github.com/go-kit/log/level"
1216
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
17+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
20+
"github.com/stretchr/testify/suite"
1321
"google.golang.org/grpc"
1422
)
1523

@@ -68,3 +76,49 @@ func ExampleInterceptorLogger() {
6876
)
6977
// Output:
7078
}
79+
80+
type kitExampleTestSuite struct {
81+
*testpb.InterceptorTestSuite
82+
logBuffer *bytes.Buffer
83+
}
84+
85+
func TestSuite(t *testing.T) {
86+
if strings.HasPrefix(runtime.Version(), "go1.7") {
87+
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
88+
return
89+
}
90+
91+
buffer := &bytes.Buffer{}
92+
logger := InterceptorLogger(log.NewLogfmtLogger(buffer))
93+
94+
s := &kitExampleTestSuite{
95+
InterceptorTestSuite: &testpb.InterceptorTestSuite{
96+
TestService: &testpb.TestPingService{},
97+
},
98+
logBuffer: buffer,
99+
}
100+
101+
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
102+
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
103+
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
104+
}
105+
106+
suite.Run(t, s)
107+
}
108+
109+
func (s *kitExampleTestSuite) TestPing() {
110+
ctx := context.Background()
111+
_, err := s.Client.Ping(ctx, testpb.GoodPing)
112+
assert.NoError(s.T(), err, "there must be not be an on a successful call")
113+
logStr := s.logBuffer.String()
114+
require.Contains(s.T(), logStr, "level=info")
115+
require.Contains(s.T(), logStr, "msg=\"started call\"")
116+
require.Contains(s.T(), logStr, "protocol=grpc")
117+
require.Contains(s.T(), logStr, "grpc.component=server")
118+
require.Contains(s.T(), logStr, "grpc.service=testing.testpb.v1.TestService")
119+
require.Contains(s.T(), logStr, "grpc.method=Ping")
120+
require.Contains(s.T(), logStr, "grpc.method_type=unary")
121+
require.Contains(s.T(), logStr, "start_time=")
122+
require.Contains(s.T(), logStr, "grpc.time_ms=")
123+
124+
}

interceptors/logging/examples/log/example_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
package log_test
55

66
import (
7+
"bytes"
78
"context"
89
"fmt"
910
"log"
1011
"os"
12+
"runtime"
13+
"strings"
14+
"testing"
1115

1216
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
17+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
20+
"github.com/stretchr/testify/suite"
1321
"google.golang.org/grpc"
1422
)
1523

@@ -68,3 +76,48 @@ func ExampleInterceptorLogger() {
6876
)
6977
// Output:
7078
}
79+
80+
type logExampleTestSuite struct {
81+
*testpb.InterceptorTestSuite
82+
logBuffer *bytes.Buffer
83+
}
84+
85+
func TestSuite(t *testing.T) {
86+
if strings.HasPrefix(runtime.Version(), "go1.7") {
87+
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
88+
return
89+
}
90+
buffer := &bytes.Buffer{}
91+
logger := InterceptorLogger(log.New(buffer, "", 0))
92+
93+
s := &logExampleTestSuite{
94+
InterceptorTestSuite: &testpb.InterceptorTestSuite{
95+
TestService: &testpb.TestPingService{},
96+
},
97+
logBuffer: buffer,
98+
}
99+
100+
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
101+
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
102+
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
103+
}
104+
105+
suite.Run(t, s)
106+
}
107+
108+
func (s *logExampleTestSuite) TestPing() {
109+
ctx := context.Background()
110+
_, err := s.Client.Ping(ctx, testpb.GoodPing)
111+
assert.NoError(s.T(), err, "there must be not be an on a successful call")
112+
logStr := s.logBuffer.String()
113+
require.Contains(s.T(), logStr, "msg INFO")
114+
require.Contains(s.T(), logStr, ":started call")
115+
require.Contains(s.T(), logStr, "protocol grpc")
116+
require.Contains(s.T(), logStr, "grpc.component server")
117+
require.Contains(s.T(), logStr, "grpc.service testing.testpb.v1.TestService")
118+
require.Contains(s.T(), logStr, "grpc.method Ping")
119+
require.Contains(s.T(), logStr, "grpc.method_type unary")
120+
require.Contains(s.T(), logStr, "start_time ")
121+
require.Contains(s.T(), logStr, "grpc.time_ms")
122+
123+
}

interceptors/logging/examples/logr/example_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ package logr_test
66
import (
77
"context"
88
"fmt"
9+
"runtime"
10+
"strings"
11+
"testing"
912

1013
"github.com/go-logr/logr"
1114
"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"
1219
"google.golang.org/grpc"
1320
"k8s.io/klog/v2"
21+
"k8s.io/klog/v2/ktesting"
1422
)
1523

1624
// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
@@ -76,3 +84,48 @@ func ExampleInterceptorLogger() {
7684
)
7785
// Output:
7886
}
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+
}

interceptors/logging/examples/logrus/example_test.go

+61-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
package logrus_test
55

66
import (
7+
"bytes"
78
"context"
9+
"encoding/json"
810
"fmt"
11+
"runtime"
12+
"strings"
13+
"testing"
914

1015
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
16+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
1117
"github.com/sirupsen/logrus"
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
20+
"github.com/stretchr/testify/suite"
1221
"google.golang.org/grpc"
1322
)
1423

@@ -18,7 +27,7 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
1827
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
1928
f := make(map[string]any, len(fields)/2)
2029
i := logging.Fields(fields).Iterator()
21-
if i.Next() {
30+
for i.Next() {
2231
k, v := i.At()
2332
f[k] = v
2433
}
@@ -74,3 +83,54 @@ func ExampleInterceptorLogger() {
7483
)
7584
// Output:
7685
}
86+
87+
type logrusExampleTestSuite struct {
88+
*testpb.InterceptorTestSuite
89+
logBuffer *bytes.Buffer
90+
}
91+
92+
func TestSuite(t *testing.T) {
93+
if strings.HasPrefix(runtime.Version(), "go1.7") {
94+
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
95+
return
96+
}
97+
98+
buffer := &bytes.Buffer{}
99+
testLogger := logrus.New()
100+
testLogger.Out = buffer
101+
testLogger.SetFormatter(&logrus.JSONFormatter{})
102+
logger := InterceptorLogger(testLogger)
103+
104+
s := &logrusExampleTestSuite{
105+
InterceptorTestSuite: &testpb.InterceptorTestSuite{
106+
TestService: &testpb.TestPingService{},
107+
},
108+
logBuffer: buffer,
109+
}
110+
111+
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
112+
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger, logging.WithLogOnEvents(logging.StartCall))),
113+
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger, logging.WithLogOnEvents(logging.StartCall))),
114+
}
115+
116+
suite.Run(t, s)
117+
}
118+
119+
func (s *logrusExampleTestSuite) TestPing() {
120+
ctx := context.Background()
121+
_, err := s.Client.Ping(ctx, testpb.GoodPing)
122+
assert.NoError(s.T(), err, "there must be not be an on a successful call")
123+
var logMap map[string]interface{}
124+
err = json.Unmarshal(s.logBuffer.Bytes(), &logMap)
125+
require.NoError(s.T(), err)
126+
127+
require.Equal(s.T(), "started call", logMap["msg"])
128+
require.Equal(s.T(), "grpc", logMap["protocol"])
129+
require.Equal(s.T(), "server", logMap["grpc.component"])
130+
require.Equal(s.T(), "testing.testpb.v1.TestService", logMap["grpc.service"])
131+
require.Equal(s.T(), "Ping", logMap["grpc.method"])
132+
require.Equal(s.T(), "unary", logMap["grpc.method_type"])
133+
require.Contains(s.T(), logMap["peer.address"], "127.0.0.1")
134+
require.NotEmpty(s.T(), logMap["grpc.start_time"])
135+
require.NotEmpty(s.T(), logMap["grpc.time_ms"])
136+
}

0 commit comments

Comments
 (0)