Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
test: add useBeta unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie17Li committed Sep 28, 2023
1 parent 2f45cbb commit 2a13c38
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 2a13c38

Please sign in to comment.