@@ -36,6 +36,7 @@ const (
3636
3737func TestGetExpectedVSphereMachines (t * testing.T ) {
3838 g := NewWithT (t )
39+ ctx := context .Background ()
3940
4041 tests := []struct {
4142 name string
@@ -64,8 +65,9 @@ func TestGetExpectedVSphereMachines(t *testing.T) {
6465 }
6566
6667 for _ , tt := range tests {
68+ fakeClient := fake .NewClientBuilder ().WithScheme (s ).WithObjects (tt .cluster ).Build ()
6769 t .Run (tt .name , func (t * testing.T ) {
68- g .Expect (getExpectedVSphereMachines (tt .cluster )).To (Equal (tt .expected ))
70+ g .Expect (getExpectedVSphereMachines (ctx , fakeClient , tt .cluster )).To (Equal (tt .expected ))
6971 })
7072 }
7173}
@@ -74,12 +76,12 @@ func TestGetCurrentVSphereMachines(t *testing.T) {
7476 g := NewWithT (t )
7577 ctx := context .Background ()
7678
77- // VM names are based on CAPI Machine names, not VSphereMachine names, but we use VSM objects here.
79+ // VM names are based on CAPI Machine names, not VSphereMachine names, but we use VSphereMachine here.
7880 vsm1 := newVSphereMachine ("vsm-1" , mdName1 , false , nil )
7981 vsm2 := newVSphereMachine ("vsm-2" , mdName2 , false , nil )
8082 vsmDeleting := newVSphereMachine ("vsm-3" , mdName1 , true , nil ) // Deleting
8183 vsmControlPlane := newVSphereMachine ("vsm-cp" , "cp-md" , false , nil )
82- vsmControlPlane .Labels [clusterv1 .MachineControlPlaneLabel ] = "true" // Should be filtered by label in production, but here filtered implicitly by only listing MD-labelled objects
84+ vsmControlPlane .Labels [clusterv1 .MachineControlPlaneLabel ] = "true"
8385
8486 tests := []struct {
8587 name string
@@ -94,7 +96,7 @@ func TestGetCurrentVSphereMachines(t *testing.T) {
9496 vsmDeleting ,
9597 vsmControlPlane ,
9698 },
97- want : 2 , // Should exclude vsm-3 (deleting) and vsm-cp (no MD label used in the actual listing logic )
99+ want : 2 , // Should exclude vsm-3 (deleting) and vsm-cp (control plane VSphereMachine )
98100 },
99101 {
100102 name : "No VSphereMachines found" ,
@@ -110,7 +112,7 @@ func TestGetCurrentVSphereMachines(t *testing.T) {
110112 g .Expect (err ).NotTo (HaveOccurred ())
111113 g .Expect (len (got )).To (Equal (tt .want ))
112114
113- // Check that the correct machines are present (e.g., vsm1 and vsm2)
115+ // Check that the correct Machines are present
114116 if tt .want > 0 {
115117 names := make ([]string , len (got ))
116118 for i , vsm := range got {
@@ -191,7 +193,7 @@ func TestGenerateVMGPlacementAnnotations(t *testing.T) {
191193 Members : []vmoprv1.VirtualMachineGroupMemberStatus {
192194 // First VM sets the placement
193195 newVMGMemberStatus (vmName1 , "VirtualMachine" , true , zoneA ),
194- // Second VM is ignored (logic skips finding placement twice)
196+ // Second VM is ignored
195197 newVMGMemberStatus (vmNameUnplaced , "VirtualMachine" , true , zoneB ),
196198 },
197199 },
@@ -208,7 +210,7 @@ func TestGenerateVMGPlacementAnnotations(t *testing.T) {
208210 ObjectMeta : metav1.ObjectMeta {Name : clusterName , Namespace : clusterNamespace },
209211 Status : vmoprv1.VirtualMachineGroupStatus {
210212 Members : []vmoprv1.VirtualMachineGroupMemberStatus {
211- newVMGMemberStatus (vmNameWrongKind , "Pod " , true , zoneA ),
213+ newVMGMemberStatus (vmNameWrongKind , "VirtualMachineGroup " , true , zoneA ),
212214 },
213215 },
214216 },
@@ -220,7 +222,6 @@ func TestGenerateVMGPlacementAnnotations(t *testing.T) {
220222
221223 for _ , tt := range tests {
222224 t .Run (tt .name , func (t * testing.T ) {
223- // Mock client is needed for the logging in the function, but not used for API calls
224225 ctx := ctrl .LoggerInto (context .Background (), ctrl .LoggerFrom (context .Background ()))
225226
226227 got , err := GenerateVMGPlacementAnnotations (ctx , tt .vmg , tt .machineDeployments )
@@ -240,7 +241,7 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
240241 ctx := context .Background ()
241242
242243 // Initial objects for the successful VMG creation path (Expected: 1, Current: 1)
243- cluster := newCluster (clusterName , clusterNamespace , true , 1 , 0 ) // Expect 1 machine
244+ cluster := newCluster (clusterName , clusterNamespace , true , 1 , 0 )
244245 vsm1 := newVSphereMachine ("vsm-1" , mdName1 , false , nil )
245246 md1 := newMachineDeployment (mdName1 )
246247
@@ -271,13 +272,13 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
271272 {
272273 name : "Requeue: ControlPlane Not Initialized" ,
273274 initialObjects : []client.Object {
274- newCluster (clusterName , clusterNamespace , false , 1 , 0 ), // Not Initialized
275+ newCluster (clusterName , clusterNamespace , false , 1 , 0 ),
275276 },
276277 expectedResult : reconcile.Result {RequeueAfter : reconciliationDelay },
277278 checkVMGExists : false ,
278279 },
279280 {
280- name : "Requeue: VMG Not Found, Machines Missing (0/1) " ,
281+ name : "Requeue: VMG Not Found" ,
281282 initialObjects : []client.Object {
282283 cluster .DeepCopy (),
283284 md1 .DeepCopy (),
@@ -286,7 +287,7 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
286287 checkVMGExists : false ,
287288 },
288289 {
289- name : "Success: VMG Created (1/1) " ,
290+ name : "Success: VMG Created" ,
290291 initialObjects : []client.Object {
291292 cluster .DeepCopy (),
292293 md1 .DeepCopy (),
@@ -301,7 +302,7 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
301302 cluster .DeepCopy (),
302303 md1 .DeepCopy (),
303304 vsm1 .DeepCopy (),
304- & vmoprv1.VirtualMachineGroup { // Pre-existing VMG
305+ & vmoprv1.VirtualMachineGroup {
305306 ObjectMeta : metav1.ObjectMeta {Name : clusterName , Namespace : clusterNamespace },
306307 },
307308 },
@@ -333,7 +334,9 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
333334 // Check that the core fields were set by the MutateFn
334335 g .Expect (vmg .Labels ).To (HaveKeyWithValue (clusterv1 .ClusterNameLabel , clusterName ))
335336 g .Expect (vmg .Spec .BootOrder ).To (HaveLen (1 ))
336- g .Expect (vmg .Spec .BootOrder [0 ].Members ).To (HaveLen (int (getExpectedVSphereMachines (cluster ))))
337+ expected , err := getExpectedVSphereMachines (ctx , fakeClient , tt .initialObjects [0 ].(* clusterv1.Cluster ))
338+ g .Expect (err ).NotTo (HaveOccurred (), "Should get expected Machines" )
339+ g .Expect (vmg .Spec .BootOrder [0 ].Members ).To (HaveLen (int (expected )))
337340
338341 // VMG members should match the VSphereMachine (name: vsm-1)
339342 g .Expect (vmg .Spec .BootOrder [0 ].Members [0 ].Name ).To (ContainElement ("vsm-1" ))
@@ -344,9 +347,6 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
344347 }
345348}
346349
347- // Helper function to create a *string
348- func stringPtr (s string ) * string { return & s }
349-
350350// Helper function to create a basic Cluster object
351351func newCluster (name , namespace string , initialized bool , replicasMD1 , replicasMD2 int32 ) * clusterv1.Cluster {
352352 cluster := & clusterv1.Cluster {
0 commit comments