github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/api/owner/owner_test.go (about) 1 // Copyright 2020 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 owner 15 16 import ( 17 "context" 18 "fmt" 19 "io" 20 "net/http" 21 "net/http/httptest" 22 "net/url" 23 "testing" 24 25 "github.com/gin-gonic/gin" 26 "github.com/pingcap/tiflow/pkg/httputil" 27 "github.com/stretchr/testify/require" 28 "go.etcd.io/etcd/client/v3/concurrency" 29 ) 30 31 func TestHTTPStatus(t *testing.T) { 32 t.Parallel() 33 router := gin.New() 34 RegisterOwnerAPIRoutes(router, nil) 35 ts := httptest.NewServer(router) 36 defer ts.Close() 37 38 addr := ts.URL 39 testReisgnOwner(t, addr) 40 testHandleChangefeedAdmin(t, addr) 41 testHandleRebalance(t, addr) 42 testHandleMoveTable(t, addr) 43 testHandleChangefeedQuery(t, addr) 44 } 45 46 func testReisgnOwner(t *testing.T, addr string) { 47 uri := fmt.Sprintf("%s/capture/owner/resign", addr) 48 testRequestNonOwnerFailed(t, uri) 49 } 50 51 func testHandleChangefeedAdmin(t *testing.T, addr string) { 52 uri := fmt.Sprintf("%s/capture/owner/admin", addr) 53 testRequestNonOwnerFailed(t, uri) 54 } 55 56 func testHandleRebalance(t *testing.T, addr string) { 57 uri := fmt.Sprintf("%s/capture/owner/rebalance_trigger", addr) 58 testRequestNonOwnerFailed(t, uri) 59 } 60 61 func testHandleMoveTable(t *testing.T, addr string) { 62 uri := fmt.Sprintf("%s/capture/owner/move_table", addr) 63 testRequestNonOwnerFailed(t, uri) 64 } 65 66 func testHandleChangefeedQuery(t *testing.T, addr string) { 67 uri := fmt.Sprintf("%s/capture/owner/changefeed/query", addr) 68 testRequestNonOwnerFailed(t, uri) 69 } 70 71 func testRequestNonOwnerFailed(t *testing.T, uri string) { 72 cli, err := httputil.NewClient(nil) 73 require.Nil(t, err) 74 resp, err := cli.PostForm(context.Background(), uri, url.Values{}) 75 require.Nil(t, err) 76 data, err := io.ReadAll(resp.Body) 77 require.Nil(t, err) 78 defer resp.Body.Close() 79 require.Equal(t, http.StatusBadRequest, resp.StatusCode) 80 require.Equal(t, concurrency.ErrElectionNotLeader.Error(), string(data)) 81 }