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
|
package gitaly
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/grpctool/test"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
_ error = &Error{}
)
func TestErrorUnwrap(t *testing.T) {
e := &Error{
Code: RPCError,
Cause: context.Canceled,
Message: "bla",
}
assert.Equal(t, context.Canceled, e.Unwrap())
assert.ErrorIs(t, e, context.Canceled)
}
func TestErrorString(t *testing.T) {
e := &Error{
Code: RPCError,
Message: "bla",
}
assert.EqualError(t, e, "RPCError: bla")
e = &Error{
Code: RPCError,
Cause: context.Canceled,
Message: "bla",
}
assert.EqualError(t, e, "RPCError: bla: context canceled")
e = &Error{
Code: RPCError,
Cause: context.Canceled,
Message: "bla",
Path: "path",
}
assert.EqualError(t, e, "RPCError: bla: path: context canceled")
e = &Error{
Code: RPCError,
Message: "bla",
Path: "path",
}
assert.EqualError(t, e, "RPCError: bla: path")
e = &Error{
Code: RPCError,
Cause: context.Canceled,
Message: "bla",
RPCName: "GetFoo",
Path: "path",
}
assert.EqualError(t, e, "RPCError: GetFoo: bla: path: context canceled")
e = &Error{
Code: RPCError,
Message: "bla",
RPCName: "GetFoo",
Path: "path",
}
assert.EqualError(t, e, "RPCError: GetFoo: bla: path")
e = &Error{
Code: RPCError,
Message: "bla",
Path: "path",
}
assert.EqualError(t, e, "RPCError: bla: path")
}
func TestUnknownErrorCode(t *testing.T) {
var e ErrorCode = -1
assert.Equal(t, "invalid ErrorCode: -1", e.String())
}
func TestErrorCodeFromError(t *testing.T) {
e := &Error{
Code: RPCError,
}
assert.Equal(t, RPCError, ErrorCodeFromError(e))
err := fmt.Errorf("%w", e)
assert.Equal(t, RPCError, ErrorCodeFromError(err))
err = errors.New("bla")
assert.Equal(t, UnknownError, ErrorCodeFromError(err))
}
func TestErrorToGrpcError(t *testing.T) {
e := &Error{
Code: RPCError,
Cause: status.Error(codes.DataLoss, "oh no"),
Message: "msg",
RPCName: test.Testing_RequestResponse_FullMethodName,
Path: "path",
}
s, ok := status.FromError(e)
require.True(t, ok)
assert.Equal(t, codes.DataLoss, s.Code())
assert.Equal(t, "RPCError: /gitlab.agent.grpctool.test.Testing/RequestResponse: msg: path: rpc error: code = DataLoss desc = oh no", s.Message())
assert.EqualError(t, s.Err(), "rpc error: code = DataLoss desc = RPCError: /gitlab.agent.grpctool.test.Testing/RequestResponse: msg: path: rpc error: code = DataLoss desc = oh no")
}
|