Skip to content

Commit 1cd61f9

Browse files
committed
Fix UT errors
Signed-off-by: Gong Zhang <[email protected]>
1 parent 8cc8e8a commit 1cd61f9

File tree

7 files changed

+54
-31
lines changed

7 files changed

+54
-31
lines changed

controllers/vmware/virtualmachinegroup_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121

2222
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
2323
apitypes "k8s.io/apimachinery/pkg/types"
24-
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
25-
capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
2624
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2725
"sigs.k8s.io/cluster-api/util/predicates"
2826
ctrl "sigs.k8s.io/controller-runtime"
@@ -34,6 +32,9 @@ import (
3432
"sigs.k8s.io/controller-runtime/pkg/manager"
3533
"sigs.k8s.io/controller-runtime/pkg/predicate"
3634
"sigs.k8s.io/controller-runtime/pkg/reconcile"
35+
36+
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
37+
capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
3738
)
3839

3940
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters,verbs=get;list;watch
@@ -77,7 +78,7 @@ func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerMa
7778
handler.EnqueueRequestsFromMapFunc(reconciler.VSphereMachineToVirtualMachineGroup),
7879
ctrlbldr.WithPredicates(
7980
predicate.Funcs{
80-
UpdateFunc: func(e event.UpdateEvent) bool { return false },
81+
UpdateFunc: func(event.UpdateEvent) bool { return false },
8182
CreateFunc: func(event.CreateEvent) bool { return true },
8283
DeleteFunc: func(event.DeleteEvent) bool { return true },
8384
GenericFunc: func(event.GenericEvent) bool { return false },
@@ -89,7 +90,7 @@ func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerMa
8990
}
9091

9192
// ClusterToVirtualMachineGroup maps Cluster events to VirtualMachineGroup reconcile requests.
92-
func (r *VirtualMachineGroupReconciler) ClusterToVirtualMachineGroup(ctx context.Context, a ctrlclient.Object) []reconcile.Request {
93+
func (r *VirtualMachineGroupReconciler) ClusterToVirtualMachineGroup(_ context.Context, a ctrlclient.Object) []reconcile.Request {
9394
cluster, ok := a.(*clusterv1.Cluster)
9495
if !ok {
9596
return nil

controllers/vmware/virtualmachinegroup_reconciler.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/client-go/tools/record"
3232
"k8s.io/klog/v2"
33+
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
3334
"sigs.k8s.io/cluster-api/util/conditions"
3435
ctrl "sigs.k8s.io/controller-runtime"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -38,7 +39,6 @@ import (
3839

3940
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
4041
infrautilv1 "sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
41-
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4242
)
4343

4444
const (
@@ -86,10 +86,9 @@ func (r *VirtualMachineGroupReconciler) Reconcile(ctx context.Context, req ctrl.
8686

8787
// Continue with the main logic.
8888
return r.createOrUpdateVMG(ctx, cluster)
89-
9089
}
9190

92-
// createOrUpdateVMG Create or Update VirtualMachineGroup
91+
// createOrUpdateVMG Create or Update VirtualMachineGroup.
9392
func (r *VirtualMachineGroupReconciler) createOrUpdateVMG(ctx context.Context, cluster *clusterv1.Cluster) (_ reconcile.Result, reterr error) {
9493
log := ctrl.LoggerFrom(ctx)
9594

@@ -228,8 +227,7 @@ func (r *VirtualMachineGroupReconciler) createOrUpdateVMG(ctx context.Context, c
228227
return reconcile.Result{}, err
229228
}
230229

231-
// getExpectedVSphereMachines returns the total number of replicas across all
232-
// MachineDeployments belonging to the Cluster
230+
// MachineDeployments belonging to the Cluster.
233231
func getExpectedVSphereMachines(ctx context.Context, kubeClient client.Client, cluster *clusterv1.Cluster) (int32, error) {
234232
var mdList clusterv1.MachineDeploymentList
235233
if err := kubeClient.List(
@@ -329,8 +327,8 @@ func GenerateVMGPlacementAnnotations(ctx context.Context, vmg *vmoprv1.VirtualMa
329327
return annotations, nil
330328
}
331329

332-
// Duplicated this logic from pkg/services/vmoperator/vmopmachine.go
333330
// GenerateVirtualMachineName generates the name of a VirtualMachine based on the naming strategy.
331+
// Duplicated this logic from pkg/services/vmoperator/vmopmachine.go.
334332
func GenerateVirtualMachineName(machineName string, namingStrategy *vmwarev1.VirtualMachineNamingStrategy) (string, error) {
335333
// Per default the name of the VirtualMachine should be equal to the Machine name (this is the same as "{{ .machine.name }}")
336334
if namingStrategy == nil || namingStrategy.Template == nil {

controllers/vmware/virtualmachinegroup_reconciler_test.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package vmware
218

319
import (
@@ -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

2843
const (
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.
351376
func 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).
379404
func 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.
400425
func 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).
417442
func newMachineDeployment(name string) *clusterv1.MachineDeployment {
418443
return &clusterv1.MachineDeployment{
419444
ObjectMeta: metav1.ObjectMeta{

controllers/vspherecluster_reconciler.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ func (r *clusterReconciler) reconcileDeploymentZones(ctx context.Context, cluste
427427
failureDomains := clusterv1beta1.FailureDomains{}
428428
for _, zone := range deploymentZoneList.Items {
429429
if zone.Spec.Server != clusterCtx.VSphereCluster.Spec.Server {
430-
431430
continue
432431
}
433432

test/framework/vmoperator/vmoperator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func ReconcileDependencies(ctx context.Context, c client.Client, dependenciesCon
534534
Namespace: config.Namespace,
535535
},
536536
Spec: vmoprv1.VirtualMachineImageSpec{
537-
ProviderRef: vmoprv1common.LocalObjectRef{
537+
ProviderRef: &vmoprv1common.LocalObjectRef{
538538
Kind: "ContentLibraryItem",
539539
},
540540
},

test/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ replace sigs.k8s.io/cluster-api-provider-vsphere => ../
1010

1111
replace github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels => github.com/vmware-tanzu/vm-operator/pkg/constants/testlabels v1.9.1-0.20251003150112-9b458d311c4c
1212

13-
// The version of vm-operator should be kept in sync with the manifests at: config/deployments/integration-testsz
14-
replace github.com/vmware-tanzu/vm-operator/api => github.com/vmware-tanzu/vm-operator/api v1.8.6
13+
// The version of vm-operator should be kept in sync with the manifests at: config/deployments/integration-tests
14+
replace github.com/vmware-tanzu/vm-operator/api => github.com/vmware-tanzu/vm-operator/api v1.9.1-0.20251003150112-9b458d311c4c
1515

1616
require (
1717
github.com/vmware-tanzu/net-operator-api v0.0.0-20240326163340-1f32d6bf7f9d

test/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ github.com/vmware-tanzu/net-operator-api v0.0.0-20240326163340-1f32d6bf7f9d h1:c
360360
github.com/vmware-tanzu/net-operator-api v0.0.0-20240326163340-1f32d6bf7f9d/go.mod h1:JbFOh22iDsT5BowJe0GgpMI5e2/S7cWaJlv9LdURVQM=
361361
github.com/vmware-tanzu/nsx-operator/pkg/apis v0.0.0-20241112044858-9da8637c1b0d h1:z9lrzKVtNlujduv9BilzPxuge/LE2F0N1ms3TP4JZvw=
362362
github.com/vmware-tanzu/nsx-operator/pkg/apis v0.0.0-20241112044858-9da8637c1b0d/go.mod h1:Q4JzNkNMvjo7pXtlB5/R3oME4Nhah7fAObWgghVmtxk=
363-
github.com/vmware-tanzu/vm-operator/api v1.8.6 h1:NIndORjcnSmIlQsCMIewpIwg/ocRVDh2lYjOroTVLrU=
364-
github.com/vmware-tanzu/vm-operator/api v1.8.6/go.mod h1:HHA2SNI9B5Yqtyp5t+Gt9WTWBi/fIkM6+MukDDSf11A=
363+
github.com/vmware-tanzu/vm-operator/api v1.9.1-0.20251003150112-9b458d311c4c h1:XISTT0dw/XwMlyyiOPHPsXCxfI1Ro2Zuozi6eIacXGo=
364+
github.com/vmware-tanzu/vm-operator/api v1.9.1-0.20251003150112-9b458d311c4c/go.mod h1:nWTPpxfe4gHuuYuFcrs86+NMxfkqPk3a3IlvI8TCWak=
365365
github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20240404200847-de75746a9505 h1:y4wXx1FUFqqSgJ/xUOEM1DLS2Uu0KaeLADWpzpioGTU=
366366
github.com/vmware-tanzu/vm-operator/external/ncp v0.0.0-20240404200847-de75746a9505/go.mod h1:5rqRJ9zGR+KnKbkGx373WgN8xJpvAj99kHnfoDYRO5I=
367367
github.com/vmware/govmomi v0.52.0 h1:JyxQ1IQdllrY7PJbv2am9mRsv3p9xWlIQ66bv+XnyLw=

0 commit comments

Comments
 (0)