github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/configuration/configuration_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/gomega"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    30  	"github.com/1aal/kubeblocks/pkg/controller/component"
    31  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    32  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    33  )
    34  
    35  const clusterDefName = "test-clusterdef"
    36  const clusterVersionName = "test-clusterversion"
    37  const clusterName = "test-cluster"
    38  const mysqlCompDefName = "replicasets"
    39  const scriptConfigName = "test-script-config"
    40  const configSpecName = "test-config-spec"
    41  const mysqlCompName = "mysql"
    42  const mysqlConfigName = "mysql-component-config"
    43  const mysqlConfigConstraintName = "mysql8.0-config-constraints"
    44  const mysqlScriptsConfigName = "apecloud-mysql-scripts"
    45  const testConfigContent = "test-config-content"
    46  
    47  func allFieldsClusterDefObj(needCreate bool) *appsv1alpha1.ClusterDefinition {
    48  	clusterDefObj := testapps.NewClusterDefFactory(clusterDefName).
    49  		AddComponentDef(testapps.StatefulMySQLComponent, mysqlCompDefName).
    50  		AddScriptTemplate(scriptConfigName, mysqlScriptsConfigName, testCtx.DefaultNamespace, testapps.ScriptsVolumeName, nil).
    51  		AddConfigTemplate(configSpecName, mysqlConfigName, mysqlConfigConstraintName, testCtx.DefaultNamespace, testapps.ConfVolumeName).
    52  		GetObject()
    53  	if needCreate {
    54  		Expect(testCtx.CreateObj(testCtx.Ctx, clusterDefObj)).Should(Succeed())
    55  	}
    56  	return clusterDefObj
    57  }
    58  
    59  func allFieldsClusterVersionObj(needCreate bool) *appsv1alpha1.ClusterVersion {
    60  	clusterVersionObj := testapps.NewClusterVersionFactory(clusterVersionName, clusterDefName).
    61  		AddComponentVersion(mysqlCompDefName).
    62  		AddContainerShort("mysql", testapps.ApeCloudMySQLImage).
    63  		GetObject()
    64  	if needCreate {
    65  		Expect(testCtx.CreateObj(testCtx.Ctx, clusterVersionObj)).Should(Succeed())
    66  	}
    67  	return clusterVersionObj
    68  }
    69  
    70  func newAllFieldsClusterObj(
    71  	clusterDefObj *appsv1alpha1.ClusterDefinition,
    72  	clusterVersionObj *appsv1alpha1.ClusterVersion,
    73  	needCreate bool,
    74  ) (*appsv1alpha1.Cluster, *appsv1alpha1.ClusterDefinition, *appsv1alpha1.ClusterVersion, types.NamespacedName) {
    75  	// setup Cluster obj requires default ClusterDefinition and ClusterVersion objects
    76  	if clusterDefObj == nil {
    77  		clusterDefObj = allFieldsClusterDefObj(needCreate)
    78  	}
    79  	if clusterVersionObj == nil {
    80  		clusterVersionObj = allFieldsClusterVersionObj(needCreate)
    81  	}
    82  	pvcSpec := testapps.NewPVCSpec("1Gi")
    83  	clusterObj := testapps.NewClusterFactory(testCtx.DefaultNamespace, clusterName,
    84  		clusterDefObj.Name, clusterVersionObj.Name).
    85  		AddComponent(mysqlCompName, mysqlCompDefName).SetReplicas(1).
    86  		AddVolumeClaimTemplate(testapps.DataVolumeName, pvcSpec).
    87  		AddService(testapps.ServiceVPCName, corev1.ServiceTypeLoadBalancer).
    88  		AddService(testapps.ServiceInternetName, corev1.ServiceTypeLoadBalancer).
    89  		GetObject()
    90  	key := client.ObjectKeyFromObject(clusterObj)
    91  	if needCreate {
    92  		Expect(testCtx.CreateObj(testCtx.Ctx, clusterObj)).Should(Succeed())
    93  	}
    94  	return clusterObj, clusterDefObj, clusterVersionObj, key
    95  }
    96  
    97  func newAllFieldsComponent(clusterDef *appsv1alpha1.ClusterDefinition, clusterVersion *appsv1alpha1.ClusterVersion) *component.SynthesizedComponent {
    98  	cluster, clusterDef, clusterVersion, _ := newAllFieldsClusterObj(clusterDef, clusterVersion, false)
    99  	component, err := component.BuildComponent(
   100  		intctrlutil.RequestCtx{
   101  			Ctx: testCtx.Ctx,
   102  			Log: logger,
   103  		},
   104  		nil,
   105  		cluster,
   106  		clusterDef,
   107  		&clusterDef.Spec.ComponentDefs[0],
   108  		&cluster.Spec.ComponentSpecs[0],
   109  		nil,
   110  		&clusterVersion.Spec.ComponentVersions[0])
   111  	Expect(err).Should(Succeed())
   112  	Expect(component).ShouldNot(BeNil())
   113  	addTestVolumeMount(component.PodSpec, mysqlCompName)
   114  	return component
   115  }
   116  
   117  func addTestVolumeMount(spec *corev1.PodSpec, containerName string) {
   118  	for i := range spec.Containers {
   119  		container := &spec.Containers[i]
   120  		if container.Name != containerName {
   121  			continue
   122  		}
   123  		container.VolumeMounts = append(container.VolumeMounts,
   124  			corev1.VolumeMount{
   125  				Name:      testapps.ScriptsVolumeName,
   126  				MountPath: "/scripts",
   127  			}, corev1.VolumeMount{
   128  				Name:      testapps.ConfVolumeName,
   129  				MountPath: "/etc/config",
   130  			})
   131  		return
   132  	}
   133  }