You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

349 lines
8.9 KiB

  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package status
  19. import (
  20. "context"
  21. "errors"
  22. "fmt"
  23. "reflect"
  24. "testing"
  25. "github.com/golang/protobuf/proto"
  26. "github.com/golang/protobuf/ptypes"
  27. apb "github.com/golang/protobuf/ptypes/any"
  28. dpb "github.com/golang/protobuf/ptypes/duration"
  29. cpb "google.golang.org/genproto/googleapis/rpc/code"
  30. epb "google.golang.org/genproto/googleapis/rpc/errdetails"
  31. spb "google.golang.org/genproto/googleapis/rpc/status"
  32. "google.golang.org/grpc/codes"
  33. )
  34. func TestErrorsWithSameParameters(t *testing.T) {
  35. const description = "some description"
  36. e1 := Errorf(codes.AlreadyExists, description)
  37. e2 := Errorf(codes.AlreadyExists, description)
  38. if e1 == e2 || !reflect.DeepEqual(e1, e2) {
  39. t.Fatalf("Errors should be equivalent but unique - e1: %v, %v e2: %p, %v", e1.(*statusError), e1, e2.(*statusError), e2)
  40. }
  41. }
  42. func TestFromToProto(t *testing.T) {
  43. s := &spb.Status{
  44. Code: int32(codes.Internal),
  45. Message: "test test test",
  46. Details: []*apb.Any{{TypeUrl: "foo", Value: []byte{3, 2, 1}}},
  47. }
  48. err := FromProto(s)
  49. if got := err.Proto(); !proto.Equal(s, got) {
  50. t.Fatalf("Expected errors to be identical - s: %v got: %v", s, got)
  51. }
  52. }
  53. func TestFromNilProto(t *testing.T) {
  54. tests := []*Status{nil, FromProto(nil)}
  55. for _, s := range tests {
  56. if c := s.Code(); c != codes.OK {
  57. t.Errorf("s: %v - Expected s.Code() = OK; got %v", s, c)
  58. }
  59. if m := s.Message(); m != "" {
  60. t.Errorf("s: %v - Expected s.Message() = \"\"; got %q", s, m)
  61. }
  62. if p := s.Proto(); p != nil {
  63. t.Errorf("s: %v - Expected s.Proto() = nil; got %q", s, p)
  64. }
  65. if e := s.Err(); e != nil {
  66. t.Errorf("s: %v - Expected s.Err() = nil; got %v", s, e)
  67. }
  68. }
  69. }
  70. func TestError(t *testing.T) {
  71. err := Error(codes.Internal, "test description")
  72. if got, want := err.Error(), "rpc error: code = Internal desc = test description"; got != want {
  73. t.Fatalf("err.Error() = %q; want %q", got, want)
  74. }
  75. s, _ := FromError(err)
  76. if got, want := s.Code(), codes.Internal; got != want {
  77. t.Fatalf("err.Code() = %s; want %s", got, want)
  78. }
  79. if got, want := s.Message(), "test description"; got != want {
  80. t.Fatalf("err.Message() = %s; want %s", got, want)
  81. }
  82. }
  83. func TestErrorOK(t *testing.T) {
  84. err := Error(codes.OK, "foo")
  85. if err != nil {
  86. t.Fatalf("Error(codes.OK, _) = %p; want nil", err.(*statusError))
  87. }
  88. }
  89. func TestErrorProtoOK(t *testing.T) {
  90. s := &spb.Status{Code: int32(codes.OK)}
  91. if got := ErrorProto(s); got != nil {
  92. t.Fatalf("ErrorProto(%v) = %v; want nil", s, got)
  93. }
  94. }
  95. func TestFromError(t *testing.T) {
  96. code, message := codes.Internal, "test description"
  97. err := Error(code, message)
  98. s, ok := FromError(err)
  99. if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
  100. t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
  101. }
  102. }
  103. func TestFromErrorOK(t *testing.T) {
  104. code, message := codes.OK, ""
  105. s, ok := FromError(nil)
  106. if !ok || s.Code() != code || s.Message() != message || s.Err() != nil {
  107. t.Fatalf("FromError(nil) = %v, %v; want <Code()=%s, Message()=%q, Err=nil>, true", s, ok, code, message)
  108. }
  109. }
  110. type customError struct {
  111. Code codes.Code
  112. Message string
  113. Details []*apb.Any
  114. }
  115. func (c customError) Error() string {
  116. return fmt.Sprintf("rpc error: code = %s desc = %s", c.Code, c.Message)
  117. }
  118. func (c customError) GRPCStatus() *Status {
  119. return &Status{
  120. s: &spb.Status{
  121. Code: int32(c.Code),
  122. Message: c.Message,
  123. Details: c.Details,
  124. },
  125. }
  126. }
  127. func TestFromErrorImplementsInterface(t *testing.T) {
  128. code, message := codes.Internal, "test description"
  129. details := []*apb.Any{{
  130. TypeUrl: "testUrl",
  131. Value: []byte("testValue"),
  132. }}
  133. err := customError{
  134. Code: code,
  135. Message: message,
  136. Details: details,
  137. }
  138. s, ok := FromError(err)
  139. if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
  140. t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
  141. }
  142. pd := s.Proto().GetDetails()
  143. if len(pd) != 1 || !reflect.DeepEqual(pd[0], details[0]) {
  144. t.Fatalf("s.Proto.GetDetails() = %v; want <Details()=%s>", pd, details)
  145. }
  146. }
  147. func TestFromErrorUnknownError(t *testing.T) {
  148. code, message := codes.Unknown, "unknown error"
  149. err := errors.New("unknown error")
  150. s, ok := FromError(err)
  151. if ok || s.Code() != code || s.Message() != message {
  152. t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q>, false", err, s, ok, code, message)
  153. }
  154. }
  155. func TestConvertKnownError(t *testing.T) {
  156. code, message := codes.Internal, "test description"
  157. err := Error(code, message)
  158. s := Convert(err)
  159. if s.Code() != code || s.Message() != message {
  160. t.Fatalf("Convert(%v) = %v; want <Code()=%s, Message()=%q>", err, s, code, message)
  161. }
  162. }
  163. func TestConvertUnknownError(t *testing.T) {
  164. code, message := codes.Unknown, "unknown error"
  165. err := errors.New("unknown error")
  166. s := Convert(err)
  167. if s.Code() != code || s.Message() != message {
  168. t.Fatalf("Convert(%v) = %v; want <Code()=%s, Message()=%q>", err, s, code, message)
  169. }
  170. }
  171. func TestStatus_ErrorDetails(t *testing.T) {
  172. tests := []struct {
  173. code codes.Code
  174. details []proto.Message
  175. }{
  176. {
  177. code: codes.NotFound,
  178. details: nil,
  179. },
  180. {
  181. code: codes.NotFound,
  182. details: []proto.Message{
  183. &epb.ResourceInfo{
  184. ResourceType: "book",
  185. ResourceName: "projects/1234/books/5678",
  186. Owner: "User",
  187. },
  188. },
  189. },
  190. {
  191. code: codes.Internal,
  192. details: []proto.Message{
  193. &epb.DebugInfo{
  194. StackEntries: []string{
  195. "first stack",
  196. "second stack",
  197. },
  198. },
  199. },
  200. },
  201. {
  202. code: codes.Unavailable,
  203. details: []proto.Message{
  204. &epb.RetryInfo{
  205. RetryDelay: &dpb.Duration{Seconds: 60},
  206. },
  207. &epb.ResourceInfo{
  208. ResourceType: "book",
  209. ResourceName: "projects/1234/books/5678",
  210. Owner: "User",
  211. },
  212. },
  213. },
  214. }
  215. for _, tc := range tests {
  216. s, err := New(tc.code, "").WithDetails(tc.details...)
  217. if err != nil {
  218. t.Fatalf("(%v).WithDetails(%+v) failed: %v", str(s), tc.details, err)
  219. }
  220. details := s.Details()
  221. for i := range details {
  222. if !proto.Equal(details[i].(proto.Message), tc.details[i]) {
  223. t.Fatalf("(%v).Details()[%d] = %+v, want %+v", str(s), i, details[i], tc.details[i])
  224. }
  225. }
  226. }
  227. }
  228. func TestStatus_WithDetails_Fail(t *testing.T) {
  229. tests := []*Status{
  230. nil,
  231. FromProto(nil),
  232. New(codes.OK, ""),
  233. }
  234. for _, s := range tests {
  235. if s, err := s.WithDetails(); err == nil || s != nil {
  236. t.Fatalf("(%v).WithDetails(%+v) = %v, %v; want nil, non-nil", str(s), []proto.Message{}, s, err)
  237. }
  238. }
  239. }
  240. func TestStatus_ErrorDetails_Fail(t *testing.T) {
  241. tests := []struct {
  242. s *Status
  243. i []interface{}
  244. }{
  245. {
  246. nil,
  247. nil,
  248. },
  249. {
  250. FromProto(nil),
  251. nil,
  252. },
  253. {
  254. New(codes.OK, ""),
  255. []interface{}{},
  256. },
  257. {
  258. FromProto(&spb.Status{
  259. Code: int32(cpb.Code_CANCELLED),
  260. Details: []*apb.Any{
  261. {
  262. TypeUrl: "",
  263. Value: []byte{},
  264. },
  265. mustMarshalAny(&epb.ResourceInfo{
  266. ResourceType: "book",
  267. ResourceName: "projects/1234/books/5678",
  268. Owner: "User",
  269. }),
  270. },
  271. }),
  272. []interface{}{
  273. errors.New(`message type url "" is invalid`),
  274. &epb.ResourceInfo{
  275. ResourceType: "book",
  276. ResourceName: "projects/1234/books/5678",
  277. Owner: "User",
  278. },
  279. },
  280. },
  281. }
  282. for _, tc := range tests {
  283. got := tc.s.Details()
  284. if !reflect.DeepEqual(got, tc.i) {
  285. t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
  286. }
  287. }
  288. }
  289. func str(s *Status) string {
  290. if s == nil {
  291. return "nil"
  292. }
  293. if s.s == nil {
  294. return "<Code=OK>"
  295. }
  296. return fmt.Sprintf("<Code=%v, Message=%q, Details=%+v>", codes.Code(s.s.GetCode()), s.s.GetMessage(), s.s.GetDetails())
  297. }
  298. // mustMarshalAny converts a protobuf message to an any.
  299. func mustMarshalAny(msg proto.Message) *apb.Any {
  300. any, err := ptypes.MarshalAny(msg)
  301. if err != nil {
  302. panic(fmt.Sprintf("ptypes.MarshalAny(%+v) failed: %v", msg, err))
  303. }
  304. return any
  305. }
  306. func TestFromContextError(t *testing.T) {
  307. testCases := []struct {
  308. in error
  309. want *Status
  310. }{
  311. {in: nil, want: New(codes.OK, "")},
  312. {in: context.DeadlineExceeded, want: New(codes.DeadlineExceeded, context.DeadlineExceeded.Error())},
  313. {in: context.Canceled, want: New(codes.Canceled, context.Canceled.Error())},
  314. {in: errors.New("other"), want: New(codes.Unknown, "other")},
  315. }
  316. for _, tc := range testCases {
  317. got := FromContextError(tc.in)
  318. if got.Code() != tc.want.Code() || got.Message() != tc.want.Message() {
  319. t.Errorf("FromContextError(%v) = %v; want %v", tc.in, got, tc.want)
  320. }
  321. }
  322. }