github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/class_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 apps
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	intctrlutil "github.com/1aal/kubeblocks/pkg/generics"
    30  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    31  )
    32  
    33  var _ = Describe("", func() {
    34  
    35  	var (
    36  		clusterDefRef            = "apecloud-mysql"
    37  		componentDefRef          = "mysql"
    38  		componentClassDefinition *v1alpha1.ComponentClassDefinition
    39  	)
    40  
    41  	cleanEnv := func() {
    42  		// must wait till resources deleted and no longer existed before the testcases start,
    43  		// otherwise if later it needs to create some new resource objects with the same name,
    44  		// in race conditions, it will find the existence of old objects, resulting failure to
    45  		// create the new objects.
    46  		By("clean resources")
    47  
    48  		// delete rest mocked objects
    49  		ml := client.HasLabels{testCtx.TestObjLabelKey}
    50  		testapps.ClearResources(&testCtx, intctrlutil.ComponentResourceConstraintSignature, ml)
    51  		testapps.ClearResources(&testCtx, intctrlutil.ComponentClassDefinitionSignature, ml)
    52  	}
    53  
    54  	BeforeEach(cleanEnv)
    55  
    56  	AfterEach(cleanEnv)
    57  
    58  	It("Class should exist in status", func() {
    59  		testapps.NewComponentResourceConstraintFactory(testapps.DefaultResourceConstraintName).
    60  			AddConstraints(testapps.GeneralResourceConstraint).
    61  			AddSelector(v1alpha1.ClusterResourceConstraintSelector{
    62  				ClusterDefRef: clusterDefRef,
    63  				Components: []v1alpha1.ComponentResourceConstraintSelector{
    64  					{
    65  						ComponentDefRef: componentDefRef,
    66  						Rules:           []string{"c1", "c2", "c3", "c4"},
    67  					},
    68  				},
    69  			}).
    70  			Create(&testCtx).GetObject()
    71  
    72  		componentClassDefinition = testapps.NewComponentClassDefinitionFactory("custom", clusterDefRef, componentDefRef).
    73  			AddClasses([]v1alpha1.ComponentClass{testapps.Class1c1g}).
    74  			Create(&testCtx).GetObject()
    75  
    76  		key := client.ObjectKeyFromObject(componentClassDefinition)
    77  		Eventually(testapps.CheckObj(&testCtx, key, func(g Gomega, pobj *v1alpha1.ComponentClassDefinition) {
    78  			g.Expect(pobj.Status.Classes).ShouldNot(BeEmpty())
    79  			g.Expect(pobj.Status.Classes[0].Name).Should(Equal(testapps.Class1c1gName))
    80  		})).Should(Succeed())
    81  	})
    82  })