1+ /*
2+ Copyright 2025 The Kubernetes Authors.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
117package vmware
218
319import (
@@ -14,16 +30,15 @@ import (
1430 "k8s.io/apimachinery/pkg/runtime"
1531 "k8s.io/apimachinery/pkg/types"
1632 "k8s.io/client-go/tools/record"
17- vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
1833 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
1934 "sigs.k8s.io/cluster-api/util/conditions"
2035 ctrl "sigs.k8s.io/controller-runtime"
2136 "sigs.k8s.io/controller-runtime/pkg/client"
2237 "sigs.k8s.io/controller-runtime/pkg/client/fake"
2338 "sigs.k8s.io/controller-runtime/pkg/reconcile"
24- )
2539
26- var s = runtime .NewScheme ()
40+ vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
41+ )
2742
2843const (
2944 clusterName = "test-cluster"
@@ -38,6 +53,9 @@ func TestGetExpectedVSphereMachines(t *testing.T) {
3853 g := NewWithT (t )
3954 ctx := context .Background ()
4055
56+ scheme := runtime .NewScheme ()
57+ g .Expect (clusterv1 .AddToScheme (scheme )).To (Succeed ())
58+
4159 tests := []struct {
4260 name string
4361 cluster * clusterv1.Cluster
@@ -65,8 +83,8 @@ func TestGetExpectedVSphereMachines(t *testing.T) {
6583 }
6684
6785 for _ , tt := range tests {
68- fakeClient := fake .NewClientBuilder ().WithScheme (s ).WithObjects (tt .cluster ).Build ()
69- t .Run (tt .name , func (t * testing.T ) {
86+ fakeClient := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .cluster ).Build ()
87+ t .Run (tt .name , func (_ * testing.T ) {
7088 g .Expect (getExpectedVSphereMachines (ctx , fakeClient , tt .cluster )).To (Equal (tt .expected ))
7189 })
7290 }
@@ -76,6 +94,9 @@ func TestGetCurrentVSphereMachines(t *testing.T) {
7694 g := NewWithT (t )
7795 ctx := context .Background ()
7896
97+ scheme := runtime .NewScheme ()
98+ g .Expect (vmwarev1 .AddToScheme (scheme )).To (Succeed ())
99+
79100 // VM names are based on CAPI Machine names, not VSphereMachine names, but we use VSphereMachine here.
80101 vsm1 := newVSphereMachine ("vsm-1" , mdName1 , false , nil )
81102 vsm2 := newVSphereMachine ("vsm-2" , mdName2 , false , nil )
@@ -106,11 +127,11 @@ func TestGetCurrentVSphereMachines(t *testing.T) {
106127 }
107128
108129 for _ , tt := range tests {
109- t .Run (tt .name , func (t * testing.T ) {
110- fakeClient := fake .NewClientBuilder ().WithScheme (s ).WithObjects (tt .objects ... ).Build ()
130+ t .Run (tt .name , func (_ * testing.T ) {
131+ fakeClient := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .objects ... ).Build ()
111132 got , err := getCurrentVSphereMachines (ctx , fakeClient , clusterNamespace , clusterName )
112133 g .Expect (err ).NotTo (HaveOccurred ())
113- g .Expect (len ( got )) .To (Equal (tt .want ))
134+ g .Expect (got ).To (HaveLen (tt .want ))
114135
115136 // Check that the correct Machines are present
116137 if tt .want > 0 {
@@ -221,7 +242,7 @@ func TestGenerateVMGPlacementAnnotations(t *testing.T) {
221242 }
222243
223244 for _ , tt := range tests {
224- t .Run (tt .name , func (t * testing.T ) {
245+ t .Run (tt .name , func (_ * testing.T ) {
225246 ctx := ctrl .LoggerInto (context .Background (), ctrl .LoggerFrom (context .Background ()))
226247
227248 got , err := GenerateVMGPlacementAnnotations (ctx , tt .vmg , tt .machineDeployments )
@@ -240,6 +261,10 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
240261 g := NewWithT (t )
241262 ctx := context .Background ()
242263
264+ scheme := runtime .NewScheme ()
265+ g .Expect (clusterv1 .AddToScheme (scheme )).To (Succeed ())
266+ g .Expect (vmwarev1 .AddToScheme (scheme )).To (Succeed ())
267+
243268 // Initial objects for the successful VMG creation path (Expected: 1, Current: 1)
244269 cluster := newCluster (clusterName , clusterNamespace , true , 1 , 0 )
245270 vsm1 := newVSphereMachine ("vsm-1" , mdName1 , false , nil )
@@ -312,8 +337,8 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
312337 }
313338
314339 for _ , tt := range tests {
315- t .Run (tt .name , func (t * testing.T ) {
316- fakeClient := fake .NewClientBuilder ().WithScheme (s ).WithObjects (tt .initialObjects ... ).Build ()
340+ t .Run (tt .name , func (_ * testing.T ) {
341+ fakeClient := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (tt .initialObjects ... ).Build ()
317342 reconciler := & VirtualMachineGroupReconciler {
318343 Client : fakeClient ,
319344 Recorder : record .NewFakeRecorder (1 ),
@@ -347,7 +372,7 @@ func TestVirtualMachineGroupReconciler_ReconcileFlow(t *testing.T) {
347372 }
348373}
349374
350- // Helper function to create a basic Cluster object
375+ // Helper function to create a basic Cluster object.
351376func newCluster (name , namespace string , initialized bool , replicasMD1 , replicasMD2 int32 ) * clusterv1.Cluster {
352377 cluster := & clusterv1.Cluster {
353378 ObjectMeta : metav1.ObjectMeta {
@@ -375,7 +400,7 @@ func newCluster(name, namespace string, initialized bool, replicasMD1, replicasM
375400 return cluster
376401}
377402
378- // Helper function to create a VSphereMachine (worker, owned by a CAPI Machine)
403+ // Helper function to create a VSphereMachine (worker, owned by a CAPI Machine).
379404func newVSphereMachine (name , mdName string , deleted bool , namingStrategy * vmwarev1.VirtualMachineNamingStrategy ) * vmwarev1.VSphereMachine {
380405 vsm := & vmwarev1.VSphereMachine {
381406 ObjectMeta : metav1.ObjectMeta {
@@ -396,7 +421,7 @@ func newVSphereMachine(name, mdName string, deleted bool, namingStrategy *vmware
396421 return vsm
397422}
398423
399- // Helper function to create a VMG member status with placement info
424+ // Helper function to create a VMG member status with placement info.
400425func newVMGMemberStatus (name , kind string , isPlacementReady bool , zone string ) vmoprv1.VirtualMachineGroupMemberStatus {
401426 memberStatus := vmoprv1.VirtualMachineGroupMemberStatus {
402427 Name : name ,
@@ -413,7 +438,7 @@ func newVMGMemberStatus(name, kind string, isPlacementReady bool, zone string) v
413438 return memberStatus
414439}
415440
416- // Helper function to create a MachineDeployment (for listing MD names)
441+ // Helper function to create a MachineDeployment (for listing MD names).
417442func newMachineDeployment (name string ) * clusterv1.MachineDeployment {
418443 return & clusterv1.MachineDeployment {
419444 ObjectMeta : metav1.ObjectMeta {
0 commit comments