github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/suite_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 "context" 24 "path/filepath" 25 "testing" 26 "time" 27 28 . "github.com/onsi/ginkgo/v2" 29 . "github.com/onsi/gomega" 30 31 "go.uber.org/zap/zapcore" 32 corev1 "k8s.io/api/core/v1" 33 "k8s.io/client-go/kubernetes/scheme" 34 "k8s.io/client-go/rest" 35 "k8s.io/client-go/tools/record" 36 ctrl "sigs.k8s.io/controller-runtime" 37 "sigs.k8s.io/controller-runtime/pkg/client" 38 "sigs.k8s.io/controller-runtime/pkg/envtest" 39 logf "sigs.k8s.io/controller-runtime/pkg/log" 40 "sigs.k8s.io/controller-runtime/pkg/log/zap" 41 42 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 43 workloads "github.com/1aal/kubeblocks/apis/workloads/v1alpha1" 44 "github.com/1aal/kubeblocks/controllers/apps/components" 45 "github.com/1aal/kubeblocks/pkg/constant" 46 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 47 "github.com/1aal/kubeblocks/pkg/testutil" 48 testapps "github.com/1aal/kubeblocks/pkg/testutil/apps" 49 viper "github.com/1aal/kubeblocks/pkg/viperx" 50 ) 51 52 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 53 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 54 55 var cfg *rest.Config 56 var k8sClient client.Client 57 var testEnv *envtest.Environment 58 var ctx context.Context 59 var cancel context.CancelFunc 60 var k8sManager ctrl.Manager 61 var testCtx testutil.TestContext 62 var eventRecorder record.EventRecorder 63 64 const ( 65 statelessComp = "stateless" 66 statefulComp = "stateful" 67 consensusComp = "consensus" 68 ) 69 70 func init() { 71 viper.AutomaticEnv() 72 viper.SetDefault(constant.KBToolsImage, "apecloud/kubeblocks-tools:latest") 73 } 74 75 func TestAPIs(t *testing.T) { 76 RegisterFailHandler(Fail) 77 78 RunSpecs(t, "Operation Controller Suite") 79 } 80 81 var _ = BeforeSuite(func() { 82 if viper.GetBool("ENABLE_DEBUG_LOG") { 83 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { 84 o.TimeEncoder = zapcore.ISO8601TimeEncoder 85 })) 86 } 87 88 ctx, cancel = context.WithCancel(context.TODO()) 89 90 By("bootstrapping test environment") 91 testEnv = &envtest.Environment{ 92 CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")}, 93 ErrorIfCRDPathMissing: true, 94 } 95 96 var err error 97 // cfg is defined in this file globally. 98 cfg, err = testEnv.Start() 99 Expect(err).NotTo(HaveOccurred()) 100 Expect(cfg).NotTo(BeNil()) 101 102 err = appsv1alpha1.AddToScheme(scheme.Scheme) 103 Expect(err).NotTo(HaveOccurred()) 104 err = workloads.AddToScheme(scheme.Scheme) 105 Expect(err).NotTo(HaveOccurred()) 106 107 // +kubebuilder:scaffold:scheme 108 109 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 110 Expect(err).NotTo(HaveOccurred()) 111 Expect(k8sClient).NotTo(BeNil()) 112 113 // run reconcile 114 k8sManager, err = ctrl.NewManager(cfg, ctrl.Options{ 115 Scheme: scheme.Scheme, 116 MetricsBindAddress: "0", 117 ClientDisableCacheFor: intctrlutil.GetUncachedObjects(), 118 }) 119 Expect(err).ToNot(HaveOccurred()) 120 121 eventRecorder = k8sManager.GetEventRecorderFor("event-controller") 122 123 appsv1alpha1.RegisterWebhookManager(k8sManager) 124 125 testCtx = testutil.NewDefaultTestContext(ctx, k8sClient, testEnv) 126 127 go func() { 128 defer GinkgoRecover() 129 err = k8sManager.Start(ctx) 130 Expect(err).ToNot(HaveOccurred(), "failed to run manager") 131 }() 132 133 }) 134 135 var _ = AfterSuite(func() { 136 cancel() 137 By("tearing down the test environment") 138 err := testEnv.Stop() 139 Expect(err).NotTo(HaveOccurred()) 140 }) 141 142 // initOperationsResources inits the operations resources. 143 func initOperationsResources(clusterDefinitionName, 144 clusterVersionName, 145 clusterName string) (*OpsResource, *appsv1alpha1.ClusterDefinition, *appsv1alpha1.Cluster) { 146 clusterDef, _, clusterObject := testapps.InitClusterWithHybridComps(&testCtx, clusterDefinitionName, 147 clusterVersionName, clusterName, statelessComp, statefulComp, consensusComp) 148 opsRes := &OpsResource{ 149 Cluster: clusterObject, 150 Recorder: k8sManager.GetEventRecorderFor("opsrequest-controller"), 151 } 152 By("mock cluster is Running and the status operations") 153 Expect(testapps.ChangeObjStatus(&testCtx, clusterObject, func() { 154 clusterObject.Status.Phase = appsv1alpha1.RunningClusterPhase 155 clusterObject.Status.Components = map[string]appsv1alpha1.ClusterComponentStatus{ 156 consensusComp: { 157 Phase: appsv1alpha1.RunningClusterCompPhase, 158 }, 159 statelessComp: { 160 Phase: appsv1alpha1.RunningClusterCompPhase, 161 }, 162 statefulComp: { 163 Phase: appsv1alpha1.RunningClusterCompPhase, 164 }, 165 } 166 })).Should(Succeed()) 167 opsRes.Cluster = clusterObject 168 return opsRes, clusterDef, clusterObject 169 } 170 171 func initConsensusPods(ctx context.Context, cli client.Client, opsRes *OpsResource, clusterName string) []corev1.Pod { 172 // mock the pods of consensusSet component 173 testapps.MockConsensusComponentPods(&testCtx, nil, clusterName, consensusComp) 174 podList, err := components.GetComponentPodList(ctx, cli, *opsRes.Cluster, consensusComp) 175 Expect(err).Should(Succeed()) 176 // the opsRequest will use startTime to check some condition. 177 // if there is no sleep for 1 second, unstable error may occur. 178 time.Sleep(time.Second) 179 return podList.Items 180 } 181 182 func mockComponentIsOperating(cluster *appsv1alpha1.Cluster, expectPhase appsv1alpha1.ClusterComponentPhase, compNames ...string) { 183 Expect(testapps.ChangeObjStatus(&testCtx, cluster, func() { 184 for _, v := range compNames { 185 compStatus := cluster.Status.Components[v] 186 compStatus.Phase = expectPhase 187 cluster.Status.Components[v] = compStatus 188 } 189 })).Should(Succeed()) 190 }