github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_query_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 "bytes" 18 "io" 19 "os" 20 "testing" 21 22 "github.com/golang/mock/gomock" 23 "github.com/pingcap/errors" 24 v2 "github.com/pingcap/tiflow/cdc/api/v2" 25 "github.com/pingcap/tiflow/cdc/model" 26 "github.com/pingcap/tiflow/pkg/api/v2/mock" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestChangefeedQueryCli(t *testing.T) { 31 ctrl := gomock.NewController(t) 32 defer ctrl.Finish() 33 cfV2 := mock.NewMockChangefeedInterface(ctrl) 34 35 f := &mockFactory{changefeeds: cfV2} 36 37 o := newQueryChangefeedOptions() 38 o.complete(f) 39 cmd := newCmdQueryChangefeed(f) 40 41 cfV2.EXPECT().List(gomock.Any(), gomock.Any(), "all").Return([]v2.ChangefeedCommonInfo{ 42 { 43 UpstreamID: 1, 44 Namespace: "default", 45 ID: "abc", 46 CheckpointTime: model.JSONTime{}, 47 RunningError: nil, 48 }, 49 }, nil) 50 51 o.simplified = true 52 o.changefeedID = "abc" 53 require.Nil(t, o.run(cmd)) 54 cfV2.EXPECT().List(gomock.Any(), gomock.Any(), "all").Return([]v2.ChangefeedCommonInfo{ 55 { 56 UpstreamID: 1, 57 Namespace: "default", 58 ID: "abc", 59 CheckpointTime: model.JSONTime{}, 60 RunningError: nil, 61 }, 62 }, nil) 63 64 o.simplified = true 65 o.changefeedID = "abcd" 66 require.NotNil(t, o.run(cmd)) 67 68 cfV2.EXPECT().List(gomock.Any(), gomock.Any(), "all").Return(nil, errors.New("test")) 69 o.simplified = true 70 o.changefeedID = "abcd" 71 require.NotNil(t, o.run(cmd)) 72 73 // query success 74 cfV2.EXPECT().Get(gomock.Any(), gomock.Any(), "bcd").Return(&v2.ChangeFeedInfo{}, nil) 75 76 o.simplified = false 77 o.changefeedID = "bcd" 78 b := bytes.NewBufferString("") 79 cmd.SetOut(b) 80 require.Nil(t, o.run(cmd)) 81 out, err := io.ReadAll(b) 82 require.Nil(t, err) 83 // make sure config is printed 84 require.Contains(t, string(out), "config") 85 86 // query failed 87 cfV2.EXPECT().Get(gomock.Any(), gomock.Any(), "bcd").Return(nil, errors.New("test")) 88 os.Args = []string{"query", "--simple=false", "--changefeed-id=bcd"} 89 require.NotNil(t, o.run(cmd)) 90 }