Skip to content

Commit fc6385c

Browse files
committed
Tests: Use Patch instead of Update
This commit refactors tests to use Patch client calls instead of Updates. Signed-off-by: Alvaro Romero <alromero@redhat.com> Assisted-by: Claude <noreply@anthropic.com>
1 parent 22fe00c commit fc6385c

5 files changed

Lines changed: 89 additions & 119 deletions

File tree

tests/framework/framework.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
kv1 "kubevirt.io/api/core/v1"
2626
"kubevirt.io/client-go/kubecli"
2727
"kubevirt.io/client-go/log"
28+
"kubevirt.io/kubevirt-velero-plugin/tests/patch"
2829
)
2930

3031
const (
@@ -493,12 +494,14 @@ func (r *KubernetesReporter) logVolumeSnapshotContents(kubeCli kubernetes.Interf
493494

494495
func UpdateVMStateStorageClass(kvClient kubecli.KubevirtClient) {
495496
kv := GetKubevirt(kvClient)
496-
kv.Spec.Configuration.VMStateStorageClass = getStorageClassFromEnv()
497+
storageClass := getStorageClassFromEnv()
497498

498-
data, err := json.Marshal(kv.Spec)
499+
patchData, err := patch.New(
500+
patch.WithReplace("/spec/configuration/vmStateStorageClass", storageClass),
501+
).GeneratePayload()
499502
gomega.Expect(err).ToNot(gomega.HaveOccurred())
500-
patchData := fmt.Sprintf(`[{ "op": "replace", "path": "/spec", "value": %s }]`, string(data))
501-
_, err = kvClient.KubeVirt(kv.Namespace).Patch(context.Background(), kv.Name, types.JSONPatchType, []byte(patchData), metav1.PatchOptions{})
503+
504+
_, err = kvClient.KubeVirt(kv.Namespace).Patch(context.Background(), kv.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
502505
gomega.Expect(err).ToNot(gomega.HaveOccurred())
503506
}
504507

tests/framework/vm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func NewDataVolumeForBlankRawImage(dataVolumeName, size string, storageClass str
6363
ObjectMeta: metav1.ObjectMeta{
6464
Name: dataVolumeName,
6565
Annotations: map[string]string{},
66+
Labels: map[string]string{},
6667
},
6768
Spec: cdiv1.DataVolumeSpec{
6869
Source: &cdiv1.DataVolumeSource{

tests/pvc_vs_labeling_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
. "github.com/onsi/gomega"
1010
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/types"
1213
"k8s.io/apimachinery/pkg/util/wait"
1314
"k8s.io/utils/strings/slices"
1415

1516
kvv1 "kubevirt.io/api/core/v1"
1617
"kubevirt.io/kubevirt-velero-plugin/pkg/util"
1718
"kubevirt.io/kubevirt-velero-plugin/tests/framework"
1819
. "kubevirt.io/kubevirt-velero-plugin/tests/framework/matcher"
20+
"kubevirt.io/kubevirt-velero-plugin/tests/patch"
1921
)
2022

2123
var _ = Describe("PVC and VolumeSnapshot Labeling", func() {
@@ -63,11 +65,11 @@ var _ = Describe("PVC and VolumeSnapshot Labeling", func() {
6365
pvc, err := framework.FindPVC(f.K8sClient, f.Namespace.Name, pvcName)
6466
Expect(err).ToNot(HaveOccurred())
6567

66-
if pvc.Labels == nil {
67-
pvc.Labels = make(map[string]string)
68-
}
69-
pvc.Labels[util.PVCUIDLabel] = userDefinedValue
70-
_, err = f.K8sClient.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Update(context.Background(), pvc, metav1.UpdateOptions{})
68+
patchData, err := patch.New(
69+
patch.WithAddLabel(util.PVCUIDLabel, userDefinedValue, pvc.Labels),
70+
).GeneratePayload()
71+
Expect(err).ToNot(HaveOccurred())
72+
_, err = f.K8sClient.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Patch(context.Background(), pvc.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
7173
Expect(err).ToNot(HaveOccurred())
7274

7375
By("Creating backup")

tests/resource_filtering_test.go

Lines changed: 49 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
"k8s.io/utils/strings/slices"
1111
"kubevirt.io/client-go/kubecli"
1212
"kubevirt.io/kubevirt-velero-plugin/tests/framework"
13+
"kubevirt.io/kubevirt-velero-plugin/tests/patch"
1314

1415
. "github.com/onsi/ginkgo/v2"
1516
. "github.com/onsi/gomega"
1617
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
1718
v1 "k8s.io/api/core/v1"
1819
apierrs "k8s.io/apimachinery/pkg/api/errors"
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/types"
2022
kvv1 "kubevirt.io/api/core/v1"
2123
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
2224
. "kubevirt.io/kubevirt-velero-plugin/tests/framework/matcher"
@@ -2606,55 +2608,67 @@ var _ = Describe("Resource excludes", func() {
26062608
})
26072609

26082610
Context("Exclude label", func() {
2609-
addExcludeLabel := func(labels map[string]string) map[string]string {
2610-
if labels == nil {
2611-
labels = make(map[string]string)
2612-
}
2613-
labels["velero.io/exclude-from-backup"] = "true"
2614-
return labels
2615-
}
2616-
26172611
addExcludeLabelToDV := func(name string) {
2618-
updateFunc := func(dataVolume *cdiv1.DataVolume) *cdiv1.DataVolume {
2619-
dataVolume.SetLabels(addExcludeLabel(dataVolume.GetLabels()))
2620-
return dataVolume
2621-
}
2612+
dv, err := framework.FindDataVolume(f.KvClient, f.Namespace.Name, name)
2613+
Expect(err).ToNot(HaveOccurred())
2614+
2615+
patchData, err := patch.New(
2616+
patch.WithAddLabel(velerov1api.ExcludeFromBackupLabel, "true", dv.Labels),
2617+
).GeneratePayload()
2618+
Expect(err).ToNot(HaveOccurred())
26222619

2623-
retryOnceOnErr(updateDataVolume(f.KvClient, f.Namespace.Name, name, updateFunc)).Should(BeNil())
2620+
_, err = f.KvClient.CdiClient().CdiV1beta1().DataVolumes(f.Namespace.Name).Patch(context.TODO(), name, types.JSONPatchType, patchData, metav1.PatchOptions{})
2621+
Expect(err).ToNot(HaveOccurred())
26242622
}
26252623

26262624
addExcludeLabelToPVC := func(name string) {
2627-
update := func(pvc *v1.PersistentVolumeClaim) *v1.PersistentVolumeClaim {
2628-
pvc.SetLabels(addExcludeLabel(pvc.GetLabels()))
2629-
return pvc
2630-
}
2631-
retryOnceOnErr(updatePvc(f.K8sClient, f.Namespace.Name, name, update)).Should(BeNil())
2625+
pvc, err := framework.FindPVC(f.K8sClient, f.Namespace.Name, name)
2626+
Expect(err).ToNot(HaveOccurred())
2627+
2628+
patchData, err := patch.New(
2629+
patch.WithAddLabel(velerov1api.ExcludeFromBackupLabel, "true", pvc.Labels),
2630+
).GeneratePayload()
2631+
Expect(err).ToNot(HaveOccurred())
2632+
2633+
_, err = f.K8sClient.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Patch(context.TODO(), name, types.JSONPatchType, patchData, metav1.PatchOptions{})
2634+
Expect(err).ToNot(HaveOccurred())
26322635
}
26332636

26342637
addExcludeLabelToVMI := func(name string) {
2635-
update := func(vmi *kvv1.VirtualMachineInstance) *kvv1.VirtualMachineInstance {
2636-
vmi.SetLabels(addExcludeLabel(vmi.GetLabels()))
2637-
return vmi
2638-
}
2639-
retryOnceOnErr(updateVmi(f.KvClient, f.Namespace.Name, name, update)).Should(BeNil())
2638+
vmi, err := f.KvClient.VirtualMachineInstance(f.Namespace.Name).Get(context.Background(), name, metav1.GetOptions{})
2639+
Expect(err).ToNot(HaveOccurred())
2640+
2641+
patchData, err := patch.New(
2642+
patch.WithAddLabel(velerov1api.ExcludeFromBackupLabel, "true", vmi.Labels),
2643+
).GeneratePayload()
2644+
Expect(err).ToNot(HaveOccurred())
2645+
2646+
_, err = f.KvClient.VirtualMachineInstance(f.Namespace.Name).Patch(context.Background(), name, types.JSONPatchType, patchData, metav1.PatchOptions{})
2647+
Expect(err).ToNot(HaveOccurred())
26402648
}
26412649

26422650
addExcludeLabelToVM := func(name string) {
2643-
update := func(vm *kvv1.VirtualMachine) *kvv1.VirtualMachine {
2644-
vm.SetLabels(addExcludeLabel(vm.GetLabels()))
2645-
return vm
2646-
}
2647-
retryOnceOnErr(updateVm(f.KvClient, f.Namespace.Name, name, update)).Should(BeNil())
2651+
vm, err := f.KvClient.VirtualMachine(f.Namespace.Name).Get(context.Background(), name, metav1.GetOptions{})
2652+
Expect(err).ToNot(HaveOccurred())
2653+
2654+
patchData, err := patch.New(
2655+
patch.WithAddLabel(velerov1api.ExcludeFromBackupLabel, "true", vm.Labels),
2656+
).GeneratePayload()
2657+
Expect(err).ToNot(HaveOccurred())
2658+
2659+
_, err = f.KvClient.VirtualMachine(f.Namespace.Name).Patch(context.Background(), name, types.JSONPatchType, patchData, metav1.PatchOptions{})
2660+
Expect(err).ToNot(HaveOccurred())
26482661
}
26492662

26502663
addExcludeLabelToLauncherPodForVM := func(vmName string) {
2651-
retryOnceOnErr(
2652-
func() error {
2653-
pod := framework.FindLauncherPod(f.K8sClient, f.Namespace.Name, vmName)
2654-
pod.SetLabels(addExcludeLabel(pod.GetLabels()))
2655-
_, err := f.K8sClient.CoreV1().Pods(f.Namespace.Name).Update(context.TODO(), &pod, metav1.UpdateOptions{})
2656-
return err
2657-
}).Should(BeNil())
2664+
pod := framework.FindLauncherPod(f.K8sClient, f.Namespace.Name, vmName)
2665+
patchData, err := patch.New(
2666+
patch.WithAddLabel(velerov1api.ExcludeFromBackupLabel, "true", pod.Labels),
2667+
).GeneratePayload()
2668+
Expect(err).ToNot(HaveOccurred())
2669+
2670+
_, err = f.K8sClient.CoreV1().Pods(f.Namespace.Name).Patch(context.TODO(), pod.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
2671+
Expect(err).ToNot(HaveOccurred())
26582672
}
26592673

26602674
Context("Standalone DV", func() {
@@ -3456,70 +3470,6 @@ func addExpectedPVs(client *kubernetes.Clientset, namespace string, resources ma
34563470
resources["PersistentVolume"] = pvs
34573471
}
34583472

3459-
func updateVm(kvClient kubecli.KubevirtClient, namespace string, name string,
3460-
update func(*kvv1.VirtualMachine) *kvv1.VirtualMachine) func() error {
3461-
return func() error {
3462-
vm, err := kvClient.VirtualMachine(namespace).Get(context.Background(), name, metav1.GetOptions{})
3463-
if err != nil {
3464-
return err
3465-
}
3466-
vm = update(vm)
3467-
3468-
_, err = kvClient.VirtualMachine(namespace).Update(context.Background(), vm, metav1.UpdateOptions{})
3469-
return err
3470-
}
3471-
}
3472-
3473-
func updateVmi(kvClient kubecli.KubevirtClient, namespace string, name string,
3474-
update func(*kvv1.VirtualMachineInstance) *kvv1.VirtualMachineInstance) func() error {
3475-
return func() error {
3476-
vmi, err := kvClient.VirtualMachineInstance(namespace).Get(context.Background(), name, metav1.GetOptions{})
3477-
if err != nil {
3478-
return err
3479-
}
3480-
vmi = update(vmi)
3481-
3482-
_, err = kvClient.VirtualMachineInstance(namespace).Update(context.Background(), vmi, metav1.UpdateOptions{})
3483-
return err
3484-
}
3485-
}
3486-
3487-
func updatePvc(client *kubernetes.Clientset, namespace string, name string,
3488-
update func(*v1.PersistentVolumeClaim) *v1.PersistentVolumeClaim) func() error {
3489-
return func() error {
3490-
pvc, err := framework.FindPVC(client, namespace, name)
3491-
if err != nil {
3492-
return err
3493-
}
3494-
pvc = update(pvc)
3495-
3496-
_, err = client.CoreV1().PersistentVolumeClaims(namespace).Update(context.TODO(), pvc, metav1.UpdateOptions{})
3497-
return err
3498-
}
3499-
}
3500-
func updateDataVolume(kvClient kubecli.KubevirtClient, namespace string, name string,
3501-
update func(dataVolume *cdiv1.DataVolume) *cdiv1.DataVolume) func() error {
3502-
return func() error {
3503-
dv, err := framework.FindDataVolume(kvClient, namespace, name)
3504-
if err != nil {
3505-
return err
3506-
}
3507-
dv = update(dv)
3508-
3509-
_, err = kvClient.CdiClient().CdiV1beta1().DataVolumes(namespace).Update(context.TODO(), dv, metav1.UpdateOptions{})
3510-
return err
3511-
}
3512-
}
3513-
3514-
func retryOnceOnErr(f func() error) Assertion {
3515-
err := f()
3516-
if err != nil {
3517-
err = f()
3518-
}
3519-
3520-
return Expect(err)
3521-
}
3522-
35233473
func runPodAndWaitSucceeded(kvClient kubecli.KubevirtClient, namespace string, podSpec *v1.Pod) *v1.Pod {
35243474
return framework.RunPodAndWaitPhase(kvClient, namespace, podSpec, v1.PodSucceeded)
35253475
}

tests/vm_backup_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
kubecli "kubevirt.io/client-go/kubecli"
2121
"kubevirt.io/kubevirt-velero-plugin/tests/framework"
2222
. "kubevirt.io/kubevirt-velero-plugin/tests/framework/matcher"
23+
"kubevirt.io/kubevirt-velero-plugin/tests/patch"
2324
)
2425

2526
const (
@@ -378,11 +379,12 @@ var _ = Describe("[smoke] VM Backup", func() {
378379
if originalMAC == "" {
379380
// This means there is no KubeMacPool running. We can simply choose a random address
380381
originalMAC = "DE-AD-00-00-BE-AF"
381-
update := func(vm *kvv1.VirtualMachine) *kvv1.VirtualMachine {
382-
vm.Spec.Template.Spec.Domain.Devices.Interfaces[0].MacAddress = originalMAC
383-
return vm
384-
}
385-
retryOnceOnErr(updateVm(f.KvClient, f.Namespace.Name, vm.Name, update)).Should(BeNil())
382+
patchData, err := patch.New(
383+
patch.WithReplace("/spec/template/spec/domain/devices/interfaces/0/macAddress", originalMAC),
384+
).GeneratePayload()
385+
Expect(err).ToNot(HaveOccurred())
386+
_, err = f.KvClient.VirtualMachine(f.Namespace.Name).Patch(context.TODO(), vm.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
387+
Expect(err).ToNot(HaveOccurred())
386388

387389
err = framework.WaitForVirtualMachineStatus(f.KvClient, f.Namespace.Name, vm.Name, kvv1.VirtualMachineStatusRunning)
388390
Expect(err).ToNot(HaveOccurred())
@@ -455,18 +457,30 @@ var _ = Describe("[smoke] VM Backup", func() {
455457
updateInstancetypeFunc := func() {
456458
instancetype, err := f.KvClient.VirtualMachineInstancetype(f.Namespace.Name).Get(context.Background(), instancetypeName, metav1.GetOptions{})
457459
Expect(err).ToNot(HaveOccurred())
458-
instancetype.Spec.CPU.Guest = instancetype.Spec.CPU.Guest + 1
459-
instancetype.Spec.Memory.Guest.Add(resource.MustParse("128Mi"))
460-
_, err = f.KvClient.VirtualMachineInstancetype(f.Namespace.Name).Update(context.Background(), instancetype, metav1.UpdateOptions{})
460+
newCPU := instancetype.Spec.CPU.Guest + 1
461+
newMemory := instancetype.Spec.Memory.Guest.DeepCopy()
462+
newMemory.Add(resource.MustParse("128Mi"))
463+
patchData, err := patch.New(
464+
patch.WithReplace("/spec/cpu/guest", newCPU),
465+
patch.WithReplace("/spec/memory/guest", newMemory.String()),
466+
).GeneratePayload()
467+
Expect(err).ToNot(HaveOccurred())
468+
_, err = f.KvClient.VirtualMachineInstancetype(f.Namespace.Name).Patch(context.Background(), instancetype.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
461469
Expect(err).ToNot(HaveOccurred())
462470
}
463471

464472
updateClusterInstancetypeFunc := func() {
465473
instancetype, err := f.KvClient.VirtualMachineClusterInstancetype().Get(context.Background(), instancetypeName, metav1.GetOptions{})
466474
Expect(err).ToNot(HaveOccurred())
467-
instancetype.Spec.CPU.Guest = instancetype.Spec.CPU.Guest + 1
468-
instancetype.Spec.Memory.Guest.Add(resource.MustParse("128Mi"))
469-
_, err = f.KvClient.VirtualMachineClusterInstancetype().Update(context.Background(), instancetype, metav1.UpdateOptions{})
475+
newCPU := instancetype.Spec.CPU.Guest + 1
476+
newMemory := instancetype.Spec.Memory.Guest.DeepCopy()
477+
newMemory.Add(resource.MustParse("128Mi"))
478+
patchData, err := patch.New(
479+
patch.WithReplace("/spec/cpu/guest", newCPU),
480+
patch.WithReplace("/spec/memory/guest", newMemory.String()),
481+
).GeneratePayload()
482+
Expect(err).ToNot(HaveOccurred())
483+
_, err = f.KvClient.VirtualMachineClusterInstancetype().Patch(context.Background(), instancetype.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
470484
Expect(err).ToNot(HaveOccurred())
471485
}
472486

0 commit comments

Comments
 (0)