-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5635 from crelax/cdc-cli-v2-tso-query
api(ticdc): add /api/v2/tso: get tso from pd
- Loading branch information
Showing
7 changed files
with
228 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v2 | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/tiflow/cdc/capture" | ||
"github.com/pingcap/tiflow/cdc/model" | ||
"github.com/pingcap/tiflow/pkg/upstream" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/client-go/v2/oracle" | ||
pd "github.com/tikv/pd/client" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type mockStatusProvider struct { | ||
mock.Mock | ||
} | ||
|
||
// MockPDClient mocks pd.Client to facilitate unit testing. | ||
type MockPDClient struct { | ||
pd.Client | ||
} | ||
|
||
func (m *MockPDClient) GetTS(ctx context.Context) (int64, int64, error) { | ||
return oracle.GetPhysical(time.Now()), 0, nil | ||
} | ||
|
||
func (p *mockStatusProvider) GetAllChangeFeedStatuses(ctx context.Context) ( | ||
map[model.ChangeFeedID]*model.ChangeFeedStatus, error, | ||
) { | ||
args := p.Called(ctx) | ||
return args.Get(0).(map[model.ChangeFeedID]*model.ChangeFeedStatus), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetChangeFeedStatus(ctx context.Context, | ||
changefeedID model.ChangeFeedID, | ||
) (*model.ChangeFeedStatus, error) { | ||
args := p.Called(ctx, changefeedID) | ||
log.Info("err", zap.Error(args.Error(1))) | ||
return args.Get(0).(*model.ChangeFeedStatus), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetAllChangeFeedInfo(ctx context.Context) ( | ||
map[model.ChangeFeedID]*model.ChangeFeedInfo, error, | ||
) { | ||
args := p.Called(ctx) | ||
return args.Get(0).(map[model.ChangeFeedID]*model.ChangeFeedInfo), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetChangeFeedInfo(ctx context.Context, | ||
changefeedID model.ChangeFeedID, | ||
) (*model.ChangeFeedInfo, error) { | ||
args := p.Called(ctx) | ||
return args.Get(0).(*model.ChangeFeedInfo), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetAllTaskStatuses(ctx context.Context, | ||
changefeedID model.ChangeFeedID, | ||
) (map[model.CaptureID]*model.TaskStatus, error) { | ||
args := p.Called(ctx) | ||
return args.Get(0).(map[model.CaptureID]*model.TaskStatus), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetTaskPositions(ctx context.Context, | ||
changefeedID model.ChangeFeedID, | ||
) (map[model.CaptureID]*model.TaskPosition, error) { | ||
args := p.Called(ctx) | ||
return args.Get(0).(map[model.CaptureID]*model.TaskPosition), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetProcessors(ctx context.Context) ([]*model.ProcInfoSnap, error) { | ||
args := p.Called(ctx) | ||
return args.Get(0).([]*model.ProcInfoSnap), args.Error(1) | ||
} | ||
|
||
func (p *mockStatusProvider) GetCaptures(ctx context.Context) ([]*model.CaptureInfo, error) { | ||
args := p.Called(ctx) | ||
return args.Get(0).([]*model.CaptureInfo), args.Error(1) | ||
} | ||
|
||
type testCase struct { | ||
url string | ||
method string | ||
} | ||
|
||
func newRouter(c *capture.Capture, p *mockStatusProvider) *gin.Engine { | ||
router := gin.New() | ||
RegisterOpenAPIV2Routes(router, NewOpenAPIV2ForTest(c, p)) | ||
return router | ||
} | ||
|
||
func newStatusProvider() *mockStatusProvider { | ||
statusProvider := &mockStatusProvider{} | ||
return statusProvider | ||
} | ||
|
||
func TestGetTso(t *testing.T) { | ||
t.Parallel() | ||
mockPDClient := &MockPDClient{} | ||
mockManager := upstream.NewManager4Test(mockPDClient) | ||
cp := capture.NewCaptureWithManager4Test(mockManager) | ||
router := newRouter(cp, newStatusProvider()) | ||
w := httptest.NewRecorder() | ||
|
||
tc := testCase{url: "/api/v2/tso", method: "GET"} | ||
req, err := http.NewRequestWithContext(context.Background(), tc.method, tc.url, nil) | ||
require.Nil(t, err) | ||
router.ServeHTTP(w, req) | ||
require.Equal(t, 200, w.Code) | ||
} |
Oops, something went wrong.