github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/maintainance_test.go (about) 1 // Licensed to the LF AI & Data foundation under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package client 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/cockroachdb/errors" 24 25 "github.com/golang/protobuf/proto" 26 "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 27 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 28 "github.com/milvus-io/milvus-sdk-go/v2/entity" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestGrpcManualCompaction(t *testing.T) { 33 ctx := context.Background() 34 c := testClient(ctx, t) 35 defer c.Close() 36 37 mockServer.SetInjection(MHasCollection, hasCollectionDefault) 38 defer mockServer.DelInjection(MHasCollection) 39 40 compactionID := int64(1001) 41 t.Run("normal manual compaction", func(t *testing.T) { 42 mockServer.SetInjection(MDescribeCollection, describeCollectionInjection(t, testCollectionID, testCollectionName, defaultSchema())) 43 defer mockServer.DelInjection(MDescribeCollection) 44 mockServer.SetInjection(MManualCompaction, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 45 req, ok := raw.(*milvuspb.ManualCompactionRequest) 46 if !ok { 47 t.FailNow() 48 } 49 50 assert.Equal(t, testCollectionID, req.GetCollectionID()) 51 52 resp := &milvuspb.ManualCompactionResponse{ 53 CompactionID: compactionID, 54 } 55 s, err := SuccessStatus() 56 resp.Status = s 57 return resp, err 58 }) 59 defer mockServer.DelInjection(MManualCompaction) 60 61 id, err := c.ManualCompaction(ctx, testCollectionName, 0) 62 assert.NoError(t, err) 63 assert.EqualValues(t, compactionID, id) 64 }) 65 66 t.Run("not ready client manual compaction", func(t *testing.T) { 67 c := GrpcClient{} 68 _, err := c.ManualCompaction(ctx, testCollectionName, 0) 69 assert.Error(t, err) 70 assert.Equal(t, ErrClientNotReady, err) 71 }) 72 73 t.Run("describe collection fail", func(t *testing.T) { 74 mockServer.SetInjection(MDescribeCollection, func(_ context.Context, _ proto.Message) (proto.Message, error) { 75 return &milvuspb.DescribeCollectionResponse{}, errors.New("mockServer.d error") 76 }) 77 defer mockServer.DelInjection(MDescribeCollection) 78 79 _, err := c.ManualCompaction(ctx, testCollectionName, 0) 80 assert.Error(t, err) 81 }) 82 83 t.Run("compaction Service error", func(t *testing.T) { 84 mockServer.SetInjection(MDescribeCollection, describeCollectionInjection(t, testCollectionID, testCollectionName, defaultSchema())) 85 defer mockServer.DelInjection(MDescribeCollection) 86 mockServer.SetInjection(MManualCompaction, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 87 resp := &milvuspb.ManualCompactionResponse{ 88 CompactionID: 1001, 89 } 90 return resp, errors.New("mockServer.d grpc error") 91 }) 92 93 _, err := c.ManualCompaction(ctx, testCollectionName, 0) 94 assert.Error(t, err) 95 mockServer.SetInjection(MManualCompaction, func(ctx context.Context, raw proto.Message) (proto.Message, error) { 96 resp := &milvuspb.ManualCompactionResponse{ 97 CompactionID: 1001, 98 } 99 resp.Status = &commonpb.Status{ 100 ErrorCode: commonpb.ErrorCode_UnexpectedError, 101 } 102 return resp, nil 103 }) 104 105 defer mockServer.DelInjection(MManualCompaction) 106 107 _, err = c.ManualCompaction(ctx, testCollectionName, 0) 108 assert.Error(t, err) 109 }) 110 } 111 112 func TestGrpcGetCompactionState(t *testing.T) { 113 ctx := context.Background() 114 c := testClient(ctx, t) 115 defer c.Close() 116 117 compactionID := int64(1001) 118 119 t.Run("normal get compaction state", func(t *testing.T) { 120 state := commonpb.CompactionState_Executing 121 122 mockServer.SetInjection(MGetCompactionState, func(_ context.Context, raw proto.Message) (proto.Message, error) { 123 req, ok := raw.(*milvuspb.GetCompactionStateRequest) 124 if !ok { 125 t.FailNow() 126 } 127 128 assert.Equal(t, compactionID, req.GetCompactionID()) 129 130 resp := &milvuspb.GetCompactionStateResponse{ 131 State: state, 132 } 133 s, err := SuccessStatus() 134 resp.Status = s 135 return resp, err 136 }) 137 defer mockServer.DelInjection(MGetCompactionState) 138 139 result, err := c.GetCompactionState(ctx, compactionID) 140 assert.NoError(t, err) 141 assert.Equal(t, entity.CompactionStateExecuting, result) 142 143 state = commonpb.CompactionState_Completed 144 result, err = c.GetCompactionState(ctx, compactionID) 145 assert.NoError(t, err) 146 assert.Equal(t, entity.CompactionStateCompleted, result) 147 }) 148 149 t.Run("get compaction Service fail", func(t *testing.T) { 150 mockServer.SetInjection(MGetCompactionState, func(_ context.Context, raw proto.Message) (proto.Message, error) { 151 resp := &milvuspb.GetCompactionStateResponse{} 152 resp.Status = &commonpb.Status{ 153 ErrorCode: commonpb.ErrorCode_UnexpectedError, 154 } 155 return resp, nil 156 }) 157 defer mockServer.DelInjection(MGetCompactionState) 158 159 _, err := c.GetCompactionState(ctx, compactionID) 160 assert.Error(t, err) 161 }) 162 } 163 164 func TestGrpcGetCompactionStateWithPlans(t *testing.T) { 165 ctx := context.Background() 166 c := testClient(ctx, t) 167 defer c.Close() 168 169 compactionID := int64(1001) 170 171 t.Run("normal get compaction state with plans", func(t *testing.T) { 172 state := commonpb.CompactionState_Executing 173 plans := []entity.CompactionPlan{ 174 {Source: []int64{1, 2}, Target: 3, PlanType: entity.CompactionPlanMergeSegments}, 175 {Source: []int64{4, 5}, Target: 6, PlanType: entity.CompactionPlanMergeSegments}, 176 } 177 178 mockServer.SetInjection(MGetCompactionStateWithPlans, func(_ context.Context, raw proto.Message) (proto.Message, error) { 179 req, ok := raw.(*milvuspb.GetCompactionPlansRequest) 180 if !ok { 181 t.FailNow() 182 } 183 184 assert.Equal(t, compactionID, req.GetCompactionID()) 185 186 resp := &milvuspb.GetCompactionPlansResponse{ 187 State: state, 188 MergeInfos: make([]*milvuspb.CompactionMergeInfo, 0, len(plans)), 189 } 190 for _, plan := range plans { 191 resp.MergeInfos = append(resp.MergeInfos, &milvuspb.CompactionMergeInfo{ 192 Sources: plan.Source, 193 Target: plan.Target, 194 }) 195 } 196 197 s, err := SuccessStatus() 198 resp.Status = s 199 return resp, err 200 }) 201 defer mockServer.DelInjection(MGetCompactionStateWithPlans) 202 203 result, rPlans, err := c.GetCompactionStateWithPlans(ctx, compactionID) 204 assert.NoError(t, err) 205 assert.Equal(t, entity.CompactionStateExecuting, result) 206 assert.ElementsMatch(t, plans, rPlans) 207 208 state = commonpb.CompactionState_Completed 209 result, rPlans, err = c.GetCompactionStateWithPlans(ctx, compactionID) 210 assert.NoError(t, err) 211 assert.Equal(t, entity.CompactionStateCompleted, result) 212 assert.ElementsMatch(t, plans, rPlans) 213 }) 214 215 t.Run("get compaction Service fail", func(t *testing.T) { 216 mockServer.SetInjection(MGetCompactionStateWithPlans, func(_ context.Context, raw proto.Message) (proto.Message, error) { 217 resp := &milvuspb.GetCompactionPlansResponse{} 218 resp.Status = &commonpb.Status{ 219 ErrorCode: commonpb.ErrorCode_UnexpectedError, 220 } 221 return resp, nil 222 }) 223 defer mockServer.DelInjection(MGetCompactionStateWithPlans) 224 225 _, _, err := c.GetCompactionStateWithPlans(ctx, compactionID) 226 assert.Error(t, err) 227 }) 228 229 }