@@ -8,18 +8,18 @@ import (
8
8
"testing"
9
9
"time"
10
10
11
- "github.com/andres-erbsen/clock"
12
11
"github.com/sirupsen/logrus/hooks/test"
13
12
"github.com/spiffe/go-spiffe/v2/spiffeid"
14
13
"github.com/spiffe/spire/pkg/common/bundleutil"
15
14
"github.com/spiffe/spire/pkg/common/telemetry"
16
15
"github.com/spiffe/spire/proto/spire/common"
16
+ "github.com/spiffe/spire/test/clock"
17
17
"github.com/stretchr/testify/assert"
18
18
"github.com/stretchr/testify/require"
19
19
)
20
20
21
21
func TestLRUCacheFetchWorkloadUpdate (t * testing.T ) {
22
- cache := newTestLRUCache ()
22
+ cache := newTestLRUCache (t )
23
23
// populate the cache with FOO and BAR without SVIDS
24
24
foo := makeRegistrationEntry ("FOO" , "A" )
25
25
bar := makeRegistrationEntry ("BAR" , "B" )
@@ -50,7 +50,7 @@ func TestLRUCacheFetchWorkloadUpdate(t *testing.T) {
50
50
}
51
51
52
52
func TestLRUCacheMatchingRegistrationIdentities (t * testing.T ) {
53
- cache := newTestLRUCache ()
53
+ cache := newTestLRUCache (t )
54
54
55
55
// populate the cache with FOO and BAR without SVIDS
56
56
foo := makeRegistrationEntry ("FOO" , "A" )
@@ -79,7 +79,7 @@ func TestLRUCacheMatchingRegistrationIdentities(t *testing.T) {
79
79
}
80
80
81
81
func TestLRUCacheCountSVIDs (t * testing.T ) {
82
- cache := newTestLRUCache ()
82
+ cache := newTestLRUCache (t )
83
83
84
84
// populate the cache with FOO and BAR without SVIDS
85
85
foo := makeRegistrationEntry ("FOO" , "A" )
@@ -103,7 +103,7 @@ func TestLRUCacheCountSVIDs(t *testing.T) {
103
103
}
104
104
105
105
func TestLRUCacheBundleChanges (t * testing.T ) {
106
- cache := newTestLRUCache ()
106
+ cache := newTestLRUCache (t )
107
107
108
108
bundleStream := cache .SubscribeToBundleChanges ()
109
109
assert .Equal (t , makeBundles (bundleV1 ), bundleStream .Value ())
@@ -127,7 +127,7 @@ func TestLRUCacheBundleChanges(t *testing.T) {
127
127
}
128
128
129
129
func TestLRUCacheAllSubscribersNotifiedOnBundleChange (t * testing.T ) {
130
- cache := newTestLRUCache ()
130
+ cache := newTestLRUCache (t )
131
131
132
132
// create some subscribers and assert they get the initial bundle
133
133
subA := subscribeToWorkloadUpdates (t , cache , makeSelectors ("A" ))
@@ -147,7 +147,7 @@ func TestLRUCacheAllSubscribersNotifiedOnBundleChange(t *testing.T) {
147
147
}
148
148
149
149
func TestLRUCacheSomeSubscribersNotifiedOnFederatedBundleChange (t * testing.T ) {
150
- cache := newTestLRUCache ()
150
+ cache := newTestLRUCache (t )
151
151
152
152
// initialize the cache with an entry FOO that has a valid SVID and
153
153
// selector "A"
@@ -221,7 +221,7 @@ func TestLRUCacheSomeSubscribersNotifiedOnFederatedBundleChange(t *testing.T) {
221
221
}
222
222
223
223
func TestLRUCacheSubscribersGetEntriesWithSelectorSubsets (t * testing.T ) {
224
- cache := newTestLRUCache ()
224
+ cache := newTestLRUCache (t )
225
225
226
226
// create subscribers for each combination of selectors
227
227
subA := subscribeToWorkloadUpdates (t , cache , makeSelectors ("A" ))
@@ -270,7 +270,7 @@ func TestLRUCacheSubscribersGetEntriesWithSelectorSubsets(t *testing.T) {
270
270
}
271
271
272
272
func TestLRUCacheSubscriberIsNotNotifiedIfNothingChanges (t * testing.T ) {
273
- cache := newTestLRUCache ()
273
+ cache := newTestLRUCache (t )
274
274
275
275
foo := makeRegistrationEntry ("FOO" , "A" )
276
276
cache .UpdateEntries (& UpdateEntries {
@@ -296,7 +296,7 @@ func TestLRUCacheSubscriberIsNotNotifiedIfNothingChanges(t *testing.T) {
296
296
}
297
297
298
298
func TestLRUCacheSubscriberNotifiedOnSVIDChanges (t * testing.T ) {
299
- cache := newTestLRUCache ()
299
+ cache := newTestLRUCache (t )
300
300
301
301
foo := makeRegistrationEntry ("FOO" , "A" )
302
302
cache .UpdateEntries (& UpdateEntries {
@@ -323,7 +323,7 @@ func TestLRUCacheSubscriberNotifiedOnSVIDChanges(t *testing.T) {
323
323
}
324
324
325
325
func TestLRUCacheSubscriberNotificationsOnSelectorChanges (t * testing.T ) {
326
- cache := newTestLRUCache ()
326
+ cache := newTestLRUCache (t )
327
327
328
328
// initialize the cache with a FOO entry with selector A and an SVID
329
329
foo := makeRegistrationEntry ("FOO" , "A" )
@@ -374,7 +374,7 @@ func TestLRUCacheSubscriberNotificationsOnSelectorChanges(t *testing.T) {
374
374
}
375
375
376
376
func TestLRUCacheSubscriberNotifiedWhenEntryDropped (t * testing.T ) {
377
- cache := newTestLRUCache ()
377
+ cache := newTestLRUCache (t )
378
378
379
379
subA := subscribeToWorkloadUpdates (t , cache , makeSelectors ("A" ))
380
380
defer subA .Finish ()
@@ -417,7 +417,7 @@ func TestLRUCacheSubscriberNotifiedWhenEntryDropped(t *testing.T) {
417
417
}
418
418
419
419
func TestLRUCacheSubscriberOnlyGetsEntriesWithSVID (t * testing.T ) {
420
- cache := newTestLRUCache ()
420
+ cache := newTestLRUCache (t )
421
421
422
422
foo := makeRegistrationEntry ("FOO" , "A" )
423
423
updateEntries := & UpdateEntries {
@@ -441,7 +441,7 @@ func TestLRUCacheSubscriberOnlyGetsEntriesWithSVID(t *testing.T) {
441
441
}
442
442
443
443
func TestLRUCacheSubscribersDoNotBlockNotifications (t * testing.T ) {
444
- cache := newTestLRUCache ()
444
+ cache := newTestLRUCache (t )
445
445
446
446
sub := subscribeToWorkloadUpdates (t , cache , makeSelectors ("A" ))
447
447
defer sub .Finish ()
@@ -460,7 +460,7 @@ func TestLRUCacheSubscribersDoNotBlockNotifications(t *testing.T) {
460
460
}
461
461
462
462
func TestLRUCacheCheckSVIDCallback (t * testing.T ) {
463
- cache := newTestLRUCache ()
463
+ cache := newTestLRUCache (t )
464
464
465
465
// no calls because there are no registration entries
466
466
cache .UpdateEntries (& UpdateEntries {
@@ -507,7 +507,7 @@ func TestLRUCacheCheckSVIDCallback(t *testing.T) {
507
507
}
508
508
509
509
func TestLRUCacheGetStaleEntries (t * testing.T ) {
510
- cache := newTestLRUCache ()
510
+ cache := newTestLRUCache (t )
511
511
512
512
bar := makeRegistrationEntryWithTTL ("BAR" , 130 , 140 , "B" )
513
513
@@ -561,7 +561,7 @@ func TestLRUCacheGetStaleEntries(t *testing.T) {
561
561
}
562
562
563
563
func TestLRUCacheSubscriberNotNotifiedOnDifferentSVIDChanges (t * testing.T ) {
564
- cache := newTestLRUCache ()
564
+ cache := newTestLRUCache (t )
565
565
566
566
foo := makeRegistrationEntry ("FOO" , "A" )
567
567
bar := makeRegistrationEntry ("BAR" , "B" )
@@ -586,7 +586,7 @@ func TestLRUCacheSubscriberNotNotifiedOnDifferentSVIDChanges(t *testing.T) {
586
586
}
587
587
588
588
func TestLRUCacheSubscriberNotNotifiedOnOverlappingSVIDChanges (t * testing.T ) {
589
- cache := newTestLRUCache ()
589
+ cache := newTestLRUCache (t )
590
590
591
591
foo := makeRegistrationEntry ("FOO" , "A" , "C" )
592
592
bar := makeRegistrationEntry ("FOO" , "A" , "B" )
@@ -611,7 +611,7 @@ func TestLRUCacheSubscriberNotNotifiedOnOverlappingSVIDChanges(t *testing.T) {
611
611
}
612
612
613
613
func TestLRUCacheSVIDCacheExpiry (t * testing.T ) {
614
- clk := clock .NewMock ()
614
+ clk := clock .NewMock (t )
615
615
cache := newTestLRUCacheWithConfig (10 , clk )
616
616
617
617
clk .Add (1 * time .Second )
@@ -696,7 +696,7 @@ func TestLRUCacheSVIDCacheExpiry(t *testing.T) {
696
696
}
697
697
698
698
func TestLRUCacheMaxSVIDCacheSize (t * testing.T ) {
699
- clk := clock .NewMock ()
699
+ clk := clock .NewMock (t )
700
700
cache := newTestLRUCacheWithConfig (10 , clk )
701
701
702
702
// create entries more than maxSvidCacheSize
@@ -730,7 +730,7 @@ func TestLRUCacheMaxSVIDCacheSize(t *testing.T) {
730
730
}
731
731
732
732
func TestSyncSVIDsWithSubscribers (t * testing.T ) {
733
- clk := clock .NewMock ()
733
+ clk := clock .NewMock (t )
734
734
cache := newTestLRUCacheWithConfig (5 , clk )
735
735
736
736
updateEntries := createUpdateEntries (5 , makeBundles (bundleV1 ))
@@ -760,7 +760,7 @@ func TestSyncSVIDsWithSubscribers(t *testing.T) {
760
760
}
761
761
762
762
func TestNotify (t * testing.T ) {
763
- cache := newTestLRUCache ()
763
+ cache := newTestLRUCache (t )
764
764
765
765
foo := makeRegistrationEntry ("FOO" , "A" )
766
766
cache .UpdateEntries (& UpdateEntries {
@@ -776,7 +776,7 @@ func TestNotify(t *testing.T) {
776
776
}
777
777
778
778
func TestSubscribeToLRUCacheChanges (t * testing.T ) {
779
- clk := clock .NewMock ()
779
+ clk := clock .NewMock (t )
780
780
cache := newTestLRUCacheWithConfig (1 , clk )
781
781
782
782
foo := makeRegistrationEntry ("FOO" , "A" )
@@ -836,27 +836,36 @@ func TestSubscribeToLRUCacheChanges(t *testing.T) {
836
836
})
837
837
assert .Equal (t , 2 , cache .CountSVIDs ())
838
838
839
- clk .Add (SVIDSyncInterval * 2 )
839
+ clk .WaitForAfter (time .Second , "waiting for after to get called" )
840
+ clk .Add (SVIDSyncInterval * 4 )
840
841
841
- sub1Err := <- sub1ErrCh
842
- assert .NoError (t , sub1Err , "subscriber 1 error" )
842
+ select {
843
+ case sub1Err := <- sub1ErrCh :
844
+ assert .NoError (t , sub1Err , "subscriber 1 error" )
845
+ case <- time .After (10 * time .Second ):
846
+ require .FailNow (t , "timed out waiting for SVID" )
847
+ }
843
848
844
- sub2Err := <- sub2ErrCh
845
- assert .NoError (t , sub2Err , "subscriber 2 error" )
849
+ select {
850
+ case sub2Err := <- sub2ErrCh :
851
+ assert .NoError (t , sub2Err , "subscriber 2 error" )
852
+ case <- time .After (10 * time .Second ):
853
+ require .FailNow (t , "timed out waiting for SVID" )
854
+ }
846
855
}
847
856
848
857
func TestNewLRUCache (t * testing.T ) {
849
858
// negative value
850
- cache := newTestLRUCacheWithConfig (- 5 , clock .NewMock ())
859
+ cache := newTestLRUCacheWithConfig (- 5 , clock .NewMock (t ))
851
860
require .Equal (t , DefaultSVIDCacheMaxSize , cache .svidCacheMaxSize )
852
861
853
862
// zero value
854
- cache = newTestLRUCacheWithConfig (0 , clock .NewMock ())
863
+ cache = newTestLRUCacheWithConfig (0 , clock .NewMock (t ))
855
864
require .Equal (t , DefaultSVIDCacheMaxSize , cache .svidCacheMaxSize )
856
865
}
857
866
858
867
func BenchmarkLRUCacheGlobalNotification (b * testing.B ) {
859
- cache := newTestLRUCache ()
868
+ cache := newTestLRUCache (b )
860
869
861
870
const numEntries = 1000
862
871
const numWorkloads = 1000
@@ -900,10 +909,10 @@ func BenchmarkLRUCacheGlobalNotification(b *testing.B) {
900
909
}
901
910
}
902
911
903
- func newTestLRUCache () * LRUCache {
912
+ func newTestLRUCache (t testing. TB ) * LRUCache {
904
913
log , _ := test .NewNullLogger ()
905
914
return NewLRUCache (log , spiffeid .RequireTrustDomainFromString ("domain.test" ), bundleV1 ,
906
- telemetry.Blackhole {}, 0 , clock .NewMock ())
915
+ telemetry.Blackhole {}, 0 , clock .NewMock (t ))
907
916
}
908
917
909
918
func newTestLRUCacheWithConfig (svidCacheMaxSize int , clk clock.Clock ) * LRUCache {
0 commit comments