github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/cluster/label_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 cluster
    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  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    29  
    30  	"github.com/1aal/kubeblocks/pkg/cli/types"
    31  )
    32  
    33  var _ = Describe("cluster label", func() {
    34  	var (
    35  		streams genericiooptions.IOStreams
    36  		tf      *cmdtesting.TestFactory
    37  	)
    38  
    39  	BeforeEach(func() {
    40  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    41  		tf = cmdtesting.NewTestFactory().WithNamespace("default")
    42  	})
    43  
    44  	AfterEach(func() {
    45  		tf.Cleanup()
    46  	})
    47  
    48  	It("label command", func() {
    49  		cmd := NewLabelCmd(tf, streams)
    50  		Expect(cmd).ShouldNot(BeNil())
    51  	})
    52  
    53  	Context("complete", func() {
    54  		var o *LabelOptions
    55  		var cmd *cobra.Command
    56  		var args []string
    57  		BeforeEach(func() {
    58  			cmd = NewLabelCmd(tf, streams)
    59  			o = NewLabelOptions(tf, streams, types.ClusterDefGVR())
    60  			args = []string{"c1", "env=dev"}
    61  		})
    62  
    63  		It("args is empty", func() {
    64  			Expect(o.complete(cmd, nil)).Should(Succeed())
    65  			Expect(o.validate()).Should(HaveOccurred())
    66  		})
    67  
    68  		It("cannot set --all and --selector at the same time", func() {
    69  			o.all = true
    70  			o.selector = "status=unhealthy"
    71  			Expect(o.complete(cmd, args)).Should(Succeed())
    72  			Expect(o.validate()).Should(HaveOccurred())
    73  		})
    74  
    75  		It("at least one label update is required", func() {
    76  			Expect(o.complete(cmd, []string{"c1"})).Should(Succeed())
    77  			Expect(o.validate()).Should(HaveOccurred())
    78  		})
    79  
    80  		It("cannot modify and remove label within the same command", func() {
    81  			Expect(o.complete(cmd, []string{"c1", "env=dev", "env-"})).Should(HaveOccurred())
    82  		})
    83  	})
    84  })