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