github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/ops_util_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 "time" 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 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 33 "github.com/1aal/kubeblocks/pkg/generics" 34 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 35 ) 36 37 var _ = Describe("OpsUtil functions", func() { 38 39 var ( 40 randomStr = testCtx.GetRandomStr() 41 clusterDefinitionName = "cluster-definition-for-ops-" + randomStr 42 clusterVersionName = "clusterversion-for-ops-" + randomStr 43 clusterName = "cluster-for-ops-" + randomStr 44 ) 45 46 cleanEnv := func() { 47 // must wait till resources deleted and no longer existed before the testcases start, 48 // otherwise if later it needs to create some new resource objects with the same name, 49 // in race conditions, it will find the existence of old objects, resulting failure to 50 // create the new objects. 51 By("clean resources") 52 53 // delete cluster(and all dependent sub-resources), clusterversion and clusterdef 54 testapps.ClearClusterResources(&testCtx) 55 56 // delete rest resources 57 inNS := client.InNamespace(testCtx.DefaultNamespace) 58 ml := client.HasLabels{testCtx.TestObjLabelKey} 59 // namespaced 60 testapps.ClearResources(&testCtx, generics.OpsRequestSignature, inNS, ml) 61 } 62 63 BeforeEach(cleanEnv) 64 65 AfterEach(cleanEnv) 66 67 Context("Test ops_util functions", func() { 68 It("Test ops_util functions", func() { 69 By("init operations resources ") 70 opsRes, _, _ := initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName) 71 72 By("Test the functions in ops_util.go") 73 opsRes.OpsRequest = createHorizontalScaling(clusterName, 1) 74 Expect(patchValidateErrorCondition(ctx, k8sClient, opsRes, "validate error")).Should(Succeed()) 75 Expect(patchOpsHandlerNotSupported(ctx, k8sClient, opsRes)).Should(Succeed()) 76 Expect(isOpsRequestFailedPhase(appsv1alpha1.OpsFailedPhase)).Should(BeTrue()) 77 Expect(PatchClusterNotFound(ctx, k8sClient, opsRes)).Should(Succeed()) 78 }) 79 80 It("Test opsRequest failed cases", func() { 81 By("init operations resources ") 82 opsRes, _, _ := initOperationsResources(clusterDefinitionName, clusterVersionName, clusterName) 83 84 By("Test the functions in ops_util.go") 85 opsRes.OpsRequest = createHorizontalScaling(clusterName, 1) 86 opsRes.OpsRequest.Status.Phase = appsv1alpha1.OpsRunningPhase 87 88 By("mock component failed") 89 clusterComp := opsRes.Cluster.Status.Components[consensusComp] 90 clusterComp.Phase = appsv1alpha1.FailedClusterCompPhase 91 opsRes.Cluster.Status.SetComponentStatus(consensusComp, clusterComp) 92 93 By("expect for opsRequest is running") 94 reqCtx := intctrlutil.RequestCtx{Ctx: ctx} 95 opsPhase, _, err := reconcileActionWithComponentOps(reqCtx, k8sClient, opsRes, "test", handleComponentStatusProgress) 96 Expect(err).Should(BeNil()) 97 Expect(opsPhase).Should(Equal(appsv1alpha1.OpsRunningPhase)) 98 99 By("mock component failed time reaches the threshold, expect for opsRequest is Failed") 100 compStatus := opsRes.OpsRequest.Status.Components[consensusComp] 101 compStatus.LastFailedTime = metav1.Time{Time: compStatus.LastFailedTime.Add(-1 * componentFailedTimeout).Add(-1 * time.Second)} 102 opsRes.OpsRequest.Status.Components[consensusComp] = compStatus 103 opsPhase, _, err = reconcileActionWithComponentOps(reqCtx, k8sClient, opsRes, "test", handleComponentStatusProgress) 104 Expect(err).Should(BeNil()) 105 Expect(opsPhase).Should(Equal(appsv1alpha1.OpsFailedPhase)) 106 107 }) 108 109 }) 110 })