github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/util/completion_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 util
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    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  	"k8s.io/kubectl/pkg/cmd/get"
    35  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    36  
    37  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    38  	"github.com/1aal/kubeblocks/pkg/constant"
    39  )
    40  
    41  var _ = Describe("completion", func() {
    42  	const (
    43  		namespace   = testing.Namespace
    44  		clusterName = testing.ClusterName
    45  	)
    46  
    47  	var (
    48  		tf      *cmdtesting.TestFactory
    49  		streams genericiooptions.IOStreams
    50  		pods    = testing.FakePods(3, namespace, clusterName)
    51  	)
    52  
    53  	BeforeEach(func() {
    54  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    55  		tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace)
    56  	})
    57  
    58  	AfterEach(func() {
    59  		tf.Cleanup()
    60  	})
    61  
    62  	It("test completion pods", func() {
    63  		cmd := get.NewCmdGet("kbcli", tf, streams)
    64  		codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    65  		tf.UnstructuredClient = &clientfake.RESTClient{
    66  			NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    67  			Client: clientfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    68  				switch req.URL.Query().Get(metav1.LabelSelectorQueryParam("v1")) {
    69  				case fmt.Sprintf("%s=%s", constant.RoleLabelKey, "leader"):
    70  					return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &pods.Items[0])}, nil
    71  				case "":
    72  					return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &pods.Items[0])}, nil
    73  				default:
    74  					return nil, fmt.Errorf("unexpected request: %v", req.URL)
    75  				}
    76  			}),
    77  		}
    78  
    79  		Expect(len(CompGetResourceWithLabels(tf, cmd, "pods", []string{}, ""))).Should(Equal(1))
    80  		Expect(len(CompGetResourceWithLabels(tf, cmd, "pods", []string{fmt.Sprintf("%s=%s", constant.RoleLabelKey, "leader")}, ""))).Should(Equal(1))
    81  	})
    82  })