github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/kubeblocks/upgrade_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 kubeblocks 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "github.com/spf13/cobra" 27 appsv1 "k8s.io/api/apps/v1" 28 "k8s.io/cli-runtime/pkg/genericiooptions" 29 clientfake "k8s.io/client-go/rest/fake" 30 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 31 32 "github.com/1aal/kubeblocks/pkg/cli/testing" 33 "github.com/1aal/kubeblocks/pkg/cli/types" 34 "github.com/1aal/kubeblocks/pkg/cli/util/helm" 35 "github.com/1aal/kubeblocks/version" 36 ) 37 38 var _ = Describe("kubeblocks upgrade", func() { 39 var cmd *cobra.Command 40 var streams genericiooptions.IOStreams 41 var tf *cmdtesting.TestFactory 42 43 BeforeEach(func() { 44 streams, _, _, _ = genericiooptions.NewTestIOStreams() 45 tf = cmdtesting.NewTestFactory().WithNamespace(namespace) 46 tf.Client = &clientfake.RESTClient{} 47 }) 48 49 AfterEach(func() { 50 tf.Cleanup() 51 }) 52 53 mockKubeBlocksDeploy := func() *appsv1.Deployment { 54 deploy := &appsv1.Deployment{} 55 deploy.SetLabels(map[string]string{ 56 "app.kubernetes.io/component": "apps", 57 "app.kubernetes.io/name": types.KubeBlocksChartName, 58 "app.kubernetes.io/version": "0.3.0", 59 }) 60 return deploy 61 } 62 63 It("check upgrade", func() { 64 var cfg string 65 cmd = newUpgradeCmd(tf, streams) 66 Expect(cmd).ShouldNot(BeNil()) 67 Expect(cmd.HasSubCommands()).Should(BeFalse()) 68 69 o := &InstallOptions{ 70 Options: Options{ 71 IOStreams: streams, 72 }, 73 } 74 75 By("command without kubeconfig flag") 76 Expect(o.Complete(tf, cmd)).Should(HaveOccurred()) 77 78 cmd.Flags().StringVar(&cfg, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.") 79 cmd.Flags().StringVar(&cfg, "context", "", "The name of the kubeconfig context to use.") 80 Expect(o.Complete(tf, cmd)).To(Succeed()) 81 Expect(o.HelmCfg).ShouldNot(BeNil()) 82 Expect(o.Namespace).To(Equal("test")) 83 }) 84 85 It("double-check when version change", func() { 86 o := &InstallOptions{ 87 Options: Options{ 88 IOStreams: streams, 89 HelmCfg: helm.NewFakeConfig(namespace), 90 Namespace: "default", 91 Client: testing.FakeClientSet(mockKubeBlocksDeploy()), 92 Dynamic: testing.FakeDynamicClient(), 93 }, 94 Version: "0.5.0-fake", 95 Check: false, 96 } 97 Expect(o.Upgrade()).Should(HaveOccurred()) 98 // o.In = bytes.NewBufferString("fake-version") mock input error 99 // Expect(o.Upgrade()).Should(Succeed()) 100 o.autoApprove = true 101 Expect(o.Upgrade()).Should(Succeed()) 102 103 }) 104 105 It("helm ValueOpts upgrade", func() { 106 o := &InstallOptions{ 107 Options: Options{ 108 IOStreams: streams, 109 HelmCfg: helm.NewFakeConfig(namespace), 110 Namespace: "default", 111 Client: testing.FakeClientSet(mockKubeBlocksDeploy()), 112 Dynamic: testing.FakeDynamicClient(), 113 }, 114 Version: "", 115 } 116 o.ValueOpts.Values = []string{"replicaCount=2"} 117 Expect(o.Upgrade()).Should(Succeed()) 118 }) 119 120 It("run upgrade", func() { 121 o := &InstallOptions{ 122 Options: Options{ 123 IOStreams: streams, 124 HelmCfg: helm.NewFakeConfig(namespace), 125 Namespace: "default", 126 Client: testing.FakeClientSet(mockKubeBlocksDeploy()), 127 Dynamic: testing.FakeDynamicClient(), 128 }, 129 Version: version.DefaultKubeBlocksVersion, 130 Check: false, 131 } 132 Expect(o.Upgrade()).Should(Succeed()) 133 Expect(len(o.ValueOpts.Values)).To(Equal(0)) 134 Expect(o.upgradeChart()).Should(Succeed()) 135 }) 136 })