github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/apps/opsrequest_util.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 apps 21 22 import ( 23 "context" 24 25 "github.com/onsi/gomega" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/types" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 30 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 31 "github.com/1aal/kubeblocks/pkg/constant" 32 "github.com/1aal/kubeblocks/pkg/testutil" 33 ) 34 35 // CreateRestartOpsRequest creates an OpsRequest of restart type for testing. 36 func CreateRestartOpsRequest(testCtx *testutil.TestContext, clusterName, opsRequestName string, componentNames []string) *appsv1alpha1.OpsRequest { 37 opsRequest := NewOpsRequestObj(opsRequestName, testCtx.DefaultNamespace, clusterName, appsv1alpha1.RestartType) 38 componentList := make([]appsv1alpha1.ComponentOps, len(componentNames)) 39 for i := range componentNames { 40 componentList[i] = appsv1alpha1.ComponentOps{ComponentName: componentNames[i]} 41 } 42 opsRequest.Spec.RestartList = componentList 43 return CreateK8sResource(testCtx, opsRequest).(*appsv1alpha1.OpsRequest) 44 } 45 46 // NewOpsRequestObj only generates the OpsRequest Object, instead of actually creating this resource. 47 func NewOpsRequestObj(opsRequestName, namespace, clusterName string, opsType appsv1alpha1.OpsType) *appsv1alpha1.OpsRequest { 48 return &appsv1alpha1.OpsRequest{ 49 ObjectMeta: metav1.ObjectMeta{ 50 Name: opsRequestName, 51 Namespace: namespace, 52 Labels: map[string]string{ 53 constant.AppInstanceLabelKey: clusterName, 54 constant.OpsRequestTypeLabelKey: string(opsType), 55 }, 56 }, 57 Spec: appsv1alpha1.OpsRequestSpec{ 58 ClusterRef: clusterName, 59 Type: opsType, 60 }, 61 } 62 } 63 64 // CreateOpsRequest calls the api to create the OpsRequest resource. 65 func CreateOpsRequest(ctx context.Context, testCtx testutil.TestContext, opsRequest *appsv1alpha1.OpsRequest) *appsv1alpha1.OpsRequest { 66 gomega.Expect(testCtx.CreateObj(ctx, opsRequest)).Should(gomega.Succeed()) 67 // wait until cluster created 68 gomega.Eventually(CheckObjExists(&testCtx, client.ObjectKeyFromObject(opsRequest), opsRequest, true)).Should(gomega.Succeed()) 69 return opsRequest 70 } 71 72 // GetOpsRequestCompPhase gets the component phase of testing OpsRequest for verification. 73 func GetOpsRequestCompPhase(ctx context.Context, testCtx testutil.TestContext, opsName, componentName string) func(g gomega.Gomega) appsv1alpha1.ClusterComponentPhase { 74 return func(g gomega.Gomega) appsv1alpha1.ClusterComponentPhase { 75 tmpOps := &appsv1alpha1.OpsRequest{} 76 g.Expect(testCtx.Cli.Get(ctx, client.ObjectKey{Name: opsName, 77 Namespace: testCtx.DefaultNamespace}, tmpOps)).Should(gomega.Succeed()) 78 if tmpOps.Status.Components == nil { 79 return "" 80 } 81 return tmpOps.Status.Components[componentName].Phase 82 } 83 } 84 85 // GetOpsRequestPhase gets the testing opsRequest phase for verification. 86 func GetOpsRequestPhase(testCtx *testutil.TestContext, opsKey types.NamespacedName) func(gomega.Gomega) appsv1alpha1.OpsPhase { 87 return func(g gomega.Gomega) appsv1alpha1.OpsPhase { 88 tmpOps := &appsv1alpha1.OpsRequest{} 89 g.Expect(testCtx.Cli.Get(testCtx.Ctx, opsKey, tmpOps)).To(gomega.Succeed()) 90 return tmpOps.Status.Phase 91 } 92 }