Skip to content

Commit

Permalink
Refactor async assertions of IsNotFound().BeTrue() to MatchError
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Kalenyuk <[email protected]>
  • Loading branch information
akalenyu committed Jan 30, 2024
1 parent e3f4be4 commit 771e152
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 146 deletions.
6 changes: 3 additions & 3 deletions tests/libwait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ func WaitForVirtualMachineToDisappearWithTimeout(vmi *v1.VirtualMachineInstance,
func WaitForMigrationToDisappearWithTimeout(migration *v1.VirtualMachineInstanceMigration, seconds int) {
virtClient, err := kubecli.GetKubevirtClient()
gomega.ExpectWithOffset(1, err).ToNot(gomega.HaveOccurred())
gomega.EventuallyWithOffset(1, func() bool {
gomega.EventuallyWithOffset(1, func() error {
_, err := virtClient.VirtualMachineInstanceMigration(migration.Namespace).Get(migration.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, seconds, 1*time.Second).Should(gomega.BeTrue(), fmt.Sprintf("migration %s was expected to dissapear after %d seconds, but it did not", migration.Name, seconds))
return err
}, seconds, 1*time.Second).Should(gomega.MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), fmt.Sprintf("migration %s was expected to disappear after %d seconds, but it did not", migration.Name, seconds))
}
6 changes: 3 additions & 3 deletions tests/migration/eviction_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ var _ = SIGMigrationDescribe("Live Migration", func() {
Expect(err).ToNot(HaveOccurred())

By("checking that the PDB disappeared")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.PolicyV1().PodDisruptionBudgets(createdVMI.Namespace).Get(context.Background(), pdb.Name, metav1.GetOptions{})
return errors.IsNotFound(err)
}, 60*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 60*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
})

It("[test_id:3244]should block the eviction api while a slow migration is in progress", func() {
Expand Down
20 changes: 6 additions & 14 deletions tests/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -1737,12 +1737,8 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
for _, podName := range createdPods {
Eventually(func() error {
err := virtClient.CoreV1().Pods(testsuite.NamespacePrivileged).Delete(context.Background(), podName, metav1.DeleteOptions{})

if err != nil && errors.IsNotFound(err) {
return nil
}
return err
}, 10*time.Second, 1*time.Second).Should(Succeed(), "Should delete helper pod")
}, 10*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), "Should delete helper pod")
}
})
BeforeEach(func() {
Expand Down Expand Up @@ -1840,12 +1836,8 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
for _, podName := range createdPods {
Eventually(func() error {
err := virtClient.CoreV1().Pods(testsuite.NamespacePrivileged).Delete(context.Background(), podName, metav1.DeleteOptions{})

if err != nil && errors.IsNotFound(err) {
return nil
}
return err
}, 10*time.Second, 1*time.Second).Should(Succeed(), "Should delete helper pod")
}, 10*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), "Should delete helper pod")

Eventually(func() error {
_, err := virtClient.CoreV1().Pods(testsuite.NamespacePrivileged).Get(context.Background(), podName, metav1.GetOptions{})
Expand Down Expand Up @@ -2349,10 +2341,10 @@ var _ = SIGMigrationDescribe("VM Live Migration", func() {
Expect(err).ShouldNot(HaveOccurred())

By("Expecting migration to be deleted")
Eventually(func() bool {
migration, err = virtClient.VirtualMachineInstanceMigration(migration.Namespace).Get(migration.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, 60*time.Second, 5*time.Second).Should(BeTrue(), `expecting to get "is not found" error`)
Eventually(func() error {
_, err = virtClient.VirtualMachineInstanceMigration(migration.Namespace).Get(migration.Name, &metav1.GetOptions{})
return err
}, 60*time.Second, 5*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))

By("Making sure source pod is still running")
sourcePod, err = virtClient.CoreV1().Pods(sourcePod.Namespace).Get(context.Background(), sourcePod.Name, metav1.GetOptions{})
Expand Down
27 changes: 12 additions & 15 deletions tests/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1917,13 +1917,10 @@ spec:
Expect(err).ToNot(HaveOccurred())

By("Waiting for VM to be removed")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.VirtualMachine(testsuite.GetTestNamespace(nil)).Get(context.Background(), vmYaml.vmName, &metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
return true
}
return false
}, 90*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 90*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
}

By("Verifying all migratable vmi workloads are updated via live migration")
Expand Down Expand Up @@ -1975,10 +1972,10 @@ spec:
}
err = virtClient.CoreV1().Namespaces().Delete(context.Background(), testsuite.NamespaceTestOperator, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.CoreV1().Namespaces().Get(context.Background(), testsuite.NamespaceTestOperator, metav1.GetOptions{})
return errors.IsNotFound(err)
}, 60*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 60*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))

By("Creating KubeVirt Object")
createKv(copyOriginalKv())
Expand Down Expand Up @@ -2747,10 +2744,10 @@ spec:
}, 120*time.Second, 4*time.Second).Should(BeTrue())

By("waiting for the kv CR to be gone")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.KubeVirt(kv.Namespace).Get(kv.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, 120*time.Second, 4*time.Second).Should(BeTrue())
return err
}, 120*time.Second, 4*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))

By("creating a new single-replica kv CR")
if kv.Spec.Infra == nil {
Expand Down Expand Up @@ -2850,10 +2847,10 @@ spec:
testsuite.WaitExportProxyReady()
tests.DisableFeatureGate(virtconfig.VMExportGate)

Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.AppsV1().Deployments(originalKv.Namespace).Get(context.TODO(), "virt-exportproxy", metav1.GetOptions{})
return errors.IsNotFound(err)
}, time.Minute*5, time.Second*2).Should(BeTrue())
return err
}, time.Minute*5, time.Second*2).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
})
})

Expand Down
10 changes: 3 additions & 7 deletions tests/pausing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,10 @@ var _ = Describe("[rfe_id:3064][crit:medium][vendor:[email protected]][level:com
Expect(command()).To(Succeed())

By("Checking deletion of VMI")
Eventually(func() bool {
Eventually(func() error {
_, err = virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, &v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
Expect(err).ToNot(HaveOccurred())
return false
}, 300*time.Second, 1*time.Second).Should(BeTrue(), "The VMI did not disappear")
return err
}, 300*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), "The VMI did not disappear")

By("Checking status of VM")
Eventually(func() bool {
Expand Down
9 changes: 3 additions & 6 deletions tests/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,10 @@ var _ = Describe("[sig-compute]VirtualMachinePool", decorators.SigCompute, func(
Expect(virtClient.VirtualMachinePool(newPool.ObjectMeta.Namespace).Delete(context.Background(), newPool.ObjectMeta.Name, v12.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
// Wait until the pool is deleted
By("Waiting until the pool got deleted")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.VirtualMachinePool(newPool.ObjectMeta.Namespace).Get(context.Background(), newPool.ObjectMeta.Name, v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 60*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 60*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))

By("Checking if two VMs are orphaned and still exist")
vms, err = virtClient.VirtualMachine(newPool.ObjectMeta.Namespace).List(context.Background(), &v12.ListOptions{})
Expand Down
18 changes: 6 additions & 12 deletions tests/replicaset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,10 @@ var _ = Describe("[rfe_id:588][crit:medium][vendor:[email protected]][level:comp
Delete(newRS.ObjectMeta.Name, &v12.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
// Wait until the replica set is deleted
By("Waiting until the replica set got deleted")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.ReplicaSet(newRS.ObjectMeta.Namespace).Get(newRS.ObjectMeta.Name, v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 60*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 60*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))

By("Checking if two VMIs are orphaned and still exist")
vmis, err = virtClient.VirtualMachineInstance(newRS.ObjectMeta.Namespace).List(context.Background(), &v12.ListOptions{})
Expand Down Expand Up @@ -396,13 +393,10 @@ var _ = Describe("[rfe_id:588][crit:medium][vendor:[email protected]][level:comp
Expect(err).ToNot(HaveOccurred())

By("Checking that the VM disappeared")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(rs)).Get(context.Background(), vmi.Name, &v12.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, 120*time.Second, time.Second).Should(BeTrue())
return err
}, 120*time.Second, time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))

By("Checking number of RS VM's to see that we got a replacement")
vmis, err = virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(rs)).List(context.Background(), &v12.ListOptions{})
Expand Down
9 changes: 3 additions & 6 deletions tests/storage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2214,13 +2214,10 @@ var _ = SIGDescribe("Export", func() {
err = virtctlCmd()
Expect(err).ToNot(HaveOccurred())
By("Verifying the vmexport was deleted")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.VirtualMachineExport(testsuite.GetTestNamespace(vmExport)).Get(context.Background(), vmExport.Name, metav1.GetOptions{})
if err == nil {
return false
}
return errors.IsNotFound(err)
}, 180*time.Second, time.Second).Should(BeTrue())
return err
}, 180*time.Second, time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
})

It("Delete succeeds when vmexport doesn't exist", func() {
Expand Down
18 changes: 9 additions & 9 deletions tests/storage/hotplug.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ var _ = SIGDescribe("Hotplug", func() {
By("Deleting virt-handler pod")
virtHandlerPod, err := libnode.GetVirtHandlerPod(virtClient, vmi.Status.NodeName)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
Eventually(func() error {
err := virtClient.CoreV1().
Pods(virtHandlerPod.GetObjectMeta().GetNamespace()).
Delete(context.Background(), virtHandlerPod.GetObjectMeta().GetName(), metav1.DeleteOptions{})
return errors.IsNotFound(err)
}, 60*time.Second, 1*time.Second).Should(BeTrue(), "virt-handler pod is expected to be deleted")
return err
}, 60*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), "virt-handler pod is expected to be deleted")

By("Waiting for virt-handler pod to restart")
Eventually(func() bool {
Expand Down Expand Up @@ -1257,10 +1257,10 @@ var _ = SIGDescribe("Hotplug", func() {
verifyWriteReadData(vmi, targets[0])

By("Verifying the source attachment pods are deleted")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.CoreV1().Pods(vmi.Namespace).Get(context.Background(), sourceAttachmentPods[0], metav1.GetOptions{})
return errors.IsNotFound(err)
}, 60*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 60*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
}

By("Verifying the volume can be detached and reattached after migration")
Expand Down Expand Up @@ -1411,10 +1411,10 @@ var _ = SIGDescribe("Hotplug", func() {
PropagationPolicy: &foreGround,
})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.CoreV1().Pods(vmi.Namespace).Get(context.Background(), podName, metav1.GetOptions{})
return errors.IsNotFound(err)
}, 300*time.Second, 1*time.Second).Should(BeTrue())
return err
}, 300*time.Second, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
}

It("should remain active", func() {
Expand Down
11 changes: 3 additions & 8 deletions tests/storage/memorydump.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,9 @@ var _ = SIGDescribe("Memory dump", func() {
}

waitDeleted := func(deleteFunc func() error) {
Eventually(func() bool {
err := deleteFunc()
if errors.IsNotFound(err) {
return true
}
Expect(err).ToNot(HaveOccurred())
return false
}, 180*time.Second, time.Second).Should(BeTrue())
Eventually(func() error {
return deleteFunc()
}, 180*time.Second, time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
}

deleteVirtualMachine := func(vm *v1.VirtualMachine) {
Expand Down
6 changes: 3 additions & 3 deletions tests/storage/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,10 +1036,10 @@ var _ = SIGDescribe("VirtualMachineRestore Tests", func() {
targetVM := getTargetVM(restoreToNewVM)

if libstorage.IsDataVolumeGC(virtClient) {
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.CdiClient().CdiV1beta1().DataVolumes(testsuite.GetTestNamespace(nil)).Get(context.Background(), *dvName, metav1.GetOptions{})
return errors.IsNotFound(err)
}, 30*time.Second, time.Second).Should(BeTrue())
return err
}, 30*time.Second, time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
verifyOwnerRef(pvc, targetVM.APIVersion, targetVM.Kind, targetVM.Name, targetVM.UID)
return
}
Expand Down
6 changes: 3 additions & 3 deletions tests/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,10 @@ var _ = SIGDescribe("Storage", func() {

func waitForPodToDisappearWithTimeout(podName string, seconds int) {
virtClient := kubevirt.Client()
EventuallyWithOffset(1, func() bool {
EventuallyWithOffset(1, func() error {
_, err := virtClient.CoreV1().Pods(testsuite.GetTestNamespace(nil)).Get(context.Background(), podName, metav1.GetOptions{})
return errors.IsNotFound(err)
}, seconds, 1*time.Second).Should(BeTrue())
return err
}, seconds, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
}

func createBlockDataVolume(virtClient kubecli.KubevirtClient) (*cdiv1.DataVolume, error) {
Expand Down
9 changes: 3 additions & 6 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1310,13 +1310,10 @@ func StopVirtualMachineWithTimeout(vm *v1.VirtualMachine, timeout time.Duration)
updatedVM, err := virtClient.VirtualMachine(vm.Namespace).Get(context.Background(), vm.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
// Observe the VirtualMachineInstance deleted
Eventually(func() bool {
Eventually(func() error {
_, err = virtClient.VirtualMachineInstance(updatedVM.Namespace).Get(context.Background(), updatedVM.Name, &metav1.GetOptions{})
if errors.IsNotFound(err) {
return true
}
return false
}, timeout, 1*time.Second).Should(BeTrue(), "The vmi did not disappear")
return err
}, timeout, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), "The vmi did not disappear")
By("VM has not the running condition")
Eventually(func() bool {
vm, err := virtClient.VirtualMachine(updatedVM.Namespace).Get(context.Background(), updatedVM.Name, &metav1.GetOptions{})
Expand Down
18 changes: 9 additions & 9 deletions tests/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,10 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
Expect(migrateCommand()).To(Succeed())

By("Check that no migration was actually created")
Consistently(func() bool {
Consistently(func() error {
_, err = virtClient.VirtualMachineInstanceMigration(vm.Namespace).Get(vm.Name, &metav1.GetOptions{})
return errors.IsNotFound(err)
}, 60*time.Second, 5*time.Second).Should(BeTrue(), "migration should not be created in a dry run mode")
return err
}, 60*time.Second, 5*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), "migration should not be created in a dry run mode")
})
})

Expand Down Expand Up @@ -1004,10 +1004,10 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
}, 10)).To(Succeed())

By("Waiting for the VMI to disappear")
Eventually(func() bool {
Eventually(func() error {
_, err := virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, &k8smetav1.GetOptions{})
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
return err
}, time.Minute, time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"))
})

It("should restart a failed VMI", func() {
Expand Down Expand Up @@ -1863,10 +1863,10 @@ status:
Expect(err).ToNot(HaveOccurred())

By("Ensure the vm has disappeared")
Eventually(func() bool {
Eventually(func() error {
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(context.Background(), vm.Name, &k8smetav1.GetOptions{})
return errors.IsNotFound(err)
}, 2*time.Minute, 1*time.Second).Should(BeTrue(), fmt.Sprintf("vm %s is not deleted", vm.Name))
return err
}, 2*time.Minute, 1*time.Second).Should(MatchError(errors.IsNotFound, "k8serrors.IsNotFound"), fmt.Sprintf("vm %s is not deleted", vm.Name))
})

It("should be added when the vm is created and removed when the vm is being deleted", func() {
Expand Down
Loading

0 comments on commit 771e152

Please sign in to comment.