github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/builder/template/template_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 template
    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("template", func() {
    37  	var (
    38  		tf      *cmdtesting.TestFactory
    39  		streams genericiooptions.IOStreams
    40  	)
    41  
    42  	BeforeEach(func() {
    43  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    44  		tf = testing.NewTestFactory("default")
    45  	})
    46  
    47  	AfterEach(func() {
    48  		tf.Cleanup()
    49  	})
    50  
    51  	testComponentTemplate := func(helmPath string, helmOutput string) {
    52  		cmd := NewComponentTemplateRenderCmd(tf, streams)
    53  		Expect(cmd).ShouldNot(BeNil())
    54  
    55  		if helmPath != "" {
    56  			_ = cmd.Flags().Set("helm", helmPath)
    57  		}
    58  		if helmOutput != "" {
    59  			_ = cmd.Flags().Set("helm-output", helmOutput)
    60  		}
    61  		_ = cmd.Flags().Set("memory", "8Gi")
    62  		_ = cmd.Flags().Set("cpu", "8")
    63  		_ = cmd.Flags().Set("replicas", "3")
    64  		_ = cmd.Flags().Set("all", "true")
    65  		cmd.Run(cmd, []string{})
    66  	}
    67  
    68  	It("should succeed", func() {
    69  		componentRootPath := testdata.SubTestDataPath("../../deploy")
    70  		testComponents := []string{
    71  			"apecloud-mysql",
    72  			"postgresql",
    73  			"redis",
    74  			"clickhouse",
    75  			"kafka",
    76  		}
    77  		helmOutputRoot, err := os.MkdirTemp(os.TempDir(), "test")
    78  		Expect(err).Should(Succeed())
    79  		defer os.RemoveAll(helmOutputRoot)
    80  
    81  		_, err = os.ReadDir(componentRootPath)
    82  		Expect(err).Should(Succeed())
    83  		for _, component := range testComponents {
    84  			componentPath := filepath.Join(componentRootPath, component)
    85  			_, err := os.ReadDir(componentPath)
    86  			Expect(err).Should(Succeed())
    87  			helmOutput := filepath.Join(helmOutputRoot, component)
    88  			Expect(HelmTemplate(componentPath, helmOutput)).Should(Succeed())
    89  			testComponentTemplate(componentPath, helmOutput)
    90  		}
    91  	})
    92  
    93  	It("test config template render without depend on helm", func() {
    94  		testComponentTemplate(testdata.SubTestDataPath("../../deploy/apecloud-mysql"), "")
    95  		testComponentTemplate(testdata.SubTestDataPath("../../deploy/postgresql"), "")
    96  	})
    97  })