@@ -27,6 +27,7 @@ import (
27
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
28
"k8s.io/apimachinery/pkg/runtime"
29
29
"k8s.io/apimachinery/pkg/types"
30
+ "k8s.io/utils/ptr"
30
31
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
31
32
"sigs.k8s.io/cluster-api-provider-azure/azure"
32
33
"sigs.k8s.io/cluster-api-provider-azure/azure/mock_azure"
@@ -45,30 +46,49 @@ func TestAzureMachinePoolMachineReconciler_Reconcile(t *testing.T) {
45
46
cases := []struct {
46
47
Name string
47
48
Setup func (cb * fake.ClientBuilder , reconciler * mock_azure.MockReconcilerMockRecorder )
48
- Verify func (g * WithT , result ctrl.Result , err error )
49
+ Verify func (g * WithT , c client. Client , result ctrl.Result , err error )
49
50
}{
50
51
{
51
52
Name : "should successfully reconcile" ,
52
53
Setup : func (cb * fake.ClientBuilder , reconciler * mock_azure.MockReconcilerMockRecorder ) {
53
- objects := getReadyMachinePoolMachineClusterObjects (false )
54
+ objects := getReadyMachinePoolMachineClusterObjects (false , nil )
54
55
reconciler .Reconcile (gomock2 .AContext ()).Return (nil )
55
56
cb .WithObjects (objects ... )
56
57
},
57
- Verify : func (g * WithT , result ctrl.Result , err error ) {
58
+ Verify : func (g * WithT , c client. Client , result ctrl.Result , err error ) {
58
59
g .Expect (err ).NotTo (HaveOccurred ())
59
60
},
60
61
},
61
62
{
62
63
Name : "should successfully delete" ,
63
64
Setup : func (cb * fake.ClientBuilder , reconciler * mock_azure.MockReconcilerMockRecorder ) {
64
- objects := getReadyMachinePoolMachineClusterObjects (true )
65
+ objects := getReadyMachinePoolMachineClusterObjects (true , nil )
65
66
reconciler .Delete (gomock2 .AContext ()).Return (nil )
66
67
cb .WithObjects (objects ... )
67
68
},
68
- Verify : func (g * WithT , result ctrl.Result , err error ) {
69
+ Verify : func (g * WithT , c client. Client , result ctrl.Result , err error ) {
69
70
g .Expect (err ).NotTo (HaveOccurred ())
70
71
},
71
72
},
73
+ {
74
+ Name : "should delete Machine if VMSS VM has state Deleting" ,
75
+ Setup : func (cb * fake.ClientBuilder , reconciler * mock_azure.MockReconcilerMockRecorder ) {
76
+ objects := getReadyMachinePoolMachineClusterObjects (false , ptr .To (infrav1 .Deleting ))
77
+ reconciler .Reconcile (gomock2 .AContext ()).Return (nil )
78
+ cb .WithObjects (objects ... )
79
+ },
80
+ Verify : func (g * WithT , c client.Client , result ctrl.Result , err error ) {
81
+ g .Expect (err ).NotTo (HaveOccurred ())
82
+
83
+ machine := & clusterv1.Machine {}
84
+ err = c .Get (context .Background (), types.NamespacedName {
85
+ Name : "ma1" ,
86
+ Namespace : "default" ,
87
+ }, machine )
88
+ g .Expect (err ).To (HaveOccurred ())
89
+ g .Expect (err .Error ()).Should (ContainSubstring ("not found" ))
90
+ },
91
+ },
72
92
}
73
93
74
94
for _ , c := range cases {
@@ -97,7 +117,8 @@ func TestAzureMachinePoolMachineReconciler_Reconcile(t *testing.T) {
97
117
defer mockCtrl .Finish ()
98
118
99
119
c .Setup (cb , reconciler .EXPECT ())
100
- controller := NewAzureMachinePoolMachineController (cb .Build (), nil , reconcilerutils.Timeouts {}, "foo" )
120
+ cl := cb .Build ()
121
+ controller := NewAzureMachinePoolMachineController (cl , nil , reconcilerutils.Timeouts {}, "foo" )
101
122
controller .reconcilerFactory = func (_ * scope.MachinePoolMachineScope ) (azure.Reconciler , error ) {
102
123
return reconciler , nil
103
124
}
@@ -107,12 +128,12 @@ func TestAzureMachinePoolMachineReconciler_Reconcile(t *testing.T) {
107
128
Namespace : "default" ,
108
129
},
109
130
})
110
- c .Verify (g , res , err )
131
+ c .Verify (g , cl , res , err )
111
132
})
112
133
}
113
134
}
114
135
115
- func getReadyMachinePoolMachineClusterObjects (ampmIsDeleting bool ) []client.Object {
136
+ func getReadyMachinePoolMachineClusterObjects (ampmIsDeleting bool , ampmProvisioningState * infrav1. ProvisioningState ) []client.Object {
116
137
azCluster := & infrav1.AzureCluster {
117
138
TypeMeta : metav1.TypeMeta {
118
139
Kind : "AzureCluster" ,
@@ -253,6 +274,11 @@ func getReadyMachinePoolMachineClusterObjects(ampmIsDeleting bool) []client.Obje
253
274
Time : time .Now (),
254
275
}
255
276
}
277
+ if ampmProvisioningState != nil {
278
+ ampm .Status = infrav1exp.AzureMachinePoolMachineStatus {
279
+ ProvisioningState : ampmProvisioningState ,
280
+ }
281
+ }
256
282
257
283
return []client.Object {cluster , azCluster , mp , amp , ma , ampm , fakeIdentity , fakeSecret }
258
284
}
0 commit comments