github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controllerutil/resource_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 controllerutil
    21  
    22  import (
    23  	"context"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    32  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    33  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    34  	testutil "github.com/1aal/kubeblocks/pkg/testutil/k8s"
    35  )
    36  
    37  const (
    38  	clusterDefName     = "test-clusterdef"
    39  	clusterVersionName = "test-clusterversion"
    40  	clusterName        = "test-cluster"
    41  
    42  	mysqlCompDefName = "replicasets"
    43  	mysqlCompName    = "mysql"
    44  	mysqlConfigName  = "mysql-config-template"
    45  	mysqlVolumeName  = "mysql-config"
    46  )
    47  
    48  var _ = Describe("resource Fetcher", func() {
    49  
    50  	var (
    51  		k8sMockClient  *testutil.K8sClientMockHelper
    52  		clusterDef     *appsv1alpha1.ClusterDefinition
    53  		clusterVersion *appsv1alpha1.ClusterVersion
    54  		cluster        *appsv1alpha1.Cluster
    55  	)
    56  
    57  	BeforeEach(func() {
    58  		k8sMockClient = testutil.NewK8sMockClient()
    59  		clusterDef = testapps.NewClusterDefFactory(clusterDefName).
    60  			AddComponentDef(testapps.StatefulMySQLComponent, mysqlCompDefName).
    61  			AddConfigTemplate(mysqlConfigName, mysqlConfigName, mysqlConfigName, "default", mysqlVolumeName).
    62  			GetObject()
    63  		clusterVersion = testapps.NewClusterVersionFactory(clusterVersionName, clusterDefName).
    64  			AddComponentVersion(mysqlCompDefName).
    65  			AddContainerShort("mysql", testapps.ApeCloudMySQLImage).
    66  			GetObject()
    67  		pvcSpec := testapps.NewPVCSpec("1Gi")
    68  		cluster = testapps.NewClusterFactory("default", clusterName,
    69  			clusterDef.Name, clusterVersion.Name).
    70  			AddComponent(mysqlCompName, mysqlCompDefName).
    71  			AddVolumeClaimTemplate(testapps.DataVolumeName, pvcSpec).
    72  			GetObject()
    73  	})
    74  
    75  	AfterEach(func() {
    76  		k8sMockClient.Finish()
    77  	})
    78  
    79  	Context("ResourceWrapper", func() {
    80  		It("Should succeed with no error", func() {
    81  			k8sMockClient.MockGetMethod(testutil.WithGetReturned(testutil.WithConstructSimpleGetResult(
    82  				[]client.Object{
    83  					clusterDef,
    84  					clusterVersion,
    85  					cluster,
    86  					testapps.NewConfigMap("default", cfgcore.GetComponentCfgName(clusterName, mysqlCompName, mysqlConfigName)),
    87  					&appsv1alpha1.ConfigConstraint{
    88  						ObjectMeta: metav1.ObjectMeta{
    89  							Name: mysqlConfigName,
    90  						},
    91  					},
    92  				},
    93  			), testutil.WithAnyTimes()))
    94  			err := NewTest(k8sMockClient.Client(), ctx).
    95  				Cluster().
    96  				ClusterDef().
    97  				ClusterVer().
    98  				ClusterComponent().
    99  				ClusterDefComponent().
   100  				ConfigMap(mysqlConfigName).
   101  				ConfigConstraints(mysqlConfigName).
   102  				Configuration().
   103  				Complete()
   104  			Expect(err).Should(Succeed())
   105  		})
   106  	})
   107  
   108  })
   109  
   110  type test struct {
   111  	ResourceFetcher[test]
   112  }
   113  
   114  func NewTest(cli client.Client, ctx context.Context) *test {
   115  	tt := &test{}
   116  	return tt.Init(&ResourceCtx{
   117  		Client:  cli,
   118  		Context: ctx,
   119  
   120  		Namespace:     "default",
   121  		ClusterName:   clusterName,
   122  		ComponentName: mysqlCompName,
   123  	}, tt)
   124  }