github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/kubeblocks/install_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  	"k8s.io/cli-runtime/pkg/genericiooptions"
    28  	clientfake "k8s.io/client-go/rest/fake"
    29  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    30  
    31  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    32  	"github.com/1aal/kubeblocks/pkg/cli/util"
    33  	"github.com/1aal/kubeblocks/pkg/cli/util/helm"
    34  	"github.com/1aal/kubeblocks/version"
    35  )
    36  
    37  const namespace = "test"
    38  
    39  var _ = Describe("kubeblocks install", func() {
    40  	var (
    41  		cmd     *cobra.Command
    42  		streams genericiooptions.IOStreams
    43  		tf      *cmdtesting.TestFactory
    44  	)
    45  
    46  	BeforeEach(func() {
    47  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    48  		tf = cmdtesting.NewTestFactory().WithNamespace(namespace)
    49  		tf.Client = &clientfake.RESTClient{}
    50  	})
    51  
    52  	AfterEach(func() {
    53  		tf.Cleanup()
    54  	})
    55  
    56  	It("check install", func() {
    57  		var cfg string
    58  		cmd = newInstallCmd(tf, streams)
    59  		Expect(cmd).ShouldNot(BeNil())
    60  		Expect(cmd.HasSubCommands()).Should(BeFalse())
    61  
    62  		o := &InstallOptions{
    63  			Options: Options{
    64  				IOStreams: streams,
    65  			},
    66  		}
    67  
    68  		By("command without kubeconfig flag")
    69  		Expect(o.Complete(tf, cmd)).Should(HaveOccurred())
    70  
    71  		cmd.Flags().StringVar(&cfg, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
    72  		cmd.Flags().StringVar(&cfg, "context", "", "The name of the kubeconfig context to use.")
    73  		Expect(o.Complete(tf, cmd)).To(Succeed())
    74  		Expect(o.HelmCfg).ShouldNot(BeNil())
    75  		Expect(o.Namespace).To(Equal("test"))
    76  	})
    77  
    78  	It("run install", func() {
    79  		o := &InstallOptions{
    80  			Options: Options{
    81  				IOStreams: streams,
    82  				HelmCfg:   helm.NewFakeConfig(namespace),
    83  				Client:    testing.FakeClientSet(),
    84  				Dynamic:   testing.FakeDynamicClient(),
    85  			},
    86  			Version:         version.DefaultKubeBlocksVersion,
    87  			CreateNamespace: true,
    88  		}
    89  		Expect(o.Install()).Should(HaveOccurred())
    90  		Expect(o.ValueOpts.Values).Should(HaveLen(0))
    91  		Expect(o.installChart()).Should(HaveOccurred())
    92  		o.printNotes()
    93  	})
    94  
    95  	It("checkVersion", func() {
    96  		o := &InstallOptions{
    97  			Options: Options{
    98  				IOStreams: genericiooptions.NewTestIOStreamsDiscard(),
    99  				Client:    testing.FakeClientSet(),
   100  			},
   101  			Check: true,
   102  		}
   103  		By("kubernetes version is empty")
   104  		v := util.Version{}
   105  		Expect(o.checkVersion(v).Error()).Should(ContainSubstring("failed to get kubernetes version"))
   106  
   107  		By("kubernetes is provided by cloud provider")
   108  		v.Kubernetes = "v1.25.0-eks"
   109  		Expect(o.checkVersion(v)).Should(Succeed())
   110  
   111  		By("kubernetes is not provided by cloud provider")
   112  		v.Kubernetes = "v1.25.0"
   113  		Expect(o.checkVersion(v)).Should(Succeed())
   114  	})
   115  
   116  	It("CompleteInstallOptions test", func() {
   117  		o := &InstallOptions{
   118  			Options: Options{
   119  				IOStreams: streams,
   120  				HelmCfg:   helm.NewFakeConfig(namespace),
   121  				Client:    testing.FakeClientSet(),
   122  				Dynamic:   testing.FakeDynamicClient(),
   123  			},
   124  			Version:         version.DefaultKubeBlocksVersion,
   125  			CreateNamespace: true,
   126  		}
   127  		Expect(o.TolerationsRaw).Should(BeNil())
   128  		Expect(o.ValueOpts.JSONValues).Should(BeNil())
   129  		Expect(o.CompleteInstallOptions()).ShouldNot(HaveOccurred())
   130  		Expect(o.TolerationsRaw).Should(Equal([]string{defaultTolerationsForInstallation}))
   131  		Expect(o.ValueOpts.JSONValues).ShouldNot(BeNil())
   132  	})
   133  })