From 56d367b1e9eca202888baa9760d3e5358bc18f1b Mon Sep 17 00:00:00 2001 From: neza2017 Date: Fri, 9 Apr 2021 16:10:12 +0800 Subject: [PATCH] Add unit test for master service Signed-off-by: neza2017 --- go.sum | 1 + internal/masterservice/master_service.go | 4 +- internal/masterservice/meta_table_test.go | 162 ++++++++++++++++++++++ 3 files changed, 165 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index 857eff518..cae7f285c 100644 --- a/go.sum +++ b/go.sum @@ -431,6 +431,7 @@ github.com/protocolbuffers/protobuf v3.15.3+incompatible h1:5WExaSYHEGvU73sVHvqe github.com/protocolbuffers/protobuf v3.15.4+incompatible h1:Blv4dGFGqHXX+r5Tqoc1ziXPMDElqZ+/ryYcE4bddN4= github.com/protocolbuffers/protobuf v3.15.5+incompatible h1:NsnktN0DZ4i7hXZ6HPFH395SptFlMVhSc8XuhkiOwzI= github.com/protocolbuffers/protobuf v3.15.6+incompatible h1:xDkn9XF/5pyO6v3GKpwIm7GFUIQj1cQcPuWnWsG9664= +github.com/protocolbuffers/protobuf v3.15.8+incompatible h1:6bDGveAnRWndINAC3CCKj1BLbRYJ+MBYKXus+Lz0eiY= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af h1:gu+uRPtBe88sKxUCEXRoeCvVG90TJmwhiqRpvdhQFng= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk= diff --git a/internal/masterservice/master_service.go b/internal/masterservice/master_service.go index 5ef8f12eb..1e1ab68ca 100644 --- a/internal/masterservice/master_service.go +++ b/internal/masterservice/master_service.go @@ -1202,7 +1202,7 @@ func (c *Core) ShowPartitions(ctx context.Context, in *milvuspb.ShowPartitionsRe zap.String("collection", in.CollectionName)) code := c.stateCode.Load().(internalpb.StateCode) if code != internalpb.StateCode_Healthy { - log.Error("ShowPartitionRequest failed: master is not healthy", zap.String("role", Params.RoleName), + log.Debug("ShowPartitionRequest failed: master is not healthy", zap.String("role", Params.RoleName), zap.Int64("msgID", in.Base.MsgID), zap.String("state", internalpb.StateCode_name[int32(code)])) return &milvuspb.ShowPartitionsResponse{ Status: &commonpb.Status{ @@ -1228,7 +1228,7 @@ func (c *Core) ShowPartitions(ctx context.Context, in *milvuspb.ShowPartitionsRe c.ddReqQueue <- t err := t.WaitToFinish() if err != nil { - log.Error("ShowPartitionsRequest failed", zap.String("role", Params.RoleName), zap.Int64("msgID", in.Base.MsgID), zap.Error(err)) + log.Debug("ShowPartitionsRequest failed", zap.String("role", Params.RoleName), zap.Int64("msgID", in.Base.MsgID), zap.Error(err)) return &milvuspb.ShowPartitionsResponse{ PartitionNames: nil, Status: &commonpb.Status{ diff --git a/internal/masterservice/meta_table_test.go b/internal/masterservice/meta_table_test.go index b50037af0..a40317dd4 100644 --- a/internal/masterservice/meta_table_test.go +++ b/internal/masterservice/meta_table_test.go @@ -6,15 +6,119 @@ import ( "testing" "time" + "github.com/golang/protobuf/proto" "github.com/stretchr/testify/assert" + "github.com/zilliztech/milvus-distributed/internal/kv" etcdkv "github.com/zilliztech/milvus-distributed/internal/kv/etcd" "github.com/zilliztech/milvus-distributed/internal/proto/commonpb" "github.com/zilliztech/milvus-distributed/internal/proto/datapb" pb "github.com/zilliztech/milvus-distributed/internal/proto/etcdpb" "github.com/zilliztech/milvus-distributed/internal/proto/schemapb" + "github.com/zilliztech/milvus-distributed/internal/util/typeutil" "go.etcd.io/etcd/clientv3" ) +type mockTestKV struct { + kv.TxnBase + + loadWithPrefix func(key string) ([]string, []string, error) + save func(key, value string) error + multiSave func(kvs map[string]string) error + multiRemoveWithPrefix func(keys []string) error +} + +func (m *mockTestKV) LoadWithPrefix(key string) ([]string, []string, error) { + return m.loadWithPrefix(key) +} + +func (m *mockTestKV) Save(key, value string) error { + return m.save(key, value) +} + +func (m *mockTestKV) MultiSave(kvs map[string]string) error { + return m.multiSave(kvs) +} + +func (m *mockTestKV) MultiRemoveWithPrefix(keys []string) error { + return m.multiRemoveWithPrefix(keys) +} + +func Test_MockKV(t *testing.T) { + k1 := &mockTestKV{} + prefix := make(map[string][]string) + k1.loadWithPrefix = func(key string) ([]string, []string, error) { + if val, ok := prefix[key]; ok { + return nil, val, nil + } + return nil, nil, fmt.Errorf("error test") + } + + _, err := NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[TenantMetaPrefix] = []string{"true"} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[TenantMetaPrefix] = []string{proto.MarshalTextString(&pb.TenantMeta{})} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[ProxyMetaPrefix] = []string{"true"} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[ProxyMetaPrefix] = []string{proto.MarshalTextString(&pb.ProxyMeta{})} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[CollectionMetaPrefix] = []string{"true"} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[CollectionMetaPrefix] = []string{proto.MarshalTextString(&pb.CollectionInfo{Schema: &schemapb.CollectionSchema{}})} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[PartitionMetaPrefix] = []string{"true"} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[PartitionMetaPrefix] = []string{proto.MarshalTextString(&pb.PartitionInfo{})} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[SegmentIndexMetaPrefix] = []string{"true"} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[SegmentIndexMetaPrefix] = []string{proto.MarshalTextString(&pb.SegmentIndexInfo{})} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[SegmentIndexMetaPrefix] = []string{proto.MarshalTextString(&pb.SegmentIndexInfo{}), proto.MarshalTextString(&pb.SegmentIndexInfo{})} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[IndexMetaPrefix] = []string{"true"} + _, err = NewMetaTable(k1) + assert.NotNil(t, err) + + prefix[IndexMetaPrefix] = []string{proto.MarshalTextString(&pb.IndexInfo{})} + m1, err := NewMetaTable(k1) + assert.Nil(t, err) + + k1.save = func(key, value string) error { + return fmt.Errorf("error test") + } + + err = m1.AddTenant(&pb.TenantMeta{}) + assert.NotNil(t, err) + + err = m1.AddProxy(&pb.ProxyMeta{}) + assert.NotNil(t, err) +} + func TestMetaTable(t *testing.T) { rand.Seed(time.Now().UnixNano()) randVal := rand.Int() @@ -280,4 +384,62 @@ func TestMetaTable(t *testing.T) { assert.Nil(t, err) }) + /////////////////////////// these tests should run at last, it only used to hit the error lines //////////////////////// + mockKV := &mockTestKV{} + mt.client = mockKV + + t.Run("add collection failed", func(t *testing.T) { + mockKV.loadWithPrefix = func(key string) ([]string, []string, error) { + return nil, nil, nil + } + mockKV.multiSave = func(kvs map[string]string) error { + return fmt.Errorf("error test") + } + collInfo.PartitionIDs = nil + err := mt.AddCollection(collInfo, partInfo, idxInfo) + assert.NotNil(t, err) + }) + + t.Run("delete collection failed", func(t *testing.T) { + mockKV.multiSave = func(kvs map[string]string) error { + return nil + } + mockKV.multiRemoveWithPrefix = func(keys []string) error { + return fmt.Errorf("error test") + } + collInfo.PartitionIDs = nil + err := mt.AddCollection(collInfo, partInfo, idxInfo) + assert.Nil(t, err) + mt.partitionID2Meta = make(map[typeutil.UniqueID]pb.PartitionInfo) + mt.indexID2Meta = make(map[int64]pb.IndexInfo) + err = mt.DeleteCollection(collInfo.ID) + assert.NotNil(t, err) + }) + + t.Run("get collection failed", func(t *testing.T) { + mockKV.save = func(key, value string) error { + return nil + } + + collInfo.PartitionIDs = nil + err := mt.AddCollection(collInfo, partInfo, idxInfo) + assert.Nil(t, err) + + seg := &datapb.SegmentInfo{ + ID: 100, + CollectionID: 1, + PartitionID: 10, + } + assert.Nil(t, mt.AddSegment(seg)) + + mt.collID2Meta = make(map[int64]pb.CollectionInfo) + _, err = mt.GetCollectionByName(collInfo.Schema.Name) + assert.NotNil(t, err) + _, err = mt.GetCollectionBySegmentID(seg.ID) + assert.NotNil(t, err) + + mt.segID2CollID = make(map[int64]int64) + _, err = mt.GetCollectionBySegmentID(seg.ID) + assert.NotNil(t, err) + }) } -- GitLab