github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/api/v2/controller_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  	"net/http"
    19  	"net/http/httptest"
    20  	"testing"
    21  
    22  	"github.com/golang/mock/gomock"
    23  	mock_capture "github.com/pingcap/tiflow/cdc/capture/mock"
    24  	mock_controller "github.com/pingcap/tiflow/cdc/controller/mock"
    25  	"github.com/pingcap/tiflow/pkg/errors"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestResignController(t *testing.T) {
    30  	t.Parallel()
    31  	// case 1: get owner successfully.
    32  	{
    33  		ctrl := gomock.NewController(t)
    34  		cp := mock_capture.NewMockCapture(ctrl)
    35  		cp.EXPECT().IsReady().Return(true).AnyTimes()
    36  		cp.EXPECT().IsController().Return(true).AnyTimes()
    37  		mo := mock_controller.NewMockController(ctrl)
    38  		cp.EXPECT().GetController().Return(mo, nil)
    39  		mo.EXPECT().AsyncStop()
    40  		apiV2 := NewOpenAPIV2ForTest(cp, APIV2HelpersImpl{})
    41  		router := newRouter(apiV2)
    42  		w := httptest.NewRecorder()
    43  		req, _ := http.NewRequestWithContext(
    44  			context.Background(),
    45  			"POST",
    46  			"/api/v2/owner/resign",
    47  			nil,
    48  		)
    49  		router.ServeHTTP(w, req)
    50  		require.Equal(t, "{}", w.Body.String())
    51  		require.Equal(t, http.StatusOK, w.Code)
    52  	}
    53  
    54  	// case 2: get owner failed
    55  	{
    56  		ctrl := gomock.NewController(t)
    57  		cp := mock_capture.NewMockCapture(ctrl)
    58  		cp.EXPECT().IsReady().Return(true).AnyTimes()
    59  		cp.EXPECT().IsController().Return(true).AnyTimes()
    60  		cp.EXPECT().GetController().Return(nil, errors.New("fake"))
    61  		apiV2 := NewOpenAPIV2ForTest(cp, APIV2HelpersImpl{})
    62  		router := newRouter(apiV2)
    63  		w := httptest.NewRecorder()
    64  		req, _ := http.NewRequestWithContext(
    65  			context.Background(),
    66  			"POST",
    67  			"/api/v2/owner/resign",
    68  			nil,
    69  		)
    70  		router.ServeHTTP(w, req)
    71  		require.Equal(t, "{}", w.Body.String())
    72  		require.Equal(t, http.StatusOK, w.Code)
    73  	}
    74  }