github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/configuration/template_wrapper_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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    31  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    32  	"github.com/1aal/kubeblocks/pkg/constant"
    33  	"github.com/1aal/kubeblocks/pkg/controller/component"
    34  	testutil "github.com/1aal/kubeblocks/pkg/testutil/k8s"
    35  )
    36  
    37  var _ = Describe("TemplateWrapperTest", func() {
    38  
    39  	var mockK8sCli *testutil.K8sClientMockHelper
    40  	var clusterObj *appsv1alpha1.Cluster
    41  	var clusterVersionObj *appsv1alpha1.ClusterVersion
    42  	var clusterDefObj *appsv1alpha1.ClusterDefinition
    43  	var clusterComponent *component.SynthesizedComponent
    44  
    45  	mockTemplateWrapper := func() renderWrapper {
    46  		mockConfigTemplater := newTemplateBuilder(clusterName, testCtx.DefaultNamespace, clusterObj, clusterVersionObj, ctx, mockK8sCli.Client())
    47  		Expect(mockConfigTemplater.injectBuiltInObjectsAndFunctions(&corev1.PodSpec{}, clusterComponent.ConfigTemplates, clusterComponent, nil)).Should(Succeed())
    48  		return newTemplateRenderWrapper(mockConfigTemplater, clusterObj, ctx, mockK8sCli.Client())
    49  	}
    50  
    51  	BeforeEach(func() {
    52  		// Add any setup steps that needs to be executed before each test
    53  		mockK8sCli = testutil.NewK8sMockClient()
    54  
    55  		clusterObj, clusterDefObj, clusterVersionObj, _ = newAllFieldsClusterObj(nil, nil, false)
    56  		clusterComponent = newAllFieldsComponent(clusterDefObj, clusterVersionObj)
    57  	})
    58  
    59  	AfterEach(func() {
    60  		DeferCleanup(mockK8sCli.Finish)
    61  	})
    62  
    63  	Context("TestConfigSpec", func() {
    64  		It("TestConfigSpec", func() {
    65  			mockK8sCli.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
    66  				&corev1.ConfigMap{
    67  					ObjectMeta: metav1.ObjectMeta{
    68  						Name:      configSpecName,
    69  						Namespace: testCtx.DefaultNamespace,
    70  					},
    71  					Data: map[string]string{
    72  						configSpecName: testConfigContent,
    73  					},
    74  				},
    75  			}), testutil.WithAnyTimes()))
    76  
    77  			tplWrapper := mockTemplateWrapper()
    78  			Expect(tplWrapper.renderConfigTemplate(clusterObj, clusterComponent, nil, nil)).ShouldNot(Succeed())
    79  		})
    80  
    81  		It("TestConfigSpec with exist configmap", func() {
    82  			mockK8sCli.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
    83  				&corev1.ConfigMap{
    84  					ObjectMeta: metav1.ObjectMeta{
    85  						Name:      cfgcore.GetComponentCfgName(clusterName, clusterComponent.Name, clusterComponent.ConfigTemplates[0].Name),
    86  						Namespace: testCtx.DefaultNamespace,
    87  					},
    88  					Data: map[string]string{
    89  						configSpecName: testConfigContent,
    90  					},
    91  				},
    92  			}), testutil.WithAnyTimes()))
    93  
    94  			tplWrapper := mockTemplateWrapper()
    95  			Expect(tplWrapper.renderConfigTemplate(clusterObj, clusterComponent, nil, nil)).Should(Succeed())
    96  		})
    97  
    98  		It("TestConfigSpec update", func() {
    99  			mockK8sCli.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
   100  				&corev1.ConfigMap{
   101  					ObjectMeta: metav1.ObjectMeta{
   102  						Name:      cfgcore.GetComponentCfgName(clusterName, clusterComponent.Name, clusterComponent.ConfigTemplates[0].Name),
   103  						Namespace: testCtx.DefaultNamespace,
   104  						Labels:    make(map[string]string),
   105  						Annotations: map[string]string{
   106  							constant.CMInsEnableRerenderTemplateKey:       "true",
   107  							constant.KBParameterUpdateSourceAnnotationKey: constant.ReconfigureManagerSource,
   108  						},
   109  					},
   110  					Data: map[string]string{
   111  						configSpecName: testConfigContent,
   112  					},
   113  				},
   114  				&corev1.ConfigMap{
   115  					ObjectMeta: metav1.ObjectMeta{
   116  						Name:        configSpecName,
   117  						Namespace:   testCtx.DefaultNamespace,
   118  						Labels:      make(map[string]string),
   119  						Annotations: make(map[string]string),
   120  					},
   121  					Data: map[string]string{
   122  						"test-config-spec-new": "test-config-spec-update",
   123  					},
   124  				},
   125  				&appsv1alpha1.ConfigConstraint{
   126  					ObjectMeta: metav1.ObjectMeta{
   127  						Name: configSpecName,
   128  					},
   129  					Spec: appsv1alpha1.ConfigConstraintSpec{
   130  						FormatterConfig: &appsv1alpha1.FormatterConfig{
   131  							Format: appsv1alpha1.Ini,
   132  						},
   133  					},
   134  				},
   135  			}), testutil.WithAnyTimes()))
   136  			mockK8sCli.MockPatchMethod(testutil.WithPatchReturned(func(obj client.Object, patch client.Patch) error {
   137  				return nil
   138  			}, testutil.WithAnyTimes()))
   139  
   140  			tplWrapper := mockTemplateWrapper()
   141  			Expect(tplWrapper.renderConfigTemplate(clusterObj, clusterComponent, nil, nil)).Should(Succeed())
   142  		})
   143  
   144  	})
   145  
   146  	Context("TestScriptsSpec", func() {
   147  
   148  		It("TestScriptSpec", func() {
   149  			mockK8sCli.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult([]client.Object{
   150  				&corev1.ConfigMap{
   151  					ObjectMeta: metav1.ObjectMeta{
   152  						Name:      mysqlScriptsConfigName,
   153  						Namespace: testCtx.DefaultNamespace,
   154  					},
   155  					Data: map[string]string{
   156  						configSpecName: testConfigContent,
   157  					},
   158  				},
   159  			}), testutil.WithAnyTimes()))
   160  
   161  			tplWrapper := mockTemplateWrapper()
   162  			Expect(tplWrapper.renderScriptTemplate(clusterObj, clusterComponent, nil)).Should(Succeed())
   163  		})
   164  
   165  		It("TestScriptSpec with exist", func() {
   166  			cmObj := &corev1.ConfigMap{
   167  				ObjectMeta: metav1.ObjectMeta{
   168  					Name:      cfgcore.GetComponentCfgName(clusterName, clusterComponent.Name, clusterComponent.ScriptTemplates[0].Name),
   169  					Namespace: testCtx.DefaultNamespace,
   170  				},
   171  				Data: map[string]string{
   172  					configSpecName: testConfigContent,
   173  				},
   174  			}
   175  			tplWrapper := mockTemplateWrapper()
   176  			Expect(tplWrapper.renderScriptTemplate(clusterObj, clusterComponent, []client.Object{cmObj})).Should(Succeed())
   177  		})
   178  	})
   179  })