github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/cluster/create_subcmds_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 cluster
    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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/apimachinery/pkg/runtime"
    31  	"k8s.io/apimachinery/pkg/runtime/schema"
    32  	"k8s.io/apimachinery/pkg/version"
    33  	"k8s.io/cli-runtime/pkg/genericiooptions"
    34  	"k8s.io/cli-runtime/pkg/resource"
    35  	fakediscovery "k8s.io/client-go/discovery/fake"
    36  	"k8s.io/client-go/kubernetes/scheme"
    37  	clientfake "k8s.io/client-go/rest/fake"
    38  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    39  
    40  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    41  	"github.com/1aal/kubeblocks/pkg/cli/cluster"
    42  	"github.com/1aal/kubeblocks/pkg/cli/create"
    43  	"github.com/1aal/kubeblocks/pkg/cli/printer"
    44  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    45  	"github.com/1aal/kubeblocks/pkg/cli/types"
    46  )
    47  
    48  var _ = Describe("create cluster by cluster type", func() {
    49  	const (
    50  		clusterType = "mysql"
    51  	)
    52  
    53  	var (
    54  		tf            *cmdtesting.TestFactory
    55  		streams       genericiooptions.IOStreams
    56  		createOptions *create.CreateOptions
    57  		mockClient    = func(data runtime.Object) *cmdtesting.TestFactory {
    58  			tf = testing.NewTestFactory(testing.Namespace)
    59  			codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    60  			tf.UnstructuredClient = &clientfake.RESTClient{
    61  				NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    62  				GroupVersion:         schema.GroupVersion{Group: types.AppsAPIGroup, Version: types.AppsAPIVersion},
    63  				Resp:                 &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, data)},
    64  			}
    65  			tf.Client = tf.UnstructuredClient
    66  			tf.FakeDynamicClient = testing.FakeDynamicClient(data)
    67  			tf.WithDiscoveryClient(cmdtesting.NewFakeCachedDiscoveryClient())
    68  			return tf
    69  		}
    70  	)
    71  
    72  	BeforeEach(func() {
    73  		_ = appsv1alpha1.AddToScheme(scheme.Scheme)
    74  		_ = metav1.AddMetaToScheme(scheme.Scheme)
    75  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    76  		tf = mockClient(testing.FakeClusterVersion())
    77  		createOptions = &create.CreateOptions{
    78  			IOStreams: streams,
    79  			Factory:   tf,
    80  		}
    81  	})
    82  
    83  	AfterEach(func() {
    84  		tf.Cleanup()
    85  	})
    86  
    87  	It("create mysql cluster command", func() {
    88  		By("create commands")
    89  		cmds := buildCreateSubCmds(createOptions)
    90  		Expect(cmds).ShouldNot(BeNil())
    91  		Expect(cmds[0].HasFlags()).Should(BeTrue())
    92  
    93  		By("create command options")
    94  		o, err := newSubCmdsOptions(createOptions, clusterType)
    95  		Expect(err).Should(Succeed())
    96  		Expect(o).ShouldNot(BeNil())
    97  		Expect(o.chartInfo).ShouldNot(BeNil())
    98  
    99  		By("complete")
   100  		var mysqlCmd *cobra.Command
   101  		for _, c := range cmds {
   102  			if c.Name() == clusterType {
   103  				mysqlCmd = c
   104  				break
   105  			}
   106  		}
   107  		o.Format = printer.YAML
   108  		Expect(o.CreateOptions.Complete()).Should(Succeed())
   109  		o.DryRun = "client"
   110  		o.Client = testing.FakeClientSet()
   111  		fakeDiscovery1, _ := o.Client.Discovery().(*fakediscovery.FakeDiscovery)
   112  		fakeDiscovery1.FakedServerVersion = &version.Info{Major: "1", Minor: "27", GitVersion: "v1.27.0"}
   113  		Expect(o.complete(mysqlCmd)).Should(Succeed())
   114  		Expect(o.Name).ShouldNot(BeEmpty())
   115  		Expect(o.values).ShouldNot(BeNil())
   116  		Expect(o.chartInfo.ClusterDef).Should(Equal(apeCloudMysql))
   117  
   118  		By("validate")
   119  		o.chartInfo.ClusterDef = testing.ClusterDefName
   120  		Expect(o.validate()).Should(Succeed())
   121  		Expect(o.values[cluster.VersionSchemaProp.String()]).Should(Equal(testing.ClusterVersionName))
   122  
   123  		By("run")
   124  		o.DryRun = "client"
   125  		o.Client = testing.FakeClientSet()
   126  		fakeDiscovery, _ := o.Client.Discovery().(*fakediscovery.FakeDiscovery)
   127  		fakeDiscovery.FakedServerVersion = &version.Info{Major: "1", Minor: "27", GitVersion: "v1.27.0"}
   128  		Expect(o.run()).Should(Succeed())
   129  	})
   130  })