@@ -2,6 +2,7 @@ package ipampool
22
33import (
44 "context"
5+ "errors"
56 "testing"
67 "time"
78
@@ -21,6 +22,12 @@ func (f *fakeNodeNetworkConfigUpdater) UpdateSpec(ctx context.Context, spec *v1a
2122 return f .nnc , nil
2223}
2324
25+ type fakeNodeNetworkConfigUpdaterFunc func (ctx context.Context , spec * v1alpha.NodeNetworkConfigSpec ) (* v1alpha.NodeNetworkConfig , error )
26+
27+ func (f fakeNodeNetworkConfigUpdaterFunc ) UpdateSpec (ctx context.Context , spec * v1alpha.NodeNetworkConfigSpec ) (* v1alpha.NodeNetworkConfig , error ) {
28+ return f (ctx , spec )
29+ }
30+
2431type directUpdatePoolMonitor struct {
2532 m * Monitor
2633 cns.IPAMPoolMonitor
@@ -45,7 +52,7 @@ type testState struct {
4552 totalIPs int64
4653}
4754
48- func initFakes (state testState ) (* fakes.HTTPServiceFake , * fakes.RequestControllerFake , * Monitor ) {
55+ func initFakes (state testState , nnccli nodeNetworkConfigSpecUpdater ) (* fakes.HTTPServiceFake , * fakes.RequestControllerFake , * Monitor ) {
4956 logger .InitLogger ("testlogs" , 0 , 0 , "./" )
5057
5158 scalarUnits := v1alpha.Scaler {
@@ -61,8 +68,11 @@ func initFakes(state testState) (*fakes.HTTPServiceFake, *fakes.RequestControlle
6168 }
6269 fakecns := fakes .NewHTTPServiceFake ()
6370 fakerc := fakes .NewRequestControllerFake (fakecns , scalarUnits , subnetaddresspace , state .totalIPs )
71+ if nnccli == nil {
72+ nnccli = & fakeNodeNetworkConfigUpdater {fakerc .NNC }
73+ }
6474
65- poolmonitor := NewMonitor (fakecns , & fakeNodeNetworkConfigUpdater { fakerc . NNC } , nil , & Options {RefreshDelay : 100 * time .Second })
75+ poolmonitor := NewMonitor (fakecns , nnccli , nil , & Options {RefreshDelay : 100 * time .Second })
6676 poolmonitor .metastate = metaState {
6777 batch : state .batch ,
6878 max : state .max ,
@@ -127,7 +137,7 @@ func TestPoolSizeIncrease(t *testing.T) {
127137 for _ , tt := range tests {
128138 tt := tt
129139 t .Run (tt .name , func (t * testing.T ) {
130- fakecns , fakerc , poolmonitor := initFakes (tt .in )
140+ fakecns , fakerc , poolmonitor := initFakes (tt .in , nil )
131141 assert .NoError (t , fakerc .Reconcile (true ))
132142
133143 // When poolmonitor reconcile is called, trigger increase and cache goal state
@@ -162,7 +172,7 @@ func TestPoolIncreaseDoesntChangeWhenIncreaseIsAlreadyInProgress(t *testing.T) {
162172 max : 30 ,
163173 }
164174
165- fakecns , fakerc , poolmonitor := initFakes (initState )
175+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
166176 assert .NoError (t , fakerc .Reconcile (true ))
167177
168178 // When poolmonitor reconcile is called, trigger increase and cache goal state
@@ -201,7 +211,7 @@ func TestPoolSizeIncreaseIdempotency(t *testing.T) {
201211 max : 30 ,
202212 }
203213
204- _ , fakerc , poolmonitor := initFakes (initState )
214+ _ , fakerc , poolmonitor := initFakes (initState , nil )
205215 assert .NoError (t , fakerc .Reconcile (true ))
206216
207217 // When poolmonitor reconcile is called, trigger increase and cache goal state
@@ -227,7 +237,7 @@ func TestPoolIncreasePastNodeLimit(t *testing.T) {
227237 max : 30 ,
228238 }
229239
230- _ , fakerc , poolmonitor := initFakes (initState )
240+ _ , fakerc , poolmonitor := initFakes (initState , nil )
231241 assert .NoError (t , fakerc .Reconcile (true ))
232242
233243 // When poolmonitor reconcile is called, trigger increase and cache goal state
@@ -247,7 +257,7 @@ func TestPoolIncreaseBatchSizeGreaterThanMaxPodIPCount(t *testing.T) {
247257 max : 30 ,
248258 }
249259
250- _ , fakerc , poolmonitor := initFakes (initState )
260+ _ , fakerc , poolmonitor := initFakes (initState , nil )
251261 assert .NoError (t , fakerc .Reconcile (true ))
252262
253263 // When poolmonitor reconcile is called, trigger increase and cache goal state
@@ -267,7 +277,7 @@ func TestIncreaseWithPendingRelease(t *testing.T) {
267277 max : 250 ,
268278 pendingRelease : 16 ,
269279 }
270- _ , rc , mon := initFakes (initState )
280+ _ , rc , mon := initFakes (initState , nil )
271281 assert .NoError (t , rc .Reconcile (true ))
272282 assert .NoError (t , mon .reconcile (context .Background ()))
273283 assert .Equal (t , int64 (32 ), mon .spec .RequestedIPCount )
@@ -333,7 +343,7 @@ func TestPoolDecrease(t *testing.T) {
333343 for _ , tt := range tests {
334344 tt := tt
335345 t .Run (tt .name , func (t * testing.T ) {
336- fakecns , fakerc , poolmonitor := initFakes (tt .in )
346+ fakecns , fakerc , poolmonitor := initFakes (tt .in , nil )
337347 assert .NoError (t , fakerc .Reconcile (true ))
338348
339349 // Decrease the number of allocated IPs down to target. This may trigger a scale down.
@@ -374,7 +384,7 @@ func TestPoolSizeDecreaseWhenDecreaseHasAlreadyBeenRequested(t *testing.T) {
374384 max : 30 ,
375385 }
376386
377- fakecns , fakerc , poolmonitor := initFakes (initState )
387+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
378388 assert .NoError (t , fakerc .Reconcile (true ))
379389
380390 // Pool monitor does nothing, as the current number of IPs falls in the threshold
@@ -413,7 +423,7 @@ func TestDecreaseAndIncreaseToSameCount(t *testing.T) {
413423 max : 30 ,
414424 }
415425
416- fakecns , fakerc , poolmonitor := initFakes (initState )
426+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
417427 assert .NoError (t , fakerc .Reconcile (true ))
418428 assert .NoError (t , poolmonitor .reconcile (context .Background ()))
419429 assert .EqualValues (t , 20 , poolmonitor .spec .RequestedIPCount )
@@ -458,7 +468,7 @@ func TestPoolSizeDecreaseToReallyLow(t *testing.T) {
458468 max : 30 ,
459469 }
460470
461- fakecns , fakerc , poolmonitor := initFakes (initState )
471+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
462472 assert .NoError (t , fakerc .Reconcile (true ))
463473
464474 // Pool monitor does nothing, as the current number of IPs falls in the threshold
@@ -499,7 +509,7 @@ func TestDecreaseAfterNodeLimitReached(t *testing.T) {
499509 releaseThresholdPercent : 150 ,
500510 max : 30 ,
501511 }
502- fakecns , fakerc , poolmonitor := initFakes (initState )
512+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
503513 assert .NoError (t , fakerc .Reconcile (true ))
504514
505515 assert .NoError (t , poolmonitor .reconcile (context .Background ()))
@@ -524,7 +534,7 @@ func TestDecreaseWithPendingRelease(t *testing.T) {
524534 totalIPs : 64 ,
525535 max : 250 ,
526536 }
527- fakecns , fakerc , poolmonitor := initFakes (initState )
537+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
528538 fakerc .NNC .Spec .RequestedIPCount = 48
529539 assert .NoError (t , fakerc .Reconcile (true ))
530540
@@ -547,6 +557,35 @@ func TestDecreaseWithPendingRelease(t *testing.T) {
547557 assert .Len (t , poolmonitor .spec .IPsNotInUse , int (initState .batch )+ int (initState .pendingRelease ))
548558}
549559
560+ func TestDecreaseWithAPIServerFailure (t * testing.T ) {
561+ initState := testState {
562+ batch : 16 ,
563+ assigned : 46 ,
564+ allocated : 64 ,
565+ pendingRelease : 0 ,
566+ requestThresholdPercent : 50 ,
567+ releaseThresholdPercent : 150 ,
568+ totalIPs : 64 ,
569+ max : 250 ,
570+ }
571+ var errNNCCLi fakeNodeNetworkConfigUpdaterFunc = func (ctx context.Context , spec * v1alpha.NodeNetworkConfigSpec ) (* v1alpha.NodeNetworkConfig , error ) {
572+ return nil , errors .New ("fake APIServer failure" ) //nolint:goerr113 // this is a fake error
573+ }
574+
575+ fakecns , fakerc , poolmonitor := initFakes (initState , errNNCCLi )
576+ fakerc .NNC .Spec .RequestedIPCount = initState .totalIPs
577+ assert .NoError (t , fakerc .Reconcile (true ))
578+
579+ assert .NoError (t , poolmonitor .reconcile (context .Background ()))
580+
581+ // release some IPs
582+ assert .NoError (t , fakecns .SetNumberOfAssignedIPs (40 ))
583+ // check that the pool monitor panics if it is not able to publish the updated NNC
584+ assert .Panics (t , func () {
585+ _ = poolmonitor .reconcile (context .Background ())
586+ })
587+ }
588+
550589func TestPoolDecreaseBatchSizeGreaterThanMaxPodIPCount (t * testing.T ) {
551590 initState := testState {
552591 batch : 31 ,
@@ -557,7 +596,7 @@ func TestPoolDecreaseBatchSizeGreaterThanMaxPodIPCount(t *testing.T) {
557596 max : 30 ,
558597 }
559598
560- fakecns , fakerc , poolmonitor := initFakes (initState )
599+ fakecns , fakerc , poolmonitor := initFakes (initState , nil )
561600 assert .NoError (t , fakerc .Reconcile (true ))
562601
563602 // When poolmonitor reconcile is called, trigger increase and cache goal state
0 commit comments