github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/clusterversion_controller_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 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 29 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 30 ) 31 32 var _ = Describe("test clusterVersion controller", func() { 33 34 var ( 35 randomStr = testCtx.GetRandomStr() 36 clusterVersionName = "mysql-version-" + randomStr 37 clusterDefName = "mysql-definition-" + randomStr 38 ) 39 40 const statefulCompDefName = "stateful" 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 BeforeEach(cleanEnv) 53 54 AfterEach(cleanEnv) 55 56 Context("test clusterVersion controller", func() { 57 It("test clusterVersion controller", func() { 58 By("create a clusterVersion obj") 59 clusterVersionObj := testapps.NewClusterVersionFactory(clusterVersionName, clusterDefName). 60 AddComponentVersion(statefulCompDefName).AddContainerShort("mysql", testapps.ApeCloudMySQLImage). 61 Create(&testCtx).GetObject() 62 63 By("wait for clusterVersion phase is unavailable when clusterDef is not found") 64 Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(clusterVersionObj), 65 func(g Gomega, tmpCV *appsv1alpha1.ClusterVersion) { 66 g.Expect(tmpCV.Status.Phase).Should(Equal(appsv1alpha1.UnavailablePhase)) 67 })).Should(Succeed()) 68 69 By("create a clusterDefinition obj") 70 testapps.NewClusterDefFactory(clusterDefName). 71 AddComponentDef(testapps.StatefulMySQLComponent, statefulCompDefName). 72 Create(&testCtx).GetObject() 73 74 By("wait for clusterVersion phase is available") 75 Eventually(testapps.CheckObj(&testCtx, 76 client.ObjectKeyFromObject(clusterVersionObj), 77 func(g Gomega, tmpCV *appsv1alpha1.ClusterVersion) { 78 g.Expect(tmpCV.Status.Phase).Should(Equal(appsv1alpha1.AvailablePhase)) 79 })).Should(Succeed()) 80 }) 81 }) 82 83 })