Skip to content

Commit

Permalink
Controller: fix unit tests by deleting resources properly in fake client
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Kalenyuk <akalenyu@redhat.com>
  • Loading branch information
akalenyu committed Jan 22, 2024
1 parent 62da8b9 commit 87e8eda
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
16 changes: 4 additions & 12 deletions pkg/controller/dataimportcron-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ var _ = Describe("All DataImportCron Tests", func() {
err = reconciler.client.Get(context.TODO(), cronKey, cron)
Expect(err).ToNot(HaveOccurred())

now := metav1.Now()
cron.DeletionTimestamp = &now
err = reconciler.client.Update(context.TODO(), cron)
err = reconciler.client.Delete(context.TODO(), cron)
Expect(err).ToNot(HaveOccurred())

_, err = reconciler.Reconcile(context.TODO(), cronReq)
Expand Down Expand Up @@ -445,9 +443,7 @@ var _ = Describe("All DataImportCron Tests", func() {
Expect(*cron.Status.LastImportedPVC).To(Equal(sourcePVC))
Expect(cron.Status.LastImportTimestamp).ToNot(BeNil())

now := metav1.Now()
cron.DeletionTimestamp = &now
err = reconciler.client.Update(context.TODO(), cron)
err = reconciler.client.Delete(context.TODO(), cron)
Expect(err).ToNot(HaveOccurred())
_, err = reconciler.Reconcile(context.TODO(), cronReq)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -695,9 +691,7 @@ var _ = Describe("All DataImportCron Tests", func() {
Expect(err).ToNot(HaveOccurred())
verifyConditions("Import succeeded", false, true, true, noImport, upToDate, ready, &corev1.PersistentVolumeClaim{})

now := metav1.Now()
cron.DeletionTimestamp = &now
err = reconciler.client.Update(context.TODO(), cron)
err = reconciler.client.Delete(context.TODO(), cron)
Expect(err).ToNot(HaveOccurred())
_, err = reconciler.Reconcile(context.TODO(), cronReq)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -967,9 +961,7 @@ var _ = Describe("All DataImportCron Tests", func() {
Expect(*snap.Status.ReadyToUse).To(BeTrue())
Expect(*snap.Spec.Source.PersistentVolumeClaimName).To(Equal(dvName))

now := metav1.Now()
cron.DeletionTimestamp = &now
err = reconciler.client.Update(context.TODO(), cron)
err = reconciler.client.Delete(context.TODO(), cron)
Expect(err).ToNot(HaveOccurred())
_, err = reconciler.Reconcile(context.TODO(), cronReq)
Expect(err).ToNot(HaveOccurred())
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/upload-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package controller
import (
"context"
"strings"
"time"

cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
featuregates "kubevirt.io/containerized-data-importer/pkg/feature-gates"
Expand Down Expand Up @@ -180,15 +179,16 @@ var _ = Describe("Upload controller reconcile loop", func() {

It("Should return nil and remove any service and pod if pvc marked for deletion", func() {
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnUploadRequest: "", cc.AnnPodPhase: string(corev1.PodPending)}, nil)
now := metav1.NewTime(time.Now())
testPvc.DeletionTimestamp = &now
testPvc.Finalizers = append(testPvc.Finalizers, "keepmearound")
reconciler := createUploadReconciler(testPvc,
createUploadPod(testPvc),
createUploadService(testPvc),
)
err := reconciler.client.Delete(context.TODO(), testPvc)
Expect(err).ToNot(HaveOccurred())
By("Verifying the pod and service exists")
uploadPod := &corev1.Pod{}
err := reconciler.client.Get(context.TODO(), types.NamespacedName{Name: createUploadResourceName("testPvc1"), Namespace: "default"}, uploadPod)
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: createUploadResourceName("testPvc1"), Namespace: "default"}, uploadPod)
Expect(err).ToNot(HaveOccurred())
Expect(uploadPod.Name).To(Equal(createUploadResourceName(testPvc.Name)))

Expand Down

0 comments on commit 87e8eda

Please sign in to comment.