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

     1  // Copyright 2022 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  
    19  	"github.com/gin-gonic/gin"
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/cdc/owner"
    22  	pd "github.com/tikv/pd/client"
    23  )
    24  
    25  type testCase struct {
    26  	url    string
    27  	method string
    28  }
    29  
    30  func newRouter(apiV2 OpenAPIV2) *gin.Engine {
    31  	router := gin.New()
    32  	RegisterOpenAPIV2Routes(router, apiV2)
    33  	return router
    34  }
    35  
    36  // mockPDClient mocks pd.Client to facilitate unit testing.
    37  type mockPDClient struct {
    38  	pd.Client
    39  	logicTime int64
    40  	timestamp int64
    41  }
    42  
    43  // UpdateServiceGCSafePoint mocks the corresponding method of a real PDClient
    44  func (c *mockPDClient) UpdateServiceGCSafePoint(ctx context.Context,
    45  	serviceID string, ttl int64, safePoint uint64,
    46  ) (uint64, error) {
    47  	return safePoint, nil
    48  }
    49  
    50  // GetTS of mockPDClient returns a mock tso
    51  func (c *mockPDClient) GetTS(ctx context.Context) (int64, int64, error) {
    52  	return c.timestamp, c.logicTime, nil
    53  }
    54  
    55  // GetClusterID of mockPDClient returns a mock ClusterID
    56  func (c *mockPDClient) GetClusterID(ctx context.Context) uint64 {
    57  	return 123
    58  }
    59  
    60  // Close mocks the Close() method of a PDClient
    61  func (c *mockPDClient) Close() {}
    62  
    63  type mockStatusProvider struct {
    64  	owner.StatusProvider
    65  	changefeedStatus       *model.ChangeFeedStatusForAPI
    66  	changefeedInfo         *model.ChangeFeedInfo
    67  	processors             []*model.ProcInfoSnap
    68  	taskStatus             map[model.CaptureID]*model.TaskStatus
    69  	changefeedInfos        map[model.ChangeFeedID]*model.ChangeFeedInfo
    70  	changefeedStatuses     map[model.ChangeFeedID]*model.ChangeFeedStatusForAPI
    71  	changeFeedSyncedStatus *model.ChangeFeedSyncedStatusForAPI
    72  	err                    error
    73  }
    74  
    75  // GetChangeFeedStatus returns a changefeeds' runtime status.
    76  func (m *mockStatusProvider) GetChangeFeedStatus(ctx context.Context,
    77  	changefeedID model.ChangeFeedID,
    78  ) (*model.ChangeFeedStatusForAPI, error) {
    79  	return m.changefeedStatus, m.err
    80  }
    81  
    82  // GetChangeFeedStatus returns a mock changefeeds' info.
    83  func (m *mockStatusProvider) GetChangeFeedInfo(ctx context.Context,
    84  	changefeedID model.ChangeFeedID,
    85  ) (*model.ChangeFeedInfo, error) {
    86  	return m.changefeedInfo, m.err
    87  }
    88  
    89  // GetProcessors returns a list of mock processor infos.
    90  func (m *mockStatusProvider) GetProcessors(ctx context.Context) (
    91  	[]*model.ProcInfoSnap,
    92  	error,
    93  ) {
    94  	return m.processors, m.err
    95  }
    96  
    97  // GetAllTaskStatuses returns a map of mock task statuses.
    98  func (m *mockStatusProvider) GetAllTaskStatuses(
    99  	ctx context.Context,
   100  	changefeedID model.ChangeFeedID,
   101  ) (
   102  	map[model.CaptureID]*model.TaskStatus,
   103  	error,
   104  ) {
   105  	return m.taskStatus, m.err
   106  }
   107  
   108  // GetAllChangeFeedInfo returns a list of mock changefeed info.
   109  func (m *mockStatusProvider) GetAllChangeFeedInfo(_ context.Context) (
   110  	map[model.ChangeFeedID]*model.ChangeFeedInfo,
   111  	error,
   112  ) {
   113  	return m.changefeedInfos, m.err
   114  }
   115  
   116  // GetAllChangeFeedStatuses returns a list of mock changefeed status.
   117  func (m *mockStatusProvider) GetAllChangeFeedStatuses(_ context.Context) (
   118  	map[model.ChangeFeedID]*model.ChangeFeedStatusForAPI,
   119  	error,
   120  ) {
   121  	return m.changefeedStatuses, m.err
   122  }
   123  
   124  // GetChangeFeedSyncedStatus returns a mock changefeed status.
   125  func (m *mockStatusProvider) GetChangeFeedSyncedStatus(_ context.Context, changefeedID model.ChangeFeedID) (
   126  	*model.ChangeFeedSyncedStatusForAPI,
   127  	error,
   128  ) {
   129  	return m.changeFeedSyncedStatus, m.err
   130  }
   131  
   132  func (m *mockStatusProvider) IsChangefeedOwner(_ context.Context, id model.ChangeFeedID) (bool, error) {
   133  	return true, nil
   134  }