github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/edit/edit_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 edit
    21  
    22  import (
    23  	"net/http"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	"github.com/spf13/cobra"
    29  	corev1 "k8s.io/api/core/v1"
    30  	"k8s.io/apimachinery/pkg/runtime/schema"
    31  	"k8s.io/cli-runtime/pkg/genericiooptions"
    32  	"k8s.io/cli-runtime/pkg/resource"
    33  	"k8s.io/client-go/kubernetes/scheme"
    34  	"k8s.io/client-go/rest/fake"
    35  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    36  
    37  	"github.com/1aal/kubeblocks/pkg/cli/types"
    38  )
    39  
    40  var _ = Describe("List", func() {
    41  	var (
    42  		streams genericiooptions.IOStreams
    43  		tf      *cmdtesting.TestFactory
    44  	)
    45  
    46  	mockClient := func() *corev1.PodList {
    47  		pods, _, _ := cmdtesting.TestData()
    48  		tf = cmdtesting.NewTestFactory().WithNamespace("test")
    49  		defer tf.Cleanup()
    50  
    51  		codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    52  		tf.UnstructuredClient = &fake.RESTClient{
    53  			NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    54  			Resp:                 &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, pods)},
    55  		}
    56  		return pods
    57  	}
    58  
    59  	AfterEach(func() {
    60  		tf.Cleanup()
    61  	})
    62  
    63  	It("test edit", func() {
    64  		pods := mockClient()
    65  		o := NewEditOptions(tf, streams, schema.GroupVersionResource{Group: "", Resource: "pods", Version: types.K8sCoreAPIVersion})
    66  		cmd := &cobra.Command{
    67  			Use:   "edit-test",
    68  			Short: "edit test.",
    69  			Run: func(cmd *cobra.Command, args []string) {
    70  
    71  			},
    72  		}
    73  		o.AddFlags(cmd)
    74  		podName := pods.Items[0].Name
    75  		Expect(o.Complete(cmd, []string{})).Should(MatchError("missing the name"))
    76  		Expect(o.Complete(cmd, []string{podName})).ShouldNot(HaveOccurred())
    77  	})
    78  })