forked from icco/logrus-stackdriver-formatter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuite_test.go
211 lines (179 loc) · 5.33 KB
/
suite_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
package logadapter_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
logadapter "github.com/StevenACoffman/logrus-stackdriver-formatter"
"github.com/StevenACoffman/logrus-stackdriver-formatter/ctxlogrus"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
)
type grpcTestSuite struct {
*grpc_testing.InterceptorTestSuite
mutexBuffer *grpc_testing.MutexReadWriter
buffer *bytes.Buffer
logger *logrus.Logger
}
func newGRPCTestSuite(t *testing.T) *grpcTestSuite {
b := &bytes.Buffer{}
muB := grpc_testing.NewMutexReadWriter(b)
logger := logrus.New()
logger.Formatter = logadapter.NewFormatter(
logadapter.WithProjectID("test-project"),
logadapter.WithService("logging-test"),
logadapter.WithVersion("v1.0.0"),
logadapter.WithStackTraceStyle(logadapter.TraceInPayload),
logadapter.WithSourceReference(
"github.com/StevenACoffman/logrus-stackdriver-formatter",
"v1.0.0",
),
logadapter.WithPrettyPrint(),
)
var out io.Writer
out = muB
if testing.Verbose() {
out = io.MultiWriter(os.Stdout, muB)
}
logger.Out = out
return &grpcTestSuite{
logger: logger,
buffer: b,
mutexBuffer: muB,
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
TestService: &loggingPingService{&grpc_testing.TestPingService{T: t}},
},
}
}
func (s *grpcTestSuite) SetupTest() {
s.mutexBuffer.Lock()
s.buffer.Reset()
s.mutexBuffer.Unlock()
}
var goodPing = &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}
type loggingPingService struct {
pb_testproto.TestServiceServer
}
func (s *loggingPingService) Ping(
ctx context.Context,
ping *pb_testproto.PingRequest,
) (*pb_testproto.PingResponse, error) {
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
ctxlogrus.AddFields(ctx, logrus.Fields{"custom_field": "custom_value"})
ctxlogrus.Extract(ctx).Info("some ping")
if ping.Value == "pls panic" {
panic("test panic RPC")
}
return s.TestServiceServer.Ping(ctx, ping)
}
func (s *loggingPingService) PingError(
ctx context.Context,
ping *pb_testproto.PingRequest,
) (*pb_testproto.Empty, error) {
empty, err := s.TestServiceServer.PingError(ctx, ping)
if ping.Value == "stack" {
st := errors.WithStack(err)
ctxlogrus.Extract(ctx).WithError(st).Error("error with stack")
}
return empty, err
}
func (s *loggingPingService) PingList(
ping *pb_testproto.PingRequest,
stream pb_testproto.TestService_PingListServer,
) error {
grpc_ctxtags.Extract(stream.Context()).
Set("custom_tags.string", "something").
Set("custom_tags.int", 1337)
ctxlogrus.AddFields(stream.Context(), logrus.Fields{"custom_field": "custom_value"})
ctxlogrus.Extract(stream.Context()).Info("some pinglist")
return s.TestServiceServer.PingList(ping, stream)
}
func (s *loggingPingService) PingEmpty(
ctx context.Context,
empty *pb_testproto.Empty,
) (*pb_testproto.PingResponse, error) {
return s.TestServiceServer.PingEmpty(ctx, empty)
}
func (s *grpcTestSuite) getOutputJSONs() []map[string]interface{} {
ret := make([]map[string]interface{}, 0)
dec := json.NewDecoder(s.mutexBuffer)
for {
var val map[string]interface{}
err := dec.Decode(&val)
if err == io.EOF {
break
}
if err != nil {
s.T().Fatalf("failed decoding output from Logrus JSON: %v", err)
}
ret = append(ret, val)
}
return ret
}
type httpTestSuite struct {
suite.Suite
server *httptest.Server
mux *http.ServeMux
Client *http.Client
mutexBuffer *grpc_testing.MutexReadWriter
buffer *bytes.Buffer
logger *logrus.Logger
}
func newHTTPTestSuite(t *testing.T) *httpTestSuite {
b := &bytes.Buffer{}
muB := grpc_testing.NewMutexReadWriter(b)
logger := logrus.New()
logger.Formatter = logadapter.NewFormatter(
logadapter.WithProjectID("test-project"),
logadapter.WithService("logging-test"),
logadapter.WithVersion("v1.0.0"),
logadapter.WithStackTraceStyle(logadapter.TraceInPayload),
logadapter.WithSourceReference(
"github.com/StevenACoffman/logrus-stackdriver-formatter",
"v1.0.0",
),
logadapter.WithPrettyPrint(),
)
var out io.Writer
out = muB
if testing.Verbose() {
out = io.MultiWriter(os.Stdout, muB)
}
logger.Out = out
return &httpTestSuite{
logger: logger,
buffer: b,
mutexBuffer: muB,
Suite: suite.Suite{},
}
}
func (s *httpTestSuite) SetupSuite() {
s.mux = http.NewServeMux()
apiHandler := http.NewServeMux()
apiHandler.Handle("/", s.mux)
recoveryHandler := logadapter.RecoveryMiddleware(apiHandler)
loggingHandler := logadapter.LoggingMiddleware(s.logger)(recoveryHandler)
s.server = httptest.NewTLSServer(loggingHandler)
s.Client = s.server.Client()
s.mux.HandleFunc("/panic", s.ServePanic)
s.mux.HandleFunc("/logging", s.ServeLogging)
}
func (s *httpTestSuite) TearDownSuite() {
s.server.Close()
}
func (s *httpTestSuite) ServePanic(w http.ResponseWriter, r *http.Request) {
panic("surprise panic attack!")
}
func (s *httpTestSuite) ServeLogging(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctxlogrus.AddFields(ctx, logrus.Fields{"testField": "testValue"})
ctxlogrus.Extract(ctx).Info("served from http request")
}