github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/upgrade_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  
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    30  	"github.com/1aal/kubeblocks/pkg/generics"
    31  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    32  )
    33  
    34  var _ = Describe("Upgrade OpsRequest", func() {
    35  
    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  	const mysqlImageForUpdate = "docker.io/apecloud/apecloud-mysql-server:8.0.30"
    43  	cleanEnv := func() {
    44  		// must wait till resources deleted and no longer existed before the testcases start,
    45  		// otherwise if later it needs to create some new resource objects with the same name,
    46  		// in race conditions, it will find the existence of old objects, resulting failure to
    47  		// create the new objects.
    48  		By("clean resources")
    49  
    50  		// delete cluster(and all dependent sub-resources), clusterversion and clusterdef
    51  		testapps.ClearClusterResources(&testCtx)
    52  
    53  		// delete rest resources
    54  		inNS := client.InNamespace(testCtx.DefaultNamespace)
    55  		ml := client.HasLabels{testCtx.TestObjLabelKey}
    56  		// namespaced
    57  		testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml)
    58  	}
    59  
    60  	BeforeEach(cleanEnv)
    61  
    62  	AfterEach(cleanEnv)
    63  
    64  	Context("Test OpsRequest", func() {
    65  		It("Test upgrade OpsRequest", func() {
    66  			By("init operations resources ")
    67  			reqCtx := intctrlutil.RequestCtx{Ctx: ctx}
    68  			opsRes, _, clusterObject := initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName)
    69  
    70  			By("create Upgrade Ops")
    71  			newClusterVersionName := "clusterversion-upgrade-" + randomStr
    72  			_ = testapps.NewClusterVersionFactory(newClusterVersionName, clusterDefinitionName).
    73  				AddComponentVersion(statelessComp).AddContainerShort(testapps.DefaultNginxContainerName, "nginx:1.14.2").
    74  				AddComponentVersion(consensusComp).AddContainerShort(testapps.DefaultMySQLContainerName, mysqlImageForUpdate).
    75  				AddComponentVersion(statefulComp).AddContainerShort(testapps.DefaultMySQLContainerName, mysqlImageForUpdate).
    76  				Create(&testCtx).GetObject()
    77  			ops := testapps.NewOpsRequestObj("upgrade-ops-"+randomStr, testCtx.DefaultNamespace,
    78  				clusterObject.Name, appsv1alpha1.UpgradeType)
    79  			ops.Spec.Upgrade = &appsv1alpha1.Upgrade{ClusterVersionRef: newClusterVersionName}
    80  			opsRes.OpsRequest = testapps.CreateOpsRequest(ctx, testCtx, ops)
    81  			mockComponentIsOperating(opsRes.Cluster, appsv1alpha1.UpdatingClusterCompPhase,
    82  				consensusComp, statelessComp, statefulComp) // appsv1alpha1.VerticalScalingPhase
    83  			// TODO: add status condition for VerticalScalingPhase
    84  
    85  			By("mock upgrade OpsRequest phase is Running")
    86  			_, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes)
    87  			Expect(err).ShouldNot(HaveOccurred())
    88  			Eventually(testapps.GetOpsRequestPhase(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest))).Should(Equal(appsv1alpha1.OpsCreatingPhase))
    89  			// do upgrade
    90  			_, err = GetOpsManager().Do(reqCtx, k8sClient, opsRes)
    91  			Expect(err).ShouldNot(HaveOccurred())
    92  
    93  			By("Test OpsManager.MainEnter function ")
    94  			_, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsRes)
    95  			Expect(err).ShouldNot(HaveOccurred())
    96  		})
    97  	})
    98  })