github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/expose_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 operations
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	corev1 "k8s.io/api/core/v1"
    26  
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    30  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    31  	"github.com/1aal/kubeblocks/pkg/generics"
    32  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    33  )
    34  
    35  var _ = Describe("", func() {
    36  	var (
    37  		randomStr             = testCtx.GetRandomStr()
    38  		clusterDefinitionName = "cluster-definition-for-ops-" + randomStr
    39  		clusterVersionName    = "clusterversion-for-ops-" + randomStr
    40  		clusterName           = "cluster-for-ops-" + randomStr
    41  	)
    42  
    43  	const (
    44  		consensusCompName = "consensus"
    45  	)
    46  
    47  	cleanEnv := func() {
    48  		// must wait till resources deleted and no longer existed before the testcases start,
    49  		// otherwise if later it needs to create some new resource objects with the same name,
    50  		// in race conditions, it will find the existence of old objects, resulting failure to
    51  		// create the new objects.
    52  		By("clean resources")
    53  
    54  		// delete cluster(and all dependent sub-resources), clusterversion and clusterdef
    55  		testapps.ClearClusterResources(&testCtx)
    56  
    57  		// delete rest resources
    58  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    59  		ml := client.HasLabels{testCtx.TestObjLabelKey}
    60  		// namespaced
    61  		testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml)
    62  	}
    63  
    64  	BeforeEach(cleanEnv)
    65  
    66  	AfterEach(cleanEnv)
    67  
    68  	Context("Test OpsRequest", func() {
    69  		It("Test expose OpsRequest", func() {
    70  			reqCtx := intctrlutil.RequestCtx{Ctx: testCtx.Ctx}
    71  			opsRes, _, clusterObject := initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName)
    72  
    73  			By("create Expose opsRequest")
    74  			ops := testapps.NewOpsRequestObj("expose-expose-"+randomStr, testCtx.DefaultNamespace,
    75  				clusterObject.Name, appsv1alpha1.ExposeType)
    76  			ops.Spec.ExposeList = []appsv1alpha1.Expose{
    77  				{
    78  					ComponentOps: appsv1alpha1.ComponentOps{ComponentName: consensusCompName},
    79  					Services: []appsv1alpha1.ClusterComponentService{
    80  						{
    81  							Name:        testapps.ServiceVPCName,
    82  							ServiceType: corev1.ServiceTypeLoadBalancer,
    83  						},
    84  					},
    85  				},
    86  			}
    87  			opsRes.OpsRequest = testapps.CreateOpsRequest(ctx, testCtx, ops)
    88  
    89  			By("mock expose OpsRequest phase is Creating")
    90  			_, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes)
    91  			Expect(err).ShouldNot(HaveOccurred())
    92  			Eventually(testapps.GetOpsRequestPhase(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest))).Should(Equal(appsv1alpha1.OpsCreatingPhase))
    93  
    94  			// do expose action
    95  			_, err = GetOpsManager().Do(reqCtx, k8sClient, opsRes)
    96  			Expect(err).ShouldNot(HaveOccurred())
    97  
    98  			By("Test OpsManager.MainEnter function")
    99  			_, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsRes)
   100  			Expect(err).ShouldNot(HaveOccurred())
   101  		})
   102  	})
   103  })