github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/accounts/base_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 accounts
    21  
    22  import (
    23  	"net/http"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/apimachinery/pkg/runtime/schema"
    29  	"k8s.io/cli-runtime/pkg/genericiooptions"
    30  	"k8s.io/cli-runtime/pkg/resource"
    31  	"k8s.io/client-go/kubernetes/scheme"
    32  	clientfake "k8s.io/client-go/rest/fake"
    33  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    34  
    35  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    36  	"github.com/1aal/kubeblocks/pkg/cli/types"
    37  )
    38  
    39  var _ = Describe("Base Account Options", func() {
    40  	const (
    41  		namespace   = "test"
    42  		clusterName = "apple"
    43  	)
    44  
    45  	var (
    46  		streams genericiooptions.IOStreams
    47  		tf      *cmdtesting.TestFactory
    48  		cluster = testing.FakeCluster(clusterName, namespace)
    49  		pods    = testing.FakePods(3, namespace, clusterName)
    50  	)
    51  
    52  	BeforeEach(func() {
    53  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    54  		tf = testing.NewTestFactory(namespace)
    55  		codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    56  		httpResp := func(obj runtime.Object) *http.Response {
    57  			return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, obj)}
    58  		}
    59  
    60  		tf.UnstructuredClient = &clientfake.RESTClient{
    61  			GroupVersion:         schema.GroupVersion{Group: types.AppsAPIGroup, Version: types.AppsAPIVersion},
    62  			NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    63  			Client: clientfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    64  				urlPrefix := "/api/v1/namespaces/" + namespace
    65  				mapping := map[string]*http.Response{
    66  					urlPrefix + "/pods":                       httpResp(pods),
    67  					urlPrefix + "/pods/" + pods.Items[0].Name: httpResp(&pods.Items[0]),
    68  				}
    69  				return mapping[req.URL.Path], nil
    70  			}),
    71  		}
    72  
    73  		tf.Client = tf.UnstructuredClient
    74  		tf.FakeDynamicClient = testing.FakeDynamicClient(cluster, testing.FakeClusterDef(), testing.FakeClusterVersion())
    75  	})
    76  
    77  	AfterEach(func() {
    78  		tf.Cleanup()
    79  	})
    80  
    81  	Context("new options", func() {
    82  		It("new option", func() {
    83  			o := NewAccountBaseOptions(tf, streams)
    84  			Expect(o).ShouldNot(BeNil())
    85  		})
    86  
    87  		It("validate options", func() {
    88  			o := NewAccountBaseOptions(tf, streams)
    89  			Expect(o).ShouldNot(BeNil())
    90  			args := []string{}
    91  			Expect(o.Validate(args)).Should(MatchError(errClusterNameorInstName))
    92  
    93  			// add two elements
    94  			By("add two args")
    95  			args = []string{"foo", "bar"}
    96  			Expect(o.Validate(args)).Should(MatchError(errClusterNameNum))
    97  
    98  			// add one element
    99  			By("add one more args, should fail")
   100  			args = []string{"foo"}
   101  			Expect(o.Validate(args)).Should(Succeed())
   102  
   103  			// set pod name
   104  			o.PodName = "testpod"
   105  			Expect(o.Validate(args)).Should(MatchError(errClusterNameorInstName))
   106  			// set component name as well
   107  			o.ComponentName = "testcomponent"
   108  			Expect(o.Validate(args)).Should(MatchError(errCompNameOrInstName))
   109  			// unset pod name, retain
   110  			o.PodName = ""
   111  			Expect(o.Validate(args)).Should(Succeed())
   112  		})
   113  
   114  		It("complete option", func() {
   115  			o := NewAccountBaseOptions(tf, streams)
   116  			Expect(o).ShouldNot(BeNil())
   117  			o.PodName = pods.Items[0].Name
   118  			o.ClusterName = clusterName
   119  			Expect(o.ComponentName).Should(BeEmpty())
   120  
   121  			Expect(o.Complete(tf)).Should(Succeed())
   122  			Expect(o.ComponentName).ShouldNot(BeEmpty())
   123  			Expect(o.Client).ShouldNot(BeNil())
   124  			Expect(o.Dynamic).ShouldNot(BeNil())
   125  			Expect(o.Namespace).Should(Equal(namespace))
   126  			Expect(o.Pod.Name).Should(Equal(pods.Items[0].Name))
   127  
   128  		})
   129  	})
   130  })