github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/cluster_plan_builder_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 apps 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 ctrl "sigs.k8s.io/controller-runtime" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 "sigs.k8s.io/controller-runtime/pkg/log" 29 30 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 31 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 32 "github.com/1aal/kubeblocks/pkg/generics" 33 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 34 ) 35 36 var _ = Describe("cluster plan builder test", func() { 37 const ( 38 clusterDefName = "test-clusterdef" 39 clusterVersionName = "test-clusterversion" 40 clusterName = "test-cluster" // this become cluster prefix name if used with testapps.NewClusterFactory().WithRandomName() 41 ) 42 43 // Cleanups 44 cleanEnv := func() { 45 // must wait till resources deleted and no longer existed before the testcases start, 46 // otherwise if later it needs to create some new resource objects with the same name, 47 // in race conditions, it will find the existence of old objects, resulting failure to 48 // create the new objects. 49 By("clean resources") 50 51 // delete cluster(and all dependent sub-resources), clusterversion and clusterdef 52 testapps.ClearClusterResourcesWithRemoveFinalizerOption(&testCtx) 53 54 // delete rest mocked objects 55 inNS := client.InNamespace(testCtx.DefaultNamespace) 56 ml := client.HasLabels{testCtx.TestObjLabelKey} 57 // namespaced 58 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.PersistentVolumeClaimSignature, true, inNS, ml) 59 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.PodSignature, true, inNS, ml) 60 testapps.ClearResources(&testCtx, generics.BackupSignature, inNS, ml) 61 testapps.ClearResources(&testCtx, generics.BackupPolicySignature, inNS, ml) 62 testapps.ClearResourcesWithRemoveFinalizerOption(&testCtx, generics.VolumeSnapshotSignature, true, inNS) 63 // non-namespaced 64 testapps.ClearResources(&testCtx, generics.BackupPolicyTemplateSignature, ml) 65 testapps.ClearResources(&testCtx, generics.ActionSetSignature, ml) 66 testapps.ClearResources(&testCtx, generics.StorageClassSignature, ml) 67 } 68 69 BeforeEach(func() { 70 cleanEnv() 71 }) 72 73 AfterEach(func() { 74 cleanEnv() 75 }) 76 77 Context("test init", func() { 78 It("should init successfully", func() { 79 clusterObj := testapps.NewClusterFactory(testCtx.DefaultNamespace, clusterName, 80 clusterDefName, clusterVersionName).WithRandomName().GetObject() 81 Expect(testCtx.Cli.Create(testCtx.Ctx, clusterObj)).Should(Succeed()) 82 clusterKey := client.ObjectKeyFromObject(clusterObj) 83 Eventually(testapps.CheckObjExists(&testCtx, clusterKey, &appsv1alpha1.Cluster{}, true)).Should(Succeed()) 84 req := ctrl.Request{ 85 NamespacedName: clusterKey, 86 } 87 reqCtx := intctrlutil.RequestCtx{ 88 Ctx: testCtx.Ctx, 89 Req: req, 90 Log: log.FromContext(ctx).WithValues("cluster", req.NamespacedName), 91 } 92 planBuilder := NewClusterPlanBuilder(reqCtx, testCtx.Cli, req) 93 Expect(planBuilder.Init()).Should(Succeed()) 94 }) 95 }) 96 })