mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
grpc.go
278 lines (242 loc) · 9.79 KB
/
grpc.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package assertions
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/smartystreets/assertions"
"github.com/smartystreets/goconvey/convey"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.chromium.org/luci/common/data/stringset"
"go.chromium.org/luci/grpc/appstatus"
"go.chromium.org/luci/grpc/grpcutil"
)
// ShouldHaveAppStatus asserts that error `actual` has an
// application-specific status and it matches the expectations.
// See ShouldBeLikeStatus for the format of `expected`.
// See appstatus package for application-specific statuses.
func ShouldHaveAppStatus(actual interface{}, expected ...interface{}) string {
if ret := assertions.ShouldImplement(actual, (*error)(nil)); ret != "" {
return ret
}
actualStatus, ok := appstatus.Get(actual.(error))
if !ok {
return fmt.Sprintf("expected error %q to have an explicit application status", actual)
}
return ShouldBeLikeStatus(actualStatus, expected...)
}
// ShouldHaveRPCCode is a goconvey assertion, asserting that the supplied
// "actual" value has a gRPC code value and, optionally, errors like a supplied
// message string.
//
// If no "expected" arguments are supplied, ShouldHaveRPCCode will assert that
// the result is codes.OK.
//
// The first "expected" argument, if supplied, is the gRPC codes.Code to assert.
//
// A second "expected" string may be optionally included. If included, the
// gRPC error message is asserted to contain the expected string using
// convey.ShouldContainSubstring.
func ShouldHaveRPCCode(actual interface{}, expected ...interface{}) string {
aerr, ok := actual.(error)
if !(ok || actual == nil) {
return "actual argument must be an error."
}
var (
ecode codes.Code
errLike string
)
switch len(expected) {
case 2:
var ok bool
if errLike, ok = expected[1].(string); !ok {
return fmt.Sprintf("The expected error substring must be a string, not a %T", expected[1])
}
fallthrough
case 1:
var ok bool
if ecode, ok = expected[0].(codes.Code); !ok {
return fmt.Sprintf("The code must be a codes.Code, not a %T", expected[0])
}
case 0:
ecode = codes.OK
default:
return "Expected argument must have the form: [codes.Code[string]]"
}
if acode := grpcutil.Code(aerr); acode != ecode {
return fmt.Sprintf("expected gRPC code %q (%d), not %q (%d), type %T: %v",
ecode, ecode, acode, acode, actual, actual)
}
if errLike != "" {
return convey.ShouldContainSubstring(grpc.ErrorDesc(aerr), errLike)
}
return ""
}
// ShouldBeRPCOK asserts that "actual" is an error that has a gRPC code value
// of codes.OK.
//
// Note that "nil" has an codes.OK value.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCOK(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.OK, expected)...)
}
// ShouldBeRPCInvalidArgument asserts that "actual" is an error that has a gRPC
// code value of codes.InvalidArgument.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCInvalidArgument(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.InvalidArgument, expected)...)
}
// ShouldBeRPCInternal asserts that "actual" is an error that has a gRPC code
// value of codes.Internal.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCInternal(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.Internal, expected)...)
}
// ShouldBeRPCUnknown asserts that "actual" is an error that has a gRPC code
// value of codes.Unknown.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCUnknown(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.Unknown, expected)...)
}
// ShouldBeRPCNotFound asserts that "actual" is an error that has a gRPC code
// value of codes.NotFound.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCNotFound(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.NotFound, expected)...)
}
// ShouldBeRPCPermissionDenied asserts that "actual" is an error that has a gRPC
// code value of codes.PermissionDenied.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCPermissionDenied(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.PermissionDenied, expected)...)
}
// ShouldBeRPCAlreadyExists asserts that "actual" is an error that has a gRPC
// code value of codes.AlreadyExists.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCAlreadyExists(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.AlreadyExists, expected)...)
}
// ShouldBeRPCUnauthenticated asserts that "actual" is an error that has a gRPC
// code value of codes.Unauthenticated.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCUnauthenticated(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.Unauthenticated, expected)...)
}
// ShouldBeRPCFailedPrecondition asserts that "actual" is an error that has a gRPC
// code value of codes.FailedPrecondition.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCFailedPrecondition(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.FailedPrecondition, expected)...)
}
// ShouldBeRPCAborted asserts that "actual" is an error that has a gRPC
// code value of codes.Aborted.
//
// One additional "expected" string may be optionally included. If included, the
// gRPC error's message is asserted to contain the expected string.
func ShouldBeRPCAborted(actual interface{}, expected ...interface{}) string {
return ShouldHaveRPCCode(actual, prepend(codes.Aborted, expected)...)
}
func prepend(c codes.Code, exp []interface{}) []interface{} {
args := make([]interface{}, len(exp)+1)
args[0] = c
copy(args[1:], exp)
return args
}
// ShouldBeLikeStatus asserts that *status.Status `actual` has code
// `expected[0]`, that the actual message has a substring `expected[1]` and
// that the status details in expected[2:] as present in the actual status.
//
// len(expected) must be at least 1.
//
// Example:
// // err must have a NotFound status
// So(s, ShouldBeLikeStatus, codes.NotFound)
//
// // and its message must contain "item not found"
// So(s, ShouldBeLikeStatus, codes.NotFound, "item not found")
//
// // and it must have a DebugInfo detail.
// So(s, ShouldBeLikeStatus, codes.NotFound, "item not found", &errdetails.DebugInfo{Details: "x"})
func ShouldBeLikeStatus(actual interface{}, expected ...interface{}) string {
if ret := assertions.ShouldHaveSameTypeAs(actual, (*status.Status)(nil)); ret != "" {
return ret
}
if ret := assertions.ShouldNotBeEmpty(expected); ret != "" {
return ret
}
actualStatus := actual.(*status.Status)
if ret := assertions.ShouldEqual(actualStatus.Code(), expected[0]); ret != "" {
return ret
}
if len(expected) == 1 {
return ""
}
if ret := assertions.ShouldContainSubstring(actualStatus.Message(), expected[1]); ret != "" {
return ret
}
if len(expected) == 2 {
return ""
}
// Serialize actual details to strings as compact text proto.
actualDetails := actualStatus.Details()
presentDetails := stringset.New(len(actualDetails))
for _, d := range actualDetails {
presentDetails.Add(proto.CompactTextString(d.(proto.Message)))
}
// Then assert presence of each expected detail.
for _, d := range expected[2:] {
if ret := assertions.ShouldImplement(d, (*proto.Message)(nil)); ret != "" {
return ret
}
eTxt := proto.CompactTextString(d.(proto.Message))
if !presentDetails.Has(eTxt) {
return fmt.Sprintf("expected presence of status detail %q, got %q", eTxt, presentDetails.ToSlice())
}
}
return ""
}
// ShouldHaveGRPCStatus asserts that error `actual` has a GRPC status and it
// matches the expectations.
// See ShouldBeStatusLike for the format of `expected`.
// The status is extracted using status.FromError.
func ShouldHaveGRPCStatus(actual interface{}, expected ...interface{}) string {
if ret := assertions.ShouldImplement(actual, (*error)(nil)); ret != "" {
return ret
}
actualStatus, ok := status.FromError(actual.(error))
if !ok {
return fmt.Sprintf("expected error %q to have a GRPC status", actual)
}
return ShouldBeLikeStatus(actualStatus, expected...)
}