From 2a13c384b61f8fa0f5288c3e8ac89d073fd02c9d Mon Sep 17 00:00:00 2001 From: charlie Date: Thu, 28 Sep 2023 16:04:41 +0800 Subject: [PATCH] test: add useBeta unit test --- .../controller/moduledeployment_controller.go | 34 ++++++++------ .../moduledeployment_controller_suit_test.go | 44 ++++++++++++++++++- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/module-controller/internal/controller/moduledeployment_controller.go b/module-controller/internal/controller/moduledeployment_controller.go index bbd399a2f..e8e9d05de 100644 --- a/module-controller/internal/controller/moduledeployment_controller.go +++ b/module-controller/internal/controller/moduledeployment_controller.go @@ -110,7 +110,7 @@ func (r *ModuleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Req return ctrl.Result{}, err } case moduledeploymentv1alpha1.ModuleDeploymentReleaseProgressExecuting: - return r.updateModuleReplicaSet(moduleDeployment, newRS, oldRSs) + return r.updateModuleReplicaSet(ctx, moduleDeployment, newRS, oldRSs) case moduledeploymentv1alpha1.ModuleDeploymentReleaseProgressCompleted: if moduleDeployment.Spec.Replicas != newRS.Spec.Replicas { moduleDeployment.Status.ReleaseStatus.Progress = moduledeploymentv1alpha1.ModuleDeploymentReleaseProgressExecuting @@ -309,11 +309,9 @@ func (r *ModuleDeploymentReconciler) updateModuleReplicas( return nil } -func (r *ModuleDeploymentReconciler) updateModuleReplicaSet(moduleDeployment *moduledeploymentv1alpha1.ModuleDeployment, +func (r *ModuleDeploymentReconciler) updateModuleReplicaSet(ctx context.Context, moduleDeployment *moduledeploymentv1alpha1.ModuleDeployment, newRS *moduledeploymentv1alpha1.ModuleReplicaSet, oldRSs []*moduledeploymentv1alpha1.ModuleReplicaSet) (ctrl.Result, error) { var ( - ctx = context.TODO() - batchCount = moduleDeployment.Spec.OperationStrategy.BatchCount curBatch = moduleDeployment.Status.ReleaseStatus.CurrentBatch @@ -350,7 +348,8 @@ func (r *ModuleDeploymentReconciler) updateModuleReplicaSet(moduleDeployment *mo replicas := int32(0) // use beta strategy - if batchCount != 1 && curBatch == 1 && moduleDeployment.Spec.OperationStrategy.UseBeta { + useBeta := batchCount != 1 && curBatch == 1 && moduleDeployment.Spec.OperationStrategy.UseBeta + if useBeta { replicas = 1 } else if curBatch == batchCount { // if it's the last batch replicas = expReplicas @@ -363,11 +362,27 @@ func (r *ModuleDeploymentReconciler) updateModuleReplicaSet(moduleDeployment *mo return ctrl.Result{}, err } + var message string now := metav1.Now() - moduleDeployment.Status.ReleaseStatus.CurrentBatch += 1 + + if useBeta { + moduleDeployment.Status.ReleaseStatus.CurrentBatch = 1 + message = "deployment release: beta deployment" + } else { + moduleDeployment.Status.ReleaseStatus.CurrentBatch += 1 + message = fmt.Sprintf("deployment release: curbatch %v, batchCount %v", curBatch, batchCount) + } + moduleDeployment.Status.ReleaseStatus.LastTransitionTime = now moduleDeployment.Status.ReleaseStatus.Progress = moduledeploymentv1alpha1.ModuleDeploymentReleaseProgressExecuting + moduleDeployment.Status.Conditions = append(moduleDeployment.Status.Conditions, moduledeploymentv1alpha1.ModuleDeploymentCondition{ + Type: moduledeploymentv1alpha1.DeploymentProgressing, + Status: corev1.ConditionTrue, + LastTransitionTime: now, + Message: message, + }) + var grayTime int if curBatch != batchCount { if moduleDeployment.Spec.OperationStrategy.NeedConfirm { // use NeedConfirm Strategy @@ -382,13 +397,6 @@ func (r *ModuleDeploymentReconciler) updateModuleReplicaSet(moduleDeployment *mo } } - moduleDeployment.Status.Conditions = append(moduleDeployment.Status.Conditions, moduledeploymentv1alpha1.ModuleDeploymentCondition{ - Type: moduledeploymentv1alpha1.DeploymentProgressing, - Status: corev1.ConditionTrue, - LastTransitionTime: metav1.Now(), - Message: fmt.Sprintf("deployment release: curbatch %v, batchCount %v", curBatch, batchCount), - }) - return ctrl.Result{Requeue: true, RequeueAfter: time.Duration(grayTime) * time.Second}, r.Status().Update(ctx, moduleDeployment) } diff --git a/module-controller/internal/controller/moduledeployment_controller_suit_test.go b/module-controller/internal/controller/moduledeployment_controller_suit_test.go index a17f93d93..3aa525f0a 100644 --- a/module-controller/internal/controller/moduledeployment_controller_suit_test.go +++ b/module-controller/internal/controller/moduledeployment_controller_suit_test.go @@ -20,7 +20,7 @@ import ( ) var _ = Describe("ModuleDeployment Controller", func() { - const timeout = time.Second * 30 + const timeout = time.Second * 120 const interval = time.Second * 5 namespace := "module-deployment-namespace" @@ -225,6 +225,48 @@ var _ = Describe("ModuleDeployment Controller", func() { return moduleDeployment.Status.ReleaseStatus.Progress == moduledeploymentv1alpha1.ModuleDeploymentReleaseProgressCompleted }, timeout, interval).Should(BeTrue()) }) + + It("5. delete moduleDeployment", func() { + Expect(k8sClient.Delete(context.TODO(), &moduleDeployment)).Should(Succeed()) + }) + }) + + Context("test useBeta strategy", func() { + moduleDeploymentName := "module-deployment-test-for-use-beta" + nn := types.NamespacedName{Namespace: namespace, Name: moduleDeploymentName} + moduleDeployment := prepareModuleDeployment(namespace, moduleDeploymentName) + moduleDeployment.Spec.Replicas = 4 + moduleDeployment.Spec.OperationStrategy.UseBeta = true + moduleDeployment.Spec.OperationStrategy.NeedConfirm = true + moduleDeployment.Spec.OperationStrategy.BatchCount = 2 + + It("0. prepare pods", func() { + Eventually(func() bool { + pod := preparePod(namespace, "fake-pod-use-beta") + pod.Labels[fmt.Sprintf("%s-%s", label.ModuleNameLabel, "dynamic-provider")] = "1.0.0" + if err := k8sClient.Create(context.TODO(), &pod); err != nil { + return false + } + // when install module, the podIP is necessary + pod.Status.PodIP = "127.0.0.1" + return k8sClient.Status().Update(context.TODO(), &pod) == nil + }, timeout, interval).Should(BeTrue()) + }) + + It("1. create a new moduleDeployment", func() { + Expect(k8sClient.Create(context.TODO(), &moduleDeployment)).Should(Succeed()) + }) + + It("2. check if use Beta strategy", func() { + Eventually(func() bool { + return checkModuleDeploymentReplicas(nn, 1) + }) + }) + + It("3. clean environment", func() { + Expect(k8sClient.Delete(context.TODO(), &moduleDeployment)).Should(Succeed()) + }) + }) Context("delete module deployment", func() {