@@ -31,10 +31,13 @@ import (
3131 "k8s.io/apimachinery/pkg/util/wait"
3232 "k8s.io/client-go/informers"
3333 "k8s.io/client-go/kubernetes/fake"
34+ testingclock "k8s.io/utils/clock/testing"
3435 "k8s.io/utils/ptr"
3536)
3637
3738func TestReconcileElectionStep (t * testing.T ) {
39+ fakeClock := testingclock .NewFakeClock (time .Now ())
40+
3841 tests := []struct {
3942 name string
4043 leaseNN types.NamespacedName
@@ -83,7 +86,7 @@ func TestReconcileElectionStep(t *testing.T) {
8386 LeaseName : "component-A" ,
8487 EmulationVersion : "1.19.0" ,
8588 BinaryVersion : "1.19.0" ,
86- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
89+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
8790 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
8891 },
8992 },
@@ -108,7 +111,7 @@ func TestReconcileElectionStep(t *testing.T) {
108111 LeaseName : "component-A" ,
109112 EmulationVersion : "1.19.0" ,
110113 BinaryVersion : "1.19.0" ,
111- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
114+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
112115 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
113116 },
114117 },
@@ -121,7 +124,7 @@ func TestReconcileElectionStep(t *testing.T) {
121124 LeaseName : "component-A" ,
122125 EmulationVersion : "1.18.0" ,
123126 BinaryVersion : "1.18.0" ,
124- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
127+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
125128 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
126129 },
127130 },
@@ -134,7 +137,7 @@ func TestReconcileElectionStep(t *testing.T) {
134137 Spec : v1.LeaseSpec {
135138 HolderIdentity : ptr .To ("component-identity-1" ),
136139 LeaseDurationSeconds : ptr .To (int32 (10 )),
137- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
140+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
138141 },
139142 },
140143 expectLease : true ,
@@ -157,8 +160,8 @@ func TestReconcileElectionStep(t *testing.T) {
157160 LeaseName : "component-A" ,
158161 EmulationVersion : "1.19.0" ,
159162 BinaryVersion : "1.19.0" ,
160- PingTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 2 * electionDuration ))),
161- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 4 * electionDuration ))),
163+ PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 2 * electionDuration ))),
164+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 4 * electionDuration ))),
162165 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
163166 },
164167 },
@@ -171,8 +174,8 @@ func TestReconcileElectionStep(t *testing.T) {
171174 LeaseName : "component-A" ,
172175 EmulationVersion : "1.20.0" ,
173176 BinaryVersion : "1.20.0" ,
174- PingTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
175- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
177+ PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now (). Add ( - 1 * time . Millisecond ))),
178+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
176179 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
177180 },
178181 },
@@ -197,7 +200,7 @@ func TestReconcileElectionStep(t *testing.T) {
197200 LeaseName : "component-A" ,
198201 EmulationVersion : "1.19.0" ,
199202 BinaryVersion : "1.19.0" ,
200- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
203+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
201204 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
202205 },
203206 },
@@ -210,7 +213,7 @@ func TestReconcileElectionStep(t *testing.T) {
210213 Spec : v1.LeaseSpec {
211214 HolderIdentity : ptr .To ("component-identity-expired" ),
212215 LeaseDurationSeconds : ptr .To (int32 (10 )),
213- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 1 * time .Minute ))),
216+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 1 * time .Minute ))),
214217 },
215218 },
216219 expectLease : true ,
@@ -232,8 +235,8 @@ func TestReconcileElectionStep(t *testing.T) {
232235 LeaseName : "component-A" ,
233236 EmulationVersion : "1.19.0" ,
234237 BinaryVersion : "1.19.0" ,
235- PingTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 1 * time .Minute ))),
236- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 2 * time .Minute ))),
238+ PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 1 * time .Minute ))),
239+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 2 * time .Minute ))),
237240 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
238241 },
239242 },
@@ -257,7 +260,7 @@ func TestReconcileElectionStep(t *testing.T) {
257260 LeaseName : "component-A" ,
258261 EmulationVersion : "1.19.0" ,
259262 BinaryVersion : "1.19.0" ,
260- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 2 * electionDuration ))),
263+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 2 * electionDuration ))),
261264 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
262265 },
263266 },
@@ -283,8 +286,8 @@ func TestReconcileElectionStep(t *testing.T) {
283286 LeaseName : "component-A" ,
284287 EmulationVersion : "1.19.0" ,
285288 BinaryVersion : "1.19.0" ,
286- PingTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
287- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 1 * time .Minute ))),
289+ PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
290+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 1 * time .Minute ))),
288291 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
289292 },
290293 },
@@ -308,7 +311,7 @@ func TestReconcileElectionStep(t *testing.T) {
308311 LeaseName : "component-A" ,
309312 EmulationVersion : "1.19.0" ,
310313 BinaryVersion : "1.19.0" ,
311- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
314+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
312315 PreferredStrategies : []v1.CoordinatedLeaseStrategy {"foo.com/bar" },
313316 },
314317 },
@@ -321,7 +324,7 @@ func TestReconcileElectionStep(t *testing.T) {
321324 Spec : v1.LeaseSpec {
322325 HolderIdentity : ptr .To ("component-identity-expired" ),
323326 LeaseDurationSeconds : ptr .To (int32 (10 )),
324- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 1 * time .Minute ))),
327+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 1 * time .Minute ))),
325328 },
326329 },
327330 expectLease : true ,
@@ -344,6 +347,7 @@ func TestReconcileElectionStep(t *testing.T) {
344347 client .CoordinationV1 (),
345348 client .CoordinationV1alpha1 (),
346349 )
350+ controller .clock = fakeClock
347351 if err != nil {
348352 t .Fatal (err )
349353 }
@@ -435,6 +439,8 @@ func TestReconcileElectionStep(t *testing.T) {
435439}
436440
437441func TestController (t * testing.T ) {
442+ fakeClock := testingclock .NewFakeClock (time .Now ())
443+
438444 cases := []struct {
439445 name string
440446 leaseNN types.NamespacedName
@@ -455,7 +461,7 @@ func TestController(t *testing.T) {
455461 LeaseName : "component-A" ,
456462 EmulationVersion : "1.19.0" ,
457463 BinaryVersion : "1.19.0" ,
458- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
464+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
459465 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
460466 },
461467 },
@@ -485,7 +491,7 @@ func TestController(t *testing.T) {
485491 LeaseName : "component-A" ,
486492 EmulationVersion : "1.19.0" ,
487493 BinaryVersion : "1.19.0" ,
488- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
494+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
489495 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
490496 },
491497 },
@@ -498,7 +504,7 @@ func TestController(t *testing.T) {
498504 LeaseName : "component-A" ,
499505 EmulationVersion : "1.19.0" ,
500506 BinaryVersion : "1.20.0" ,
501- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
507+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
502508 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
503509 },
504510 },
@@ -511,7 +517,7 @@ func TestController(t *testing.T) {
511517 LeaseName : "component-A" ,
512518 EmulationVersion : "1.20.0" ,
513519 BinaryVersion : "1.20.0" ,
514- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
520+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
515521 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
516522 },
517523 },
@@ -549,7 +555,7 @@ func TestController(t *testing.T) {
549555 LeaseName : "component-A" ,
550556 EmulationVersion : "1.19.0" ,
551557 BinaryVersion : "1.19.0" ,
552- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
558+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
553559 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
554560 },
555561 },
@@ -590,7 +596,7 @@ func TestController(t *testing.T) {
590596 LeaseName : "component-A" ,
591597 EmulationVersion : "1.20.0" ,
592598 BinaryVersion : "1.20.0" ,
593- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
599+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
594600 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
595601 },
596602 },
@@ -603,7 +609,7 @@ func TestController(t *testing.T) {
603609 LeaseName : "component-A" ,
604610 EmulationVersion : "1.19.0" ,
605611 BinaryVersion : "1.19.0" ,
606- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
612+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
607613 PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
608614 },
609615 },
@@ -680,7 +686,7 @@ func TestController(t *testing.T) {
680686 if err == nil {
681687 if lease .Spec .PingTime != nil {
682688 c := lease .DeepCopy ()
683- c .Spec .RenewTime = & metav1.MicroTime {Time : time .Now ()}
689+ c .Spec .RenewTime = & metav1.MicroTime {Time : fakeClock .Now ()}
684690 _ , err = client .CoordinationV1alpha1 ().LeaseCandidates (lc .Namespace ).Update (ctx , c , metav1.UpdateOptions {})
685691 if err != nil {
686692 runtime .HandleError (err )
0 commit comments