github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/restart_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 "sigs.k8s.io/controller-runtime/pkg/client" 26 27 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 28 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 29 "github.com/1aal/kubeblocks/pkg/generics" 30 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 31 ) 32 33 var _ = Describe("Restart OpsRequest", func() { 34 35 var ( 36 randomStr = testCtx.GetRandomStr() 37 clusterDefinitionName = "cluster-definition-for-ops-" + randomStr 38 clusterVersionName = "clusterversion-for-ops-" + randomStr 39 clusterName = "cluster-for-ops-" + randomStr 40 ) 41 42 cleanEnv := func() { 43 // must wait till resources deleted and no longer existed before the testcases start, 44 // otherwise if later it needs to create some new resource objects with the same name, 45 // in race conditions, it will find the existence of old objects, resulting failure to 46 // create the new objects. 47 By("clean resources") 48 49 // delete cluster(and all dependent sub-resources), clusterversion and clusterdef 50 testapps.ClearClusterResources(&testCtx) 51 52 // delete rest resources 53 inNS := client.InNamespace(testCtx.DefaultNamespace) 54 ml := client.HasLabels{testCtx.TestObjLabelKey} 55 // namespaced 56 testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml) 57 } 58 59 BeforeEach(cleanEnv) 60 61 AfterEach(cleanEnv) 62 63 Context("Test OpsRequest", func() { 64 var ( 65 opsRes *OpsResource 66 cluster *appsv1alpha1.Cluster 67 reqCtx intctrlutil.RequestCtx 68 ) 69 BeforeEach(func() { 70 By("init operations resources ") 71 opsRes, _, cluster = initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName) 72 reqCtx = intctrlutil.RequestCtx{Ctx: testCtx.Ctx} 73 }) 74 75 It("Test restart OpsRequest", func() { 76 By("create Restart opsRequest") 77 opsRes.OpsRequest = createRestartOpsObj(clusterName, "restart-ops-"+randomStr) 78 mockComponentIsOperating(opsRes.Cluster, appsv1alpha1.UpdatingClusterCompPhase, consensusComp, statelessComp) 79 80 By("mock restart OpsRequest is Running") 81 _, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes) 82 Expect(err).ShouldNot(HaveOccurred()) 83 Eventually(testapps.GetOpsRequestPhase(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest))).Should(Equal(appsv1alpha1.OpsCreatingPhase)) 84 85 By("test restart action and reconcile function") 86 testapps.MockConsensusComponentStatefulSet(&testCtx, clusterName, consensusComp) 87 testapps.MockStatelessComponentDeploy(&testCtx, clusterName, statelessComp) 88 rHandler := restartOpsHandler{} 89 _ = rHandler.Action(reqCtx, k8sClient, opsRes) 90 91 _, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsRes) 92 Expect(err == nil).Should(BeTrue()) 93 }) 94 95 It("expect failed when cluster is stopped", func() { 96 By("mock cluster is stopped") 97 Expect(testapps.ChangeObjStatus(&testCtx, cluster, func() { 98 cluster.Status.Phase = appsv1alpha1.StoppedClusterPhase 99 })).Should(Succeed()) 100 By("create Restart opsRequest") 101 opsRes.OpsRequest = createRestartOpsObj(clusterName, "restart-ops-"+randomStr) 102 _, err := GetOpsManager().Do(reqCtx, k8sClient, opsRes) 103 Expect(err).Should(HaveOccurred()) 104 Expect(err.Error()).Should(ContainSubstring("OpsRequest.spec.type=Restart is forbidden when Cluster.status.phase=Stopped")) 105 Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(opsRes.OpsRequest), 106 func(g Gomega, fetched *appsv1alpha1.OpsRequest) { 107 g.Expect(fetched.Status.Phase).To(Equal(appsv1alpha1.OpsFailedPhase)) 108 })).Should(Succeed()) 109 }) 110 }) 111 }) 112 113 func createRestartOpsObj(clusterName, restartOpsName string) *appsv1alpha1.OpsRequest { 114 ops := testapps.NewOpsRequestObj(restartOpsName, testCtx.DefaultNamespace, 115 clusterName, appsv1alpha1.RestartType) 116 ops.Spec.RestartList = []appsv1alpha1.ComponentOps{ 117 {ComponentName: consensusComp}, 118 {ComponentName: statelessComp}, 119 } 120 return testapps.CreateOpsRequest(ctx, testCtx, ops) 121 }