util_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v3rpc
  15. import (
  16. "context"
  17. "errors"
  18. "testing"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. "github.com/coreos/etcd/mvcc"
  21. "google.golang.org/grpc/codes"
  22. "google.golang.org/grpc/status"
  23. )
  24. func TestGRPCError(t *testing.T) {
  25. tt := []struct {
  26. err error
  27. exp error
  28. }{
  29. {err: mvcc.ErrCompacted, exp: rpctypes.ErrGRPCCompacted},
  30. {err: mvcc.ErrFutureRev, exp: rpctypes.ErrGRPCFutureRev},
  31. {err: context.Canceled, exp: context.Canceled},
  32. {err: context.DeadlineExceeded, exp: context.DeadlineExceeded},
  33. {err: errors.New("foo"), exp: status.Error(codes.Unknown, "foo")},
  34. }
  35. for i := range tt {
  36. if err := togRPCError(tt[i].err); err != tt[i].exp {
  37. if _, ok := status.FromError(err); ok {
  38. if err.Error() == tt[i].exp.Error() {
  39. continue
  40. }
  41. }
  42. t.Errorf("#%d: got %v, expected %v", i, err, tt[i].exp)
  43. }
  44. }
  45. }