Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mock server suite base #412

Merged
merged 1 commit into from
Mar 6, 2023
Merged
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
24 changes: 12 additions & 12 deletions client/client_grpc_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestGrpcCreateAlias(t *testing.T) {

t.Run("normal create alias", func(t *testing.T) {

mock.SetInjection(MCreateAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MCreateAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
req, ok := raw.(*server.CreateAliasRequest)
if !ok {
t.FailNow()
Expand All @@ -43,14 +43,14 @@ func TestGrpcCreateAlias(t *testing.T) {

return &common.Status{ErrorCode: common.ErrorCode_Success}, nil
})
defer mock.DelInjection(MCreateAlias)
defer mockServer.DelInjection(MCreateAlias)
err := c.CreateAlias(ctx, "testcoll", "collAlias")
assert.NoError(t, err)
})

t.Run("alias duplicated", func(t *testing.T) {
m := make(map[string]struct{})
mock.SetInjection(MCreateAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MCreateAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
req, ok := raw.(*server.CreateAliasRequest)
if !ok {
t.FailNow()
Expand All @@ -63,7 +63,7 @@ func TestGrpcCreateAlias(t *testing.T) {
m[req.GetAlias()] = struct{}{}
return &common.Status{ErrorCode: status}, nil
})
defer mock.DelInjection(MCreateAlias)
defer mockServer.DelInjection(MCreateAlias)

collName := "testColl"
aliasName := "collAlias"
Expand All @@ -80,7 +80,7 @@ func TestGrpcDropAlias(t *testing.T) {
defer c.Close()

t.Run("normal drop alias", func(t *testing.T) {
mock.SetInjection(MDropAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MDropAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
req, ok := raw.(*server.DropAliasRequest)
if !ok {
t.FailNow()
Expand All @@ -89,13 +89,13 @@ func TestGrpcDropAlias(t *testing.T) {

return &common.Status{ErrorCode: common.ErrorCode_Success}, nil
})
defer mock.DelInjection(MDropAlias)
defer mockServer.DelInjection(MDropAlias)
err := c.DropAlias(ctx, "collAlias")
assert.NoError(t, err)
})

t.Run("drop alias error", func(t *testing.T) {
mock.SetInjection(MDropAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MDropAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
req, ok := raw.(*server.DropAliasRequest)
if !ok {
t.FailNow()
Expand All @@ -104,7 +104,7 @@ func TestGrpcDropAlias(t *testing.T) {

return &common.Status{ErrorCode: common.ErrorCode_UnexpectedError}, nil
})
defer mock.DelInjection(MDropAlias)
defer mockServer.DelInjection(MDropAlias)
err := c.DropAlias(ctx, "collAlias")
assert.Error(t, err)
})
Expand All @@ -119,7 +119,7 @@ func TestGrpcAlterAlias(t *testing.T) {
aliasName := "collAlias"

t.Run("normal alter alias", func(t *testing.T) {
mock.SetInjection(MAlterAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MAlterAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
req, ok := raw.(*server.AlterAliasRequest)
if !ok {
t.FailNow()
Expand All @@ -129,13 +129,13 @@ func TestGrpcAlterAlias(t *testing.T) {

return &common.Status{ErrorCode: common.ErrorCode_Success}, nil
})
defer mock.DelInjection(MAlterAlias)
defer mockServer.DelInjection(MAlterAlias)
err := c.AlterAlias(ctx, collName, aliasName)
assert.NoError(t, err)
})

t.Run("alter alias error", func(t *testing.T) {
mock.SetInjection(MAlterAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MAlterAlias, func(_ context.Context, raw proto.Message) (proto.Message, error) {
req, ok := raw.(*server.AlterAliasRequest)
if !ok {
t.FailNow()
Expand All @@ -145,7 +145,7 @@ func TestGrpcAlterAlias(t *testing.T) {

return &common.Status{ErrorCode: common.ErrorCode_UnexpectedError}, nil
})
defer mock.DelInjection(MAlterAlias)
defer mockServer.DelInjection(MAlterAlias)
err := c.AlterAlias(ctx, collName, aliasName)
assert.Error(t, err)
})
Expand Down
63 changes: 32 additions & 31 deletions client/client_grpc_authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package client

import (
"context"
"errors"
"testing"

"github.com/cockroachdb/errors"

"github.com/golang/protobuf/proto"
common "github.com/milvus-io/milvus-proto/go-api/commonpb"
server "github.com/milvus-io/milvus-proto/go-api/milvuspb"
Expand All @@ -16,42 +17,42 @@ func TestGrpcClient_CreateCredential(t *testing.T) {
c := testClient(ctx, t)

t.Run("create credential normal", func(t *testing.T) {
mock.SetInjection(MCreateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
mockServer.SetInjection(MCreateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
s, err := SuccessStatus()
return s, err
})
defer mock.DelInjection(MCreateCredential)
defer mockServer.DelInjection(MCreateCredential)
err := c.CreateCredential(ctx, testUsername, testPassword)
assert.Nil(t, err)
})

t.Run("create credential invalid name", func(t *testing.T) {
mock.SetInjection(MCreateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
mockServer.SetInjection(MCreateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
s, err := BadRequestStatus()
return s, err
})
defer mock.DelInjection(MCreateCredential)
defer mockServer.DelInjection(MCreateCredential)
err := c.CreateCredential(ctx, "123", testPassword)
assert.Error(t, err)
})

t.Run("create credential grpc error", func(t *testing.T) {
mock.SetInjection(MCreateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{}, errors.New("mocked grpc error")
mockServer.SetInjection(MCreateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{}, errors.New("mockServer.d grpc error")
})
defer mock.DelInjection(MCreateCredential)
defer mockServer.DelInjection(MCreateCredential)
err := c.CreateCredential(ctx, testUsername, testPassword)
assert.Error(t, err)
})

t.Run("create credential server error", func(t *testing.T) {
mock.SetInjection(MCreateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MCreateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{
ErrorCode: common.ErrorCode_UnexpectedError,
Reason: "Service is not healthy",
}, nil
})
defer mock.DelInjection(MCreateCredential)
defer mockServer.DelInjection(MCreateCredential)
err := c.CreateCredential(ctx, testUsername, testPassword)
assert.Error(t, err)
})
Expand All @@ -62,32 +63,32 @@ func TestGrpcClient_UpdateCredential(t *testing.T) {
c := testClient(ctx, t)

t.Run("update credential normal", func(t *testing.T) {
mock.SetInjection(MUpdateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
mockServer.SetInjection(MUpdateCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
s, err := SuccessStatus()
return s, err
})
defer mock.DelInjection(MUpdateCredential)
defer mockServer.DelInjection(MUpdateCredential)
err := c.UpdateCredential(ctx, testUsername, testPassword, testPassword)
assert.Nil(t, err)
})

t.Run("update credential grpc error", func(t *testing.T) {
mock.SetInjection(MUpdateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{}, errors.New("mocked grpc error")
mockServer.SetInjection(MUpdateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{}, errors.New("mockServer.d grpc error")
})
defer mock.DelInjection(MUpdateCredential)
defer mockServer.DelInjection(MUpdateCredential)
err := c.UpdateCredential(ctx, testUsername, testPassword, testPassword)
assert.Error(t, err)
})

t.Run("update credential server error", func(t *testing.T) {
mock.SetInjection(MUpdateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MUpdateCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{
ErrorCode: common.ErrorCode_UnexpectedError,
Reason: "Service is not healthy",
}, nil
})
defer mock.DelInjection(MUpdateCredential)
defer mockServer.DelInjection(MUpdateCredential)
err := c.UpdateCredential(ctx, testUsername, testPassword, testPassword)
assert.Error(t, err)
})
Expand All @@ -98,32 +99,32 @@ func TestGrpcClient_DeleteCredential(t *testing.T) {
c := testClient(ctx, t)

t.Run("delete credential normal", func(t *testing.T) {
mock.SetInjection(MDeleteCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
mockServer.SetInjection(MDeleteCredential, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
s, err := SuccessStatus()
return s, err
})
defer mock.DelInjection(MDeleteCredential)
defer mockServer.DelInjection(MDeleteCredential)
err := c.DeleteCredential(ctx, testUsername)
assert.Nil(t, err)
})

t.Run("delete credential grpc error", func(t *testing.T) {
mock.SetInjection(MDeleteCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{}, errors.New("mocked grpc error")
mockServer.SetInjection(MDeleteCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{}, errors.New("mockServer.d grpc error")
})
defer mock.DelInjection(MDeleteCredential)
defer mockServer.DelInjection(MDeleteCredential)
err := c.DeleteCredential(ctx, testUsername)
assert.Error(t, err)
})

t.Run("delete credential server error", func(t *testing.T) {
mock.SetInjection(MDeleteCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MDeleteCredential, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &common.Status{
ErrorCode: common.ErrorCode_UnexpectedError,
Reason: "Service is not healthy",
}, nil
})
defer mock.DelInjection(MDeleteCredential)
defer mockServer.DelInjection(MDeleteCredential)
err := c.DeleteCredential(ctx, testUsername)
assert.Error(t, err)
})
Expand All @@ -134,39 +135,39 @@ func TestGrpcClient_ListCredUsers(t *testing.T) {
c := testClient(ctx, t)

t.Run("list credential users normal", func(t *testing.T) {
mock.SetInjection(MListCredUsers, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
mockServer.SetInjection(MListCredUsers, func(ctx context.Context, _ proto.Message) (proto.Message, error) {
resp := &server.ListCredUsersResponse{
Usernames: []string{testUsername},
}
s, err := SuccessStatus()
resp.Status = s
return resp, err
})
defer mock.DelInjection(MListCredUsers)
defer mockServer.DelInjection(MListCredUsers)
names, err := c.ListCredUsers(ctx)
assert.Nil(t, err)
assert.Equal(t, []string{testUsername}, names)
})

t.Run("list credential users grpc error", func(t *testing.T) {
mock.SetInjection(MListCredUsers, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &server.ListCredUsersResponse{}, errors.New("mocked grpc error")
mockServer.SetInjection(MListCredUsers, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &server.ListCredUsersResponse{}, errors.New("mockServer.d grpc error")
})
defer mock.DelInjection(MListCredUsers)
defer mockServer.DelInjection(MListCredUsers)
_, err := c.ListCredUsers(ctx)
assert.Error(t, err)
})

t.Run("list credential users server error", func(t *testing.T) {
mock.SetInjection(MListCredUsers, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
mockServer.SetInjection(MListCredUsers, func(ctx context.Context, raw proto.Message) (proto.Message, error) {
return &server.ListCredUsersResponse{
Status: &common.Status{
ErrorCode: common.ErrorCode_UnexpectedError,
Reason: "Service is not healthy",
},
}, nil
})
defer mock.DelInjection(MListCredUsers)
defer mockServer.DelInjection(MListCredUsers)
_, err := c.ListCredUsers(ctx)
assert.Error(t, err)
})
Expand Down
3 changes: 2 additions & 1 deletion client/client_grpc_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ package client

import (
"context"
"errors"
"fmt"
"time"

"github.com/cockroachdb/errors"

"github.com/golang/protobuf/proto"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
"google.golang.org/grpc"
Expand Down
Loading