@@ -22,26 +22,26 @@ import (
22
22
"testing"
23
23
"time"
24
24
25
- "github.com/oracle/cluster-api-provider-oci/cloud/ociutil"
26
- "github.com/oracle/cluster-api-provider-oci/cloud/services/networkloadbalancer/mock_nlb"
27
- "github.com/oracle/cluster-api-provider-oci/cloud/services/vcn/mock_vcn"
28
- "github.com/oracle/oci-go-sdk/v65/networkloadbalancer"
29
- "sigs.k8s.io/controller-runtime/pkg/client"
30
-
31
25
"github.com/golang/mock/gomock"
32
26
. "github.com/onsi/gomega"
33
27
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
28
+ "github.com/oracle/cluster-api-provider-oci/cloud/ociutil"
34
29
"github.com/oracle/cluster-api-provider-oci/cloud/scope"
35
30
"github.com/oracle/cluster-api-provider-oci/cloud/services/compute/mock_compute"
31
+ "github.com/oracle/cluster-api-provider-oci/cloud/services/networkloadbalancer/mock_nlb"
32
+ "github.com/oracle/cluster-api-provider-oci/cloud/services/vcn/mock_vcn"
36
33
"github.com/oracle/oci-go-sdk/v65/common"
37
34
"github.com/oracle/oci-go-sdk/v65/core"
35
+ "github.com/oracle/oci-go-sdk/v65/networkloadbalancer"
38
36
corev1 "k8s.io/api/core/v1"
39
37
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
40
38
"k8s.io/apimachinery/pkg/runtime"
41
39
"k8s.io/apimachinery/pkg/types"
42
40
"k8s.io/client-go/tools/record"
43
41
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
44
42
"sigs.k8s.io/cluster-api/util/conditions"
43
+ ctrl "sigs.k8s.io/controller-runtime"
44
+ "sigs.k8s.io/controller-runtime/pkg/client"
45
45
"sigs.k8s.io/controller-runtime/pkg/client/fake"
46
46
"sigs.k8s.io/controller-runtime/pkg/log"
47
47
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -194,19 +194,22 @@ func TestNormalReconciliationFunction(t *testing.T) {
194
194
teardown := func (t * testing.T , g * WithT ) {
195
195
mockCtrl .Finish ()
196
196
}
197
- tests := [] struct {
197
+ type test struct {
198
198
name string
199
199
errorExpected bool
200
200
expectedEvent string
201
201
eventNotExpected string
202
202
conditionAssertion []conditionAssertion
203
- testSpecificSetup func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient )
204
- }{
203
+ deleteMachines []clusterv1.Machine
204
+ testSpecificSetup func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient )
205
+ validate func (g * WithT , t * test , result ctrl.Result )
206
+ }
207
+ tests := []test {
205
208
{
206
209
name : "instance in provisioning state" ,
207
210
errorExpected : false ,
208
211
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionFalse , clusterv1 .ConditionSeverityInfo , infrastructurev1beta2 .InstanceNotReadyReason }},
209
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
212
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
210
213
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
211
214
InstanceId : common .String ("test" ),
212
215
})).
@@ -222,7 +225,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
222
225
name : "instance in running state" ,
223
226
errorExpected : false ,
224
227
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionTrue , "" , "" }},
225
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
228
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
226
229
machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
227
230
{
228
231
Type : clusterv1 .MachineInternalIP ,
@@ -240,12 +243,67 @@ func TestNormalReconciliationFunction(t *testing.T) {
240
243
}, nil )
241
244
},
242
245
},
246
+ {
247
+ name : "instance in running state, reconcile every 5 minutes" ,
248
+ errorExpected : false ,
249
+ conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionTrue , "" , "" }},
250
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
251
+ if machineScope .Machine .ObjectMeta .Annotations == nil {
252
+ machineScope .Machine .ObjectMeta .Annotations = make (map [string ]string )
253
+ }
254
+ machineScope .Machine .ObjectMeta .Annotations [infrastructurev1beta2 .DeleteMachineOnInstanceTermination ] = ""
255
+ machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
256
+ {
257
+ Type : clusterv1 .MachineInternalIP ,
258
+ Address : "1.1.1.1" ,
259
+ },
260
+ }
261
+ computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
262
+ InstanceId : common .String ("test" ),
263
+ })).
264
+ Return (core.GetInstanceResponse {
265
+ Instance : core.Instance {
266
+ Id : common .String ("test" ),
267
+ LifecycleState : core .InstanceLifecycleStateRunning ,
268
+ },
269
+ }, nil )
270
+ },
271
+ },
272
+ {
273
+ name : "instance in running state, reconcile every 5 minutes" ,
274
+ errorExpected : false ,
275
+ conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionTrue , "" , "" }},
276
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
277
+ if machineScope .Machine .ObjectMeta .Annotations == nil {
278
+ machineScope .Machine .ObjectMeta .Annotations = make (map [string ]string )
279
+ }
280
+ machineScope .Machine .ObjectMeta .Annotations [infrastructurev1beta2 .DeleteMachineOnInstanceTermination ] = ""
281
+ machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
282
+ {
283
+ Type : clusterv1 .MachineInternalIP ,
284
+ Address : "1.1.1.1" ,
285
+ },
286
+ }
287
+ computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
288
+ InstanceId : common .String ("test" ),
289
+ })).
290
+ Return (core.GetInstanceResponse {
291
+ Instance : core.Instance {
292
+ Id : common .String ("test" ),
293
+ LifecycleState : core .InstanceLifecycleStateRunning ,
294
+ },
295
+ }, nil )
296
+ },
297
+ validate : func (g * WithT , t * test , result ctrl.Result ) {
298
+ g .Expect (result .RequeueAfter ).To (Equal (300 * time .Second ))
299
+ },
300
+ },
243
301
{
244
302
name : "instance in terminated state" ,
245
303
errorExpected : true ,
246
304
expectedEvent : "invalid lifecycle state TERMINATED" ,
247
305
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionFalse , clusterv1 .ConditionSeverityError , infrastructurev1beta2 .InstanceProvisionFailedReason }},
248
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
306
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
249
307
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
250
308
InstanceId : common .String ("test" ),
251
309
})).
@@ -256,13 +314,16 @@ func TestNormalReconciliationFunction(t *testing.T) {
256
314
},
257
315
}, nil )
258
316
},
317
+ validate : func (g * WithT , t * test , result ctrl.Result ) {
318
+ g .Expect (result .RequeueAfter ).To (Equal (0 * time .Second ))
319
+ },
259
320
},
260
321
{
261
322
name : "control plane backend vnic attachments call error" ,
262
323
errorExpected : true ,
263
324
expectedEvent : "server error" ,
264
325
conditionAssertion : []conditionAssertion {{infrastructurev1beta2 .InstanceReadyCondition , corev1 .ConditionFalse , clusterv1 .ConditionSeverityError , infrastructurev1beta2 .InstanceIPAddressNotFound }},
265
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
326
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbclient * mock_nlb.MockNetworkLoadBalancerClient ) {
266
327
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
267
328
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
268
329
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -285,7 +346,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
285
346
{
286
347
name : "control plane backend backend exists" ,
287
348
errorExpected : false ,
288
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
349
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
289
350
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
290
351
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
291
352
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -342,7 +403,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
342
403
{
343
404
name : "backend creation" ,
344
405
errorExpected : false ,
345
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
406
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
346
407
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
347
408
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
348
409
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -417,7 +478,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
417
478
{
418
479
name : "ip address exists" ,
419
480
errorExpected : false ,
420
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
481
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
421
482
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
422
483
machineScope .OCIMachine .Status .Addresses = []clusterv1.MachineAddress {
423
484
{
@@ -476,7 +537,7 @@ func TestNormalReconciliationFunction(t *testing.T) {
476
537
{
477
538
name : "backend creation fails" ,
478
539
errorExpected : true ,
479
- testSpecificSetup : func (machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
540
+ testSpecificSetup : func (t * test , machineScope * scope.MachineScope , computeClient * mock_compute.MockComputeClient , vcnClient * mock_vcn.MockClient , nlbClient * mock_nlb.MockNetworkLoadBalancerClient ) {
480
541
machineScope .Machine .ObjectMeta .Labels = make (map [string ]string )
481
542
machineScope .Machine .ObjectMeta .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
482
543
computeClient .EXPECT ().GetInstance (gomock .Any (), gomock .Eq (core.GetInstanceRequest {
@@ -555,9 +616,9 @@ func TestNormalReconciliationFunction(t *testing.T) {
555
616
g := NewWithT (t )
556
617
defer teardown (t , g )
557
618
setup (t , g )
558
- tc .testSpecificSetup (ms , computeClient , vcnClient , nlbClient )
619
+ tc .testSpecificSetup (& tc , ms , computeClient , vcnClient , nlbClient )
559
620
ctx := context .Background ()
560
- _ , err := r .reconcileNormal (ctx , log .FromContext (ctx ), ms )
621
+ result , err := r .reconcileNormal (ctx , log .FromContext (ctx ), ms )
561
622
if len (tc .conditionAssertion ) > 0 {
562
623
expectConditions (g , ociMachine , tc .conditionAssertion )
563
624
}
@@ -569,6 +630,9 @@ func TestNormalReconciliationFunction(t *testing.T) {
569
630
if tc .expectedEvent != "" {
570
631
g .Eventually (recorder .Events ).Should (Receive (ContainSubstring (tc .expectedEvent )))
571
632
}
633
+ if tc .validate != nil {
634
+ tc .validate (g , & tc , result )
635
+ }
572
636
})
573
637
}
574
638
}
0 commit comments