github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/factory/factory_impl_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 factory 15 16 import ( 17 "context" 18 "testing" 19 20 "github.com/golang/mock/gomock" 21 "github.com/pingcap/errors" 22 "github.com/pingcap/tiflow/cdc/model" 23 v2 "github.com/pingcap/tiflow/pkg/api/v2" 24 "github.com/pingcap/tiflow/pkg/api/v2/mock" 25 cmdcontext "github.com/pingcap/tiflow/pkg/cmd/context" 26 mock_factory "github.com/pingcap/tiflow/pkg/cmd/factory/mock" 27 "github.com/pingcap/tiflow/pkg/security" 28 "github.com/stretchr/testify/require" 29 "google.golang.org/grpc" 30 ) 31 32 func TestFactoryImplPdClient(t *testing.T) { 33 t.Parallel() 34 c := mock_factory.NewMockClientGetter(gomock.NewController(t)) 35 f := factoryImpl{ClientGetter: c} 36 ctx, cancel := context.WithCancel(context.Background()) 37 defer cancel() 38 cmdcontext.SetDefaultContext(ctx) 39 40 var certAllowedCN []string 41 credential := &security.Credential{CertAllowedCN: certAllowedCN} 42 c.EXPECT().GetCredential().Return(credential).Times(2) 43 grpcTLSOption, err := credential.ToGRPCDialOption() 44 require.NoError(t, err) 45 c.EXPECT().ToGRPCDialOption().Return(grpcTLSOption, nil).Times(2) 46 47 pdAddr := "" 48 c.EXPECT().GetPdAddr().Return(pdAddr).Times(1) 49 pdClient, err := f.PdClient() 50 require.Nil(t, pdClient) 51 require.Contains(t, err.Error(), "ErrInvalidServerOption") 52 53 pdAddr = "http://127.0.0.1:10000,https://127.0.0.1:10001" 54 c.EXPECT().GetPdAddr().Return(pdAddr).Times(1) 55 pdClient, err = f.PdClient() 56 require.Nil(t, pdClient) 57 require.Contains(t, err.Error(), "please provide certificate") 58 59 pdAddr = "https://127.0.0.1:10000,https://127.0.0.1:10001" 60 credential = &security.Credential{ 61 CAPath: "../../../tests/integration_tests/_certificates/ca.pem", 62 CertPath: "../../../tests/integration_tests/_certificates/server.pem", 63 KeyPath: "../../../tests/integration_tests/_certificates/server-key.pem", 64 } 65 c.EXPECT().ToGRPCDialOption().DoAndReturn(func() (grpc.DialOption, error) { 66 grpcTLSOption, err = credential.ToGRPCDialOption() 67 require.NoError(t, err) 68 return grpcTLSOption, nil 69 }).Times(1) 70 c.EXPECT().GetCredential().Return(credential).Times(1) 71 c.EXPECT().GetPdAddr().Return(pdAddr).Times(1) 72 pdClient, err = f.PdClient() 73 require.Nil(t, pdClient) 74 require.Contains(t, err.Error(), "Fail to open PD client") 75 } 76 77 type mockAPIClient struct { 78 v2.APIV2Interface 79 status v2.StatusInterface 80 } 81 82 func (c *mockAPIClient) Status() v2.StatusInterface { 83 return c.status 84 } 85 86 func TestCheckCDCVersion(t *testing.T) { 87 ctrl := gomock.NewController(t) 88 defer ctrl.Finish() 89 status := mock.NewMockStatusInterface(ctrl) 90 client := &mockAPIClient{status: status} 91 status.EXPECT().Get(gomock.Any()).Return(nil, errors.New("test")) 92 require.NotNil(t, checkCDCVersion(client)) 93 status.EXPECT().Get(gomock.Any()).Return(&model.ServerStatus{ 94 Version: "", 95 }, nil) 96 require.NotNil(t, checkCDCVersion(client)) 97 status.EXPECT().Get(gomock.Any()).Return(&model.ServerStatus{ 98 Version: "b6.2.0", 99 }, nil) 100 require.NotNil(t, checkCDCVersion(client)) 101 status.EXPECT().Get(gomock.Any()).Return(&model.ServerStatus{ 102 Version: "v6.2.0-master", 103 }, nil) 104 require.Nil(t, checkCDCVersion(client)) 105 status.EXPECT().Get(gomock.Any()).Return(&model.ServerStatus{ 106 Version: "v6.2.0-rc1", 107 }, nil) 108 require.Nil(t, checkCDCVersion(client)) 109 status.EXPECT().Get(gomock.Any()).Return(&model.ServerStatus{ 110 Version: "v6.3.0", 111 }, nil) 112 require.Nil(t, checkCDCVersion(client)) 113 status.EXPECT().Get(gomock.Any()).Return(&model.ServerStatus{ 114 Version: "v6.1.0", 115 }, nil) 116 err := checkCDCVersion(client) 117 require.NotNil(t, err) 118 require.Contains(t, err.Error(), "the cluster version is too old") 119 }