Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/integration/v3_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestV3AuthEmptyUserGet(t *testing.T) {
authSetupRoot(t, api.Auth)

_, err := api.KV.Range(ctx, &pb.RangeRequest{Key: []byte("abc")})
require.Truef(t, eqErrGRPC(err, rpctypes.ErrUserEmpty), "got %v, expected %v", err, rpctypes.ErrUserEmpty)
require.Truef(t, eqErrGRPC(err, rpctypes.ErrGRPCUserEmpty), "got %v, expected %v", err, rpctypes.ErrGRPCUserEmpty)
}

// TestV3AuthEmptyUserPut ensures that a put with an empty user will return an empty user error,
Expand All @@ -68,7 +68,7 @@ func TestV3AuthEmptyUserPut(t *testing.T) {
// cluster terminating.
for i := 0; i < 10; i++ {
_, err := api.KV.Put(ctx, &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
require.Truef(t, eqErrGRPC(err, rpctypes.ErrUserEmpty), "got %v, expected %v", err, rpctypes.ErrUserEmpty)
require.Truef(t, eqErrGRPC(err, rpctypes.ErrGRPCUserEmpty), "got %v, expected %v", err, rpctypes.ErrGRPCUserEmpty)
}
}

Expand Down
127 changes: 126 additions & 1 deletion tests/integration/v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1961,8 +1961,133 @@ func TestV3AdditionalGRPCOptions(t *testing.T) {
}
}

// eqErrGRPC validates both inputs must be gRPC status errors
// (e.g., rpctypes.ErrGRPCXxx) or nil, and then compares them.
Comment on lines +1964 to +1965
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of writing comments improve function name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't come up with a better name.

  • matchGRPCErrors
  • errorsEqualByGRPCStatus
  • mustEqualGRPCErrors
  • mustGRPCErrorsEqual
  • verifyGRPCErrorsEqual
  • requireGRPCErrorsEqual
  • validateAndCompareGRPCErrors
  • grpcStatusCodeAndMessageEqual

func eqErrGRPC(err1 error, err2 error) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is badly designed, it promises too much, validates too little. Instead of writing a essay on how to use a test function, just panic if user provides incorrect input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Refactored to just panic. And removed my essay.

return !(err1 == nil && err2 != nil) || err1.Error() == err2.Error()
s1 := mustGRPCStatus(err1)
s2 := mustGRPCStatus(err2)
return s1.Code() == s2.Code() && s1.Message() == s2.Message()
}

func mustGRPCStatus(err error) *status.Status {
if err == nil {
return status.New(codes.OK, "")
}
s, ok := status.FromError(err)
if !ok {
panic(fmt.Sprintf("eqErrGRPC: not a gRPC status error: %T %v", err, err))
}
return s
}

func TestEqErrGRPC(t *testing.T) {
tests := []struct {
name string
err1 error
err2 error
expected bool
}{
{
name: "both nil",
err1: nil,
err2: nil,
expected: true,
},
{
name: "first nil, second non-nil",
err1: nil,
err2: rpctypes.ErrGRPCLeaseExist,
expected: false,
},
{
name: "first non-nil, second nil",
err1: rpctypes.ErrGRPCLeaseExist,
err2: nil,
expected: false,
},
{
name: "same error - same object",
err1: rpctypes.ErrGRPCLeaseExist,
err2: rpctypes.ErrGRPCLeaseExist,
expected: true,
},
// Wire reconstruction: different objects with same code and message
{
name: "wire reconstruction - same code and message",
err1: status.Error(codes.FailedPrecondition, "etcdserver: lease already exists"),
err2: rpctypes.ErrGRPCLeaseExist,
expected: true,
},
// Code/message permutation tests
{
name: "same code, same message",
err1: status.Error(codes.FailedPrecondition, "error message"),
err2: status.Error(codes.FailedPrecondition, "error message"),
expected: true,
},
{
name: "same code, different message",
err1: status.Error(codes.FailedPrecondition, "error A"),
err2: status.Error(codes.FailedPrecondition, "error B"),
expected: false,
},
{
name: "different code, same message",
err1: status.Error(codes.FailedPrecondition, "same message"),
err2: status.Error(codes.NotFound, "same message"),
expected: false,
},
{
name: "different code, different message",
err1: rpctypes.ErrGRPCLeaseExist,
err2: rpctypes.ErrGRPCLeaseNotFound,
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := eqErrGRPC(tt.err1, tt.err2)
if result != tt.expected {
t.Errorf("eqErrGRPC(%v, %v) = %v, want %v", tt.err1, tt.err2, result, tt.expected)
}
})
}
}

func TestEqErrGRPCPanicsOnNonGRPCError(t *testing.T) {
tests := []struct {
name string
err1 error
err2 error
}{
{
name: "plain error vs gRPC error",
err1: errors.New("plain error"),
err2: rpctypes.ErrGRPCLeaseExist,
},
{
name: "gRPC error vs plain error",
err1: rpctypes.ErrGRPCLeaseExist,
err2: errors.New("plain error"),
},
{
name: "client-side EtcdError vs gRPC error",
err1: rpctypes.ErrUserEmpty,
err2: rpctypes.ErrGRPCUserEmpty,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("eqErrGRPC(%v, %v) should panic but didn't", tt.err1, tt.err2)
}
}()
eqErrGRPC(tt.err1, tt.err2)
})
}
}

// waitForRestart tries a range request until the client's server responds.
Expand Down