Skip to content

Commit 0c9fe14

Browse files
Wei WengWei Weng
authored andcommitted
use accurate now per test
Signed-off-by: Wei Weng <[email protected]>
1 parent 3820bb0 commit 0c9fe14

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

pkg/controllers/rollout/controller_test.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,7 @@ func TestPickBindingsToRoll(t *testing.T) {
12311231
wantNeedRoll bool
12321232
wantWaitTime time.Duration
12331233
wantErr error
1234+
bindingsFunc func() []*placementv1beta1.ClusterResourceBinding // alternative to allBindings, called at test time with fresh 'now'
12341235
}{
12351236
// TODO: add more tests
12361237
"test scheduled binding to bound, outdated resources and nil overrides - rollout allowed": {
@@ -1870,10 +1871,13 @@ func TestPickBindingsToRoll(t *testing.T) {
18701871
},
18711872
"test bound bindings with different waitTimes and check the wait time should be the min of them all": {
18721873
// want the min wait time of bound bindings that are not ready
1873-
allBindings: []*placementv1beta1.ClusterResourceBinding{
1874-
generateNotTrackableClusterResourceBinding(placementv1beta1.BindingStateBound, "snapshot-1", cluster3, metav1.Time{Time: now.Add(-35 * time.Second)}), // notReady, waitTime = t - 35s
1875-
generateCanBeReadyClusterResourceBinding(placementv1beta1.BindingStateBound, "snapshot-1", cluster1), // notReady, no wait time because it does not have available condition yet,
1876-
generateReadyClusterResourceBinding(placementv1beta1.BindingStateBound, "snapshot-2", cluster2), // Ready
1874+
bindingsFunc: func() []*placementv1beta1.ClusterResourceBinding {
1875+
now := time.Now() // Fresh timestamp for accurate timing
1876+
return []*placementv1beta1.ClusterResourceBinding{
1877+
generateNotTrackableClusterResourceBinding(placementv1beta1.BindingStateBound, "snapshot-1", cluster3, metav1.Time{Time: now.Add(-35 * time.Second)}), // notReady, waitTime = t - 35s
1878+
generateCanBeReadyClusterResourceBinding(placementv1beta1.BindingStateBound, "snapshot-1", cluster1), // notReady, no wait time because it does not have available condition yet,
1879+
generateReadyClusterResourceBinding(placementv1beta1.BindingStateBound, "snapshot-2", cluster2), // Ready
1880+
}
18771881
},
18781882
latestResourceSnapshotName: "snapshot-2",
18791883
crp: clusterResourcePlacementForTest("test",
@@ -1909,9 +1913,12 @@ func TestPickBindingsToRoll(t *testing.T) {
19091913
},
19101914
"test unscheduled bindings with different waitTimes and check the wait time is correct": {
19111915
// want the min wait time of unscheduled bindings that are not ready
1912-
allBindings: []*placementv1beta1.ClusterResourceBinding{
1913-
generateNotTrackableClusterResourceBinding(placementv1beta1.BindingStateUnscheduled, "snapshot-1", cluster2, metav1.Time{Time: now.Add(-1 * time.Minute)}), // NotReady, waitTime = t - 60s
1914-
generateNotTrackableClusterResourceBinding(placementv1beta1.BindingStateUnscheduled, "snapshot-1", cluster3, metav1.Time{Time: now.Add(-35 * time.Second)}), // NotReady, waitTime = t - 35s
1916+
bindingsFunc: func() []*placementv1beta1.ClusterResourceBinding {
1917+
now := time.Now() // Fresh timestamp for accurate timing
1918+
return []*placementv1beta1.ClusterResourceBinding{
1919+
generateNotTrackableClusterResourceBinding(placementv1beta1.BindingStateUnscheduled, "snapshot-1", cluster2, metav1.Time{Time: now.Add(-1 * time.Minute)}), // NotReady, waitTime = t - 60s
1920+
generateNotTrackableClusterResourceBinding(placementv1beta1.BindingStateUnscheduled, "snapshot-1", cluster3, metav1.Time{Time: now.Add(-35 * time.Second)}), // NotReady, waitTime = t - 35s
1921+
}
19151922
},
19161923
latestResourceSnapshotName: "snapshot-2",
19171924
crp: clusterResourcePlacementForTest("test",
@@ -2501,6 +2508,15 @@ func TestPickBindingsToRoll(t *testing.T) {
25012508
}
25022509
for name, tt := range tests {
25032510
t.Run(name, func(t *testing.T) {
2511+
now := time.Now() // Capture fresh timestamp for this specific test case
2512+
_ = now // Suppress unused variable warning for tests that don't use it
2513+
2514+
// Use bindingsFunc if provided (for timing-sensitive tests), otherwise use allBindings
2515+
allBindings := tt.allBindings
2516+
if tt.bindingsFunc != nil {
2517+
allBindings = tt.bindingsFunc()
2518+
}
2519+
25042520
scheme := serviceScheme(t)
25052521
var objects []client.Object
25062522
for i := range tt.clusters {
@@ -2519,7 +2535,7 @@ func TestPickBindingsToRoll(t *testing.T) {
25192535
Name: tt.latestResourceSnapshotName,
25202536
},
25212537
}
2522-
gotUpdatedBindings, gotStaleUnselectedBindings, gotUpToDateBoundBindings, gotNeedRoll, gotWaitTime, err := r.pickBindingsToRoll(context.Background(), controller.ConvertCRBArrayToBindingObjs(tt.allBindings), resourceSnapshot, tt.crp, tt.matchedCROs, tt.matchedROs)
2538+
gotUpdatedBindings, gotStaleUnselectedBindings, gotUpToDateBoundBindings, gotNeedRoll, gotWaitTime, err := r.pickBindingsToRoll(context.Background(), controller.ConvertCRBArrayToBindingObjs(allBindings), resourceSnapshot, tt.crp, tt.matchedCROs, tt.matchedROs)
25232539
if (err != nil) != (tt.wantErr != nil) || err != nil && !errors.Is(err, tt.wantErr) {
25242540
t.Fatalf("pickBindingsToRoll() error = %v, wantErr %v", err, tt.wantErr)
25252541
}
@@ -2530,30 +2546,30 @@ func TestPickBindingsToRoll(t *testing.T) {
25302546
wantTobeUpdatedBindings := make([]toBeUpdatedBinding, len(tt.wantTobeUpdatedBindings))
25312547
for i, index := range tt.wantTobeUpdatedBindings {
25322548
// Unscheduled bindings are only removed in a single rollout cycle.
2533-
bindingSpec := tt.allBindings[index].GetBindingSpec()
2549+
bindingSpec := allBindings[index].GetBindingSpec()
25342550
if bindingSpec.State != placementv1beta1.BindingStateUnscheduled {
2535-
wantTobeUpdatedBindings[i].currentBinding = tt.allBindings[index]
2536-
wantTobeUpdatedBindings[i].desiredBinding = tt.allBindings[index].DeepCopy()
2551+
wantTobeUpdatedBindings[i].currentBinding = allBindings[index]
2552+
wantTobeUpdatedBindings[i].desiredBinding = allBindings[index].DeepCopy()
25372553
wantTobeUpdatedBindings[i].desiredBinding.SetBindingSpec(tt.wantDesiredBindingsSpec[index])
25382554
} else {
2539-
wantTobeUpdatedBindings[i].currentBinding = tt.allBindings[index]
2555+
wantTobeUpdatedBindings[i].currentBinding = allBindings[index]
25402556
}
25412557
}
25422558
wantStaleUnselectedBindings := make([]toBeUpdatedBinding, len(tt.wantStaleUnselectedBindings))
25432559
for i, index := range tt.wantStaleUnselectedBindings {
25442560
// Unscheduled bindings are only removed in a single rollout cycle.
2545-
bindingSpec := tt.allBindings[index].GetBindingSpec()
2561+
bindingSpec := allBindings[index].GetBindingSpec()
25462562
if bindingSpec.State != placementv1beta1.BindingStateUnscheduled {
2547-
wantStaleUnselectedBindings[i].currentBinding = tt.allBindings[index]
2548-
wantStaleUnselectedBindings[i].desiredBinding = tt.allBindings[index].DeepCopy()
2563+
wantStaleUnselectedBindings[i].currentBinding = allBindings[index]
2564+
wantStaleUnselectedBindings[i].desiredBinding = allBindings[index].DeepCopy()
25492565
wantStaleUnselectedBindings[i].desiredBinding.SetBindingSpec(tt.wantDesiredBindingsSpec[index])
25502566
} else {
2551-
wantStaleUnselectedBindings[i].currentBinding = tt.allBindings[index]
2567+
wantStaleUnselectedBindings[i].currentBinding = allBindings[index]
25522568
}
25532569
}
25542570
wantUpToDateBoundBindings := make([]toBeUpdatedBinding, len(tt.wantUpToDateBoundBindings))
25552571
for i, index := range tt.wantUpToDateBoundBindings {
2556-
wantUpToDateBoundBindings[i].currentBinding = tt.allBindings[index]
2572+
wantUpToDateBoundBindings[i].currentBinding = allBindings[index]
25572573
}
25582574

25592575
if diff := cmp.Diff(wantTobeUpdatedBindings, gotUpdatedBindings, cmpOptions...); diff != "" {

0 commit comments

Comments
 (0)