@@ -17,7 +17,9 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
+ "context"
20
21
"testing"
22
+ "time"
21
23
22
24
. "github.com/onsi/gomega"
23
25
corev1 "k8s.io/api/core/v1"
@@ -31,6 +33,7 @@ import (
31
33
"sigs.k8s.io/controller-runtime/pkg/reconcile"
32
34
33
35
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
36
+ "sigs.k8s.io/cluster-api/controllers/remote"
34
37
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
35
38
"sigs.k8s.io/cluster-api/internal/test/builder"
36
39
"sigs.k8s.io/cluster-api/util"
@@ -277,8 +280,8 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
277
280
},
278
281
}
279
282
280
- time := metav1 .Now ()
281
-
283
+ timeNow := metav1 .Now ()
284
+ timePlus1H := metav1. Time { Time : time . Now (). Add ( time . Hour * 1 )}
282
285
testCluster := clusterv1.Cluster {
283
286
TypeMeta : metav1.TypeMeta {Kind : "Cluster" , APIVersion : clusterv1 .GroupVersion .String ()},
284
287
ObjectMeta : metav1.ObjectMeta {Namespace : metav1 .NamespaceDefault , Name : "test-cluster" },
@@ -302,6 +305,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
302
305
testCases := []struct {
303
306
machinePool expv1.MachinePool
304
307
expected expected
308
+ withTracker bool
305
309
}{
306
310
{
307
311
machinePool : expv1.MachinePool {
@@ -316,7 +320,6 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
316
320
Replicas : ptr.To [int32 ](1 ),
317
321
Template : clusterv1.MachineTemplateSpec {
318
322
Spec : clusterv1.MachineSpec {
319
-
320
323
InfrastructureRef : corev1.ObjectReference {
321
324
APIVersion : builder .InfrastructureGroupVersion .String (),
322
325
Kind : builder .TestInfrastructureMachineTemplateKind ,
@@ -339,6 +342,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
339
342
result : reconcile.Result {},
340
343
err : false ,
341
344
},
345
+ withTracker : true ,
342
346
},
343
347
{
344
348
machinePool : expv1.MachinePool {
@@ -375,6 +379,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
375
379
result : reconcile.Result {},
376
380
err : false ,
377
381
},
382
+ withTracker : true ,
378
383
},
379
384
{
380
385
machinePool : expv1.MachinePool {
@@ -385,7 +390,8 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
385
390
clusterv1 .MachineControlPlaneLabel : "" ,
386
391
},
387
392
Finalizers : []string {expv1 .MachinePoolFinalizer },
388
- DeletionTimestamp : & time ,
393
+ CreationTimestamp : timeNow ,
394
+ DeletionTimestamp : & timeNow ,
389
395
},
390
396
Spec : expv1.MachinePoolSpec {
391
397
ClusterName : "test-cluster" ,
@@ -397,7 +403,61 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
397
403
Kind : builder .TestInfrastructureMachineTemplateKind ,
398
404
Name : "infra-config1" ,
399
405
},
400
- Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
406
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : ptr .To ("data" )},
407
+ NodeDrainTimeout : & metav1.Duration {Duration : 10 * time .Second },
408
+ },
409
+ },
410
+ ProviderIDList : []string {"aws:///us-test-2a/i-013ab00756982217f" },
411
+ },
412
+ Status : expv1.MachinePoolStatus {
413
+ NodeRefs : []corev1.ObjectReference {
414
+ {
415
+ APIVersion : "v1" ,
416
+ Kind : "Node" ,
417
+ Name : "test-node" ,
418
+ },
419
+ },
420
+ },
421
+ },
422
+ expected : expected {
423
+ result : reconcile.Result {},
424
+ err : false ,
425
+ },
426
+ withTracker : true ,
427
+ },
428
+ {
429
+ machinePool : expv1.MachinePool {
430
+ ObjectMeta : metav1.ObjectMeta {
431
+ Name : "deleted-timeoutNode" ,
432
+ Namespace : metav1 .NamespaceDefault ,
433
+ Labels : map [string ]string {
434
+ clusterv1 .MachineControlPlaneLabel : "" ,
435
+ },
436
+ Finalizers : []string {expv1 .MachinePoolFinalizer },
437
+ CreationTimestamp : timeNow ,
438
+ DeletionTimestamp : & timePlus1H ,
439
+ },
440
+ Spec : expv1.MachinePoolSpec {
441
+ ClusterName : "test-cluster" ,
442
+ Replicas : ptr.To [int32 ](1 ),
443
+ Template : clusterv1.MachineTemplateSpec {
444
+ Spec : clusterv1.MachineSpec {
445
+ InfrastructureRef : corev1.ObjectReference {
446
+ APIVersion : builder .InfrastructureGroupVersion .String (),
447
+ Kind : builder .TestInfrastructureMachineTemplateKind ,
448
+ Name : "infra-config1" ,
449
+ },
450
+ NodeDeletionTimeout : & metav1.Duration {Duration : - 3 * time .Hour },
451
+ },
452
+ },
453
+ ProviderIDList : []string {"aws:///us-test-2a/i-013ab00756982217f" },
454
+ },
455
+ Status : expv1.MachinePoolStatus {
456
+ NodeRefs : []corev1.ObjectReference {
457
+ {
458
+ APIVersion : "v1" ,
459
+ Kind : "Node" ,
460
+ Name : "test-node" ,
401
461
},
402
462
},
403
463
},
@@ -407,6 +467,43 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
407
467
err : false ,
408
468
},
409
469
},
470
+ {
471
+ machinePool : expv1.MachinePool {
472
+ ObjectMeta : metav1.ObjectMeta {
473
+ Name : "try-delete-forever" ,
474
+ Namespace : metav1 .NamespaceDefault ,
475
+ Labels : map [string ]string {
476
+ clusterv1 .MachineControlPlaneLabel : "" ,
477
+ },
478
+ Finalizers : []string {expv1 .MachinePoolFinalizer },
479
+ CreationTimestamp : timeNow ,
480
+ DeletionTimestamp : & timePlus1H ,
481
+ },
482
+ Spec : expv1.MachinePoolSpec {
483
+ ClusterName : "test-cluster" ,
484
+ Replicas : ptr.To [int32 ](1 ),
485
+ Template : clusterv1.MachineTemplateSpec {
486
+ Spec : clusterv1.MachineSpec {
487
+ NodeDeletionTimeout : & metav1.Duration {Duration : 0 * time .Second },
488
+ },
489
+ },
490
+ ProviderIDList : []string {"aws:///us-test-2a/i-013ab00756982217f" },
491
+ },
492
+ Status : expv1.MachinePoolStatus {
493
+ NodeRefs : []corev1.ObjectReference {
494
+ {
495
+ APIVersion : "v1" ,
496
+ Kind : "Node" ,
497
+ Name : "test-node" ,
498
+ },
499
+ },
500
+ },
501
+ },
502
+ expected : expected {
503
+ result : reconcile.Result {},
504
+ err : true ,
505
+ },
506
+ },
410
507
}
411
508
412
509
for i := range testCases {
@@ -428,6 +525,11 @@ func TestReconcileMachinePoolRequest(t *testing.T) {
428
525
APIReader : clientFake ,
429
526
}
430
527
528
+ // Set the tracker.
529
+ if tc .withTracker {
530
+ r .Tracker = remote .NewTestClusterCacheTracker (ctrl .LoggerFrom (context .TODO ()), clientFake , clientFake , clientFake .Scheme (), client.ObjectKey {Name : testCluster .Name , Namespace : testCluster .Namespace })
531
+ }
532
+
431
533
result , err := r .Reconcile (ctx , reconcile.Request {NamespacedName : util .ObjectKey (& tc .machinePool )})
432
534
if tc .expected .err {
433
535
g .Expect (err ).To (HaveOccurred ())
@@ -591,8 +693,10 @@ func TestRemoveMachinePoolFinalizerAfterDeleteReconcile(t *testing.T) {
591
693
},
592
694
}
593
695
key := client.ObjectKey {Namespace : m .Namespace , Name : m .Name }
696
+ clientFake := fake .NewClientBuilder ().WithObjects (testCluster , m ).WithStatusSubresource (& expv1.MachinePool {}).Build ()
594
697
mr := & MachinePoolReconciler {
595
- Client : fake .NewClientBuilder ().WithObjects (testCluster , m ).WithStatusSubresource (& expv1.MachinePool {}).Build (),
698
+ Client : clientFake ,
699
+ Tracker : remote .NewTestClusterCacheTracker (ctrl .LoggerFrom (context .TODO ()), clientFake , clientFake , clientFake .Scheme (), client.ObjectKey {Name : testCluster .Name , Namespace : testCluster .Namespace }),
596
700
}
597
701
_ , err := mr .Reconcile (ctx , reconcile.Request {NamespacedName : key })
598
702
g .Expect (err ).ToNot (HaveOccurred ())
@@ -864,6 +968,7 @@ func TestMachinePoolConditions(t *testing.T) {
864
968
r := & MachinePoolReconciler {
865
969
Client : clientFake ,
866
970
APIReader : clientFake ,
971
+ Tracker : remote .NewTestClusterCacheTracker (ctrl .LoggerFrom (context .TODO ()), clientFake , clientFake , clientFake .Scheme (), client.ObjectKey {Name : testCluster .Name , Namespace : testCluster .Namespace }),
867
972
}
868
973
869
974
_ , err := r .Reconcile (ctx , reconcile.Request {NamespacedName : util .ObjectKey (machinePool )})
0 commit comments