@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"math/rand"
23
+ "sync"
23
24
"testing"
24
25
"time"
25
26
@@ -31,7 +32,6 @@ import (
31
32
"k8s.io/apimachinery/pkg/util/wait"
32
33
"k8s.io/client-go/informers"
33
34
"k8s.io/client-go/kubernetes/fake"
34
- "k8s.io/client-go/tools/cache"
35
35
testingclock "k8s.io/utils/clock/testing"
36
36
"k8s.io/utils/ptr"
37
37
)
@@ -175,7 +175,7 @@ func TestReconcileElectionStep(t *testing.T) {
175
175
LeaseName : "component-A" ,
176
176
EmulationVersion : "1.20.0" ,
177
177
BinaryVersion : "1.20.0" ,
178
- PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now (). Add ( - 1 * time . Millisecond ) )),
178
+ PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
179
179
RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
180
180
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
181
181
},
@@ -440,8 +440,6 @@ func TestReconcileElectionStep(t *testing.T) {
440
440
}
441
441
442
442
func TestController (t * testing.T ) {
443
- fakeClock := testingclock .NewFakeClock (time .Now ())
444
-
445
443
cases := []struct {
446
444
name string
447
445
leases []* v1.Lease
@@ -462,7 +460,7 @@ func TestController(t *testing.T) {
462
460
LeaseName : "component-A" ,
463
461
EmulationVersion : "1.19.0" ,
464
462
BinaryVersion : "1.19.0" ,
465
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
463
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
466
464
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
467
465
},
468
466
},
@@ -491,7 +489,7 @@ func TestController(t *testing.T) {
491
489
LeaseName : "component-A" ,
492
490
EmulationVersion : "1.19.0" ,
493
491
BinaryVersion : "1.19.0" ,
494
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
492
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
495
493
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
496
494
},
497
495
},
@@ -504,7 +502,7 @@ func TestController(t *testing.T) {
504
502
LeaseName : "component-A" ,
505
503
EmulationVersion : "1.19.0" ,
506
504
BinaryVersion : "1.20.0" ,
507
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
505
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
508
506
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
509
507
},
510
508
},
@@ -517,7 +515,7 @@ func TestController(t *testing.T) {
517
515
LeaseName : "component-A" ,
518
516
EmulationVersion : "1.20.0" ,
519
517
BinaryVersion : "1.20.0" ,
520
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
518
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
521
519
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
522
520
},
523
521
},
@@ -544,7 +542,7 @@ func TestController(t *testing.T) {
544
542
},
545
543
Spec : v1.LeaseSpec {
546
544
HolderIdentity : ptr .To ("some-other-component" ),
547
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (time .Second ))),
545
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (time .Second ))),
548
546
LeaseDurationSeconds : ptr .To (int32 (10 )),
549
547
},
550
548
},
@@ -559,7 +557,7 @@ func TestController(t *testing.T) {
559
557
LeaseName : "component-A" ,
560
558
EmulationVersion : "1.19.0" ,
561
559
BinaryVersion : "1.19.0" ,
562
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
560
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
563
561
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
564
562
},
565
563
},
@@ -589,7 +587,7 @@ func TestController(t *testing.T) {
589
587
},
590
588
Spec : v1.LeaseSpec {
591
589
HolderIdentity : ptr .To ("some-other-component" ),
592
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 11 * time .Second ))),
590
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 11 * time .Second ))),
593
591
LeaseDurationSeconds : ptr .To (int32 (10 )),
594
592
Strategy : ptr.To [v1.CoordinatedLeaseStrategy ]("OldestEmulationVersion" ),
595
593
},
@@ -605,7 +603,7 @@ func TestController(t *testing.T) {
605
603
LeaseName : "component-A" ,
606
604
EmulationVersion : "1.19.0" ,
607
605
BinaryVersion : "1.19.0" ,
608
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
606
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
609
607
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
610
608
},
611
609
},
@@ -633,7 +631,7 @@ func TestController(t *testing.T) {
633
631
Spec : v1.LeaseSpec {
634
632
HolderIdentity : ptr .To ("component-identity-1" ),
635
633
Strategy : ptr.To [v1.CoordinatedLeaseStrategy ]("OldestEmulationVersion" ),
636
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (time .Second ))),
634
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (time .Second ))),
637
635
LeaseDurationSeconds : ptr .To (int32 (10 )),
638
636
},
639
637
},
@@ -648,7 +646,7 @@ func TestController(t *testing.T) {
648
646
LeaseName : "component-A" ,
649
647
EmulationVersion : "1.20.0" ,
650
648
BinaryVersion : "1.20.0" ,
651
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
649
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
652
650
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
653
651
},
654
652
},
@@ -663,7 +661,7 @@ func TestController(t *testing.T) {
663
661
LeaseName : "component-A" ,
664
662
EmulationVersion : "1.19.0" ,
665
663
BinaryVersion : "1.19.0" ,
666
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
664
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
667
665
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
668
666
},
669
667
},
@@ -684,6 +682,10 @@ func TestController(t *testing.T) {
684
682
685
683
for _ , tc := range cases {
686
684
t .Run (tc .name , func (t * testing.T ) {
685
+ // collect go routines using t.logf
686
+ var wg sync.WaitGroup
687
+ defer wg .Wait ()
688
+
687
689
ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
688
690
defer cancel ()
689
691
@@ -698,7 +700,6 @@ func TestController(t *testing.T) {
698
700
if err != nil {
699
701
t .Fatal (err )
700
702
}
701
- controller .clock = fakeClock
702
703
703
704
for _ , obj := range tc .leases {
704
705
t .Logf ("Pre-creating lease %s/%s" , obj .Namespace , obj .Name )
@@ -717,17 +718,16 @@ func TestController(t *testing.T) {
717
718
718
719
informerFactory .Start (ctx .Done ())
719
720
informerFactory .WaitForCacheSync (ctx .Done ())
720
- if ! cache .WaitForNamedCacheSync (controllerName , ctx .Done (), controller .leaseRegistration .HasSynced , controller .leaseCandidateRegistration .HasSynced ) {
721
- return
722
- }
723
721
go controller .Run (ctx , 1 )
724
722
725
723
if rand .Intn (2 ) == 0 {
726
724
t .Logf ("Giving controller a chance to run" )
727
725
time .Sleep (time .Second )
728
726
}
729
727
728
+ wg .Add (1 )
730
729
go func () {
730
+ defer wg .Done ()
731
731
ticker := time .NewTicker (10 * time .Millisecond )
732
732
// Mock out the removal of preferredHolder leases.
733
733
// When controllers are running, they are expected to do this voluntarily
@@ -752,16 +752,18 @@ func TestController(t *testing.T) {
752
752
continue // only candidate-aware controllers will follow preferredHolder
753
753
}
754
754
755
- fmt . Printf ("Deleting lease %s/%s because of preferredHolder %q != %q" , l .Namespace , l .Name , * ph , * l .Spec .HolderIdentity )
755
+ t . Logf ("Deleting lease %s/%s because of preferredHolder %q != %q" , l .Namespace , l .Name , * ph , * l .Spec .HolderIdentity )
756
756
if err = client .CoordinationV1 ().Leases (expectedLease .Namespace ).Delete (ctx , expectedLease .Name , metav1.DeleteOptions {}); err != nil {
757
- fmt . Printf ("Error deleting lease %s/%s: %v" , l .Namespace , l .Name , err )
757
+ t . Logf ("Error deleting lease %s/%s: %v" , l .Namespace , l .Name , err )
758
758
}
759
759
}
760
760
}
761
761
}
762
762
}()
763
763
764
+ wg .Add (1 )
764
765
go func () {
766
+ defer wg .Done ()
765
767
ticker := time .NewTicker (10 * time .Millisecond )
766
768
// Mock out leasecandidate ack.
767
769
// When controllers are running, they are expected to watch and ack
@@ -770,16 +772,18 @@ func TestController(t *testing.T) {
770
772
case <- ctx .Done ():
771
773
return
772
774
case <- ticker .C :
773
- for _ , lc := range tc .createAfterControllerStart {
774
- c , err := client .CoordinationV1alpha1 ().LeaseCandidates (lc .Namespace ).Get (ctx , lc .Name , metav1.GetOptions {})
775
- if err == nil {
776
- if c .Spec .PingTime != nil && c .Spec .PingTime .Before (c .Spec .RenewTime ) {
777
- fmt .Printf ("Answering ping for %s/%s" , c .Namespace , c .Name )
778
- c .Spec .RenewTime = & metav1.MicroTime {Time : fakeClock .Now ()}
779
- _ , err = client .CoordinationV1alpha1 ().LeaseCandidates (lc .Namespace ).Update (ctx , c , metav1.UpdateOptions {})
780
- if err != nil {
781
- fmt .Printf ("Error updating lease candidate %s/%s: %v" , c .Namespace , c .Name , err )
782
- }
775
+ cs , err := client .CoordinationV1alpha1 ().LeaseCandidates ("" ).List (ctx , metav1.ListOptions {})
776
+ if err != nil {
777
+ t .Logf ("Error listing lease candidates: %v" , err )
778
+ continue
779
+ }
780
+ for _ , c := range cs .Items {
781
+ if c .Spec .PingTime != nil && (c .Spec .RenewTime == nil || c .Spec .PingTime .Time .After (c .Spec .RenewTime .Time )) {
782
+ t .Logf ("Answering ping for %s/%s" , c .Namespace , c .Name )
783
+ c .Spec .RenewTime = & metav1.MicroTime {Time : time .Now ()}
784
+ _ , err = client .CoordinationV1alpha1 ().LeaseCandidates (c .Namespace ).Update (ctx , & c , metav1.UpdateOptions {})
785
+ if err != nil {
786
+ t .Logf ("Error updating lease candidate %s/%s: %v" , c .Namespace , c .Name , err )
783
787
}
784
788
}
785
789
}
@@ -820,10 +824,7 @@ func TestController(t *testing.T) {
820
824
if lease .Spec .HolderIdentity == nil {
821
825
return false , nil
822
826
}
823
- if * expectedLease .Spec .HolderIdentity != * lease .Spec .HolderIdentity {
824
- return false , nil
825
- }
826
- return true , nil
827
+ return * expectedLease .Spec .HolderIdentity == * lease .Spec .HolderIdentity , nil
827
828
})
828
829
if err != nil {
829
830
if lease == nil {
0 commit comments