@@ -40,6 +40,7 @@ import (
40
40
"k8s.io/client-go/tools/record"
41
41
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
42
42
"sigs.k8s.io/cluster-api/util/conditions"
43
+ ctrl "sigs.k8s.io/controller-runtime"
43
44
"sigs.k8s.io/controller-runtime/pkg/client"
44
45
"sigs.k8s.io/controller-runtime/pkg/client/fake"
45
46
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -196,19 +197,22 @@ func TestNormalReconciliationFunction(t *testing.T) {
196
197
teardown := func (t * testing.T , g * WithT ) {
197
198
mockCtrl .Finish ()
198
199
}
199
- tests := [] struct {
200
+ type test struct {
200
201
name string
201
202
errorExpected bool
202
203
expectedEvent string
203
204
eventNotExpected string
204
205
conditionAssertion []conditionAssertion
205
- testSpecificSetup func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient )
206
- }{
206
+ deleteMachines []clusterv1.Machine
207
+ testSpecificSetup func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient )
208
+ validate func (g * WithT , t * test , result ctrl.Result )
209
+ }
210
+ tests := []test {
207
211
{
208
212
name : "instance in provisioning state" ,
209
213
errorExpected : false ,
210
214
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionFalse , clusterv1 .ConditionSeverityInfo , infrastructurev1beta2 .InstanceNotReadyReason }},
211
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
215
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
212
216
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
213
217
InstanceId : common .String ("test" ),
214
218
})).
@@ -224,7 +228,59 @@ func TestNormalReconciliationFunction(t *testing.T) {
224
228
name : "instance in running state" ,
225
229
errorExpected : false ,
226
230
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionTrue , "" , "" }},
227
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
231
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
232
+ machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
233
+ {
234
+ Type : clusterv1 .MachineInternalIP ,
235
+ Address : "1.1.1.1" ,
236
+ },
237
+ }
238
+ computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
239
+ InstanceId : common .String ("test" ),
240
+ })).
241
+ Return (core.GetInstanceResponse {
242
+ Instance : core.Instance {
243
+ Id : common .String ("test" ),
244
+ LifecycleState : core .InstanceLifecycleStateRunning ,
245
+ },
246
+ }, nil )
247
+ },
248
+ },
249
+ {
250
+ name : "instance in running state, reconcile every 5 minutes" ,
251
+ errorExpected : false ,
252
+ conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionTrue , "" , "" }},
253
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
254
+ if machineScope .Machine .ObjectMeta .Annotations == nil {
255
+ machineScope .Machine .ObjectMeta .Annotations = make (map [string ]string )
256
+ }
257
+ machineScope .Machine .ObjectMeta .Annotations [infrastructurev1beta2 .DeleteMachineOnInstanceTermination ] = ""
258
+ machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
259
+ {
260
+ Type : clusterv1 .MachineInternalIP ,
261
+ Address : "1.1.1.1" ,
262
+ },
263
+ }
264
+ computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
265
+ InstanceId : common .String ("test" ),
266
+ })).
267
+ Return (core.GetInstanceResponse {
268
+ Instance : core.Instance {
269
+ Id : common .String ("test" ),
270
+ LifecycleState : core .InstanceLifecycleStateRunning ,
271
+ },
272
+ }, nil )
273
+ },
274
+ },
275
+ {
276
+ name : "instance in running state, reconcile every 5 minutes" ,
277
+ errorExpected : false ,
278
+ conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionTrue , "" , "" }},
279
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
280
+ if machineScope .Machine .ObjectMeta .Annotations == nil {
281
+ machineScope .Machine .ObjectMeta .Annotations = make (map [string ]string )
282
+ }
283
+ machineScope .Machine .ObjectMeta .Annotations [infrastructurev1beta2 .DeleteMachineOnInstanceTermination ] = ""
228
284
machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
229
285
{
230
286
Type : clusterv1 .MachineInternalIP ,
@@ -241,13 +297,16 @@ func TestNormalReconciliationFunction(t *testing.T) {
241
297
},
242
298
}, nil )
243
299
},
300
+ validate : func (g * WithT , t * test , result ctrl.Result ) {
301
+ g .Expect (result .RequeueAfter ).To (Equal (300 * time .Second ))
302
+ },
244
303
},
245
304
{
246
305
name : "instance in terminated state" ,
247
306
errorExpected : true ,
248
307
expectedEvent : "invalid lifecycle state TERMINATED" ,
249
308
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionFalse , clusterv1 .ConditionSeverityError , infrastructurev1beta2 .InstanceProvisionFailedReason }},
250
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
309
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
251
310
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
252
311
InstanceId : common .String ("test" ),
253
312
})).
@@ -258,13 +317,16 @@ func TestNormalReconciliationFunction(t *testing.T) {
258
317
},
259
318
}, nil )
260
319
},
320
+ validate : func (g * WithT , t * test , result ctrl.Result ) {
321
+ g .Expect (result .RequeueAfter ).To (Equal (0 * time .Second ))
322
+ },
261
323
},
262
324
{
263
325
name : "control plane backend vnic attachments call error" ,
264
326
errorExpected : true ,
265
327
expectedEvent : "server error" ,
266
328
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionFalse , clusterv1 .ConditionSeverityError , infrastructurev1beta2 .InstanceIPAddressNotFound }},
267
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
329
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
268
330
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
269
331
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
270
332
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -287,7 +349,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
287
349
{
288
350
name : "control plane backend backend exists" ,
289
351
errorExpected : false ,
290
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
352
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
291
353
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
292
354
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
293
355
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -344,7 +406,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
344
406
{
345
407
name : "backend creation" ,
346
408
errorExpected : false ,
347
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
409
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
348
410
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
349
411
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
350
412
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -419,7 +481,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
419
481
{
420
482
name : "ip address exists" ,
421
483
errorExpected : false ,
422
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
484
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
423
485
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
424
486
machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
425
487
{
@@ -478,7 +540,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
478
540
{
479
541
name : "backend creation fails" ,
480
542
errorExpected : true ,
481
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
543
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
482
544
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
483
545
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
484
546
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -557,9 +619,9 @@ func TestNormalReconciliationFunction(t *testing.T) {
557
619
g := NewWithT (t )
558
620
defer teardown (t , g )
559
621
setup (t , g )
560
- tc .testSpecificSetup (ms , computeClient , vcnClient , nlbClient )
622
+ tc .testSpecificSetup (& tc , ms , computeClient , vcnClient , nlbClient )
561
623
ctx := context .Background ()
562
- _ , err := r .reconcileNormal (ctx , log .FromContext (ctx ), ms )
624
+ result , err := r .reconcileNormal (ctx , log .FromContext (ctx ), ms )
563
625
if len (tc .conditionAssertion ) > 0 {
564
626
expectConditions (g , ociMachine , tc .conditionAssertion )
565
627
}
@@ -571,6 +633,9 @@ func TestNormalReconciliationFunction(t *testing.T) {
571
633
if tc .expectedEvent != "" {
572
634
g .Eventually (recorder .Events ).Should (Receive (ContainSubstring (tc .expectedEvent )))
573
635
}
636
+ if tc .validate != nil {
637
+ tc .validate (g , & tc , result )
638
+ }
574
639
})
575
640
}
576
641
}
0 commit comments