Skip to content

Commit

Permalink
tests, storage: Use patch instead of update
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev committed Jul 31, 2022
1 parent 4602443 commit 236d3a3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 50 deletions.
1 change: 0 additions & 1 deletion tests/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//vendor/k8s.io/client-go/util/retry:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
"//vendor/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1:go_default_library",
],
Expand Down
15 changes: 10 additions & 5 deletions tests/storage/datavolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/utils/pointer"

v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"

k6ttypes "kubevirt.io/kubevirt/pkg/util/types"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"

"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/clientcmd"
"kubevirt.io/kubevirt/tests/console"
Expand Down Expand Up @@ -112,12 +115,14 @@ var _ = SIGDescribe("DataVolume Integration", func() {
By("Expecting the VirtualMachineInstance console")
Expect(console.LoginToCirros(vmi)).To(Succeed())

pvc, err := virtClient.CoreV1().PersistentVolumeClaims(util.NamespaceTestDefault).Get(context.Background(), dataVolume.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())

By("Expanding PVC")
pvc.Spec.Resources.Requests[k8sv1.ResourceStorage] = resource.MustParse("2Gi")
_, err = virtClient.CoreV1().PersistentVolumeClaims(util.NamespaceTestDefault).Update(context.Background(), pvc, metav1.UpdateOptions{})
patchData, err := k6ttypes.GeneratePatchPayload(k6ttypes.PatchOperation{
Op: k6ttypes.PatchAddOp,
Path: "/spec/resources/requests/storage",
Value: resource.MustParse("2Gi"),
})
Expect(err).ToNot(HaveOccurred())
_, err = virtClient.CoreV1().PersistentVolumeClaims(util.NamespaceTestDefault).Patch(context.Background(), dataVolume.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())

By("Waiting for notification about size change")
Expand Down
22 changes: 17 additions & 5 deletions tests/storage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ import (
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"

"kubevirt.io/kubevirt/pkg/certificates/triple/cert"
k6ttypes "kubevirt.io/kubevirt/pkg/util/types"
"kubevirt.io/kubevirt/pkg/virt-operator/resource/generate/components"

"kubevirt.io/kubevirt/tests"
cd "kubevirt.io/kubevirt/tests/containerdisk"
"kubevirt.io/kubevirt/tests/flags"
Expand Down Expand Up @@ -349,13 +351,23 @@ var _ = SIGDescribe("Export", func() {

populateArchiveContent := func(sc string, volumeMode k8sv1.PersistentVolumeMode) (*k8sv1.PersistentVolumeClaim, string) {
pvc, md5sum := populateKubeVirtContent(sc, volumeMode)
pvc, err := virtClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(context.Background(), pvc.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())

pvc.Annotations[annContentType] = "archive"
pvc.ObjectMeta.OwnerReferences = []metav1.OwnerReference{}
pvc, err = virtClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Update(context.Background(), pvc, metav1.UpdateOptions{})
patchData, err := k6ttypes.GeneratePatchPayload(
k6ttypes.PatchOperation{
Op: k6ttypes.PatchAddOp,
Path: "/metadata/annotations/" + k6ttypes.EscapeJSONPointer(annContentType),
Value: "archive",
},
k6ttypes.PatchOperation{
Op: k6ttypes.PatchAddOp,
Path: "/metadata/ownerReferences",
Value: []metav1.OwnerReference{},
},
)
Expect(err).ToNot(HaveOccurred())
pvc, err = virtClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Patch(context.Background(), pvc.Name, types.JSONPatchType, patchData, metav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())

log.DefaultLogger().Infof("Calculated MD5 %s", md5sum)
return pvc, md5sum
}
Expand Down
59 changes: 30 additions & 29 deletions tests/storage/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"

"k8s.io/client-go/util/retry"

"kubevirt.io/api/core"
v1 "kubevirt.io/api/core/v1"
snapshotv1 "kubevirt.io/api/snapshot/v1alpha1"
Expand Down Expand Up @@ -317,21 +315,19 @@ var _ = SIGDescribe("VirtualMachineRestore Tests", func() {
return vm.Status.SnapshotInProgress
}, 180*time.Second, time.Second).Should(BeNil())

initialRequestedMemory := resource.MustParse("128Mi")
Expect(vm.Spec.Template.Spec.Domain.Resources.Requests[corev1.ResourceMemory]).To(Equal(initialRequestedMemory))

origSpec = vm.Spec.DeepCopy()

err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
if err != nil {
return err
}
increasedRequestedMemory := resource.MustParse("256Mi")
vm.Spec.Template.Spec.Domain.Resources.Requests[corev1.ResourceMemory] = increasedRequestedMemory
vm, err = virtClient.VirtualMachine(vm.Namespace).Update(vm)
return err
})
initialRequestedMemory := resource.MustParse("128Mi")
increasedRequestedMemory := resource.MustParse("256Mi")
patchData, err := typesutil.GenerateTestReplacePatch(
"/spec/template/spec/domain/resources/requests/"+string(corev1.ResourceMemory),
initialRequestedMemory,
increasedRequestedMemory,
)
Expect(err).ToNot(HaveOccurred())

vm, err = virtClient.VirtualMachine(vm.Namespace).Patch(vm.Name, types.JSONPatchType, patchData, &metav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())

restore := createRestoreDef(vm.Name, snapshot.Name)

Expand Down Expand Up @@ -1187,11 +1183,15 @@ var _ = SIGDescribe("VirtualMachineRestore Tests", func() {
*updatedVM.Status.RestoreInProgress == restore.Name
}, 180*time.Second, 3*time.Second).Should(BeTrue())

running := true
updatedVM, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
updatedVM.Spec.Running = &running
_, err = virtClient.VirtualMachine(updatedVM.Namespace).Update(updatedVM)
patchData, err := typesutil.GeneratePatchPayload(
typesutil.PatchOperation{
Op: typesutil.PatchAddOp,
Path: "/spec/running",
Value: true,
},
)
Expect(err).NotTo(HaveOccurred())
_, err = virtClient.VirtualMachine(vm.Namespace).Patch(vm.Name, types.JSONPatchType, patchData, &metav1.PatchOptions{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Cannot start VM until restore %q completes", restore.Name)))

Expand Down Expand Up @@ -1333,15 +1333,16 @@ var _ = SIGDescribe("VirtualMachineRestore Tests", func() {
newMemory := resource.MustParse("2Gi")
Expect(newMemory).ToNot(Equal(initialMemory))

newVM, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())

updatedVM := newVM.DeepCopy()
updatedVM.Spec.Template.Spec.Domain.Resources.Requests = corev1.ResourceList{
corev1.ResourceMemory: newMemory,
}
updatedVM, err = virtClient.VirtualMachine(updatedVM.Namespace).Update(updatedVM)
Expect(err).ToNot(HaveOccurred())
patchData, err := typesutil.GeneratePatchPayload(
typesutil.PatchOperation{
Op: typesutil.PatchReplaceOp,
Path: "/spec/template/spec/domain/resources/requests/" + string(corev1.ResourceMemory),
Value: newMemory,
},
)
Expect(err).NotTo(HaveOccurred())
updatedVM, err := virtClient.VirtualMachine(vm.Namespace).Patch(vm.Name, types.JSONPatchType, patchData, &metav1.PatchOptions{})
Expect(err).NotTo(HaveOccurred())

By(creatingSnapshot)
snapshot = createSnapshot(vm)
Expand Down
19 changes: 9 additions & 10 deletions tests/storage/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"

typesutil "kubevirt.io/kubevirt/pkg/util/types"

expect "github.com/google/goexpect"
vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -17,7 +19,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/util/retry"

. "kubevirt.io/kubevirt/tests/framework/matcher"

Expand Down Expand Up @@ -437,16 +438,14 @@ var _ = SIGDescribe("VirtualMachineSnapshot Tests", func() {

//update vm to make sure vm revision is saved in the snapshot
By("Updating the VM template spec")
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
updatedVM, err := virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
patchData, err := typesutil.GenerateTestReplacePatch(
"/spec/template/spec/domain/resources/requests/"+string(corev1.ResourceMemory),
initialMemory,
newMemory,
)
Expect(err).ToNot(HaveOccurred())

updatedVM.Spec.Template.Spec.Domain.Resources.Requests = corev1.ResourceList{
corev1.ResourceMemory: newMemory,
}
_, err = virtClient.VirtualMachine(updatedVM.Namespace).Update(updatedVM)
return err
})
_, err = virtClient.VirtualMachine(vm.Namespace).Patch(vm.Name, types.JSONPatchType, patchData, &metav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())

snapshot = newSnapshot()
Expand Down

0 comments on commit 236d3a3

Please sign in to comment.