@@ -31,10 +31,13 @@ import (
31
31
"k8s.io/apimachinery/pkg/util/wait"
32
32
"k8s.io/client-go/informers"
33
33
"k8s.io/client-go/kubernetes/fake"
34
+ testingclock "k8s.io/utils/clock/testing"
34
35
"k8s.io/utils/ptr"
35
36
)
36
37
37
38
func TestReconcileElectionStep (t * testing.T ) {
39
+ fakeClock := testingclock .NewFakeClock (time .Now ())
40
+
38
41
tests := []struct {
39
42
name string
40
43
leaseNN types.NamespacedName
@@ -83,7 +86,7 @@ func TestReconcileElectionStep(t *testing.T) {
83
86
LeaseName : "component-A" ,
84
87
EmulationVersion : "1.19.0" ,
85
88
BinaryVersion : "1.19.0" ,
86
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
89
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
87
90
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
88
91
},
89
92
},
@@ -108,7 +111,7 @@ func TestReconcileElectionStep(t *testing.T) {
108
111
LeaseName : "component-A" ,
109
112
EmulationVersion : "1.19.0" ,
110
113
BinaryVersion : "1.19.0" ,
111
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
114
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
112
115
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
113
116
},
114
117
},
@@ -121,7 +124,7 @@ func TestReconcileElectionStep(t *testing.T) {
121
124
LeaseName : "component-A" ,
122
125
EmulationVersion : "1.18.0" ,
123
126
BinaryVersion : "1.18.0" ,
124
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
127
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
125
128
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
126
129
},
127
130
},
@@ -134,7 +137,7 @@ func TestReconcileElectionStep(t *testing.T) {
134
137
Spec : v1.LeaseSpec {
135
138
HolderIdentity : ptr .To ("component-identity-1" ),
136
139
LeaseDurationSeconds : ptr .To (int32 (10 )),
137
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
140
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
138
141
},
139
142
},
140
143
expectLease : true ,
@@ -157,8 +160,8 @@ func TestReconcileElectionStep(t *testing.T) {
157
160
LeaseName : "component-A" ,
158
161
EmulationVersion : "1.19.0" ,
159
162
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 ))),
162
165
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
163
166
},
164
167
},
@@ -171,8 +174,8 @@ func TestReconcileElectionStep(t *testing.T) {
171
174
LeaseName : "component-A" ,
172
175
EmulationVersion : "1.20.0" ,
173
176
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 ())),
176
179
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
177
180
},
178
181
},
@@ -197,7 +200,7 @@ func TestReconcileElectionStep(t *testing.T) {
197
200
LeaseName : "component-A" ,
198
201
EmulationVersion : "1.19.0" ,
199
202
BinaryVersion : "1.19.0" ,
200
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
203
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
201
204
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
202
205
},
203
206
},
@@ -210,7 +213,7 @@ func TestReconcileElectionStep(t *testing.T) {
210
213
Spec : v1.LeaseSpec {
211
214
HolderIdentity : ptr .To ("component-identity-expired" ),
212
215
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 ))),
214
217
},
215
218
},
216
219
expectLease : true ,
@@ -232,8 +235,8 @@ func TestReconcileElectionStep(t *testing.T) {
232
235
LeaseName : "component-A" ,
233
236
EmulationVersion : "1.19.0" ,
234
237
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 ))),
237
240
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
238
241
},
239
242
},
@@ -257,7 +260,7 @@ func TestReconcileElectionStep(t *testing.T) {
257
260
LeaseName : "component-A" ,
258
261
EmulationVersion : "1.19.0" ,
259
262
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 ))),
261
264
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
262
265
},
263
266
},
@@ -283,8 +286,8 @@ func TestReconcileElectionStep(t *testing.T) {
283
286
LeaseName : "component-A" ,
284
287
EmulationVersion : "1.19.0" ,
285
288
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 ))),
288
291
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
289
292
},
290
293
},
@@ -308,7 +311,7 @@ func TestReconcileElectionStep(t *testing.T) {
308
311
LeaseName : "component-A" ,
309
312
EmulationVersion : "1.19.0" ,
310
313
BinaryVersion : "1.19.0" ,
311
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
314
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
312
315
PreferredStrategies : []v1.CoordinatedLeaseStrategy {"foo.com/bar" },
313
316
},
314
317
},
@@ -321,7 +324,7 @@ func TestReconcileElectionStep(t *testing.T) {
321
324
Spec : v1.LeaseSpec {
322
325
HolderIdentity : ptr .To ("component-identity-expired" ),
323
326
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 ))),
325
328
},
326
329
},
327
330
expectLease : true ,
@@ -344,6 +347,7 @@ func TestReconcileElectionStep(t *testing.T) {
344
347
client .CoordinationV1 (),
345
348
client .CoordinationV1alpha1 (),
346
349
)
350
+ controller .clock = fakeClock
347
351
if err != nil {
348
352
t .Fatal (err )
349
353
}
@@ -435,6 +439,8 @@ func TestReconcileElectionStep(t *testing.T) {
435
439
}
436
440
437
441
func TestController (t * testing.T ) {
442
+ fakeClock := testingclock .NewFakeClock (time .Now ())
443
+
438
444
cases := []struct {
439
445
name string
440
446
leaseNN types.NamespacedName
@@ -455,7 +461,7 @@ func TestController(t *testing.T) {
455
461
LeaseName : "component-A" ,
456
462
EmulationVersion : "1.19.0" ,
457
463
BinaryVersion : "1.19.0" ,
458
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
464
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
459
465
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
460
466
},
461
467
},
@@ -485,7 +491,7 @@ func TestController(t *testing.T) {
485
491
LeaseName : "component-A" ,
486
492
EmulationVersion : "1.19.0" ,
487
493
BinaryVersion : "1.19.0" ,
488
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
494
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
489
495
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
490
496
},
491
497
},
@@ -498,7 +504,7 @@ func TestController(t *testing.T) {
498
504
LeaseName : "component-A" ,
499
505
EmulationVersion : "1.19.0" ,
500
506
BinaryVersion : "1.20.0" ,
501
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
507
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
502
508
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
503
509
},
504
510
},
@@ -511,7 +517,7 @@ func TestController(t *testing.T) {
511
517
LeaseName : "component-A" ,
512
518
EmulationVersion : "1.20.0" ,
513
519
BinaryVersion : "1.20.0" ,
514
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
520
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
515
521
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
516
522
},
517
523
},
@@ -549,7 +555,7 @@ func TestController(t *testing.T) {
549
555
LeaseName : "component-A" ,
550
556
EmulationVersion : "1.19.0" ,
551
557
BinaryVersion : "1.19.0" ,
552
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
558
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
553
559
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
554
560
},
555
561
},
@@ -590,7 +596,7 @@ func TestController(t *testing.T) {
590
596
LeaseName : "component-A" ,
591
597
EmulationVersion : "1.20.0" ,
592
598
BinaryVersion : "1.20.0" ,
593
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
599
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
594
600
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
595
601
},
596
602
},
@@ -603,7 +609,7 @@ func TestController(t *testing.T) {
603
609
LeaseName : "component-A" ,
604
610
EmulationVersion : "1.19.0" ,
605
611
BinaryVersion : "1.19.0" ,
606
- RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
612
+ RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
607
613
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
608
614
},
609
615
},
@@ -680,7 +686,7 @@ func TestController(t *testing.T) {
680
686
if err == nil {
681
687
if lease .Spec .PingTime != nil {
682
688
c := lease .DeepCopy ()
683
- c .Spec .RenewTime = & metav1.MicroTime {Time : time .Now ()}
689
+ c .Spec .RenewTime = & metav1.MicroTime {Time : fakeClock .Now ()}
684
690
_ , err = client .CoordinationV1alpha1 ().LeaseCandidates (lc .Namespace ).Update (ctx , c , metav1.UpdateOptions {})
685
691
if err != nil {
686
692
runtime .HandleError (err )
0 commit comments