github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/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 infrastructure
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	"k8s.io/cli-runtime/pkg/genericiooptions"
    30  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    31  
    32  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    33  	"github.com/1aal/kubeblocks/test/testdata"
    34  )
    35  
    36  var _ = Describe("infra create test", func() {
    37  
    38  	var (
    39  		tf      *cmdtesting.TestFactory
    40  		streams genericiooptions.IOStreams
    41  	)
    42  
    43  	BeforeEach(func() {
    44  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    45  		tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace)
    46  	})
    47  
    48  	AfterEach(func() {
    49  		tf.Cleanup()
    50  	})
    51  
    52  	mockPrivateKeyFile := func(tmpDir string) string {
    53  		privateKeyFile := filepath.Join(tmpDir, "id_rsa.pem")
    54  		Expect(os.WriteFile(privateKeyFile, []byte("private key"), os.ModePerm)).Should(Succeed())
    55  		return privateKeyFile
    56  	}
    57  
    58  	It("test create k8s cluster with config file", func() {
    59  		tmpDir, _ := os.MkdirTemp(os.TempDir(), "test-")
    60  		defer os.RemoveAll(tmpDir)
    61  
    62  		By("Create cluster with config file")
    63  		o := &createOptions{
    64  			clusterOptions: clusterOptions{
    65  				IOStreams: streams,
    66  			}}
    67  		o.checkAndSetDefaultVersion()
    68  		o.clusterConfig = testdata.SubTestDataPath("infrastructure/infra-cluster.yaml")
    69  		Expect(o.Complete()).To(Succeed())
    70  		o.Cluster.User.PrivateKeyPath = mockPrivateKeyFile(tmpDir)
    71  		Expect(o.Validate()).To(Succeed())
    72  	})
    73  
    74  	It("test create k8s cluster with params", func() {
    75  		tmpDir, _ := os.MkdirTemp(os.TempDir(), "test-")
    76  		defer os.RemoveAll(tmpDir)
    77  
    78  		By("Create cluster with config file")
    79  		o := &createOptions{
    80  			clusterOptions: clusterOptions{
    81  				IOStreams: streams,
    82  			}}
    83  		o.checkAndSetDefaultVersion()
    84  
    85  		o.nodes = []string{
    86  			"node0:1.1.1.1:10.128.0.1",
    87  			"node1:1.1.1.2:10.128.0.2",
    88  			"node2:1.1.1.3:10.128.0.3",
    89  		}
    90  		o.Cluster.User.PrivateKeyPath = mockPrivateKeyFile(tmpDir)
    91  		Expect(o.Complete()).Should(Succeed())
    92  		Expect(o.Validate()).ShouldNot(Succeed())
    93  
    94  		o.Cluster.RoleGroup.Master = []string{"node0"}
    95  		o.Cluster.RoleGroup.ETCD = []string{"node0"}
    96  		o.Cluster.RoleGroup.Worker = []string{"node1", "node2"}
    97  		Expect(o.Validate()).Should(Succeed())
    98  	})
    99  
   100  })