-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging_test.go
101 lines (93 loc) · 2.54 KB
/
logging_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
package aviation
import (
"context"
"testing"
"github.com/mongodb/grip"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/logging"
"github.com/mongodb/grip/send"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
func TestGripInterceptors(t *testing.T) {
for _, test := range []struct {
name string
unaryInfo *grpc.UnaryServerInfo
streamInfo *grpc.StreamServerInfo
action string
err bool
}{
{
name: "ValidLogging",
unaryInfo: &grpc.UnaryServerInfo{},
},
{
name: "ErrorLogging",
unaryInfo: &grpc.UnaryServerInfo{},
action: "error",
err: true,
},
{
name: "PanicLogging",
unaryInfo: &grpc.UnaryServerInfo{},
action: "panic",
err: true,
},
} {
t.Run(test.name, func(t *testing.T) {
t.Run("Unary", func(t *testing.T) {
sender, err := send.NewInternalLogger("test", grip.GetSender().Level())
require.NoError(t, err)
journaler := logging.MakeGrip(sender)
startAt := getNumber()
interceptor := MakeGripUnaryInterceptor(journaler)
_, err = interceptor(context.TODO(), test.action, &grpc.UnaryServerInfo{}, mockUnaryHandler)
if test.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, startAt+2, getNumber())
if assert.True(t, sender.HasMessage()) {
require.Equal(t, 2, sender.Len())
msg := sender.GetMessage()
assert.Equal(t, level.Info, msg.Priority)
msg = sender.GetMessage()
assert.Equal(t, expectedPriority(test.action), msg.Priority)
}
})
t.Run("Streaming", func(t *testing.T) {
sender, err := send.NewInternalLogger("test", grip.GetSender().Level())
require.NoError(t, err)
journaler := logging.MakeGrip(sender)
startAt := getNumber()
interceptor := MakeGripStreamInterceptor(journaler)
err = interceptor(test.action, &mockServerStream{}, &grpc.StreamServerInfo{}, mockStreamHandler)
if test.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, startAt+2, getNumber())
if assert.True(t, sender.HasMessage()) {
require.Equal(t, 2, sender.Len())
msg := sender.GetMessage()
assert.Equal(t, level.Info, msg.Priority)
msg = sender.GetMessage()
assert.Equal(t, expectedPriority(test.action), msg.Priority)
}
})
})
}
}
func expectedPriority(action string) level.Priority {
switch action {
case "error":
return level.Error
case "panic":
return level.Alert
default:
return level.Info
}
}