github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/util/common_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 util 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 dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1" 30 intctrlutil "github.com/1aal/kubeblocks/pkg/constant" 31 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 32 ) 33 34 var _ = Describe("OpsRequest Controller", func() { 35 36 var ( 37 randomStr = testCtx.GetRandomStr() 38 clusterDefinitionName = "cluster-definition-" + randomStr 39 clusterVersionName = "clusterversion-" + randomStr 40 clusterName = "cluster-" + randomStr 41 consensusCompName = "consensus" 42 ) 43 44 cleanupObjects := func() { 45 err := k8sClient.DeleteAllOf(ctx, &appsv1alpha1.Cluster{}, client.InNamespace(testCtx.DefaultNamespace), client.HasLabels{testCtx.TestObjLabelKey}) 46 Expect(err).NotTo(HaveOccurred()) 47 err = k8sClient.DeleteAllOf(ctx, &appsv1alpha1.OpsRequest{}, client.InNamespace(testCtx.DefaultNamespace), client.HasLabels{testCtx.TestObjLabelKey}) 48 Expect(err).NotTo(HaveOccurred()) 49 } 50 51 BeforeEach(func() { 52 // Add any steup steps that needs to be executed before each test 53 cleanupObjects() 54 }) 55 56 AfterEach(func() { 57 // Add any teardown steps that needs to be executed after each test 58 cleanupObjects() 59 }) 60 61 Context("Test OpsRequest", func() { 62 It("Should Test all OpsRequest", func() { 63 cluster := testapps.CreateConsensusMysqlCluster(&testCtx, clusterDefinitionName, 64 clusterVersionName, clusterName, "consensus", consensusCompName) 65 By("init restart OpsRequest") 66 testOpsName := "restart-" + randomStr 67 ops := testapps.NewOpsRequestObj(testOpsName, testCtx.DefaultNamespace, 68 clusterName, appsv1alpha1.RestartType) 69 ops.Spec.RestartList = []appsv1alpha1.ComponentOps{ 70 {ComponentName: consensusCompName}, 71 } 72 testapps.CreateOpsRequest(ctx, testCtx, ops) 73 74 By("test PatchOpsRequestReconcileAnnotation function") 75 Expect(PatchOpsRequestReconcileAnnotation(ctx, k8sClient, cluster.Namespace, testOpsName)).Should(Succeed()) 76 opsRecordSlice := []appsv1alpha1.OpsRecorder{ 77 { 78 Name: testOpsName, 79 Type: appsv1alpha1.RestartType, 80 }, 81 { 82 Name: "not-exists-ops", 83 Type: appsv1alpha1.RestartType, 84 }, 85 } 86 Expect(PatchClusterOpsAnnotations(ctx, k8sClient, cluster, opsRecordSlice)).Should(Succeed()) 87 88 By("test GetOpsRequestSliceFromCluster function") 89 opsRecordSlice, _ = GetOpsRequestSliceFromCluster(cluster) 90 Expect(len(opsRecordSlice) == 2 && opsRecordSlice[0].Name == testOpsName).Should(BeTrue()) 91 92 By("test no OpsRequest annotation in cluster") 93 Expect(PatchClusterOpsAnnotations(ctx, k8sClient, cluster, nil)).Should(Succeed()) 94 opsRecordSlice, _ = GetOpsRequestSliceFromCluster(cluster) 95 Expect(len(opsRecordSlice) == 0).Should(BeTrue()) 96 }) 97 98 It("Should Test Backup OpsRequest", func() { 99 By("test GetOpsRequestFromBackup function") 100 backup := &dpv1alpha1.Backup{} 101 backup.Labels = map[string]string{ 102 intctrlutil.OpsRequestNameLabelKey: "backup-ops", 103 intctrlutil.OpsRequestTypeLabelKey: string(appsv1alpha1.BackupType), 104 } 105 Expect(GetOpsRequestFromBackup(backup)).ShouldNot(BeNil()) 106 107 By("test GetOpsRequestFromBackup function without ops type") 108 backup.Labels = map[string]string{ 109 intctrlutil.OpsRequestNameLabelKey: "backup-ops", 110 } 111 Expect(GetOpsRequestFromBackup(backup)).Should(BeNil()) 112 113 By("test GetOpsRequestFromBackup function without ops name") 114 backup.Labels = map[string]string{ 115 intctrlutil.OpsRequestTypeLabelKey: string(appsv1alpha1.BackupType), 116 } 117 Expect(GetOpsRequestFromBackup(backup)).Should(BeNil()) 118 }) 119 }) 120 })