github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/configuration/tool_image_builder_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 configuration
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	corev1 "k8s.io/api/core/v1"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	cfgcm "github.com/1aal/kubeblocks/pkg/configuration/config_manager"
    30  	"github.com/1aal/kubeblocks/pkg/constant"
    31  	"github.com/1aal/kubeblocks/pkg/controller/component"
    32  	"github.com/1aal/kubeblocks/pkg/controller/factory"
    33  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    34  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    35  )
    36  
    37  var _ = Describe("ToolsImageBuilderTest", func() {
    38  
    39  	const kbToolsImage = "apecloud/kubeblocks-tools:latest"
    40  
    41  	var noneCommand = []string{"/bin/true"}
    42  	var clusterObj *appsv1alpha1.Cluster
    43  	var clusterVersionObj *appsv1alpha1.ClusterVersion
    44  	var ClusterDefObj *appsv1alpha1.ClusterDefinition
    45  	var clusterComponent *component.SynthesizedComponent
    46  
    47  	BeforeEach(func() {
    48  		// Add any setup steps that needs to be executed before each test
    49  		clusterObj, ClusterDefObj, clusterVersionObj, _ = newAllFieldsClusterObj(nil, nil, false)
    50  		clusterComponent = newAllFieldsComponent(ClusterDefObj, clusterVersionObj)
    51  		viper.SetDefault(constant.KBToolsImage, kbToolsImage)
    52  	})
    53  
    54  	AfterEach(func() {
    55  		// Add any teardown steps that needs to be executed after each test
    56  	})
    57  
    58  	Context("ToolsImageBuilderTest", func() {
    59  		It("TestScriptSpec", func() {
    60  			sts, err := factory.BuildSts(intctrlutil.RequestCtx{
    61  				Ctx: testCtx.Ctx,
    62  				Log: logger,
    63  			}, clusterObj, clusterComponent, "for_test_env")
    64  			Expect(err).Should(Succeed())
    65  
    66  			cfgManagerParams := &cfgcm.CfgManagerBuildParams{
    67  				ManagerName:   constant.ConfigSidecarName,
    68  				CharacterType: clusterComponent.CharacterType,
    69  				ComponentName: clusterComponent.Name,
    70  				SecreteName:   component.GenerateConnCredential(clusterObj.Name),
    71  				EnvConfigName: component.GenerateComponentEnvName(clusterObj.Name, clusterComponent.Name),
    72  				Image:         viper.GetString(constant.KBToolsImage),
    73  				Volumes:       make([]corev1.VolumeMount, 0),
    74  				Cluster:       clusterObj,
    75  				ConfigSpecsBuildParams: []cfgcm.ConfigSpecMeta{{
    76  					ConfigSpecInfo: cfgcm.ConfigSpecInfo{
    77  						ConfigSpec:      clusterComponent.ConfigTemplates[0],
    78  						ReloadType:      appsv1alpha1.TPLScriptType,
    79  						FormatterConfig: appsv1alpha1.FormatterConfig{},
    80  					},
    81  					ToolsImageSpec: &appsv1alpha1.ToolsImageSpec{
    82  						MountPoint: "/opt/images",
    83  						ToolConfigs: []appsv1alpha1.ToolConfig{
    84  							{
    85  								Name:    "test",
    86  								Image:   "test_images",
    87  								Command: noneCommand,
    88  							},
    89  							{
    90  								Name:    "test2",
    91  								Image:   "",
    92  								Command: noneCommand,
    93  							},
    94  							{
    95  								Name:    "test3",
    96  								Image:   "$(KUBEBLOCKS_TOOLS_IMAGE)",
    97  								Command: noneCommand,
    98  							},
    99  						},
   100  					},
   101  				}},
   102  				ConfigLazyRenderedVolumes: make(map[string]corev1.VolumeMount),
   103  			}
   104  			cfgManagerParams.ConfigSpecsBuildParams[0].ConfigSpec.VolumeName = "data"
   105  			cfgManagerParams.ConfigSpecsBuildParams[0].ConfigSpec.LegacyRenderedConfigSpec = &appsv1alpha1.LegacyRenderedTemplateSpec{
   106  				ConfigTemplateExtension: appsv1alpha1.ConfigTemplateExtension{
   107  					Namespace:   testCtx.DefaultNamespace,
   108  					TemplateRef: "secondary_template",
   109  					Policy:      appsv1alpha1.NoneMergePolicy,
   110  				},
   111  			}
   112  			Expect(buildConfigToolsContainer(cfgManagerParams, &sts.Spec.Template.Spec, clusterComponent)).Should(Succeed())
   113  			Expect(4).Should(BeEquivalentTo(len(cfgManagerParams.ToolsContainers)))
   114  			Expect("test_images").Should(BeEquivalentTo(cfgManagerParams.ToolsContainers[0].Image))
   115  			Expect(sts.Spec.Template.Spec.Containers[0].Image).Should(BeEquivalentTo(cfgManagerParams.ToolsContainers[1].Image))
   116  			Expect(kbToolsImage).Should(BeEquivalentTo(cfgManagerParams.ToolsContainers[2].Image))
   117  			Expect(kbToolsImage).Should(BeEquivalentTo(cfgManagerParams.ToolsContainers[3].Image))
   118  			Expect(initSecRenderedToolContainerName).Should(BeEquivalentTo(cfgManagerParams.ToolsContainers[3].Name))
   119  		})
   120  	})
   121  
   122  })