@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020package operations
2121
2222import (
23+ "fmt"
2324 "time"
2425
2526 . "github.com/onsi/ginkgo/v2"
@@ -350,5 +351,91 @@ var _ = Describe("OpsUtil functions", func() {
350351 _ , _ = GetOpsManager ().Do (reqCtx , k8sClient , opsRes )
351352 Eventually (testops .GetOpsRequestPhase (& testCtx , client .ObjectKeyFromObject (opsRes .OpsRequest ))).Should (Equal (opsv1alpha1 .OpsCreatingPhase ))
352353 })
354+
355+ It ("Test swapOpsWithDependentBefore when force ops enqueued before dependent ops" , func () {
356+ By ("init operations resources " )
357+ opsRes , _ , _ := initOperationsResources (compDefName , clusterName )
358+ testapps .MockInstanceSetComponent (& testCtx , clusterName , defaultCompName )
359+
360+ By ("create a force opsRequest that depends on another ops" )
361+ ops1 := createHorizontalScaling (clusterName , opsv1alpha1.HorizontalScaling {
362+ ComponentOps : opsv1alpha1.ComponentOps {ComponentName : defaultCompName },
363+ ScaleIn : & opsv1alpha1.ScaleIn {
364+ ReplicaChanger : opsv1alpha1.ReplicaChanger {ReplicaChanges : pointer .Int32 (1 )},
365+ },
366+ }, false )
367+ ops2 := createHorizontalScaling (clusterName , opsv1alpha1.HorizontalScaling {
368+ ComponentOps : opsv1alpha1.ComponentOps {ComponentName : defaultCompName },
369+ ScaleOut : & opsv1alpha1.ScaleOut {
370+ ReplicaChanger : opsv1alpha1.ReplicaChanger {ReplicaChanges : pointer .Int32 (1 )},
371+ },
372+ }, false )
373+ ops3 := createHorizontalScaling (clusterName , opsv1alpha1.HorizontalScaling {
374+ ComponentOps : opsv1alpha1.ComponentOps {ComponentName : defaultCompName },
375+ ScaleOut : & opsv1alpha1.ScaleOut {
376+ ReplicaChanger : opsv1alpha1.ReplicaChanger {ReplicaChanges : pointer .Int32 (1 )},
377+ },
378+ }, false )
379+ ops3 .Annotations = map [string ]string {constant .OpsDependentOnSuccessfulOpsAnnoKey : fmt .Sprintf ("%s,%s" , ops1 .Name , ops2 .Name )}
380+ ops3 .Spec .Force = true
381+
382+ By ("manually add ops3 to cluster annotation first with InQueue=false" )
383+ ops3Recorder := opsv1alpha1.OpsRecorder {
384+ Name : ops3 .Name ,
385+ Type : opsv1alpha1 .HorizontalScalingType ,
386+ InQueue : false ,
387+ QueueBySelf : false ,
388+ }
389+ Expect (testapps .ChangeObj (& testCtx , opsRes .Cluster , func (cluster * appsv1.Cluster ) {
390+ opsutil .SetOpsRequestToCluster (cluster , []opsv1alpha1.OpsRecorder {ops3Recorder })
391+ })).Should (Succeed ())
392+
393+ By ("ops2 should be in queue" )
394+ opsRequestSlice , _ := opsutil .GetOpsRequestSliceFromCluster (opsRes .Cluster )
395+ Expect (len (opsRequestSlice )).Should (Equal (1 ))
396+ Expect (opsRequestSlice [0 ].Name ).Should (Equal (ops3 .Name ))
397+ Expect (opsRequestSlice [0 ].InQueue ).Should (BeFalse ())
398+
399+ By ("now create ops1 and add it to cluster annotation after ops2" )
400+ ops1Recorder := opsv1alpha1.OpsRecorder {
401+ Name : ops1 .Name ,
402+ Type : opsv1alpha1 .HorizontalScalingType ,
403+ InQueue : true ,
404+ QueueBySelf : false ,
405+ }
406+ ops2Recorder := opsv1alpha1.OpsRecorder {
407+ Name : ops2 .Name ,
408+ Type : opsv1alpha1 .HorizontalScalingType ,
409+ InQueue : true ,
410+ QueueBySelf : false ,
411+ }
412+ Expect (testapps .ChangeObj (& testCtx , opsRes .Cluster , func (cluster * appsv1.Cluster ) {
413+ opsutil .SetOpsRequestToCluster (cluster , []opsv1alpha1.OpsRecorder {ops3Recorder , ops1Recorder , ops2Recorder })
414+ })).Should (Succeed ())
415+
416+ By ("verify ops1 is after ops2 in the slice" )
417+ opsRequestSlice , _ = opsutil .GetOpsRequestSliceFromCluster (opsRes .Cluster )
418+ Expect (len (opsRequestSlice )).Should (Equal (3 ))
419+ Expect (opsRequestSlice [0 ].Name ).Should (Equal (ops3 .Name ))
420+ Expect (opsRequestSlice [1 ].Name ).Should (Equal (ops1 .Name ))
421+ Expect (opsRequestSlice [2 ].Name ).Should (Equal (ops2 .Name ))
422+
423+ By ("simulate ops3 reconciliation that triggers swap" )
424+ opsRes .OpsRequest = ops3
425+ ops3 .Status .Phase = opsv1alpha1 .OpsPendingPhase
426+
427+ reqCtx := intctrlutil.RequestCtx {Ctx : testCtx .Ctx }
428+ opsBehaviour := GetOpsManager ().OpsMap [opsv1alpha1 .HorizontalScalingType ]
429+ opsRecorder , err := enqueueOpsRequestToClusterAnnotation (reqCtx .Ctx , k8sClient , opsRes , opsBehaviour )
430+ Expect (err ).ShouldNot (HaveOccurred ())
431+ Expect (opsRecorder ).ShouldNot (BeNil ())
432+
433+ By ("verify ops1 and ops2 have been swapped" )
434+ opsRequestSlice , _ = opsutil .GetOpsRequestSliceFromCluster (opsRes .Cluster )
435+ Expect (len (opsRequestSlice )).Should (Equal (3 ))
436+ Expect (opsRequestSlice [0 ].Name ).Should (Equal (ops1 .Name ))
437+ Expect (opsRequestSlice [1 ].Name ).Should (Equal (ops2 .Name ))
438+ Expect (opsRequestSlice [2 ].Name ).Should (Equal (ops3 .Name ))
439+ })
353440 })
354441})
0 commit comments