github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/migration/create_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package migration 21 22 import ( 23 "bytes" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 28 "k8s.io/cli-runtime/pkg/genericiooptions" 29 "k8s.io/client-go/kubernetes/scheme" 30 cmdTest "k8s.io/kubectl/pkg/cmd/testing" 31 32 app "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 33 "github.com/1aal/kubeblocks/pkg/cli/testing" 34 v1alpha1 "github.com/1aal/kubeblocks/pkg/cli/types/migrationapi" 35 ) 36 37 var ( 38 streams genericiooptions.IOStreams 39 out *bytes.Buffer 40 tf *cmdTest.TestFactory 41 ) 42 43 const ( 44 namespace = "test" 45 ) 46 47 var _ = Describe("create", func() { 48 o := &CreateMigrationOptions{} 49 50 BeforeEach(func() { 51 streams, _, out, _ = genericiooptions.NewTestIOStreams() 52 tf = testing.NewTestFactory(namespace) 53 54 _ = app.AddToScheme(scheme.Scheme) 55 56 tf.Client = tf.UnstructuredClient 57 }) 58 59 Context("Input params validate", func() { 60 var err error 61 errMsgArr := make([]string, 0, 3) 62 It("Endpoint with database", func() { 63 o.Source = "user:123456@127.0.0.1:5432/database" 64 err = o.SourceEndpointModel.BuildFromStr(&errMsgArr, o.Source) 65 Expect(err).ShouldNot(HaveOccurred()) 66 Expect(o.SourceEndpointModel.UserName).Should(Equal("user")) 67 Expect(o.SourceEndpointModel.Password).Should(Equal("123456")) 68 Expect(o.SourceEndpointModel.Address).Should(Equal("127.0.0.1:5432")) 69 Expect(o.SourceEndpointModel.Database).Should(Equal("database")) 70 Expect(len(errMsgArr)).Should(Equal(0)) 71 72 o.Sink = "user:123456127.0.0.1:5432/database" 73 err = o.SinkEndpointModel.BuildFromStr(&errMsgArr, o.Sink) 74 Expect(err).ShouldNot(HaveOccurred()) 75 Expect(len(errMsgArr)).Should(Equal(1)) 76 }) 77 78 It("Endpoint with no database", func() { 79 o.Source = "user:123456@127.0.0.1:3306" 80 errMsgArr := make([]string, 0, 3) 81 err = o.SourceEndpointModel.BuildFromStr(&errMsgArr, o.Source) 82 Expect(err).ShouldNot(HaveOccurred()) 83 Expect(o.SourceEndpointModel.UserName).Should(Equal("user")) 84 Expect(o.SourceEndpointModel.Password).Should(Equal("123456")) 85 Expect(o.SourceEndpointModel.Address).Should(Equal("127.0.0.1:3306")) 86 Expect(o.SourceEndpointModel.Database).Should(BeEmpty()) 87 Expect(len(errMsgArr)).Should(Equal(0)) 88 89 o.Sink = "user:123456127.0.0.1:3306" 90 err = o.SinkEndpointModel.BuildFromStr(&errMsgArr, o.Sink) 91 Expect(err).ShouldNot(HaveOccurred()) 92 Expect(len(errMsgArr)).Should(Equal(1)) 93 }) 94 95 It("MigrationObject", func() { 96 o.MigrationObject = []string{"schema_public.table1", "schema2.table2_1", "schema2.table2_2", "schema3"} 97 err = o.MigrationObjectModel.BuildFromStrs(&errMsgArr, o.MigrationObject) 98 Expect(err).ShouldNot(HaveOccurred()) 99 for _, obj := range o.MigrationObjectModel.WhiteList { 100 Expect(obj.SchemaName).Should(BeElementOf("schema_public", "schema2", "schema3")) 101 switch obj.SchemaName { 102 case "schema_public": 103 Expect(len(obj.TableList)).Should(Equal(1)) 104 Expect(obj.TableList[0].TableName).Should(Equal("table1")) 105 Expect(obj.TableList[0].IsAll).Should(BeTrue()) 106 case "schema2": 107 Expect(len(obj.TableList)).Should(Equal(2)) 108 for _, tb := range obj.TableList { 109 Expect(tb.TableName).Should(BeElementOf("table2_1", "table2_2")) 110 Expect(tb.IsAll).Should(BeTrue()) 111 } 112 case "schema3": 113 Expect(obj.IsAll).Should(BeTrue()) 114 } 115 } 116 }) 117 118 It("Steps", func() { 119 o.Steps = make([]string, 0) 120 err = o.BuildWithSteps(&errMsgArr) 121 Expect(err).ShouldNot(HaveOccurred()) 122 Expect(o.TaskType).Should(Equal(InitializationAndCdc.String())) 123 Expect(o.StepsModel).Should(ContainElements(v1alpha1.StepPreCheck.String(), v1alpha1.StepStructPreFullLoad.String(), v1alpha1.StepFullLoad.String())) 124 o.Steps = []string{"precheck=true", "init-struct=false", "cdc=false"} 125 err = o.BuildWithSteps(&errMsgArr) 126 Expect(err).ShouldNot(HaveOccurred()) 127 Expect(o.TaskType).Should(Equal(Initialization.String())) 128 Expect(o.StepsModel).Should(ContainElements(v1alpha1.StepPreCheck.String(), v1alpha1.StepFullLoad.String())) 129 }) 130 131 It("Tolerations", func() { 132 o.Tolerations = []string{ 133 "step=global,key=engineType,value=pg,operator=Equal,effect=NoSchedule", 134 "step=init-data,key=engineType,value=pg1,operator=Equal,effect=NoSchedule", 135 "key=engineType,value=pg2,operator=Equal,effect=NoSchedule", 136 } 137 err = o.BuildWithTolerations() 138 Expect(err).ShouldNot(HaveOccurred()) 139 Expect(o.TolerationModel[v1alpha1.CliStepGlobal.String()]).ShouldNot(BeEmpty()) 140 Expect(o.TolerationModel[v1alpha1.CliStepInitData.String()]).ShouldNot(BeEmpty()) 141 Expect(len(o.TolerationModel[v1alpha1.CliStepInitData.String()])).Should(Equal(1)) 142 Expect(len(o.TolerationModel[v1alpha1.CliStepGlobal.String()])).Should(Equal(2)) 143 Expect(len(o.TolerationModel[v1alpha1.CliStepPreCheck.String()])).Should(Equal(0)) 144 }) 145 146 It("Resources", func() { 147 o.Resources = []string{ 148 "step=global,cpu=1000m,memory=1Gi", 149 "step=init-data,cpu=2000m,memory=2Gi", 150 "cpu=3000m,memory=3Gi", 151 } 152 err = o.BuildWithResources() 153 Expect(err).ShouldNot(HaveOccurred()) 154 Expect(o.ResourceModel[v1alpha1.CliStepGlobal.String()]).ShouldNot(BeEmpty()) 155 Expect(o.ResourceModel[v1alpha1.CliStepInitData.String()]).ShouldNot(BeEmpty()) 156 Expect(o.ResourceModel[v1alpha1.CliStepPreCheck.String()]).Should(BeEmpty()) 157 }) 158 159 It("RuntimeParams", func() { 160 type void struct{} 161 var setValue void 162 serverIDSet := make(map[uint32]void) 163 164 loopCount := 0 165 for loopCount < 1000 { 166 newServerID := o.generateRandomMySQLServerID() 167 Expect(newServerID >= 10001 && newServerID <= 1<<32-10001).Should(BeTrue()) 168 serverIDSet[newServerID] = setValue 169 170 loopCount += 1 171 } 172 Expect(len(serverIDSet) > 500).Should(BeTrue()) 173 }) 174 }) 175 176 Context("Mock run", func() { 177 It("test", func() { 178 cmd := NewMigrationCreateCmd(tf, streams) 179 Expect(cmd).ShouldNot(BeNil()) 180 }) 181 }) 182 })