github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/api/v2/status_test.go (about)

     1  // Copyright 2023 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v2
    15  
    16  import (
    17  	"context"
    18  	"encoding/json"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/golang/mock/gomock"
    24  	mock_capture "github.com/pingcap/tiflow/cdc/capture/mock"
    25  	"github.com/pingcap/tiflow/cdc/model"
    26  	mock_etcd "github.com/pingcap/tiflow/pkg/etcd/mock"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestGetStatus(t *testing.T) {
    31  	status := testCase{url: "/api/v2/status", method: "GET"}
    32  	helpers := NewMockAPIV2Helpers(gomock.NewController(t))
    33  	cp := mock_capture.NewMockCapture(gomock.NewController(t))
    34  	apiV2 := NewOpenAPIV2ForTest(cp, helpers)
    35  	router := newRouter(apiV2)
    36  	etcdClient := mock_etcd.NewMockCDCEtcdClient(gomock.NewController(t))
    37  
    38  	etcdClient.EXPECT().
    39  		GetClusterID().Return("1234").AnyTimes()
    40  	cp.EXPECT().GetEtcdClient().Return(etcdClient).AnyTimes()
    41  	cp.EXPECT().IsReady().Return(true).AnyTimes()
    42  	cp.EXPECT().IsController().Return(true).AnyTimes()
    43  	cp.EXPECT().Liveness().Return(model.LivenessCaptureStopping).AnyTimes()
    44  	cp.EXPECT().Info().Return(model.CaptureInfo{
    45  		ID: "capture-id",
    46  	}, nil)
    47  
    48  	w := httptest.NewRecorder()
    49  	req, _ := http.NewRequestWithContext(context.Background(), status.method, status.url, nil)
    50  	router.ServeHTTP(w, req)
    51  	resp := model.ServerStatus{}
    52  	err := json.NewDecoder(w.Body).Decode(&resp)
    53  	require.Nil(t, err)
    54  	require.Equal(t, "1234", resp.ClusterID)
    55  	require.Equal(t, model.LivenessCaptureStopping, resp.Liveness)
    56  	require.True(t, resp.IsOwner)
    57  	require.Equal(t, "capture-id", resp.ID)
    58  	require.Equal(t, http.StatusOK, w.Code)
    59  }