Skip to content

Commit 5522376

Browse files
author
Arvind Thirumurugan
committed
add more UTs
Signed-off-by: Arvind Thirumurugan <[email protected]>
1 parent 3cd1e48 commit 5522376

File tree

1 file changed

+136
-29
lines changed

1 file changed

+136
-29
lines changed

pkg/controllers/updaterun/execution_test.go

Lines changed: 136 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,12 @@ func TestBuildApprovalRequestObject(t *testing.T) {
411411

412412
func TestExecuteUpdatingStage_Error(t *testing.T) {
413413
tests := []struct {
414-
name string
415-
updateRun *placementv1beta1.ClusterStagedUpdateRun
416-
bindings []placementv1beta1.BindingObj
417-
interceptorFunc *interceptor.Funcs
418-
wantErr error
419-
expectWaitTime time.Duration
420-
verifyClusterFailed bool
414+
name string
415+
updateRun *placementv1beta1.ClusterStagedUpdateRun
416+
bindings []placementv1beta1.BindingObj
417+
interceptorFunc *interceptor.Funcs
418+
wantErr error
419+
expectWaitTime time.Duration
421420
}{
422421
{
423422
name: "cluster update failed",
@@ -460,11 +459,10 @@ func TestExecuteUpdatingStage_Error(t *testing.T) {
460459
},
461460
},
462461
},
463-
bindings: nil,
464-
interceptorFunc: nil,
465-
wantErr: errors.New("the cluster `cluster-1` in the stage test-stage has failed"),
466-
expectWaitTime: 0,
467-
verifyClusterFailed: false,
462+
bindings: nil,
463+
interceptorFunc: nil,
464+
wantErr: errors.New("the cluster `cluster-1` in the stage test-stage has failed"),
465+
expectWaitTime: 0,
468466
},
469467
{
470468
name: "binding update failure",
@@ -515,9 +513,8 @@ func TestExecuteUpdatingStage_Error(t *testing.T) {
515513
return fmt.Errorf("simulated update error")
516514
},
517515
},
518-
wantErr: errors.New("simulated update error"),
519-
expectWaitTime: 0,
520-
verifyClusterFailed: false,
516+
wantErr: errors.New("simulated update error"),
517+
expectWaitTime: 0,
521518
},
522519
{
523520
name: "binding preemption",
@@ -581,10 +578,129 @@ func TestExecuteUpdatingStage_Error(t *testing.T) {
581578
},
582579
},
583580
},
584-
interceptorFunc: nil,
585-
wantErr: errors.New("the binding of the updating cluster `cluster-1` in the stage `test-stage` is not up-to-date with the desired status"),
586-
expectWaitTime: 0,
587-
verifyClusterFailed: true,
581+
interceptorFunc: nil,
582+
wantErr: errors.New("the binding of the updating cluster `cluster-1` in the stage `test-stage` is not up-to-date with the desired status"),
583+
expectWaitTime: 0,
584+
},
585+
{
586+
name: "binding synced but state not bound - update binding state fails",
587+
updateRun: &placementv1beta1.ClusterStagedUpdateRun{
588+
ObjectMeta: metav1.ObjectMeta{
589+
Name: "test-update-run",
590+
Generation: 1,
591+
},
592+
Spec: placementv1beta1.UpdateRunSpec{
593+
PlacementName: "test-placement",
594+
ResourceSnapshotIndex: "1",
595+
},
596+
Status: placementv1beta1.UpdateRunStatus{
597+
StagesStatus: []placementv1beta1.StageUpdatingStatus{
598+
{
599+
StageName: "test-stage",
600+
Clusters: []placementv1beta1.ClusterUpdatingStatus{
601+
{
602+
ClusterName: "cluster-1",
603+
// No conditions - cluster has not started updating yet.
604+
},
605+
},
606+
},
607+
},
608+
UpdateStrategySnapshot: &placementv1beta1.UpdateStrategySpec{
609+
Stages: []placementv1beta1.StageConfig{
610+
{
611+
Name: "test-stage",
612+
MaxConcurrency: &intstr.IntOrString{Type: intstr.Int, IntVal: 1},
613+
},
614+
},
615+
},
616+
},
617+
},
618+
bindings: []placementv1beta1.BindingObj{
619+
&placementv1beta1.ClusterResourceBinding{
620+
ObjectMeta: metav1.ObjectMeta{
621+
Name: "binding-1",
622+
Generation: 1,
623+
},
624+
Spec: placementv1beta1.ResourceBindingSpec{
625+
TargetCluster: "cluster-1",
626+
ResourceSnapshotName: "test-placement-1-snapshot", // Already synced.
627+
State: placementv1beta1.BindingStateScheduled, // But not Bound yet.
628+
},
629+
},
630+
},
631+
interceptorFunc: &interceptor.Funcs{
632+
Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
633+
return fmt.Errorf("failed to update binding state")
634+
},
635+
},
636+
wantErr: errors.New("failed to update binding state"),
637+
expectWaitTime: 0,
638+
},
639+
{
640+
name: "binding synced and bound but generation updated - update rolloutStarted fails",
641+
updateRun: &placementv1beta1.ClusterStagedUpdateRun{
642+
ObjectMeta: metav1.ObjectMeta{
643+
Name: "test-update-run",
644+
Generation: 1,
645+
},
646+
Spec: placementv1beta1.UpdateRunSpec{
647+
PlacementName: "test-placement",
648+
ResourceSnapshotIndex: "1",
649+
},
650+
Status: placementv1beta1.UpdateRunStatus{
651+
StagesStatus: []placementv1beta1.StageUpdatingStatus{
652+
{
653+
StageName: "test-stage",
654+
Clusters: []placementv1beta1.ClusterUpdatingStatus{
655+
{
656+
ClusterName: "cluster-1",
657+
// No conditions - cluster has not started updating yet.
658+
},
659+
},
660+
},
661+
},
662+
UpdateStrategySnapshot: &placementv1beta1.UpdateStrategySpec{
663+
Stages: []placementv1beta1.StageConfig{
664+
{
665+
Name: "test-stage",
666+
MaxConcurrency: &intstr.IntOrString{Type: intstr.Int, IntVal: 1},
667+
},
668+
},
669+
},
670+
},
671+
},
672+
bindings: []placementv1beta1.BindingObj{
673+
&placementv1beta1.ClusterResourceBinding{
674+
ObjectMeta: metav1.ObjectMeta{
675+
Name: "binding-1",
676+
Generation: 2, // Generation updated by scheduler.
677+
},
678+
Spec: placementv1beta1.ResourceBindingSpec{
679+
TargetCluster: "cluster-1",
680+
ResourceSnapshotName: "test-placement-1-snapshot", // Already synced.
681+
State: placementv1beta1.BindingStateBound, // Already Bound.
682+
},
683+
Status: placementv1beta1.ResourceBindingStatus{
684+
Conditions: []metav1.Condition{
685+
{
686+
Type: string(placementv1beta1.ResourceBindingRolloutStarted),
687+
Status: metav1.ConditionTrue,
688+
ObservedGeneration: 1, // Old generation - needs update.
689+
Reason: "RolloutStarted",
690+
Message: "Rollout started",
691+
},
692+
},
693+
},
694+
},
695+
},
696+
interceptorFunc: &interceptor.Funcs{
697+
SubResourceUpdate: func(ctx context.Context, client client.Client, subResourceName string, obj client.Object, opts ...client.SubResourceUpdateOption) error {
698+
// Fail the status update for rolloutStarted.
699+
return fmt.Errorf("failed to update binding rolloutStarted status")
700+
},
701+
},
702+
wantErr: errors.New("failed to update binding rolloutStarted status"),
703+
expectWaitTime: 0,
588704
},
589705
}
590706

@@ -615,7 +731,7 @@ func TestExecuteUpdatingStage_Error(t *testing.T) {
615731
// Execute the stage.
616732
waitTime, gotErr := r.executeUpdatingStage(ctx, tt.updateRun, 0, tt.bindings, 1)
617733

618-
// Verify error expectation.x
734+
// Verify error expectation.
619735
if tt.wantErr != nil && gotErr == nil {
620736
t.Fatalf("executeUpdatingStage() expected error containing %v, got nil", tt.wantErr)
621737
}
@@ -634,15 +750,6 @@ func TestExecuteUpdatingStage_Error(t *testing.T) {
634750
if waitTime != tt.expectWaitTime {
635751
t.Fatalf("executeUpdatingStage() expected waitTime=%v, got: %v", tt.expectWaitTime, waitTime)
636752
}
637-
638-
// Verify cluster failed status if needed.
639-
if tt.verifyClusterFailed {
640-
clusterStatus := &tt.updateRun.Status.StagesStatus[0].Clusters[0]
641-
failedCond := meta.FindStatusCondition(clusterStatus.Conditions, string(placementv1beta1.ClusterUpdatingConditionSucceeded))
642-
if failedCond == nil || failedCond.Status != metav1.ConditionFalse {
643-
t.Fatal("executeUpdatingStage() failed to mark cluster as failed")
644-
}
645-
}
646753
})
647754
}
648755
}

0 commit comments

Comments
 (0)