// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package grpcdatacoord

import (
	"context"
	"testing"

	"github.com/cockroachdb/errors"

	"github.com/milvus-io/milvus-proto/go-api/commonpb"
	"github.com/milvus-io/milvus-proto/go-api/milvuspb"
	"github.com/milvus-io/milvus/internal/proto/datapb"
	"github.com/milvus-io/milvus/internal/proto/indexpb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
	"github.com/milvus-io/milvus/internal/types"
	"github.com/milvus-io/milvus/internal/util/paramtable"
	"github.com/stretchr/testify/assert"
	clientv3 "go.etcd.io/etcd/client/v3"
)

type MockDataCoord struct {
	types.DataCoord

	states                    *milvuspb.ComponentStates
	status                    *commonpb.Status
	err                       error
	initErr                   error
	startErr                  error
	stopErr                   error
	regErr                    error
	strResp                   *milvuspb.StringResponse
	infoResp                  *datapb.GetSegmentInfoResponse
	flushResp                 *datapb.FlushResponse
	assignResp                *datapb.AssignSegmentIDResponse
	segStateResp              *datapb.GetSegmentStatesResponse
	binResp                   *datapb.GetInsertBinlogPathsResponse
	colStatResp               *datapb.GetCollectionStatisticsResponse
	partStatResp              *datapb.GetPartitionStatisticsResponse
	recoverResp               *datapb.GetRecoveryInfoResponse
	flushSegResp              *datapb.GetFlushedSegmentsResponse
	SegByStatesResp           *datapb.GetSegmentsByStatesResponse
	configResp                *internalpb.ShowConfigurationsResponse
	metricResp                *milvuspb.GetMetricsResponse
	compactionStateResp       *milvuspb.GetCompactionStateResponse
	manualCompactionResp      *milvuspb.ManualCompactionResponse
	compactionPlansResp       *milvuspb.GetCompactionPlansResponse
	watchChannelsResp         *datapb.WatchChannelsResponse
	getFlushStateResp         *milvuspb.GetFlushStateResponse
	dropVChanResp             *datapb.DropVirtualChannelResponse
	setSegmentStateResp       *datapb.SetSegmentStateResponse
	importResp                *datapb.ImportTaskResponse
	updateSegStatResp         *commonpb.Status
	updateChanPos             *commonpb.Status
	addSegmentResp            *commonpb.Status
	unsetIsImportingStateResp *commonpb.Status
	markSegmentsDroppedResp   *commonpb.Status
	broadCastResp             *commonpb.Status

	createIndexResp           *commonpb.Status
	describeIndexResp         *indexpb.DescribeIndexResponse
	dropIndexResp             *commonpb.Status
	getIndexStateResp         *indexpb.GetIndexStateResponse
	getIndexBuildProgressResp *indexpb.GetIndexBuildProgressResponse
	getSegmentIndexStateResp  *indexpb.GetSegmentIndexStateResponse
	getIndexInfosResp         *indexpb.GetIndexInfoResponse
}

func (m *MockDataCoord) Init() error {
	return m.initErr
}

func (m *MockDataCoord) Start() error {
	return m.startErr
}

func (m *MockDataCoord) Stop() error {
	return m.stopErr
}

func (m *MockDataCoord) Register() error {
	return m.regErr
}

func (*MockDataCoord) SetAddress(address string) {
}

func (m *MockDataCoord) SetEtcdClient(etcdClient *clientv3.Client) {
}

func (m *MockDataCoord) SetRootCoord(rootCoord types.RootCoord) {
}

func (m *MockDataCoord) SetDataNodeCreator(func(context.Context, string) (types.DataNode, error)) {
}

func (m *MockDataCoord) SetIndexNodeCreator(func(context.Context, string) (types.IndexNode, error)) {
}

func (m *MockDataCoord) GetComponentStates(ctx context.Context) (*milvuspb.ComponentStates, error) {
	return m.states, m.err
}

func (m *MockDataCoord) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
	return m.strResp, m.err
}

func (m *MockDataCoord) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
	return m.strResp, m.err
}

func (m *MockDataCoord) GetSegmentInfo(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
	return m.infoResp, m.err
}

func (m *MockDataCoord) Flush(ctx context.Context, req *datapb.FlushRequest) (*datapb.FlushResponse, error) {
	return m.flushResp, m.err
}

func (m *MockDataCoord) AssignSegmentID(ctx context.Context, req *datapb.AssignSegmentIDRequest) (*datapb.AssignSegmentIDResponse, error) {
	return m.assignResp, m.err
}

func (m *MockDataCoord) GetSegmentStates(ctx context.Context, req *datapb.GetSegmentStatesRequest) (*datapb.GetSegmentStatesResponse, error) {
	return m.segStateResp, m.err
}

func (m *MockDataCoord) GetInsertBinlogPaths(ctx context.Context, req *datapb.GetInsertBinlogPathsRequest) (*datapb.GetInsertBinlogPathsResponse, error) {
	return m.binResp, m.err
}

func (m *MockDataCoord) GetCollectionStatistics(ctx context.Context, req *datapb.GetCollectionStatisticsRequest) (*datapb.GetCollectionStatisticsResponse, error) {
	return m.colStatResp, m.err
}

func (m *MockDataCoord) GetPartitionStatistics(ctx context.Context, req *datapb.GetPartitionStatisticsRequest) (*datapb.GetPartitionStatisticsResponse, error) {
	return m.partStatResp, m.err
}

func (m *MockDataCoord) GetSegmentInfoChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
	return m.strResp, m.err
}

func (m *MockDataCoord) SaveBinlogPaths(ctx context.Context, req *datapb.SaveBinlogPathsRequest) (*commonpb.Status, error) {
	return m.status, m.err
}

func (m *MockDataCoord) GetRecoveryInfo(ctx context.Context, req *datapb.GetRecoveryInfoRequest) (*datapb.GetRecoveryInfoResponse, error) {
	return m.recoverResp, m.err
}

func (m *MockDataCoord) GetFlushedSegments(ctx context.Context, req *datapb.GetFlushedSegmentsRequest) (*datapb.GetFlushedSegmentsResponse, error) {
	return m.flushSegResp, m.err
}

func (m *MockDataCoord) GetSegmentsByStates(ctx context.Context, req *datapb.GetSegmentsByStatesRequest) (*datapb.GetSegmentsByStatesResponse, error) {
	return m.SegByStatesResp, m.err
}

func (m *MockDataCoord) ShowConfigurations(ctx context.Context, req *internalpb.ShowConfigurationsRequest) (*internalpb.ShowConfigurationsResponse, error) {
	return m.configResp, m.err
}

func (m *MockDataCoord) GetMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
	return m.metricResp, m.err
}

func (m *MockDataCoord) CompleteCompaction(ctx context.Context, req *datapb.CompactionResult) (*commonpb.Status, error) {
	return m.status, m.err
}

func (m *MockDataCoord) ManualCompaction(ctx context.Context, req *milvuspb.ManualCompactionRequest) (*milvuspb.ManualCompactionResponse, error) {
	return m.manualCompactionResp, m.err
}

func (m *MockDataCoord) GetCompactionState(ctx context.Context, req *milvuspb.GetCompactionStateRequest) (*milvuspb.GetCompactionStateResponse, error) {
	return m.compactionStateResp, m.err
}

func (m *MockDataCoord) GetCompactionStateWithPlans(ctx context.Context, req *milvuspb.GetCompactionPlansRequest) (*milvuspb.GetCompactionPlansResponse, error) {
	return m.compactionPlansResp, m.err
}

func (m *MockDataCoord) WatchChannels(ctx context.Context, req *datapb.WatchChannelsRequest) (*datapb.WatchChannelsResponse, error) {
	return m.watchChannelsResp, m.err
}

func (m *MockDataCoord) GetFlushState(ctx context.Context, req *milvuspb.GetFlushStateRequest) (*milvuspb.GetFlushStateResponse, error) {
	return m.getFlushStateResp, m.err
}

func (m *MockDataCoord) DropVirtualChannel(ctx context.Context, req *datapb.DropVirtualChannelRequest) (*datapb.DropVirtualChannelResponse, error) {
	return m.dropVChanResp, m.err
}

func (m *MockDataCoord) SetSegmentState(ctx context.Context, req *datapb.SetSegmentStateRequest) (*datapb.SetSegmentStateResponse, error) {
	return m.setSegmentStateResp, m.err
}

func (m *MockDataCoord) Import(ctx context.Context, req *datapb.ImportTaskRequest) (*datapb.ImportTaskResponse, error) {
	return m.importResp, m.err
}

func (m *MockDataCoord) UpdateSegmentStatistics(ctx context.Context, req *datapb.UpdateSegmentStatisticsRequest) (*commonpb.Status, error) {
	return m.updateSegStatResp, m.err
}

func (m *MockDataCoord) UpdateChannelCheckpoint(ctx context.Context, req *datapb.UpdateChannelCheckpointRequest) (*commonpb.Status, error) {
	return m.updateChanPos, m.err
}

func (m *MockDataCoord) SaveImportSegment(ctx context.Context, req *datapb.SaveImportSegmentRequest) (*commonpb.Status, error) {
	return m.addSegmentResp, m.err
}

func (m *MockDataCoord) UnsetIsImportingState(context.Context, *datapb.UnsetIsImportingStateRequest) (*commonpb.Status, error) {
	return m.unsetIsImportingStateResp, m.err
}

func (m *MockDataCoord) MarkSegmentsDropped(ctx context.Context, req *datapb.MarkSegmentsDroppedRequest) (*commonpb.Status, error) {
	return m.markSegmentsDroppedResp, m.err
}

func (m *MockDataCoord) BroadcastAlteredCollection(ctx context.Context, req *datapb.AlterCollectionRequest) (*commonpb.Status, error) {
	return m.broadCastResp, m.err
}

func (m *MockDataCoord) CheckHealth(ctx context.Context, req *milvuspb.CheckHealthRequest) (*milvuspb.CheckHealthResponse, error) {
	return &milvuspb.CheckHealthResponse{
		IsHealthy: true,
	}, nil
}

func (m *MockDataCoord) CreateIndex(ctx context.Context, req *indexpb.CreateIndexRequest) (*commonpb.Status, error) {
	return m.createIndexResp, m.err
}

func (m *MockDataCoord) DescribeIndex(ctx context.Context, req *indexpb.DescribeIndexRequest) (*indexpb.DescribeIndexResponse, error) {
	return m.describeIndexResp, m.err
}

func (m *MockDataCoord) GetIndexInfos(ctx context.Context, req *indexpb.GetIndexInfoRequest) (*indexpb.GetIndexInfoResponse, error) {
	return m.getIndexInfosResp, m.err
}

func (m *MockDataCoord) GetIndexState(ctx context.Context, req *indexpb.GetIndexStateRequest) (*indexpb.GetIndexStateResponse, error) {
	return m.getIndexStateResp, m.err
}

func (m *MockDataCoord) GetIndexBuildProgress(ctx context.Context, req *indexpb.GetIndexBuildProgressRequest) (*indexpb.GetIndexBuildProgressResponse, error) {
	return m.getIndexBuildProgressResp, m.err
}

func (m *MockDataCoord) GetSegmentIndexState(ctx context.Context, req *indexpb.GetSegmentIndexStateRequest) (*indexpb.GetSegmentIndexStateResponse, error) {
	return m.getSegmentIndexStateResp, m.err
}

func (m *MockDataCoord) DropIndex(ctx context.Context, req *indexpb.DropIndexRequest) (*commonpb.Status, error) {
	return m.dropIndexResp, m.err
}

func Test_NewServer(t *testing.T) {
	paramtable.Init()
	ctx := context.Background()
	server := NewServer(ctx, nil)
	assert.NotNil(t, server)

	t.Run("Run", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{}
		//indexCoord := mocks.NewMockIndexCoord(t)
		//indexCoord.EXPECT().Init().Return(nil)
		//server.indexCoord = indexCoord

		err := server.Run()
		assert.Nil(t, err)
	})

	t.Run("GetComponentStates", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			states: &milvuspb.ComponentStates{},
		}
		states, err := server.GetComponentStates(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, states)
	})

	t.Run("GetTimeTickChannel", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			strResp: &milvuspb.StringResponse{},
		}
		resp, err := server.GetTimeTickChannel(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetStatisticsChannel", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			strResp: &milvuspb.StringResponse{},
		}
		resp, err := server.GetStatisticsChannel(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetSegmentInfo", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			infoResp: &datapb.GetSegmentInfoResponse{},
		}
		resp, err := server.GetSegmentInfo(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("Flush", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			flushResp: &datapb.FlushResponse{},
		}
		resp, err := server.Flush(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("AssignSegmentID", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			assignResp: &datapb.AssignSegmentIDResponse{},
		}
		resp, err := server.AssignSegmentID(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetSegmentStates", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			segStateResp: &datapb.GetSegmentStatesResponse{},
		}
		resp, err := server.GetSegmentStates(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetInsertBinlogPaths", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			binResp: &datapb.GetInsertBinlogPathsResponse{},
		}
		resp, err := server.GetInsertBinlogPaths(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetCollectionStatistics", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			colStatResp: &datapb.GetCollectionStatisticsResponse{},
		}
		resp, err := server.GetCollectionStatistics(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetPartitionStatistics", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			partStatResp: &datapb.GetPartitionStatisticsResponse{},
		}
		resp, err := server.GetPartitionStatistics(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetSegmentInfoChannel", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			strResp: &milvuspb.StringResponse{},
		}
		resp, err := server.GetSegmentInfoChannel(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("SaveBinlogPaths", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			status: &commonpb.Status{},
		}
		resp, err := server.SaveBinlogPaths(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetRecoveryInfo", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			recoverResp: &datapb.GetRecoveryInfoResponse{},
		}
		resp, err := server.GetRecoveryInfo(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetFlushedSegments", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			flushSegResp: &datapb.GetFlushedSegmentsResponse{},
		}
		resp, err := server.GetFlushedSegments(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("ShowConfigurations", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			configResp: &internalpb.ShowConfigurationsResponse{},
		}
		resp, err := server.ShowConfigurations(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetMetrics", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			metricResp: &milvuspb.GetMetricsResponse{},
		}
		resp, err := server.GetMetrics(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("WatchChannels", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			watchChannelsResp: &datapb.WatchChannelsResponse{},
		}
		resp, err := server.WatchChannels(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetFlushState", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			getFlushStateResp: &milvuspb.GetFlushStateResponse{},
		}
		resp, err := server.GetFlushState(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("DropVirtualChannel", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			dropVChanResp: &datapb.DropVirtualChannelResponse{},
		}
		resp, err := server.DropVirtualChannel(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("ManualCompaction", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			manualCompactionResp: &milvuspb.ManualCompactionResponse{},
		}
		resp, err := server.ManualCompaction(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetCompactionState", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			compactionStateResp: &milvuspb.GetCompactionStateResponse{},
		}
		resp, err := server.GetCompactionState(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("GetCompactionStateWithPlans", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			compactionPlansResp: &milvuspb.GetCompactionPlansResponse{},
		}
		resp, err := server.GetCompactionStateWithPlans(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("set segment state", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			setSegmentStateResp: &datapb.SetSegmentStateResponse{},
		}
		resp, err := server.SetSegmentState(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("import", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			importResp: &datapb.ImportTaskResponse{
				Status: &commonpb.Status{},
			},
		}
		resp, err := server.Import(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("update seg stat", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			updateSegStatResp: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_Success,
			},
		}
		resp, err := server.UpdateSegmentStatistics(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("UpdateChannelCheckpoint", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			updateChanPos: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_Success,
			},
		}
		resp, err := server.UpdateChannelCheckpoint(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("save import segment", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			addSegmentResp: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_Success,
			},
		}
		resp, err := server.SaveImportSegment(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("unset isImporting state", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			unsetIsImportingStateResp: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_Success,
			},
		}
		resp, err := server.UnsetIsImportingState(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("mark segments dropped", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			markSegmentsDroppedResp: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_Success,
			},
		}
		resp, err := server.MarkSegmentsDropped(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("broadcast altered collection", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			broadCastResp: &commonpb.Status{},
		}
		resp, err := server.BroadcastAlteredCollection(ctx, nil)
		assert.Nil(t, err)
		assert.NotNil(t, resp)
	})

	t.Run("CheckHealth", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{}
		ret, err := server.CheckHealth(ctx, nil)
		assert.Nil(t, err)
		assert.Equal(t, true, ret.IsHealthy)
	})

	t.Run("CreateIndex", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			createIndexResp: &commonpb.Status{},
		}
		ret, err := server.CreateIndex(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	t.Run("DescribeIndex", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			describeIndexResp: &indexpb.DescribeIndexResponse{},
		}
		ret, err := server.DescribeIndex(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	t.Run("DropIndex", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			dropIndexResp: &commonpb.Status{},
		}
		ret, err := server.DropIndex(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	t.Run("GetIndexState", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			getIndexStateResp: &indexpb.GetIndexStateResponse{},
		}
		ret, err := server.GetIndexState(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	t.Run("GetIndexBuildProgress", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			getIndexBuildProgressResp: &indexpb.GetIndexBuildProgressResponse{},
		}
		ret, err := server.GetIndexBuildProgress(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	t.Run("GetSegmentIndexState", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			getSegmentIndexStateResp: &indexpb.GetSegmentIndexStateResponse{},
		}
		ret, err := server.GetSegmentIndexState(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	t.Run("GetIndexInfos", func(t *testing.T) {
		server.dataCoord = &MockDataCoord{
			getIndexInfosResp: &indexpb.GetIndexInfoResponse{},
		}
		ret, err := server.GetIndexInfos(ctx, nil)
		assert.NoError(t, err)
		assert.NotNil(t, ret)
	})

	err := server.Stop()
	assert.Nil(t, err)
}

func Test_Run(t *testing.T) {
	ctx := context.Background()
	server := NewServer(ctx, nil)
	assert.NotNil(t, server)

	server.dataCoord = &MockDataCoord{
		regErr: errors.New("error"),
	}

	err := server.Run()
	assert.Error(t, err)

	server.dataCoord = &MockDataCoord{
		startErr: errors.New("error"),
	}

	err = server.Run()
	assert.Error(t, err)

	server.dataCoord = &MockDataCoord{
		initErr: errors.New("error"),
	}

	err = server.Run()
	assert.Error(t, err)

	server.dataCoord = &MockDataCoord{
		stopErr: errors.New("error"),
	}

	err = server.Stop()
	assert.Error(t, err)
}
