github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/client_mock_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "net" 6 "sync" 7 8 "github.com/cockroachdb/errors" 9 10 "github.com/golang/protobuf/proto" 11 "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" 12 "github.com/milvus-io/milvus-proto/go-api/v2/federpb" 13 "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" 14 "github.com/milvus-io/milvus-proto/go-api/v2/schemapb" 15 "github.com/milvus-io/milvus-sdk-go/v2/entity" 16 "github.com/milvus-io/milvus-sdk-go/v2/merr" 17 "github.com/milvus-io/milvus-sdk-go/v2/mocks" 18 "github.com/stretchr/testify/mock" 19 "github.com/stretchr/testify/suite" 20 "google.golang.org/grpc" 21 "google.golang.org/grpc/credentials/insecure" 22 "google.golang.org/grpc/test/bufconn" 23 ) 24 25 type MockSuiteBase struct { 26 suite.Suite 27 28 lis *bufconn.Listener 29 svr *grpc.Server 30 mock *mocks.MilvusServiceServer 31 32 client Client 33 } 34 35 func (s *MockSuiteBase) SetupSuite() { 36 s.lis = bufconn.Listen(bufSize) 37 s.svr = grpc.NewServer() 38 39 s.mock = &mocks.MilvusServiceServer{} 40 41 milvuspb.RegisterMilvusServiceServer(s.svr, s.mock) 42 43 go func() { 44 s.T().Log("start mock server") 45 if err := s.svr.Serve(s.lis); err != nil { 46 s.Fail("failed to start mock server", err.Error()) 47 } 48 }() 49 s.setupConnect() 50 } 51 52 func (s *MockSuiteBase) TearDownSuite() { 53 s.svr.Stop() 54 s.lis.Close() 55 } 56 57 func (s *MockSuiteBase) mockDialer(context.Context, string) (net.Conn, error) { 58 return s.lis.Dial() 59 } 60 61 func (s *MockSuiteBase) SetupTest() { 62 c, err := NewClient(context.Background(), Config{ 63 Address: "bufnet2", 64 DisableConn: true, 65 DialOptions: []grpc.DialOption{ 66 grpc.WithBlock(), 67 grpc.WithTransportCredentials(insecure.NewCredentials()), 68 grpc.WithContextDialer(s.mockDialer), 69 }, 70 }) 71 s.Require().NoError(err) 72 s.setupConnect() 73 74 s.client = c 75 } 76 77 func (s *MockSuiteBase) TearDownTest() { 78 s.client.Close() 79 s.client = nil 80 } 81 82 func (s *MockSuiteBase) resetMock() { 83 MetaCache.reset() 84 if s.mock != nil { 85 s.mock.Calls = nil 86 s.mock.ExpectedCalls = nil 87 s.setupConnect() 88 } 89 } 90 91 func (s *MockSuiteBase) setupConnect() { 92 s.mock.EXPECT().Connect(mock.Anything, mock.AnythingOfType("*milvuspb.ConnectRequest")). 93 Return(&milvuspb.ConnectResponse{ 94 Status: &commonpb.Status{}, 95 Identifier: 1, 96 }, nil).Maybe() 97 } 98 99 func (s *MockSuiteBase) setupHasCollection(collNames ...string) { 100 s.mock.EXPECT().HasCollection(mock.Anything, mock.AnythingOfType("*milvuspb.HasCollectionRequest")). 101 Call.Return(func(ctx context.Context, req *milvuspb.HasCollectionRequest) *milvuspb.BoolResponse { 102 resp := &milvuspb.BoolResponse{Status: &commonpb.Status{}} 103 for _, collName := range collNames { 104 if req.GetCollectionName() == collName { 105 resp.Value = true 106 break 107 } 108 } 109 return resp 110 }, nil) 111 } 112 113 func (s *MockSuiteBase) setupHasCollectionError(errorCode commonpb.ErrorCode, err error) { 114 s.mock.EXPECT().HasCollection(mock.Anything, mock.AnythingOfType("*milvuspb.HasCollectionRequest")). 115 Return(&milvuspb.BoolResponse{ 116 Status: &commonpb.Status{ErrorCode: errorCode}, 117 }, err) 118 } 119 120 func (s *MockSuiteBase) setupHasPartition(collName string, partNames ...string) { 121 s.mock.EXPECT().HasPartition(mock.Anything, mock.AnythingOfType("*milvuspb.HasPartitionRequest")). 122 Call.Return(func(ctx context.Context, req *milvuspb.HasPartitionRequest) *milvuspb.BoolResponse { 123 resp := &milvuspb.BoolResponse{Status: &commonpb.Status{}} 124 if req.GetCollectionName() == collName { 125 for _, partName := range partNames { 126 if req.GetPartitionName() == partName { 127 resp.Value = true 128 break 129 } 130 } 131 } 132 return resp 133 }, nil) 134 } 135 136 func (s *MockSuiteBase) setupHasPartitionError(errorCode commonpb.ErrorCode, err error) { 137 s.mock.EXPECT().HasPartition(mock.Anything, mock.AnythingOfType("*milvuspb.HasPartitionRequest")). 138 Return(&milvuspb.BoolResponse{ 139 Status: &commonpb.Status{ErrorCode: errorCode}, 140 }, err) 141 } 142 143 func (s *MockSuiteBase) setupDescribeCollection(_ string, schema *entity.Schema) { 144 s.mock.EXPECT().DescribeCollection(mock.Anything, mock.AnythingOfType("*milvuspb.DescribeCollectionRequest")). 145 Call.Return(func(ctx context.Context, req *milvuspb.DescribeCollectionRequest) *milvuspb.DescribeCollectionResponse { 146 return &milvuspb.DescribeCollectionResponse{ 147 Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, 148 Schema: schema.ProtoMessage(), 149 } 150 }, nil) 151 } 152 153 func (s *MockSuiteBase) setupDescribeCollectionError(errorCode commonpb.ErrorCode, err error) { 154 s.mock.EXPECT().DescribeCollection(mock.Anything, mock.AnythingOfType("*milvuspb.DescribeCollectionRequest")). 155 Return(&milvuspb.DescribeCollectionResponse{ 156 Status: &commonpb.Status{ErrorCode: errorCode}, 157 }, err) 158 } 159 160 func (s *MockSuiteBase) getInt64FieldData(name string, data []int64) *schemapb.FieldData { 161 return &schemapb.FieldData{ 162 Type: schemapb.DataType_Int64, 163 FieldName: name, 164 Field: &schemapb.FieldData_Scalars{ 165 Scalars: &schemapb.ScalarField{ 166 Data: &schemapb.ScalarField_LongData{ 167 LongData: &schemapb.LongArray{ 168 Data: data, 169 }, 170 }, 171 }, 172 }, 173 } 174 } 175 176 func (s *MockSuiteBase) getVarcharFieldData(name string, data []string) *schemapb.FieldData { 177 return &schemapb.FieldData{ 178 Type: schemapb.DataType_VarChar, 179 FieldName: name, 180 Field: &schemapb.FieldData_Scalars{ 181 Scalars: &schemapb.ScalarField{ 182 Data: &schemapb.ScalarField_StringData{ 183 StringData: &schemapb.StringArray{ 184 Data: data, 185 }, 186 }, 187 }, 188 }, 189 } 190 } 191 192 func (s *MockSuiteBase) getJSONBytesFieldData(name string, data [][]byte, isDynamic bool) *schemapb.FieldData { 193 return &schemapb.FieldData{ 194 Type: schemapb.DataType_JSON, 195 FieldName: name, 196 Field: &schemapb.FieldData_Scalars{ 197 Scalars: &schemapb.ScalarField{ 198 Data: &schemapb.ScalarField_JsonData{ 199 JsonData: &schemapb.JSONArray{ 200 Data: data, 201 }, 202 }, 203 }, 204 }, 205 IsDynamic: isDynamic, 206 } 207 } 208 209 func (s *MockSuiteBase) getFloatVectorFieldData(name string, dim int64, data []float32) *schemapb.FieldData { 210 return &schemapb.FieldData{ 211 Type: schemapb.DataType_FloatVector, 212 FieldName: name, 213 Field: &schemapb.FieldData_Vectors{ 214 Vectors: &schemapb.VectorField{ 215 Dim: dim, 216 Data: &schemapb.VectorField_FloatVector{ 217 FloatVector: &schemapb.FloatArray{ 218 Data: data, 219 }, 220 }, 221 }, 222 }, 223 } 224 } 225 226 func (s *MockSuiteBase) getSuccessStatus() *commonpb.Status { 227 return s.getStatus(commonpb.ErrorCode_Success, "") 228 } 229 230 func (s *MockSuiteBase) getStatus(code commonpb.ErrorCode, reason string) *commonpb.Status { 231 return &commonpb.Status{ 232 ErrorCode: code, 233 Reason: reason, 234 } 235 } 236 237 // ref https://stackoverflow.com/questions/42102496/testing-a-grpc-service 238 239 var errNotImplemented = errors.New("not implemented") 240 241 // type alias for Service method 242 type ServiceMethod int 243 244 const ( 245 MCreateCollection ServiceMethod = 101 246 MDropCollection ServiceMethod = 102 247 MHasCollection ServiceMethod = 103 248 MLoadCollection ServiceMethod = 104 249 MReleaseCollection ServiceMethod = 105 250 MDescribeCollection ServiceMethod = 106 251 MListCollection ServiceMethod = 107 252 MGetCollectionStatistics ServiceMethod = 108 253 MAlterCollection ServiceMethod = 109 254 MGetLoadingProgress ServiceMethod = 110 255 MGetLoadState ServiceMethod = 111 256 257 MCreatePartition ServiceMethod = 201 258 MDropPartition ServiceMethod = 202 259 MHasPartition ServiceMethod = 203 260 MLoadPartitions ServiceMethod = 204 261 MReleasePartitions ServiceMethod = 205 262 MShowPartitions ServiceMethod = 206 263 264 MShowCollections ServiceMethod = 301 265 MCreateAlias ServiceMethod = 302 266 MDropAlias ServiceMethod = 303 267 MAlterAlias ServiceMethod = 304 268 MGetReplicas ServiceMethod = 305 269 270 MCreateIndex ServiceMethod = 401 271 MDropIndex ServiceMethod = 402 272 MDescribeIndex ServiceMethod = 403 273 MGetIndexState ServiceMethod = 404 274 MGetIndexBuildProgress ServiceMethod = 405 275 MAlterIndex ServiceMethod = 406 276 277 MCreateCredential ServiceMethod = 500 278 MUpdateCredential ServiceMethod = 501 279 MDeleteCredential ServiceMethod = 502 280 MListCredUsers ServiceMethod = 503 281 282 MInsert ServiceMethod = 600 283 MFlush ServiceMethod = 601 284 MSearch ServiceMethod = 602 285 MCalcDistance ServiceMethod = 603 286 MGetFlushState ServiceMethod = 604 287 MDelete ServiceMethod = 605 288 MQuery ServiceMethod = 606 289 MUpsert ServiceMethod = 607 290 MSearchV2 ServiceMethod = 608 291 292 MManualCompaction ServiceMethod = 700 293 MGetCompactionState ServiceMethod = 701 294 MGetCompactionStateWithPlans ServiceMethod = 702 295 296 MGetPersistentSegmentInfo ServiceMethod = 800 297 MGetQuerySegmentInfo ServiceMethod = 801 298 299 MGetComponentStates ServiceMethod = 900 300 MGetVersion ServiceMethod = 901 301 MCheckHealth ServiceMethod = 902 302 303 MListDatabase ServiceMethod = 1000 304 MCreateDatabase ServiceMethod = 1001 305 MDropDatabase ServiceMethod = 1002 306 MAlterDatabase ServiceMethod = 1003 307 MDescribeDatabase ServiceMethod = 1004 308 309 MReplicateMessage ServiceMethod = 1100 310 ) 311 312 // injection function definition 313 type TestInjection func(context.Context, proto.Message) (proto.Message, error) 314 315 // mock Milvus Server 316 type MockServer struct { 317 sync.RWMutex 318 Injections map[ServiceMethod]TestInjection 319 } 320 321 func (m *MockServer) SetInjection(n ServiceMethod, f TestInjection) { 322 m.Lock() 323 defer m.Unlock() 324 if m.Injections != nil { 325 m.Injections[n] = f 326 } 327 } 328 329 func (m *MockServer) GetInjection(n ServiceMethod) TestInjection { 330 if m.Injections == nil { 331 return nil 332 } 333 m.RLock() 334 defer m.RUnlock() 335 return m.Injections[n] 336 } 337 338 func (m *MockServer) DelInjection(n ServiceMethod) { 339 if m.Injections == nil { 340 return 341 } 342 m.Lock() 343 defer m.Unlock() 344 delete(m.Injections, n) 345 } 346 347 // -- database -- 348 // ListDatabases list all database in milvus cluster. 349 func (m *MockServer) ListDatabases(ctx context.Context, req *milvuspb.ListDatabasesRequest) (*milvuspb.ListDatabasesResponse, error) { 350 f := m.GetInjection(MListDatabase) 351 if f != nil { 352 r, err := f(ctx, req) 353 return r.(*milvuspb.ListDatabasesResponse), err 354 } 355 r := &milvuspb.ListDatabasesResponse{} 356 s, err := SuccessStatus() 357 r.Status = s 358 return r, err 359 } 360 361 // CreateDatabase create database with the given name. 362 func (m *MockServer) CreateDatabase(ctx context.Context, req *milvuspb.CreateDatabaseRequest) (*commonpb.Status, error) { 363 f := m.GetInjection(MCreateDatabase) 364 if f != nil { 365 r, err := f(ctx, req) 366 return r.(*commonpb.Status), err 367 } 368 return SuccessStatus() 369 } 370 371 // DropDatabase drop database with the given db name. 372 func (m *MockServer) DropDatabase(ctx context.Context, req *milvuspb.DropDatabaseRequest) (*commonpb.Status, error) { 373 f := m.GetInjection(MDropDatabase) 374 if f != nil { 375 r, err := f(ctx, req) 376 return r.(*commonpb.Status), err 377 } 378 return SuccessStatus() 379 } 380 381 func (m *MockServer) AlterDatabase(ctx context.Context, req *milvuspb.AlterDatabaseRequest) (*commonpb.Status, error) { 382 f := m.GetInjection(MAlterDatabase) 383 if f != nil { 384 r, err := f(ctx, req) 385 return r.(*commonpb.Status), err 386 } 387 return SuccessStatus() 388 } 389 390 func (m *MockServer) DescribeDatabase(ctx context.Context, req *milvuspb.DescribeDatabaseRequest) (*milvuspb.DescribeDatabaseResponse, error) { 391 f := m.GetInjection(MDescribeDatabase) 392 if f != nil { 393 r, err := f(ctx, req) 394 return r.(*milvuspb.DescribeDatabaseResponse), err 395 } 396 397 resp := &milvuspb.DescribeDatabaseResponse{ 398 Status: merr.Success(), 399 } 400 return resp, nil 401 } 402 403 func (m *MockServer) CreateCollection(ctx context.Context, req *milvuspb.CreateCollectionRequest) (*commonpb.Status, error) { 404 f := m.GetInjection(MCreateCollection) 405 if f != nil { 406 r, err := f(ctx, req) 407 return r.(*commonpb.Status), err 408 } 409 return SuccessStatus() 410 } 411 412 func (m *MockServer) DropCollection(ctx context.Context, req *milvuspb.DropCollectionRequest) (*commonpb.Status, error) { 413 f := m.GetInjection(MDropCollection) 414 if f != nil { 415 r, err := f(ctx, req) 416 return r.(*commonpb.Status), err 417 } 418 419 return SuccessStatus() 420 } 421 422 func (m *MockServer) HasCollection(ctx context.Context, req *milvuspb.HasCollectionRequest) (*milvuspb.BoolResponse, error) { 423 f := m.GetInjection(MHasCollection) 424 if f != nil { 425 r, err := f(ctx, req) 426 return r.(*milvuspb.BoolResponse), err 427 } 428 s, err := SuccessStatus() 429 return &milvuspb.BoolResponse{Status: s, Value: false}, err 430 } 431 432 func (m *MockServer) LoadCollection(ctx context.Context, req *milvuspb.LoadCollectionRequest) (*commonpb.Status, error) { 433 f := m.GetInjection(MLoadCollection) 434 if f != nil { 435 r, err := f(ctx, req) 436 return r.(*commonpb.Status), err 437 } 438 return SuccessStatus() 439 } 440 441 func (m *MockServer) ReleaseCollection(ctx context.Context, req *milvuspb.ReleaseCollectionRequest) (*commonpb.Status, error) { 442 f := m.GetInjection(MReleaseCollection) 443 if f != nil { 444 r, err := f(ctx, req) 445 return r.(*commonpb.Status), err 446 } 447 return SuccessStatus() 448 } 449 450 func (m *MockServer) DescribeCollection(ctx context.Context, req *milvuspb.DescribeCollectionRequest) (*milvuspb.DescribeCollectionResponse, error) { 451 f := m.GetInjection(MDescribeCollection) 452 if f != nil { 453 r, err := f(ctx, req) 454 return r.(*milvuspb.DescribeCollectionResponse), err 455 } 456 r := &milvuspb.DescribeCollectionResponse{} 457 s, err := SuccessStatus() 458 r.Status = s 459 return r, err 460 } 461 462 func (m *MockServer) GetCollectionStatistics(ctx context.Context, req *milvuspb.GetCollectionStatisticsRequest) (*milvuspb.GetCollectionStatisticsResponse, error) { 463 f := m.GetInjection(MGetCollectionStatistics) 464 if f != nil { 465 r, err := f(ctx, req) 466 return r.(*milvuspb.GetCollectionStatisticsResponse), err 467 } 468 r := &milvuspb.GetCollectionStatisticsResponse{} 469 s, err := SuccessStatus() 470 r.Status = s 471 return r, err 472 } 473 474 func (m *MockServer) ShowCollections(ctx context.Context, req *milvuspb.ShowCollectionsRequest) (*milvuspb.ShowCollectionsResponse, error) { 475 f := m.GetInjection(MShowCollections) 476 if f != nil { 477 r, err := f(ctx, req) 478 return r.(*milvuspb.ShowCollectionsResponse), err 479 } 480 s, err := SuccessStatus() 481 return &milvuspb.ShowCollectionsResponse{Status: s}, err 482 } 483 484 func (m *MockServer) AlterCollection(ctx context.Context, req *milvuspb.AlterCollectionRequest) (*commonpb.Status, error) { 485 f := m.GetInjection(MAlterCollection) 486 if f != nil { 487 r, err := f(ctx, req) 488 return r.(*commonpb.Status), err 489 } 490 return SuccessStatus() 491 } 492 493 func (m *MockServer) CreatePartition(ctx context.Context, req *milvuspb.CreatePartitionRequest) (*commonpb.Status, error) { 494 f := m.GetInjection(MCreatePartition) 495 if f != nil { 496 r, err := f(ctx, req) 497 return r.(*commonpb.Status), err 498 } 499 return SuccessStatus() 500 } 501 502 func (m *MockServer) DropPartition(ctx context.Context, req *milvuspb.DropPartitionRequest) (*commonpb.Status, error) { 503 f := m.GetInjection(MDropPartition) 504 if f != nil { 505 r, err := f(ctx, req) 506 return r.(*commonpb.Status), err 507 } 508 return SuccessStatus() 509 } 510 511 func (m *MockServer) HasPartition(ctx context.Context, req *milvuspb.HasPartitionRequest) (*milvuspb.BoolResponse, error) { 512 f := m.GetInjection(MHasPartition) 513 if f != nil { 514 r, err := f(ctx, req) 515 return r.(*milvuspb.BoolResponse), err 516 } 517 s, err := SuccessStatus() 518 return &milvuspb.BoolResponse{Status: s, Value: false}, err 519 } 520 521 func (m *MockServer) LoadPartitions(ctx context.Context, req *milvuspb.LoadPartitionsRequest) (*commonpb.Status, error) { 522 f := m.GetInjection(MLoadPartitions) 523 if f != nil { 524 r, err := f(ctx, req) 525 return r.(*commonpb.Status), err 526 } 527 return SuccessStatus() 528 } 529 530 func (m *MockServer) ReleasePartitions(ctx context.Context, req *milvuspb.ReleasePartitionsRequest) (*commonpb.Status, error) { 531 f := m.GetInjection(MReleasePartitions) 532 if f != nil { 533 r, err := f(ctx, req) 534 return r.(*commonpb.Status), err 535 } 536 return SuccessStatus() 537 } 538 539 func (m *MockServer) GetPartitionStatistics(_ context.Context, _ *milvuspb.GetPartitionStatisticsRequest) (*milvuspb.GetPartitionStatisticsResponse, error) { 540 panic("not implemented") // TODO: Implement 541 } 542 543 func (m *MockServer) ShowPartitions(ctx context.Context, req *milvuspb.ShowPartitionsRequest) (*milvuspb.ShowPartitionsResponse, error) { 544 f := m.GetInjection(MShowPartitions) 545 if f != nil { 546 r, err := f(ctx, req) 547 return r.(*milvuspb.ShowPartitionsResponse), err 548 } 549 s, err := SuccessStatus() 550 return &milvuspb.ShowPartitionsResponse{Status: s}, err 551 } 552 553 func (m *MockServer) GetLoadingProgress(ctx context.Context, req *milvuspb.GetLoadingProgressRequest) (*milvuspb.GetLoadingProgressResponse, error) { 554 f := m.GetInjection(MGetLoadingProgress) 555 if f != nil { 556 r, err := f(ctx, req) 557 return r.(*milvuspb.GetLoadingProgressResponse), err 558 } 559 s, err := SuccessStatus() 560 return &milvuspb.GetLoadingProgressResponse{Status: s}, err 561 } 562 563 func (m *MockServer) GetLoadState(ctx context.Context, req *milvuspb.GetLoadStateRequest) (*milvuspb.GetLoadStateResponse, error) { 564 f := m.GetInjection(MGetLoadState) 565 if f != nil { 566 r, err := f(ctx, req) 567 return r.(*milvuspb.GetLoadStateResponse), err 568 } 569 s, err := SuccessStatus() 570 return &milvuspb.GetLoadStateResponse{Status: s}, err 571 } 572 573 func (m *MockServer) CreateIndex(ctx context.Context, req *milvuspb.CreateIndexRequest) (*commonpb.Status, error) { 574 f := m.GetInjection(MCreateIndex) 575 if f != nil { 576 r, err := f(ctx, req) 577 return r.(*commonpb.Status), err 578 } 579 return SuccessStatus() 580 } 581 582 func (m *MockServer) AlterIndex(ctx context.Context, req *milvuspb.AlterIndexRequest) (*commonpb.Status, error) { 583 f := m.GetInjection(MAlterIndex) 584 if f != nil { 585 r, err := f(ctx, req) 586 return r.(*commonpb.Status), err 587 } 588 return SuccessStatus() 589 } 590 591 func (m *MockServer) DescribeIndex(ctx context.Context, req *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) { 592 f := m.GetInjection(MDescribeIndex) 593 if f != nil { 594 r, err := f(ctx, req) 595 return r.(*milvuspb.DescribeIndexResponse), err 596 } 597 s, err := SuccessStatus() 598 return &milvuspb.DescribeIndexResponse{Status: s}, err 599 } 600 601 func (m *MockServer) GetIndexState(ctx context.Context, req *milvuspb.GetIndexStateRequest) (*milvuspb.GetIndexStateResponse, error) { 602 f := m.GetInjection(MGetIndexState) 603 if f != nil { 604 r, err := f(ctx, req) 605 return r.(*milvuspb.GetIndexStateResponse), err 606 } 607 s, err := SuccessStatus() 608 return &milvuspb.GetIndexStateResponse{Status: s}, err 609 } 610 611 func (m *MockServer) GetIndexBuildProgress(ctx context.Context, req *milvuspb.GetIndexBuildProgressRequest) (*milvuspb.GetIndexBuildProgressResponse, error) { 612 f := m.GetInjection(MGetIndexBuildProgress) 613 if f != nil { 614 r, err := f(ctx, req) 615 return r.(*milvuspb.GetIndexBuildProgressResponse), err 616 } 617 s, err := SuccessStatus() 618 return &milvuspb.GetIndexBuildProgressResponse{Status: s}, err 619 } 620 621 func (m *MockServer) DropIndex(ctx context.Context, req *milvuspb.DropIndexRequest) (*commonpb.Status, error) { 622 f := m.GetInjection(MDropIndex) 623 if f != nil { 624 r, err := f(ctx, req) 625 return r.(*commonpb.Status), err 626 } 627 return SuccessStatus() 628 } 629 630 func (m *MockServer) Insert(ctx context.Context, req *milvuspb.InsertRequest) (*milvuspb.MutationResult, error) { 631 f := m.GetInjection(MInsert) 632 if f != nil { 633 r, err := f(ctx, req) 634 return r.(*milvuspb.MutationResult), err 635 } 636 s, err := SuccessStatus() 637 return &milvuspb.MutationResult{Status: s}, err 638 } 639 640 func (m *MockServer) Search(ctx context.Context, req *milvuspb.SearchRequest) (*milvuspb.SearchResults, error) { 641 f := m.GetInjection(MSearch) 642 if f != nil { 643 r, err := f(ctx, req) 644 return r.(*milvuspb.SearchResults), err 645 } 646 s, err := SuccessStatus() 647 return &milvuspb.SearchResults{Status: s}, err 648 } 649 650 func (m *MockServer) Flush(ctx context.Context, req *milvuspb.FlushRequest) (*milvuspb.FlushResponse, error) { 651 f := m.GetInjection(MFlush) 652 if f != nil { 653 r, err := f(ctx, req) 654 return r.(*milvuspb.FlushResponse), err 655 } 656 s, err := SuccessStatus() 657 return &milvuspb.FlushResponse{Status: s}, err 658 } 659 660 func (m *MockServer) Query(ctx context.Context, req *milvuspb.QueryRequest) (*milvuspb.QueryResults, error) { 661 f := m.GetInjection(MQuery) 662 if f != nil { 663 r, err := f(ctx, req) 664 return r.(*milvuspb.QueryResults), err 665 } 666 s, err := SuccessStatus() 667 return &milvuspb.QueryResults{Status: s}, err 668 } 669 670 func (m *MockServer) GetPersistentSegmentInfo(ctx context.Context, req *milvuspb.GetPersistentSegmentInfoRequest) (*milvuspb.GetPersistentSegmentInfoResponse, error) { 671 f := m.GetInjection(MGetPersistentSegmentInfo) 672 if f != nil { 673 r, err := f(ctx, req) 674 return r.(*milvuspb.GetPersistentSegmentInfoResponse), err 675 } 676 s, err := SuccessStatus() 677 return &milvuspb.GetPersistentSegmentInfoResponse{Status: s}, err 678 } 679 680 func (m *MockServer) GetQuerySegmentInfo(ctx context.Context, req *milvuspb.GetQuerySegmentInfoRequest) (*milvuspb.GetQuerySegmentInfoResponse, error) { 681 f := m.GetInjection(MGetQuerySegmentInfo) 682 if f != nil { 683 r, err := f(ctx, req) 684 return r.(*milvuspb.GetQuerySegmentInfoResponse), err 685 } 686 s, err := SuccessStatus() 687 return &milvuspb.GetQuerySegmentInfoResponse{Status: s}, err 688 } 689 690 func (m *MockServer) Dummy(_ context.Context, _ *milvuspb.DummyRequest) (*milvuspb.DummyResponse, error) { 691 panic("not implemented") // TODO: Implement 692 } 693 694 // TODO: remove 695 func (m *MockServer) RegisterLink(_ context.Context, _ *milvuspb.RegisterLinkRequest) (*milvuspb.RegisterLinkResponse, error) { 696 panic("not implemented") // TODO: Implement 697 } 698 699 func (m *MockServer) CalcDistance(ctx context.Context, req *milvuspb.CalcDistanceRequest) (*milvuspb.CalcDistanceResults, error) { 700 f := m.GetInjection(MCalcDistance) 701 if f != nil { 702 r, err := f(ctx, req) 703 return r.(*milvuspb.CalcDistanceResults), err 704 } 705 resp := &milvuspb.CalcDistanceResults{} 706 s, err := SuccessStatus() 707 resp.Status = s 708 return resp, err 709 } 710 711 func (m *MockServer) Delete(ctx context.Context, req *milvuspb.DeleteRequest) (*milvuspb.MutationResult, error) { 712 f := m.GetInjection(MDelete) 713 if f != nil { 714 r, err := f(ctx, req) 715 return r.(*milvuspb.MutationResult), err 716 } 717 resp := &milvuspb.MutationResult{} 718 s, err := SuccessStatus() 719 resp.Status = s 720 return resp, err 721 } 722 723 // https://wiki.lfaidata.foundation/display/MIL/MEP+8+--+Add+metrics+for+proxy 724 func (m *MockServer) GetMetrics(_ context.Context, _ *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) { 725 panic("not implemented") // TODO: Implement 726 } 727 728 func (m *MockServer) CreateAlias(ctx context.Context, req *milvuspb.CreateAliasRequest) (*commonpb.Status, error) { 729 f := m.GetInjection(MCreateAlias) 730 if f != nil { 731 r, err := f(ctx, req) 732 return r.(*commonpb.Status), err 733 } 734 return SuccessStatus() 735 } 736 737 func (m *MockServer) DropAlias(ctx context.Context, req *milvuspb.DropAliasRequest) (*commonpb.Status, error) { 738 f := m.GetInjection(MDropAlias) 739 if f != nil { 740 r, err := f(ctx, req) 741 return r.(*commonpb.Status), err 742 } 743 return SuccessStatus() 744 } 745 746 func (m *MockServer) AlterAlias(ctx context.Context, req *milvuspb.AlterAliasRequest) (*commonpb.Status, error) { 747 f := m.GetInjection(MAlterAlias) 748 if f != nil { 749 r, err := f(ctx, req) 750 return r.(*commonpb.Status), err 751 } 752 753 return SuccessStatus() 754 } 755 756 func (m *MockServer) GetFlushState(ctx context.Context, req *milvuspb.GetFlushStateRequest) (*milvuspb.GetFlushStateResponse, error) { 757 f := m.GetInjection(MGetFlushState) 758 if f != nil { 759 r, err := f(ctx, req) 760 return r.(*milvuspb.GetFlushStateResponse), err 761 } 762 763 resp := &milvuspb.GetFlushStateResponse{} 764 s, err := SuccessStatus() 765 resp.Status = s 766 return resp, err 767 } 768 769 func (m *MockServer) LoadBalance(_ context.Context, _ *milvuspb.LoadBalanceRequest) (*commonpb.Status, error) { 770 panic("not implemented") // TODO: Implement 771 } 772 773 func (m *MockServer) GetCompactionState(ctx context.Context, req *milvuspb.GetCompactionStateRequest) (*milvuspb.GetCompactionStateResponse, error) { 774 f := m.GetInjection(MGetCompactionState) 775 if f != nil { 776 r, err := f(ctx, req) 777 return r.(*milvuspb.GetCompactionStateResponse), err 778 } 779 780 resp := &milvuspb.GetCompactionStateResponse{} 781 s, err := SuccessStatus() 782 resp.Status = s 783 return resp, err 784 } 785 786 func (m *MockServer) ManualCompaction(ctx context.Context, req *milvuspb.ManualCompactionRequest) (*milvuspb.ManualCompactionResponse, error) { 787 f := m.GetInjection(MManualCompaction) 788 if f != nil { 789 r, err := f(ctx, req) 790 return r.(*milvuspb.ManualCompactionResponse), err 791 } 792 resp := &milvuspb.ManualCompactionResponse{} 793 s, err := SuccessStatus() 794 resp.Status = s 795 return resp, err 796 } 797 798 func (m *MockServer) GetCompactionStateWithPlans(ctx context.Context, req *milvuspb.GetCompactionPlansRequest) (*milvuspb.GetCompactionPlansResponse, error) { 799 f := m.GetInjection(MGetCompactionStateWithPlans) 800 if f != nil { 801 r, err := f(ctx, req) 802 return r.(*milvuspb.GetCompactionPlansResponse), err 803 } 804 805 resp := &milvuspb.GetCompactionPlansResponse{} 806 s, err := SuccessStatus() 807 resp.Status = s 808 return resp, err 809 } 810 811 func (m *MockServer) GetReplicas(ctx context.Context, req *milvuspb.GetReplicasRequest) (*milvuspb.GetReplicasResponse, error) { 812 f := m.GetInjection(MGetReplicas) 813 if f != nil { 814 r, err := f(ctx, req) 815 return r.(*milvuspb.GetReplicasResponse), err 816 } 817 resp := &milvuspb.GetReplicasResponse{} 818 s, err := SuccessStatus() 819 resp.Status = s 820 return resp, err 821 } 822 823 // https://wiki.lfaidata.foundation/display/MIL/MEP+24+--+Support+bulk+load 824 func (m *MockServer) Import(_ context.Context, _ *milvuspb.ImportRequest) (*milvuspb.ImportResponse, error) { 825 panic("not implemented") // TODO: Implement 826 } 827 828 func (m *MockServer) GetImportState(_ context.Context, _ *milvuspb.GetImportStateRequest) (*milvuspb.GetImportStateResponse, error) { 829 panic("not implemented") // TODO: Implement 830 } 831 832 func (m *MockServer) ListImportTasks(_ context.Context, _ *milvuspb.ListImportTasksRequest) (*milvuspb.ListImportTasksResponse, error) { 833 panic("not implemented") // TODO: Implement 834 } 835 836 // https://wiki.lfaidata.foundation/display/MIL/MEP+27+--+Support+Basic+Authentication 837 func (m *MockServer) CreateCredential(ctx context.Context, req *milvuspb.CreateCredentialRequest) (*commonpb.Status, error) { 838 f := m.GetInjection(MCreateCredential) 839 if f != nil { 840 r, err := f(ctx, req) 841 return r.(*commonpb.Status), err 842 } 843 return SuccessStatus() 844 } 845 846 func (m *MockServer) UpdateCredential(ctx context.Context, req *milvuspb.UpdateCredentialRequest) (*commonpb.Status, error) { 847 f := m.GetInjection(MUpdateCredential) 848 if f != nil { 849 r, err := f(ctx, req) 850 return r.(*commonpb.Status), err 851 } 852 return SuccessStatus() 853 } 854 855 func (m *MockServer) DeleteCredential(ctx context.Context, req *milvuspb.DeleteCredentialRequest) (*commonpb.Status, error) { 856 f := m.GetInjection(MDeleteCredential) 857 if f != nil { 858 r, err := f(ctx, req) 859 return r.(*commonpb.Status), err 860 } 861 return SuccessStatus() 862 } 863 864 func (m *MockServer) ListCredUsers(ctx context.Context, req *milvuspb.ListCredUsersRequest) (*milvuspb.ListCredUsersResponse, error) { 865 f := m.GetInjection(MListCredUsers) 866 if f != nil { 867 r, err := f(ctx, req) 868 return r.(*milvuspb.ListCredUsersResponse), err 869 } 870 resp := &milvuspb.ListCredUsersResponse{} 871 s, err := SuccessStatus() 872 resp.Status = s 873 return resp, err 874 } 875 876 // https://wiki.lfaidata.foundation/display/MIL/MEP+29+--+Support+Role-Based+Access+Control 877 func (m *MockServer) CreateRole(_ context.Context, _ *milvuspb.CreateRoleRequest) (*commonpb.Status, error) { 878 panic("not implemented") // TODO: Implement 879 } 880 881 func (m *MockServer) DropRole(_ context.Context, _ *milvuspb.DropRoleRequest) (*commonpb.Status, error) { 882 panic("not implemented") // TODO: Implement 883 } 884 885 func (m *MockServer) OperateUserRole(_ context.Context, _ *milvuspb.OperateUserRoleRequest) (*commonpb.Status, error) { 886 panic("not implemented") // TODO: Implement 887 } 888 889 func (m *MockServer) SelectRole(_ context.Context, _ *milvuspb.SelectRoleRequest) (*milvuspb.SelectRoleResponse, error) { 890 panic("not implemented") // TODO: Implement 891 } 892 893 func (m *MockServer) SelectUser(_ context.Context, _ *milvuspb.SelectUserRequest) (*milvuspb.SelectUserResponse, error) { 894 panic("not implemented") // TODO: Implement 895 } 896 897 func (m *MockServer) OperatePrivilege(_ context.Context, _ *milvuspb.OperatePrivilegeRequest) (*commonpb.Status, error) { 898 panic("not implemented") // TODO: Implement 899 } 900 901 func (m *MockServer) SelectGrant(_ context.Context, _ *milvuspb.SelectGrantRequest) (*milvuspb.SelectGrantResponse, error) { 902 panic("not implemented") // TODO: Implement 903 } 904 905 func (m *MockServer) CreateResourceGroup(_ context.Context, _ *milvuspb.CreateResourceGroupRequest) (*commonpb.Status, error) { 906 panic("not implemented") // TODO: Implement 907 } 908 909 func (m *MockServer) DropResourceGroup(_ context.Context, _ *milvuspb.DropResourceGroupRequest) (*commonpb.Status, error) { 910 panic("not implemented") // TODO: Implement 911 } 912 913 func (m *MockServer) TransferNode(_ context.Context, _ *milvuspb.TransferNodeRequest) (*commonpb.Status, error) { 914 panic("not implemented") // TODO: Implement 915 } 916 917 func (m *MockServer) TransferReplica(_ context.Context, _ *milvuspb.TransferReplicaRequest) (*commonpb.Status, error) { 918 panic("not implemented") // TODO: Implement 919 } 920 921 func (m *MockServer) ListResourceGroups(_ context.Context, _ *milvuspb.ListResourceGroupsRequest) (*milvuspb.ListResourceGroupsResponse, error) { 922 panic("not implemented") // TODO: Implement 923 } 924 925 func (m *MockServer) DescribeResourceGroup(_ context.Context, _ *milvuspb.DescribeResourceGroupRequest) (*milvuspb.DescribeResourceGroupResponse, error) { 926 panic("not implemented") // TODO: Implement 927 } 928 929 func (m *MockServer) RenameCollection(_ context.Context, _ *milvuspb.RenameCollectionRequest) (*commonpb.Status, error) { 930 panic("not implemented") // TODO: Implement 931 } 932 933 func (m *MockServer) DescribeAlias(_ context.Context, _ *milvuspb.DescribeAliasRequest) (*milvuspb.DescribeAliasResponse, error) { 934 panic("not implemented") // TODO: Implement 935 } 936 937 func (m *MockServer) ListAliases(_ context.Context, _ *milvuspb.ListAliasesRequest) (*milvuspb.ListAliasesResponse, error) { 938 panic("not implemented") // TODO: Implement 939 } 940 941 func (m *MockServer) FlushAll(_ context.Context, _ *milvuspb.FlushAllRequest) (*milvuspb.FlushAllResponse, error) { 942 panic("not implemented") // TODO: Implement 943 } 944 945 func (m *MockServer) GetFlushAllState(_ context.Context, _ *milvuspb.GetFlushAllStateRequest) (*milvuspb.GetFlushAllStateResponse, error) { 946 panic("not implemented") // TODO: Implement 947 } 948 949 func (m *MockServer) ListIndexedSegment(_ context.Context, _ *federpb.ListIndexedSegmentRequest) (*federpb.ListIndexedSegmentResponse, error) { 950 panic("not implemented") // TODO: Implement 951 } 952 953 func (m *MockServer) DescribeSegmentIndexData(_ context.Context, _ *federpb.DescribeSegmentIndexDataRequest) (*federpb.DescribeSegmentIndexDataResponse, error) { 954 panic("not implemented") // TODO: Implement 955 } 956 957 func (m *MockServer) GetIndexStatistics(_ context.Context, _ *milvuspb.GetIndexStatisticsRequest) (*milvuspb.GetIndexStatisticsResponse, error) { 958 panic("not implemented") // TODO: Implement 959 } 960 961 func (m *MockServer) AllocTimestamp(_ context.Context, _ *milvuspb.AllocTimestampRequest) (*milvuspb.AllocTimestampResponse, error) { 962 panic("not implemented") 963 } 964 965 func (m *MockServer) ReplicateMessage(ctx context.Context, req *milvuspb.ReplicateMessageRequest) (*milvuspb.ReplicateMessageResponse, error) { 966 f := m.GetInjection(MReplicateMessage) 967 if f != nil { 968 r, err := f(ctx, req) 969 return r.(*milvuspb.ReplicateMessageResponse), err 970 } 971 s, err := SuccessStatus() 972 return &milvuspb.ReplicateMessageResponse{Status: s}, err 973 } 974 975 func (m *MockServer) Connect(_ context.Context, _ *milvuspb.ConnectRequest) (*milvuspb.ConnectResponse, error) { 976 return &milvuspb.ConnectResponse{ 977 Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, 978 Identifier: 1, 979 }, nil 980 } 981 982 //func (m *MockServer) DescribePartition(ctx context.Context, req *milvuspb.DescribePartitionRequest) (*milvuspb.DescribePartitionResponse, error) { 983 // panic("not implemented") // TODO: Implement 984 //} 985 986 func (m *MockServer) GetComponentStates(ctx context.Context, req *milvuspb.GetComponentStatesRequest) (*milvuspb.ComponentStates, error) { 987 f := m.GetInjection(MGetComponentStates) 988 if f != nil { 989 r, err := f(ctx, req) 990 return r.(*milvuspb.ComponentStates), err 991 } 992 s, err := SuccessStatus() 993 return &milvuspb.ComponentStates{Status: s}, err 994 } 995 996 func (m *MockServer) GetVersion(ctx context.Context, req *milvuspb.GetVersionRequest) (*milvuspb.GetVersionResponse, error) { 997 f := m.GetInjection(MGetVersion) 998 if f != nil { 999 r, err := f(ctx, req) 1000 return r.(*milvuspb.GetVersionResponse), err 1001 } 1002 s, err := SuccessStatus() 1003 return &milvuspb.GetVersionResponse{Status: s}, err 1004 } 1005 1006 func (m *MockServer) CheckHealth(ctx context.Context, req *milvuspb.CheckHealthRequest) (*milvuspb.CheckHealthResponse, error) { 1007 f := m.GetInjection(MCheckHealth) 1008 if f != nil { 1009 r, err := f(ctx, req) 1010 return r.(*milvuspb.CheckHealthResponse), err 1011 } 1012 s, err := SuccessStatus() 1013 return &milvuspb.CheckHealthResponse{Status: s}, err 1014 } 1015 1016 func getSuccessStatus() *commonpb.Status { 1017 return &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success} 1018 } 1019 1020 func SuccessStatus() (*commonpb.Status, error) { 1021 return &commonpb.Status{ErrorCode: commonpb.ErrorCode_Success}, nil 1022 } 1023 1024 func BadRequestStatus() (*commonpb.Status, error) { 1025 return &commonpb.Status{ErrorCode: commonpb.ErrorCode_IllegalArgument}, errors.New("illegal request type") 1026 } 1027 1028 func BadStatus() (*commonpb.Status, error) { 1029 return &commonpb.Status{ 1030 ErrorCode: commonpb.ErrorCode_UnexpectedError, 1031 Reason: "fail reason", 1032 }, nil 1033 } 1034 1035 func (m *MockServer) Upsert(ctx context.Context, req *milvuspb.UpsertRequest) (*milvuspb.MutationResult, error) { 1036 f := m.GetInjection(MUpsert) 1037 if f != nil { 1038 r, err := f(ctx, req) 1039 return r.(*milvuspb.MutationResult), err 1040 } 1041 s, err := SuccessStatus() 1042 return &milvuspb.MutationResult{Status: s}, err 1043 } 1044 1045 func (m *MockServer) HybridSearch(ctx context.Context, req *milvuspb.HybridSearchRequest) (*milvuspb.SearchResults, error) { 1046 f := m.GetInjection(MSearchV2) 1047 if f != nil { 1048 r, err := f(ctx, req) 1049 return r.(*milvuspb.SearchResults), err 1050 } 1051 status, err := SuccessStatus() 1052 return &milvuspb.SearchResults{Status: status}, err 1053 } 1054 1055 func (m *MockServer) UpdateResourceGroups(_ context.Context, _ *milvuspb.UpdateResourceGroupsRequest) (*commonpb.Status, error) { 1056 return nil, errors.New("not implemented") 1057 }