Skip to content

Commit 796983b

Browse files
committed
add tests for another examples
1 parent d470498 commit 796983b

File tree

12 files changed

+486
-310
lines changed

12 files changed

+486
-310
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) The go-grpc-middleware Authors.
2+
// Licensed under the Apache License 2.0.
3+
4+
package examplekit
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/go-kit/log"
11+
"github.com/go-kit/log/level"
12+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
13+
)
14+
15+
// InterceptorLogger adapts go-kit logger to interceptor logger.
16+
// This code is simple enough to be copied and not imported.
17+
func InterceptorLogger(l log.Logger) logging.Logger {
18+
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
19+
largs := append([]any{"msg", msg}, fields...)
20+
switch lvl {
21+
case logging.LevelDebug:
22+
_ = level.Debug(l).Log(largs...)
23+
case logging.LevelInfo:
24+
_ = level.Info(l).Log(largs...)
25+
case logging.LevelWarn:
26+
_ = level.Warn(l).Log(largs...)
27+
case logging.LevelError:
28+
_ = level.Error(l).Log(largs...)
29+
default:
30+
panic(fmt.Sprintf("unknown level %v", lvl))
31+
}
32+
})
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,67 @@
11
// Copyright (c) The go-grpc-middleware Authors.
22
// Licensed under the Apache License 2.0.
33

4-
package kit_test
4+
package examplekit_test
55

66
import (
7+
"bytes"
78
"context"
8-
"fmt"
9+
"runtime"
10+
"strings"
11+
"testing"
912

1013
"github.com/go-kit/log"
11-
"github.com/go-kit/log/level"
14+
examplekit "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/kit"
1215
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
16+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
"github.com/stretchr/testify/suite"
1320
"google.golang.org/grpc"
1421
)
1522

16-
// InterceptorLogger adapts go-kit logger to interceptor logger.
17-
// This code is simple enough to be copied and not imported.
18-
func InterceptorLogger(l log.Logger) logging.Logger {
19-
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
20-
largs := append([]any{"msg", msg}, fields...)
21-
switch lvl {
22-
case logging.LevelDebug:
23-
_ = level.Debug(l).Log(largs...)
24-
case logging.LevelInfo:
25-
_ = level.Info(l).Log(largs...)
26-
case logging.LevelWarn:
27-
_ = level.Warn(l).Log(largs...)
28-
case logging.LevelError:
29-
_ = level.Error(l).Log(largs...)
30-
default:
31-
panic(fmt.Sprintf("unknown level %v", lvl))
32-
}
33-
})
23+
type kitExampleTestSuite struct {
24+
*testpb.InterceptorTestSuite
25+
logBuffer *bytes.Buffer
3426
}
3527

36-
func ExampleInterceptorLogger() {
37-
logger := log.NewNopLogger()
28+
func TestSuite(t *testing.T) {
29+
if strings.HasPrefix(runtime.Version(), "go1.7") {
30+
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
31+
return
32+
}
33+
34+
buffer := &bytes.Buffer{}
35+
logger := examplekit.InterceptorLogger(log.NewLogfmtLogger(buffer))
36+
37+
s := &kitExampleTestSuite{
38+
InterceptorTestSuite: &testpb.InterceptorTestSuite{
39+
TestService: &testpb.TestPingService{},
40+
},
41+
logBuffer: buffer,
42+
}
3843

39-
opts := []logging.Option{
40-
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
41-
// Add any other option (check functions starting with logging.With).
44+
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
45+
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
46+
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
4247
}
4348

44-
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
45-
_ = grpc.NewServer(
46-
grpc.ChainUnaryInterceptor(
47-
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
48-
// Add any other interceptor you want.
49-
),
50-
grpc.ChainStreamInterceptor(
51-
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
52-
// Add any other interceptor you want.
53-
),
54-
)
55-
// ...user server.
49+
suite.Run(t, s)
50+
}
51+
52+
func (s *kitExampleTestSuite) TestPing() {
53+
ctx := context.Background()
54+
_, err := s.Client.Ping(ctx, testpb.GoodPing)
55+
assert.NoError(s.T(), err, "there must be not be an on a successful call")
56+
logStr := s.logBuffer.String()
57+
require.Contains(s.T(), logStr, "level=info")
58+
require.Contains(s.T(), logStr, "msg=\"started call\"")
59+
require.Contains(s.T(), logStr, "protocol=grpc")
60+
require.Contains(s.T(), logStr, "grpc.component=server")
61+
require.Contains(s.T(), logStr, "grpc.service=testing.testpb.v1.TestService")
62+
require.Contains(s.T(), logStr, "grpc.method=Ping")
63+
require.Contains(s.T(), logStr, "grpc.method_type=unary")
64+
require.Contains(s.T(), logStr, "start_time=")
65+
require.Contains(s.T(), logStr, "grpc.time_ms=")
5666

57-
// Similarly you can create client that will log for the unary and stream client started or finished calls.
58-
_, _ = grpc.Dial(
59-
"some-target",
60-
grpc.WithChainUnaryInterceptor(
61-
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
62-
// Add any other interceptor you want.
63-
),
64-
grpc.WithChainStreamInterceptor(
65-
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
66-
// Add any other interceptor you want.
67-
),
68-
)
69-
// Output:
7067
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) The go-grpc-middleware Authors.
2+
// Licensed under the Apache License 2.0.
3+
4+
package examplelog
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
11+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
12+
)
13+
14+
// InterceptorLogger adapts standard Go logger to interceptor logger.
15+
// This code is simple enough to be copied and not imported.
16+
func InterceptorLogger(l *log.Logger) logging.Logger {
17+
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
18+
switch lvl {
19+
case logging.LevelDebug:
20+
msg = fmt.Sprintf("DEBUG :%v", msg)
21+
case logging.LevelInfo:
22+
msg = fmt.Sprintf("INFO :%v", msg)
23+
case logging.LevelWarn:
24+
msg = fmt.Sprintf("WARN :%v", msg)
25+
case logging.LevelError:
26+
msg = fmt.Sprintf("ERROR :%v", msg)
27+
default:
28+
panic(fmt.Sprintf("unknown level %v", lvl))
29+
}
30+
l.Println(append([]any{"msg", msg}, fields...))
31+
})
32+
}
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,66 @@
11
// Copyright (c) The go-grpc-middleware Authors.
22
// Licensed under the Apache License 2.0.
33

4-
package log_test
4+
package examplelog_test
55

66
import (
7+
"bytes"
78
"context"
8-
"fmt"
99
"log"
10-
"os"
10+
"runtime"
11+
"strings"
12+
"testing"
1113

14+
examplelog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/log"
1215
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
16+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
"github.com/stretchr/testify/suite"
1320
"google.golang.org/grpc"
1421
)
1522

16-
// InterceptorLogger adapts standard Go logger to interceptor logger.
17-
// This code is simple enough to be copied and not imported.
18-
func InterceptorLogger(l *log.Logger) logging.Logger {
19-
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
20-
switch lvl {
21-
case logging.LevelDebug:
22-
msg = fmt.Sprintf("DEBUG :%v", msg)
23-
case logging.LevelInfo:
24-
msg = fmt.Sprintf("INFO :%v", msg)
25-
case logging.LevelWarn:
26-
msg = fmt.Sprintf("WARN :%v", msg)
27-
case logging.LevelError:
28-
msg = fmt.Sprintf("ERROR :%v", msg)
29-
default:
30-
panic(fmt.Sprintf("unknown level %v", lvl))
31-
}
32-
l.Println(append([]any{"msg", msg}, fields...))
33-
})
23+
type logExampleTestSuite struct {
24+
*testpb.InterceptorTestSuite
25+
logBuffer *bytes.Buffer
3426
}
3527

36-
func ExampleInterceptorLogger() {
37-
logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
28+
func TestSuite(t *testing.T) {
29+
if strings.HasPrefix(runtime.Version(), "go1.7") {
30+
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
31+
return
32+
}
33+
buffer := &bytes.Buffer{}
34+
logger := examplelog.InterceptorLogger(log.New(buffer, "", 0))
35+
36+
s := &logExampleTestSuite{
37+
InterceptorTestSuite: &testpb.InterceptorTestSuite{
38+
TestService: &testpb.TestPingService{},
39+
},
40+
logBuffer: buffer,
41+
}
3842

39-
opts := []logging.Option{
40-
logging.WithLogOnEvents(logging.StartCall, logging.FinishCall),
41-
// Add any other option (check functions starting with logging.With).
43+
s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{
44+
grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)),
45+
grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)),
4246
}
4347

44-
// You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished.
45-
_ = grpc.NewServer(
46-
grpc.ChainUnaryInterceptor(
47-
logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...),
48-
// Add any other interceptor you want.
49-
),
50-
grpc.ChainStreamInterceptor(
51-
logging.StreamServerInterceptor(InterceptorLogger(logger), opts...),
52-
// Add any other interceptor you want.
53-
),
54-
)
55-
// ...user server.
48+
suite.Run(t, s)
49+
}
50+
51+
func (s *logExampleTestSuite) TestPing() {
52+
ctx := context.Background()
53+
_, err := s.Client.Ping(ctx, testpb.GoodPing)
54+
assert.NoError(s.T(), err, "there must be not be an on a successful call")
55+
logStr := s.logBuffer.String()
56+
require.Contains(s.T(), logStr, "msg INFO")
57+
require.Contains(s.T(), logStr, ":started call")
58+
require.Contains(s.T(), logStr, "protocol grpc")
59+
require.Contains(s.T(), logStr, "grpc.component server")
60+
require.Contains(s.T(), logStr, "grpc.service testing.testpb.v1.TestService")
61+
require.Contains(s.T(), logStr, "grpc.method Ping")
62+
require.Contains(s.T(), logStr, "grpc.method_type unary")
63+
require.Contains(s.T(), logStr, "start_time ")
64+
require.Contains(s.T(), logStr, "grpc.time_ms")
5665

57-
// Similarly you can create client that will log for the unary and stream client started or finished calls.
58-
_, _ = grpc.Dial(
59-
"some-target",
60-
grpc.WithChainUnaryInterceptor(
61-
logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...),
62-
// Add any other interceptor you want.
63-
),
64-
grpc.WithChainStreamInterceptor(
65-
logging.StreamClientInterceptor(InterceptorLogger(logger), opts...),
66-
// Add any other interceptor you want.
67-
),
68-
)
69-
// Output:
7066
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) The go-grpc-middleware Authors.
2+
// Licensed under the Apache License 2.0.
3+
4+
package examplelogr
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/go-logr/logr"
11+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
12+
)
13+
14+
// verbosity https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use
15+
const (
16+
debugVerbosity = 4
17+
infoVerbosity = 2
18+
warnVerbosity = 1
19+
errorVerbosity = 0
20+
)
21+
22+
// InterceptorLogger adapts logr logger to interceptor logger.
23+
// This code is simple enough to be copied and not imported.
24+
func InterceptorLogger(l logr.Logger) logging.Logger {
25+
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) {
26+
l = l.WithValues(fields...)
27+
switch lvl {
28+
case logging.LevelDebug:
29+
l.V(debugVerbosity).Info(msg)
30+
case logging.LevelInfo:
31+
l.V(infoVerbosity).Info(msg)
32+
case logging.LevelWarn:
33+
l.V(warnVerbosity).Info(msg)
34+
case logging.LevelError:
35+
l.V(errorVerbosity).Info(msg)
36+
default:
37+
panic(fmt.Sprintf("unknown level %v", lvl))
38+
}
39+
})
40+
}

0 commit comments

Comments
 (0)