github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/configuration/envfrom_utils_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  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    30  	"github.com/1aal/kubeblocks/pkg/configuration/core"
    31  	"github.com/1aal/kubeblocks/pkg/controller/component"
    32  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    33  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    34  	testutil "github.com/1aal/kubeblocks/pkg/testutil/k8s"
    35  )
    36  
    37  var _ = Describe("ConfigEnvFrom test", func() {
    38  
    39  	const (
    40  		clusterDefName     = "test-clusterdef"
    41  		clusterVersionName = "test-clusterversion"
    42  		clusterName        = "test-cluster"
    43  
    44  		mysqlCompDefName = "replicasets"
    45  		mysqlCompName    = "mysql"
    46  	)
    47  	var (
    48  		clusterDef     *appsv1alpha1.ClusterDefinition
    49  		clusterVersion *appsv1alpha1.ClusterVersion
    50  		cluster        *appsv1alpha1.Cluster
    51  
    52  		k8sMockClient    *testutil.K8sClientMockHelper
    53  		origCMObject     *corev1.ConfigMap
    54  		configConstraint *appsv1alpha1.ConfigConstraint
    55  	)
    56  
    57  	BeforeEach(func() {
    58  		k8sMockClient = testutil.NewK8sMockClient()
    59  
    60  		cm := testapps.NewCustomizedObj("config/envfrom-config.yaml", &corev1.ConfigMap{},
    61  			testCtx.UseDefaultNamespace())
    62  
    63  		configConstraint = testapps.NewCustomizedObj("config/envfrom-constraint.yaml",
    64  			&appsv1alpha1.ConfigConstraint{})
    65  
    66  		clusterDef = testapps.NewClusterDefFactory(clusterDefName).
    67  			AddComponentDef(testapps.StatefulMySQLComponent, mysqlCompDefName).
    68  			AddConfigTemplate(cm.Name, cm.Name, configConstraint.Name, testCtx.DefaultNamespace, "mysql-config", testapps.DefaultMySQLContainerName).
    69  			GetObject()
    70  		clusterVersion = testapps.NewClusterVersionFactory(clusterVersionName, clusterDefName).
    71  			AddComponentVersion(mysqlCompDefName).
    72  			AddContainerShort("mysql", testapps.ApeCloudMySQLImage).
    73  			GetObject()
    74  		pvcSpec := testapps.NewPVCSpec("1Gi")
    75  		cluster = testapps.NewClusterFactory(testCtx.DefaultNamespace, clusterName,
    76  			clusterDef.Name, clusterVersion.Name).
    77  			AddComponent(mysqlCompName, mysqlCompDefName).
    78  			AddVolumeClaimTemplate(testapps.DataVolumeName, pvcSpec).
    79  			GetObject()
    80  
    81  		origCMObject = cm.DeepCopy()
    82  		origCMObject.Name = core.GetComponentCfgName(clusterName, mysqlCompName, cm.Name)
    83  	})
    84  
    85  	AfterEach(func() {
    86  		k8sMockClient.Finish()
    87  	})
    88  
    89  	Context("test config template inject envfrom", func() {
    90  		It("should inject success", func() {
    91  			reqCtx := intctrlutil.RequestCtx{
    92  				Ctx: ctx,
    93  				Log: logger,
    94  			}
    95  			component, err := component.BuildComponent(
    96  				reqCtx,
    97  				nil,
    98  				cluster,
    99  				clusterDef,
   100  				&clusterDef.Spec.ComponentDefs[0],
   101  				&cluster.Spec.ComponentSpecs[0],
   102  				nil,
   103  				&clusterVersion.Spec.ComponentVersions[0])
   104  			Expect(err).Should(Succeed())
   105  
   106  			podSpec := &corev1.PodSpec{
   107  				Containers: []corev1.Container{
   108  					{
   109  						Name: testapps.DefaultMySQLContainerName,
   110  					},
   111  				},
   112  			}
   113  			k8sMockClient.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
   114  				origCMObject,
   115  				configConstraint,
   116  			}), testutil.WithAnyTimes()))
   117  			k8sMockClient.MockCreateMethod(testutil.WithCreateReturned(testutil.WithCreatedFailedResult(), testutil.WithTimes(1)),
   118  				testutil.WithCreateReturned(testutil.WithCreatedSucceedResult(), testutil.WithAnyTimes()))
   119  
   120  			Expect(injectTemplateEnvFrom(cluster, component, podSpec, k8sMockClient.Client(), reqCtx.Ctx, nil)).ShouldNot(Succeed())
   121  			Expect(injectTemplateEnvFrom(cluster, component, podSpec, k8sMockClient.Client(), reqCtx.Ctx, nil)).Should(Succeed())
   122  		})
   123  
   124  		It("should SyncEnvConfigmap success", func() {
   125  			configSpec := clusterDef.Spec.ComponentDefs[0].ConfigSpecs[0]
   126  			configSpec.Keys = []string{"env-config"}
   127  
   128  			cmObj := origCMObject.DeepCopy()
   129  			cmObj.SetName(core.GenerateEnvFromName(origCMObject.Name))
   130  			k8sMockClient.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
   131  				cmObj,
   132  				configConstraint,
   133  			}), testutil.WithAnyTimes()))
   134  			k8sMockClient.MockPatchMethod(testutil.WithFailed(core.MakeError("failed to patch"), testutil.WithTimes(1)),
   135  				testutil.WithSucceed(), testutil.WithAnyTimes())
   136  
   137  			Expect(SyncEnvConfigmap(configSpec, origCMObject, &configConstraint.Spec, k8sMockClient.Client(), ctx)).ShouldNot(Succeed())
   138  			Expect(SyncEnvConfigmap(configSpec, origCMObject, &configConstraint.Spec, k8sMockClient.Client(), ctx)).Should(Succeed())
   139  		})
   140  
   141  		It("SyncEnvConfigmap abnormal test", func() {
   142  			configSpec := clusterDef.Spec.ComponentDefs[0].ConfigSpecs[0]
   143  			configSpec.AsEnvFrom = nil
   144  			Expect(SyncEnvConfigmap(configSpec, origCMObject, &configConstraint.Spec, k8sMockClient.Client(), ctx)).Should(Succeed())
   145  
   146  			configSpec.AsEnvFrom = nil
   147  			cmObj := origCMObject.DeepCopy()
   148  			cmObj.SetName(core.GenerateEnvFromName(origCMObject.Name))
   149  			k8sMockClient.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
   150  				cmObj,
   151  				configConstraint,
   152  			}), testutil.WithAnyTimes()))
   153  			k8sMockClient.MockPatchMethod(testutil.WithSucceed(testutil.WithAnyTimes()))
   154  
   155  			configSpec = clusterDef.Spec.ComponentDefs[0].ConfigSpecs[0]
   156  			configSpec.Keys = []string{"env-config", "not-exist"}
   157  			Expect(SyncEnvConfigmap(configSpec, origCMObject, &configConstraint.Spec, k8sMockClient.Client(), ctx)).Should(Succeed())
   158  		})
   159  
   160  	})
   161  
   162  })