Skip to content

Commit 041b602

Browse files
authored
fix: fix TestPickBindingsToRoll rollout unit test (#964)
fix naming conventions, correct UT accordingly, and add comments
1 parent b7fd384 commit 041b602

File tree

1 file changed

+35
-44
lines changed

1 file changed

+35
-44
lines changed

pkg/controllers/rollout/controller_test.go

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,14 @@ func TestPickBindingsToRoll(t *testing.T) {
744744
crpWithApplyStrategy.Spec.Strategy.ApplyStrategy = &fleetv1beta1.ApplyStrategy{
745745
Type: fleetv1beta1.ApplyStrategyTypeServerSideApply,
746746
}
747+
// crpWithUnavailablePeriod has a 60s unavailable period
747748
crpWithUnavailablePeriod := clusterResourcePlacementForTest("test",
748749
createPlacementPolicyForTest(fleetv1beta1.PickNPlacementType, 3))
749750
crpWithUnavailablePeriod.Spec.Strategy.RollingUpdate.UnavailablePeriodSeconds = ptr.To(60)
750751

751-
readyBinding := generateClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-1", cluster1)
752-
readyBinding.Generation = 15
753-
readyBinding.Status.Conditions = []metav1.Condition{
752+
canBeReadyBinding := generateClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-1", cluster1)
753+
canBeReadyBinding.Generation = 15
754+
canBeReadyBinding.Status.Conditions = []metav1.Condition{
754755
{
755756
Type: string(fleetv1beta1.ResourceBindingApplied),
756757
Status: metav1.ConditionTrue,
@@ -760,6 +761,8 @@ func TestPickBindingsToRoll(t *testing.T) {
760761
},
761762
},
762763
}
764+
765+
// create bindings below to test different wait times
763766
notReadyUnscheduledBinding := generateClusterResourceBinding(fleetv1beta1.BindingStateUnscheduled, "snapshot-1", cluster2)
764767
notReadyUnscheduledBinding.Generation = 15
765768
notReadyUnscheduledBinding.Status.Conditions = []metav1.Condition{
@@ -775,7 +778,7 @@ func TestPickBindingsToRoll(t *testing.T) {
775778
LastTransitionTime: metav1.Time{
776779
Time: now.Add(-1 * time.Minute),
777780
},
778-
Reason: work.WorkNotTrackableReason,
781+
Reason: work.WorkNotTrackableReason, // Make it not ready
779782
},
780783
}
781784

@@ -794,13 +797,13 @@ func TestPickBindingsToRoll(t *testing.T) {
794797
LastTransitionTime: metav1.Time{
795798
Time: now.Add(-35 * time.Second),
796799
},
797-
Reason: work.WorkNotTrackableReason,
800+
Reason: work.WorkNotTrackableReason, // Make it not ready
798801
},
799802
}
800803

801-
notReadyBoundBinding := generateClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-2", cluster2)
802-
notReadyBoundBinding.Generation = 15
803-
notReadyBoundBinding.Status.Conditions = []metav1.Condition{
804+
readyBoundBinding := generateClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-2", cluster2)
805+
readyBoundBinding.Generation = 15
806+
readyBoundBinding.Status.Conditions = []metav1.Condition{
804807
{
805808
Type: string(fleetv1beta1.ResourceBindingApplied),
806809
Status: metav1.ConditionTrue,
@@ -810,13 +813,13 @@ func TestPickBindingsToRoll(t *testing.T) {
810813
Type: string(fleetv1beta1.ResourceBindingAvailable),
811814
Status: metav1.ConditionTrue,
812815
ObservedGeneration: 15,
813-
Reason: work.WorkAvailableReason,
816+
Reason: work.WorkAvailableReason, // Make it ready
814817
},
815818
}
816819

817-
notReadyBoundBinding2 := generateClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-1", cluster3)
818-
notReadyUnscheduledBinding2.Generation = 15
819-
notReadyUnscheduledBinding2.Status.Conditions = []metav1.Condition{
820+
notReadyBoundBinding := generateClusterResourceBinding(fleetv1beta1.BindingStateBound, "snapshot-1", cluster3)
821+
notReadyBoundBinding.Generation = 15
822+
notReadyBoundBinding.Status.Conditions = []metav1.Condition{
820823
{
821824
Type: string(fleetv1beta1.ResourceBindingApplied),
822825
Status: metav1.ConditionTrue,
@@ -829,7 +832,7 @@ func TestPickBindingsToRoll(t *testing.T) {
829832
LastTransitionTime: metav1.Time{
830833
Time: now.Add(-35 * time.Second),
831834
},
832-
Reason: work.WorkNotTrackableReason,
835+
Reason: work.WorkNotTrackableReason, // Make it not ready
833836
},
834837
}
835838
tests := map[string]struct {
@@ -1365,7 +1368,7 @@ func TestPickBindingsToRoll(t *testing.T) {
13651368
},
13661369
"test with scheduled bindings (always update the scheduled binding first)": {
13671370
allBindings: []*fleetv1beta1.ClusterResourceBinding{
1368-
readyBinding,
1371+
canBeReadyBinding,
13691372
generateClusterResourceBinding(fleetv1beta1.BindingStateScheduled, "snapshot-1", cluster2),
13701373
},
13711374
latestResourceSnapshotName: "snapshot-2",
@@ -1457,55 +1460,43 @@ func TestPickBindingsToRoll(t *testing.T) {
14571460
wantErr: controller.ErrExpectedBehavior,
14581461
},
14591462
"test bound bindings with different waitTimes": {
1463+
// want the min wait time of bound bindings that are not ready
14601464
allBindings: []*fleetv1beta1.ClusterResourceBinding{
1461-
notReadyBoundBinding,
1462-
readyBinding,
1463-
notReadyBoundBinding2,
1465+
notReadyBoundBinding, // notReady, waitTime = t - 35s
1466+
canBeReadyBinding, // notReady, no wait time because it does not have available condition yet
1467+
readyBoundBinding, // Ready
14641468
},
14651469
latestResourceSnapshotName: "snapshot-2",
1466-
crp: crpWithUnavailablePeriod,
1467-
wantStaleUnselectedBindings: []int{1, 2},
1470+
crp: crpWithUnavailablePeriod, // UnavailablePeriodSeconds is 60s -> readyTimeCutOff = t - 60s
1471+
wantStaleUnselectedBindings: []int{0, 1},
14681472
wantDesiredBindingsSpec: []fleetv1beta1.ResourceBindingSpec{
14691473
{
14701474
State: fleetv1beta1.BindingStateBound,
1471-
TargetCluster: cluster2,
1475+
TargetCluster: cluster3,
14721476
ResourceSnapshotName: "snapshot-2",
14731477
},
14741478
{
14751479
State: fleetv1beta1.BindingStateBound,
14761480
TargetCluster: cluster1,
14771481
ResourceSnapshotName: "snapshot-2",
14781482
},
1479-
{
1480-
State: fleetv1beta1.BindingStateBound,
1481-
TargetCluster: cluster3,
1482-
ResourceSnapshotName: "snapshot-2",
1483-
},
1483+
{}, // bound binding is ready and up-to-date pointing to the latest resource snapshot.
14841484
},
14851485
wantNeedRoll: true,
1486-
wantWaitTime: 60 * time.Second,
1486+
wantWaitTime: 25 * time.Second, // minWaitTime = (t - 35 seconds) - (t - 60 seconds) = 25 seconds
14871487
},
14881488
"test unscheduled bindings with different waitTimes": {
1489+
// want the min wait time of unscheduled bindings that are not ready
14891490
allBindings: []*fleetv1beta1.ClusterResourceBinding{
1490-
notReadyUnscheduledBinding,
1491-
notReadyUnscheduledBinding2,
1491+
notReadyUnscheduledBinding, // NotReady, waitTime = t - 60s
1492+
notReadyUnscheduledBinding2, // NotReady, waitTime = t - 35s
14921493
},
1493-
latestResourceSnapshotName: "snapshot-2",
1494-
crp: crpWithUnavailablePeriod,
1495-
wantDesiredBindingsSpec: []fleetv1beta1.ResourceBindingSpec{
1496-
{
1497-
State: fleetv1beta1.BindingStateBound,
1498-
TargetCluster: cluster2,
1499-
ResourceSnapshotName: "snapshot-2",
1500-
},
1501-
{
1502-
State: fleetv1beta1.BindingStateBound,
1503-
TargetCluster: cluster3,
1504-
ResourceSnapshotName: "snapshot-2",
1505-
},
1506-
},
1507-
wantNeedRoll: true,
1508-
wantWaitTime: 25 * time.Second,
1494+
latestResourceSnapshotName: "snapshot-2",
1495+
crp: crpWithUnavailablePeriod, // UnavailablePeriodSeconds is 60s -> readyTimeCutOff = t - 60s
1496+
wantStaleUnselectedBindings: []int{}, // empty list as unscheduled bindings will be removed and are not tracked in the CRP today.
1497+
wantDesiredBindingsSpec: []fleetv1beta1.ResourceBindingSpec{}, // unscheduled binding does not have desired spec so that putting the empty here
1498+
wantNeedRoll: true,
1499+
wantWaitTime: 25 * time.Second, // minWaitTime = (t - 35 seconds) - (t - 60 seconds) = 25 seconds
15091500
},
15101501
}
15111502
for name, tt := range tests {

0 commit comments

Comments
 (0)