OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / status / status_test.go
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
19 package status
20
21 import (
22         "errors"
23         "fmt"
24         "reflect"
25         "testing"
26
27         "github.com/golang/protobuf/proto"
28         "github.com/golang/protobuf/ptypes"
29         apb "github.com/golang/protobuf/ptypes/any"
30         dpb "github.com/golang/protobuf/ptypes/duration"
31         cpb "google.golang.org/genproto/googleapis/rpc/code"
32         epb "google.golang.org/genproto/googleapis/rpc/errdetails"
33         spb "google.golang.org/genproto/googleapis/rpc/status"
34         "google.golang.org/grpc/codes"
35 )
36
37 func TestErrorsWithSameParameters(t *testing.T) {
38         const description = "some description"
39         e1 := Errorf(codes.AlreadyExists, description)
40         e2 := Errorf(codes.AlreadyExists, description)
41         if e1 == e2 || !reflect.DeepEqual(e1, e2) {
42                 t.Fatalf("Errors should be equivalent but unique - e1: %v, %v  e2: %p, %v", e1.(*statusError), e1, e2.(*statusError), e2)
43         }
44 }
45
46 func TestFromToProto(t *testing.T) {
47         s := &spb.Status{
48                 Code:    int32(codes.Internal),
49                 Message: "test test test",
50                 Details: []*apb.Any{{TypeUrl: "foo", Value: []byte{3, 2, 1}}},
51         }
52
53         err := FromProto(s)
54         if got := err.Proto(); !proto.Equal(s, got) {
55                 t.Fatalf("Expected errors to be identical - s: %v  got: %v", s, got)
56         }
57 }
58
59 func TestFromNilProto(t *testing.T) {
60         tests := []*Status{nil, FromProto(nil)}
61         for _, s := range tests {
62                 if c := s.Code(); c != codes.OK {
63                         t.Errorf("s: %v - Expected s.Code() = OK; got %v", s, c)
64                 }
65                 if m := s.Message(); m != "" {
66                         t.Errorf("s: %v - Expected s.Message() = \"\"; got %q", s, m)
67                 }
68                 if p := s.Proto(); p != nil {
69                         t.Errorf("s: %v - Expected s.Proto() = nil; got %q", s, p)
70                 }
71                 if e := s.Err(); e != nil {
72                         t.Errorf("s: %v - Expected s.Err() = nil; got %v", s, e)
73                 }
74         }
75 }
76
77 func TestError(t *testing.T) {
78         err := Error(codes.Internal, "test description")
79         if got, want := err.Error(), "rpc error: code = Internal desc = test description"; got != want {
80                 t.Fatalf("err.Error() = %q; want %q", got, want)
81         }
82         s, _ := FromError(err)
83         if got, want := s.Code(), codes.Internal; got != want {
84                 t.Fatalf("err.Code() = %s; want %s", got, want)
85         }
86         if got, want := s.Message(), "test description"; got != want {
87                 t.Fatalf("err.Message() = %s; want %s", got, want)
88         }
89 }
90
91 func TestErrorOK(t *testing.T) {
92         err := Error(codes.OK, "foo")
93         if err != nil {
94                 t.Fatalf("Error(codes.OK, _) = %p; want nil", err.(*statusError))
95         }
96 }
97
98 func TestErrorProtoOK(t *testing.T) {
99         s := &spb.Status{Code: int32(codes.OK)}
100         if got := ErrorProto(s); got != nil {
101                 t.Fatalf("ErrorProto(%v) = %v; want nil", s, got)
102         }
103 }
104
105 func TestFromError(t *testing.T) {
106         code, message := codes.Internal, "test description"
107         err := Error(code, message)
108         s, ok := FromError(err)
109         if !ok || s.Code() != code || s.Message() != message || s.Err() == nil {
110                 t.Fatalf("FromError(%v) = %v, %v; want <Code()=%s, Message()=%q, Err()!=nil>, true", err, s, ok, code, message)
111         }
112 }
113
114 func TestFromErrorOK(t *testing.T) {
115         code, message := codes.OK, ""
116         s, ok := FromError(nil)
117         if !ok || s.Code() != code || s.Message() != message || s.Err() != nil {
118                 t.Fatalf("FromError(nil) = %v, %v; want <Code()=%s, Message()=%q, Err=nil>, true", s, ok, code, message)
119         }
120 }
121
122 func TestStatus_ErrorDetails(t *testing.T) {
123         tests := []struct {
124                 code    codes.Code
125                 details []proto.Message
126         }{
127                 {
128                         code:    codes.NotFound,
129                         details: nil,
130                 },
131                 {
132                         code: codes.NotFound,
133                         details: []proto.Message{
134                                 &epb.ResourceInfo{
135                                         ResourceType: "book",
136                                         ResourceName: "projects/1234/books/5678",
137                                         Owner:        "User",
138                                 },
139                         },
140                 },
141                 {
142                         code: codes.Internal,
143                         details: []proto.Message{
144                                 &epb.DebugInfo{
145                                         StackEntries: []string{
146                                                 "first stack",
147                                                 "second stack",
148                                         },
149                                 },
150                         },
151                 },
152                 {
153                         code: codes.Unavailable,
154                         details: []proto.Message{
155                                 &epb.RetryInfo{
156                                         RetryDelay: &dpb.Duration{Seconds: 60},
157                                 },
158                                 &epb.ResourceInfo{
159                                         ResourceType: "book",
160                                         ResourceName: "projects/1234/books/5678",
161                                         Owner:        "User",
162                                 },
163                         },
164                 },
165         }
166
167         for _, tc := range tests {
168                 s, err := New(tc.code, "").WithDetails(tc.details...)
169                 if err != nil {
170                         t.Fatalf("(%v).WithDetails(%+v) failed: %v", str(s), tc.details, err)
171                 }
172                 details := s.Details()
173                 for i := range details {
174                         if !proto.Equal(details[i].(proto.Message), tc.details[i]) {
175                                 t.Fatalf("(%v).Details()[%d] = %+v, want %+v", str(s), i, details[i], tc.details[i])
176                         }
177                 }
178         }
179 }
180
181 func TestStatus_WithDetails_Fail(t *testing.T) {
182         tests := []*Status{
183                 nil,
184                 FromProto(nil),
185                 New(codes.OK, ""),
186         }
187         for _, s := range tests {
188                 if s, err := s.WithDetails(); err == nil || s != nil {
189                         t.Fatalf("(%v).WithDetails(%+v) = %v, %v; want nil, non-nil", str(s), []proto.Message{}, s, err)
190                 }
191         }
192 }
193
194 func TestStatus_ErrorDetails_Fail(t *testing.T) {
195         tests := []struct {
196                 s *Status
197                 i []interface{}
198         }{
199                 {
200                         nil,
201                         nil,
202                 },
203                 {
204                         FromProto(nil),
205                         nil,
206                 },
207                 {
208                         New(codes.OK, ""),
209                         []interface{}{},
210                 },
211                 {
212                         FromProto(&spb.Status{
213                                 Code: int32(cpb.Code_CANCELLED),
214                                 Details: []*apb.Any{
215                                         {
216                                                 TypeUrl: "",
217                                                 Value:   []byte{},
218                                         },
219                                         mustMarshalAny(&epb.ResourceInfo{
220                                                 ResourceType: "book",
221                                                 ResourceName: "projects/1234/books/5678",
222                                                 Owner:        "User",
223                                         }),
224                                 },
225                         }),
226                         []interface{}{
227                                 errors.New(`message type url "" is invalid`),
228                                 &epb.ResourceInfo{
229                                         ResourceType: "book",
230                                         ResourceName: "projects/1234/books/5678",
231                                         Owner:        "User",
232                                 },
233                         },
234                 },
235         }
236         for _, tc := range tests {
237                 got := tc.s.Details()
238                 if !reflect.DeepEqual(got, tc.i) {
239                         t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
240                 }
241         }
242 }
243
244 func str(s *Status) string {
245         if s == nil {
246                 return "nil"
247         }
248         if s.s == nil {
249                 return "<Code=OK>"
250         }
251         return fmt.Sprintf("<Code=%v, Message=%q, Details=%+v>", codes.Code(s.s.GetCode()), s.s.GetMessage(), s.s.GetDetails())
252 }
253
254 // mustMarshalAny converts a protobuf message to an any.
255 func mustMarshalAny(msg proto.Message) *apb.Any {
256         any, err := ptypes.MarshalAny(msg)
257         if err != nil {
258                 panic(fmt.Sprintf("ptypes.MarshalAny(%+v) failed: %v", msg, err))
259         }
260         return any
261 }