@@ -7,15 +7,20 @@ import (
7
7
"encoding/base64"
8
8
"errors"
9
9
"testing"
10
+ "time"
10
11
11
12
. "github.com/onsi/gomega"
12
13
14
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
15
+
13
16
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
14
17
15
18
flintlocktypes "github.com/weaveworks/flintlock/api/types"
16
19
20
+ "github.com/weaveworks/cluster-api-provider-microvm/api/v1alpha1"
17
21
infrav1 "github.com/weaveworks/cluster-api-provider-microvm/api/v1alpha1"
18
22
"github.com/weaveworks/cluster-api-provider-microvm/internal/services/microvm/mock_client"
23
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
24
)
20
25
21
26
func TestMachineReconcile (t * testing.T ) {
@@ -57,6 +62,15 @@ func TestMachineReconcile(t *testing.T) {
57
62
t .Run ("and create microvm succeeds" , machineReconcileNoVmCreateSucceeds )
58
63
t .Run ("and create microvm succeeds and reconciles again" , machineReconcileNoVmCreateAdditionReconcile )
59
64
})
65
+
66
+ t .Run ("microvm_has_deletion_timestamp" , func (t * testing.T ) {
67
+ t .Parallel ()
68
+
69
+ t .Run ("and delete microvm succeeds" , machineReconcileDeleteVmSucceeds )
70
+ t .Run ("microvm get returns nil" , machineReconcileDeleteGetReturnsNil )
71
+ t .Run ("microvm get returns error" , machineReconcileDeleteGetErrors )
72
+ t .Run ("microvm delete returns error" , machineReconcileDeleteDeleteErrors )
73
+ })
60
74
}
61
75
62
76
func machineReconcileMissingMvmMachine (t * testing.T ) {
@@ -337,9 +351,109 @@ func machineReconcileNoVmCreateAdditionReconcile(t *testing.T) {
337
351
g .Expect (result .IsZero ()).To (BeFalse (), "Expect requeue to be requested after create" )
338
352
339
353
withExistingMicrovm (& fakeAPIClient , flintlocktypes .MicroVMStatus_CREATED )
340
- result , err = reconcileMachine (client , & fakeAPIClient )
354
+ _ , err = reconcileMachine (client , & fakeAPIClient )
355
+ g .Expect (err ).NotTo (HaveOccurred (), "Reconciling should not return an error" )
341
356
342
357
reconciled , err := getMicrovmMachine (client , testMachineName , testClusterNamespace )
343
358
g .Expect (err ).NotTo (HaveOccurred (), "Getting microvm machine should not fail" )
344
359
assertMachineReconciled (g , reconciled )
345
360
}
361
+
362
+ func machineReconcileDeleteVmSucceeds (t * testing.T ) {
363
+ t .Parallel ()
364
+ g := NewWithT (t )
365
+
366
+ apiObjects := defaultClusterObjects ()
367
+ apiObjects .MvmMachine .DeletionTimestamp = & metav1.Time {
368
+ Time : time .Now (),
369
+ }
370
+ apiObjects .MvmMachine .Finalizers = []string {v1alpha1 .MachineFinalizer }
371
+
372
+ fakeAPIClient := mock_client.FakeClient {}
373
+ withExistingMicrovm (& fakeAPIClient , flintlocktypes .MicroVMStatus_CREATED )
374
+
375
+ client := createFakeClient (g , apiObjects .AsRuntimeObjects ())
376
+
377
+ result , err := reconcileMachine (client , & fakeAPIClient )
378
+ g .Expect (err ).NotTo (HaveOccurred (), "Reconciling when deleting microvm should not return error" )
379
+ g .Expect (result .Requeue ).To (BeFalse ())
380
+ g .Expect (result .RequeueAfter ).To (BeNumerically (">" , time .Duration (0 )))
381
+
382
+ g .Expect (fakeAPIClient .DeleteMicroVMCallCount ()).To (Equal (1 ))
383
+ _ , deleteReq , _ := fakeAPIClient .DeleteMicroVMArgsForCall (0 )
384
+ g .Expect (deleteReq .Id ).To (Equal (testMachineName ))
385
+ g .Expect (deleteReq .Namespace ).To (Equal (testClusterNamespace ))
386
+
387
+ _ , err = getMicrovmMachine (client , testMachineName , testClusterNamespace )
388
+ g .Expect (apierrors .IsNotFound (err )).To (BeFalse ())
389
+ }
390
+
391
+ func machineReconcileDeleteGetReturnsNil (t * testing.T ) {
392
+ t .Parallel ()
393
+ g := NewWithT (t )
394
+
395
+ apiObjects := defaultClusterObjects ()
396
+ apiObjects .MvmMachine .DeletionTimestamp = & metav1.Time {
397
+ Time : time .Now (),
398
+ }
399
+ apiObjects .MvmMachine .Finalizers = []string {v1alpha1 .MachineFinalizer }
400
+
401
+ fakeAPIClient := mock_client.FakeClient {}
402
+ withMissingMicrovm (& fakeAPIClient )
403
+
404
+ client := createFakeClient (g , apiObjects .AsRuntimeObjects ())
405
+
406
+ result , err := reconcileMachine (client , & fakeAPIClient )
407
+ g .Expect (err ).NotTo (HaveOccurred (), "Reconciling when deleting microvm should not return error" )
408
+ g .Expect (result .Requeue ).To (BeFalse ())
409
+ g .Expect (result .RequeueAfter ).To (Equal (time .Duration (0 )))
410
+
411
+ g .Expect (fakeAPIClient .DeleteMicroVMCallCount ()).To (Equal (0 ))
412
+
413
+ _ , err = getMicrovmMachine (client , testMachineName , testClusterNamespace )
414
+ g .Expect (apierrors .IsNotFound (err )).To (BeTrue ())
415
+ }
416
+
417
+ func machineReconcileDeleteGetErrors (t * testing.T ) {
418
+ t .Parallel ()
419
+ g := NewWithT (t )
420
+
421
+ apiObjects := defaultClusterObjects ()
422
+ apiObjects .MvmMachine .DeletionTimestamp = & metav1.Time {
423
+ Time : time .Now (),
424
+ }
425
+ apiObjects .MvmMachine .Finalizers = []string {v1alpha1 .MachineFinalizer }
426
+
427
+ fakeAPIClient := mock_client.FakeClient {}
428
+ withExistingMicrovm (& fakeAPIClient , flintlocktypes .MicroVMStatus_CREATED )
429
+ fakeAPIClient .GetMicroVMReturns (nil , errors .New ("something terrible happened" ))
430
+
431
+ client := createFakeClient (g , apiObjects .AsRuntimeObjects ())
432
+ _ , err := reconcileMachine (client , & fakeAPIClient )
433
+ g .Expect (err ).To (HaveOccurred (), "Reconciling when microvm service exists errors should return error" )
434
+ }
435
+
436
+ func machineReconcileDeleteDeleteErrors (t * testing.T ) {
437
+ t .Parallel ()
438
+ g := NewWithT (t )
439
+
440
+ apiObjects := defaultClusterObjects ()
441
+ apiObjects .MvmMachine .DeletionTimestamp = & metav1.Time {
442
+ Time : time .Now (),
443
+ }
444
+ apiObjects .MvmMachine .Finalizers = []string {v1alpha1 .MachineFinalizer }
445
+
446
+ fakeAPIClient := mock_client.FakeClient {}
447
+ withExistingMicrovm (& fakeAPIClient , flintlocktypes .MicroVMStatus_CREATED )
448
+ fakeAPIClient .DeleteMicroVMReturns (nil , errors .New ("something terrible happened" ))
449
+
450
+ client := createFakeClient (g , apiObjects .AsRuntimeObjects ())
451
+ _ , err := reconcileMachine (client , & fakeAPIClient )
452
+ g .Expect (err ).To (HaveOccurred (), "Reconciling when deleting microvm errors should return error" )
453
+
454
+ reconciled , err := getMicrovmMachine (client , testMachineName , testClusterNamespace )
455
+ g .Expect (err ).NotTo (HaveOccurred (), "Getting microvm machine should not fail" )
456
+
457
+ assertConditionFalse (g , reconciled , infrav1 .MicrovmReadyCondition , infrav1 .MicrovmDeleteFailedReason )
458
+ assertMachineNotReady (g , reconciled )
459
+ }
0 commit comments