github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/addon/addon_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 addon
    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/dynamic/fake"
    33  	"k8s.io/client-go/kubernetes/scheme"
    34  	restfake "k8s.io/client-go/rest/fake"
    35  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    36  
    37  	"github.com/1aal/kubeblocks/pkg/cli/patch"
    38  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    39  	"github.com/1aal/kubeblocks/pkg/cli/types"
    40  )
    41  
    42  const (
    43  	testNamespace = "test"
    44  )
    45  
    46  var _ = Describe("Manage applications related to KubeBlocks", func() {
    47  	var streams genericiooptions.IOStreams
    48  	var tf *cmdtesting.TestFactory
    49  
    50  	BeforeEach(func() {
    51  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    52  		tf = cmdtesting.NewTestFactory().WithNamespace(testNamespace)
    53  	})
    54  
    55  	AfterEach(func() {
    56  		tf.Cleanup()
    57  	})
    58  
    59  	When("Iterate addon sub-cmds", func() {
    60  		It("do sanity check", func() {
    61  			addonCmd := NewAddonCmd(tf, streams)
    62  			Expect(addonCmd).ShouldNot(BeNil())
    63  			Expect(addonCmd.HasSubCommands()).Should(BeTrue())
    64  
    65  			listCmd := newListCmd(tf, streams)
    66  			Expect(listCmd).ShouldNot(BeNil())
    67  			Expect(listCmd.HasSubCommands()).ShouldNot(BeTrue())
    68  
    69  			enableCmd := newEnableCmd(tf, streams)
    70  			Expect(enableCmd).ShouldNot(BeNil())
    71  			Expect(enableCmd.HasSubCommands()).ShouldNot(BeTrue())
    72  
    73  			disableCmd := newDisableCmd(tf, streams)
    74  			Expect(disableCmd).ShouldNot(BeNil())
    75  			Expect(disableCmd.HasSubCommands()).ShouldNot(BeTrue())
    76  
    77  			describeCmd := newDescribeCmd(tf, streams)
    78  			Expect(describeCmd).ShouldNot(BeNil())
    79  			Expect(describeCmd.HasSubCommands()).ShouldNot(BeTrue())
    80  		})
    81  	})
    82  
    83  	When("Validate at enable an addon", func() {
    84  		It("should return error", func() {
    85  			o := &addonCmdOpts{
    86  				Options:          patch.NewOptions(tf, streams, types.AddonGVR()),
    87  				Factory:          tf,
    88  				IOStreams:        streams,
    89  				addonEnableFlags: &addonEnableFlags{},
    90  				complete:         addonEnableDisableHandler,
    91  			}
    92  			addonObj := testing.FakeAddon("addon-test")
    93  			o.addon = *addonObj
    94  			codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    95  			httpResp := func(obj runtime.Object) *http.Response {
    96  				return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, obj)}
    97  			}
    98  			tf.Client = &restfake.RESTClient{
    99  				GroupVersion:         schema.GroupVersion{Group: "version", Version: ""},
   100  				NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
   101  				Client: restfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
   102  					urlPrefix := "/version"
   103  					return map[string]*http.Response{
   104  						urlPrefix: httpResp(testing.FakeServices()),
   105  					}[req.URL.Path], nil
   106  				}),
   107  			}
   108  			tf.FakeDynamicClient = fake.NewSimpleDynamicClient(
   109  				scheme.Scheme, addonObj)
   110  			Expect(o.validate()).Should(HaveOccurred())
   111  		})
   112  	})
   113  
   114  	// When("Enable an addon", func() {
   115  	// 	It("should set addon.spec.install.enabled=true", func() {
   116  	// 		By("Checking install helm chart by fake helm action config")
   117  	// 		enableCmd := newEnableCmd(tf, streams)
   118  	// 		enableCmd.Run(enableCmd, []string{"my-addon"})
   119  	// 	})
   120  	// })
   121  	//
   122  	// When("Disable an addon", func() {
   123  	// 	It("should set addon.spec.install.enabled=false", func() {
   124  	// 		By("Checking install helm chart by fake helm action config")
   125  	// 		disableCmd := newDisableCmd(tf, streams)
   126  	// 		disableCmd.Run(disableCmd, []string{"my-addon"})
   127  	// 	})
   128  	// })
   129  })