github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_capture_list_test.go (about) 1 // Copyright 2021 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 cli 15 16 import ( 17 "os" 18 "testing" 19 20 "github.com/golang/mock/gomock" 21 "github.com/pingcap/errors" 22 "github.com/pingcap/tiflow/cdc/model" 23 apiv2client "github.com/pingcap/tiflow/pkg/api/v2" 24 "github.com/pingcap/tiflow/pkg/api/v2/mock" 25 "github.com/pingcap/tiflow/pkg/cmd/factory" 26 "github.com/stretchr/testify/require" 27 ) 28 29 type mockAPIV2Client struct { 30 apiv2client.APIV2Interface 31 tso apiv2client.TsoInterface 32 changefeeds apiv2client.ChangefeedInterface 33 unsafes apiv2client.UnsafeInterface 34 captures apiv2client.CaptureInterface 35 processors apiv2client.ProcessorInterface 36 } 37 38 func (f *mockAPIV2Client) Changefeeds() apiv2client.ChangefeedInterface { 39 return f.changefeeds 40 } 41 42 func (f *mockAPIV2Client) Tso() apiv2client.TsoInterface { 43 return f.tso 44 } 45 46 func (f *mockAPIV2Client) Unsafe() apiv2client.UnsafeInterface { 47 return f.unsafes 48 } 49 50 func (f *mockAPIV2Client) Captures() apiv2client.CaptureInterface { 51 return f.captures 52 } 53 54 func (f *mockAPIV2Client) Processors() apiv2client.ProcessorInterface { 55 return f.processors 56 } 57 58 type mockFactory struct { 59 factory.Factory 60 captures *mock.MockCaptureInterface 61 changefeeds *mock.MockChangefeedInterface 62 processors *mock.MockProcessorInterface 63 status *mock.MockStatusInterface 64 tso *mock.MockTsoInterface 65 unsafes *mock.MockUnsafeInterface 66 } 67 68 func newMockFactory(ctrl *gomock.Controller) *mockFactory { 69 cps := mock.NewMockCaptureInterface(ctrl) 70 processor := mock.NewMockProcessorInterface(ctrl) 71 cf := mock.NewMockChangefeedInterface(ctrl) 72 statuses := mock.NewMockStatusInterface(ctrl) 73 unsafes := mock.NewMockUnsafeInterface(ctrl) 74 tso := mock.NewMockTsoInterface(ctrl) 75 return &mockFactory{ 76 captures: cps, 77 changefeeds: cf, 78 processors: processor, 79 status: statuses, 80 tso: tso, 81 unsafes: unsafes, 82 } 83 } 84 85 func (f *mockFactory) APIV2Client() (apiv2client.APIV2Interface, error) { 86 return &mockAPIV2Client{ 87 captures: f.captures, 88 changefeeds: f.changefeeds, 89 tso: f.tso, 90 unsafes: f.unsafes, 91 processors: f.processors, 92 }, nil 93 } 94 95 func TestCaptureListCli(t *testing.T) { 96 ctrl := gomock.NewController(t) 97 defer ctrl.Finish() 98 cf := mock.NewMockCaptureInterface(ctrl) 99 f := &mockFactory{captures: cf} 100 cmd := newCmdListCapture(f) 101 cf.EXPECT().List(gomock.Any()).Return([]model.Capture{ 102 { 103 ID: "owner", 104 IsOwner: true, 105 AdvertiseAddr: "127.0.0.1:8300", 106 }, 107 }, nil) 108 os.Args = []string{"list"} 109 require.Nil(t, cmd.Execute()) 110 111 cf.EXPECT().List(gomock.Any()).Return(nil, errors.New("test")) 112 o := newListCaptureOptions() 113 o.complete(f) 114 require.NotNil(t, o.run(cmd)) 115 }