github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/configuration/configuration_controller_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  	"time"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    31  	"github.com/1aal/kubeblocks/pkg/configuration/core"
    32  	cfgutil "github.com/1aal/kubeblocks/pkg/configuration/util"
    33  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    34  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    35  )
    36  
    37  var _ = Describe("Configuration Controller", func() {
    38  
    39  	BeforeEach(cleanEnv)
    40  
    41  	AfterEach(cleanEnv)
    42  
    43  	Context("When updating configuration", func() {
    44  		It("Should reconcile success", func() {
    45  			_, _, clusterObj, clusterVersionObj, synthesizedComp := mockReconcileResource()
    46  
    47  			cfgKey := client.ObjectKey{
    48  				Name:      core.GenerateComponentConfigurationName(clusterName, statefulCompName),
    49  				Namespace: testCtx.DefaultNamespace,
    50  			}
    51  			checkCfgStatus := func(phase appsv1alpha1.ConfigurationPhase) func() bool {
    52  				return func() bool {
    53  					cfg := &appsv1alpha1.Configuration{}
    54  					Expect(k8sClient.Get(ctx, cfgKey, cfg)).Should(Succeed())
    55  					itemStatus := cfg.Status.GetItemStatus(configSpecName)
    56  					return itemStatus != nil && itemStatus.Phase == phase
    57  				}
    58  			}
    59  
    60  			By("wait for configuration status to be init phase.")
    61  			Eventually(checkCfgStatus(appsv1alpha1.CInitPhase)).Should(BeFalse())
    62  			Expect(initConfiguration(&intctrlutil.ResourceCtx{
    63  				Client:        k8sClient,
    64  				Context:       ctx,
    65  				Namespace:     testCtx.DefaultNamespace,
    66  				ClusterName:   clusterName,
    67  				ComponentName: statefulCompName,
    68  			}, synthesizedComp, clusterObj, clusterVersionObj)).Should(Succeed())
    69  
    70  			Eventually(checkCfgStatus(appsv1alpha1.CFinishedPhase)).Should(BeTrue())
    71  
    72  			By("reconfiguring parameters.")
    73  			Eventually(testapps.GetAndChangeObj(&testCtx, cfgKey, func(cfg *appsv1alpha1.Configuration) {
    74  				cfg.Spec.GetConfigurationItem(configSpecName).ConfigFileParams = map[string]appsv1alpha1.ConfigParams{
    75  					"my.cnf": {
    76  						Parameters: map[string]*string{
    77  							"max_connections": cfgutil.ToPointer("1000"),
    78  							"gtid_mode":       cfgutil.ToPointer("ON"),
    79  						},
    80  					},
    81  				}
    82  			})).Should(Succeed())
    83  
    84  			Eventually(func(g Gomega) {
    85  				cfg := &appsv1alpha1.Configuration{}
    86  				g.Expect(k8sClient.Get(ctx, cfgKey, cfg)).Should(Succeed())
    87  				itemStatus := cfg.Status.GetItemStatus(configSpecName)
    88  				g.Expect(itemStatus).ShouldNot(BeNil())
    89  				g.Expect(itemStatus.UpdateRevision).Should(BeEquivalentTo("2"))
    90  				g.Expect(itemStatus.Phase).Should(BeEquivalentTo(appsv1alpha1.CFinishedPhase))
    91  			}, time.Second*60, time.Second*1).Should(Succeed())
    92  		})
    93  
    94  		It("Invalid component test", func() {
    95  			_, _, clusterObj, clusterVersionObj, synthesizedComp := mockReconcileResource()
    96  
    97  			cfgKey := client.ObjectKey{
    98  				Name:      core.GenerateComponentConfigurationName(clusterName, "invalid-component"),
    99  				Namespace: testCtx.DefaultNamespace,
   100  			}
   101  
   102  			Expect(initConfiguration(&intctrlutil.ResourceCtx{
   103  				Client:        k8sClient,
   104  				Context:       ctx,
   105  				Namespace:     testCtx.DefaultNamespace,
   106  				ClusterName:   clusterName,
   107  				ComponentName: "invalid-component",
   108  			}, synthesizedComp, clusterObj, clusterVersionObj)).Should(Succeed())
   109  
   110  			Eventually(func(g Gomega) {
   111  				cfg := &appsv1alpha1.Configuration{}
   112  				g.Expect(k8sClient.Get(ctx, cfgKey, cfg)).Should(Succeed())
   113  				g.Expect(cfg.Status.Message).Should(ContainSubstring("not found cluster component"))
   114  			}, time.Second*60, time.Second*1).Should(Succeed())
   115  		})
   116  	})
   117  
   118  })