github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/create/create_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 create
    21  
    22  import (
    23  	"fmt"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	"k8s.io/cli-runtime/pkg/genericiooptions"
    29  	clientfake "k8s.io/client-go/rest/fake"
    30  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    31  
    32  	"github.com/1aal/kubeblocks/pkg/cli/printer"
    33  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    34  	"github.com/1aal/kubeblocks/pkg/cli/types"
    35  )
    36  
    37  var _ = Describe("Create", func() {
    38  	const (
    39  		clusterName = "test"
    40  		cueFileName = "create_template_test.cue"
    41  	)
    42  
    43  	var (
    44  		tf      *cmdtesting.TestFactory
    45  		streams genericiooptions.IOStreams
    46  		options CreateOptions
    47  	)
    48  
    49  	BeforeEach(func() {
    50  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    51  		tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace)
    52  		tf.Client = &clientfake.RESTClient{}
    53  		clusterOptions := map[string]interface{}{
    54  			"clusterDefRef":     "test-def",
    55  			"clusterVersionRef": "test-clusterversion-ref",
    56  			"components":        []string{},
    57  			"terminationPolicy": "Halt",
    58  		}
    59  		options = CreateOptions{
    60  			Factory:         tf,
    61  			Name:            clusterName,
    62  			Namespace:       testing.Namespace,
    63  			IOStreams:       streams,
    64  			GVR:             types.ClusterGVR(),
    65  			CueTemplateName: cueFileName,
    66  			Options:         clusterOptions,
    67  		}
    68  	})
    69  
    70  	AfterEach(func() {
    71  		tf.Cleanup()
    72  	})
    73  
    74  	Context("Create Objects", func() {
    75  		It("Complete", func() {
    76  			options.Args = []string{}
    77  			Expect(options.Complete()).Should(Succeed())
    78  		})
    79  
    80  		It("test create with dry-run", func() {
    81  			options.Format = printer.YAML
    82  			testCases := []struct {
    83  				clusterName    string
    84  				isUseDryRun    bool
    85  				mode           string
    86  				dryRunStrategy DryRunStrategy
    87  				success        bool
    88  			}{
    89  				{ // test do not use dry-run strategy
    90  					"test1",
    91  					false,
    92  					"",
    93  					DryRunNone,
    94  					true,
    95  				},
    96  				{ // test no parameter strategy
    97  					"test2",
    98  					true,
    99  					"unchanged",
   100  					DryRunClient,
   101  					true,
   102  				},
   103  				{ // test client strategy
   104  					"test3",
   105  					true,
   106  					"client",
   107  					DryRunClient,
   108  					true,
   109  				},
   110  				{ // test server strategy
   111  					"test4",
   112  					true,
   113  					"server",
   114  					DryRunServer,
   115  					true,
   116  				},
   117  				{ // test error parameter
   118  					"test5",
   119  					true,
   120  					"ape",
   121  					DryRunServer,
   122  					false,
   123  				},
   124  			}
   125  
   126  			for _, t := range testCases {
   127  				By(fmt.Sprintf("when isDryRun %v, dryRunStrategy %v, mode %s",
   128  					t.isUseDryRun, t.dryRunStrategy, t.mode))
   129  				options.Name = t.clusterName
   130  				if t.isUseDryRun {
   131  					options.DryRun = t.mode
   132  				}
   133  				Expect(options.Complete()).Should(Succeed())
   134  
   135  				s, _ := options.GetDryRunStrategy()
   136  				if t.success {
   137  					Expect(s == t.dryRunStrategy).Should(BeTrue())
   138  					Expect(options.Run()).Should(Succeed())
   139  				} else {
   140  					Expect(s).ShouldNot(Equal(t.dryRunStrategy))
   141  				}
   142  			}
   143  		})
   144  	})
   145  })