Skip to content

Commit

Permalink
fix: test improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Dec 18, 2024
1 parent 1a8f036 commit ea85095
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 114 deletions.
145 changes: 93 additions & 52 deletions internal/controller/nonadminbackup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -63,6 +63,7 @@ type nonAdminBackupFullReconcileScenario struct {
enforcedBackupSpec *velerov1.BackupSpec
spec nacv1alpha1.NonAdminBackupSpec
status nacv1alpha1.NonAdminBackupStatus
errorLogs int
}

func buildTestNonAdminBackup(nonAdminNamespace string, nonAdminName string, spec nacv1alpha1.NonAdminBackupSpec) *nacv1alpha1.NonAdminBackup {
Expand Down Expand Up @@ -235,7 +236,13 @@ var _ = ginkgo.Describe("Test single reconciles of NonAdminBackup Reconcile func
},
nonAdminBackup,
) == nil {
gomega.Expect(k8sClient.Delete(ctx, nonAdminBackup)).To(gomega.Succeed())
for _, finalizer := range nonAdminBackup.GetFinalizers() {
controllerutil.RemoveFinalizer(nonAdminBackup, finalizer)
}
gomega.Expect(k8sClient.Update(ctx, nonAdminBackup)).To(gomega.Succeed())
if nonAdminBackup.GetDeletionTimestamp().IsZero() {
gomega.Expect(k8sClient.Delete(ctx, nonAdminBackup)).To(gomega.Succeed())
}
}
gomega.Expect(deleteTestNamespaces(ctx, nonAdminObjectNamespace, oadpNamespace)).To(gomega.Succeed())
})
Expand Down Expand Up @@ -841,8 +848,12 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackup Controller",
gomega.Expect(deleteTestNamespaces(ctx, nonAdminObjectNamespace, oadpNamespace)).To(gomega.Succeed())

cancel()
// wait cancel
time.Sleep(1 * time.Second)
// wait manager shutdown
gomega.Eventually(func() (bool, error) {
logOutput := ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput
shutdownlog := "INFO Wait completed, proceeding to shutdown the manager"
return strings.Contains(logOutput, shutdownlog) && strings.Count(logOutput, shutdownlog) == 1, nil
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())
})

ginkgo.DescribeTable("Reconcile triggered by NonAdminBackup Create event",
Expand All @@ -853,6 +864,12 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackup Controller",

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: k8sClient.Scheme(),
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
nonAdminObjectNamespace: {},
oadpNamespace: {},
},
},
})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

Expand All @@ -874,53 +891,44 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackup Controller",
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to run manager")
}()
// wait manager start
managerStartTimeout := 10 * time.Second
pollInterval := 100 * time.Millisecond
ctxTimeout, cancel := context.WithTimeout(ctx, managerStartTimeout)
defer cancel()

err = wait.PollUntilContextTimeout(ctxTimeout, pollInterval, managerStartTimeout, true, func(ctx context.Context) (done bool, err error) {
select {
case <-ctx.Done():
return false, ctx.Err()
default:
// Check if the manager has started by verifying if the client is initialized
return k8sManager.GetClient() != nil, nil
}
})
// Check if the context timeout or another error occurred
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "Manager failed to start within the timeout period")
gomega.Eventually(func() (bool, error) {
logOutput := ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput
startUplog := `INFO Starting workers {"controller": "nonadminbackup", "controllerGroup": "oadp.openshift.io", "controllerKind": "NonAdminBackup", "worker count": 1}`
return strings.Contains(logOutput, startUplog) &&
strings.Count(logOutput, startUplog) == 1 &&
!strings.Contains(logOutput, "DEBUG Accepted NAB Create event"), nil
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())

ginkgo.By("Waiting Reconcile of create event")
nonAdminBackup := buildTestNonAdminBackup(nonAdminObjectNamespace, nonAdminObjectName, scenario.spec)
gomega.Expect(k8sClient.Create(ctxTimeout, nonAdminBackup)).To(gomega.Succeed())
gomega.Expect(k8sClient.Create(ctx, nonAdminBackup)).To(gomega.Succeed())
// wait NAB reconcile
time.Sleep(2 * time.Second)

ginkgo.By("Fetching NonAdminBackup after Reconcile")
gomega.Expect(k8sClient.Get(
ctxTimeout,
types.NamespacedName{
Name: nonAdminObjectName,
Namespace: nonAdminObjectNamespace,
},
nonAdminBackup,
)).To(gomega.Succeed())

ginkgo.By("Validating NonAdminBackup Status")
gomega.Eventually(func() (bool, error) {
err := k8sClient.Get(
ctx,
types.NamespacedName{
Name: nonAdminObjectName,
Namespace: nonAdminObjectNamespace,
},
nonAdminBackup,
)
if err != nil {
return false, err
}
err = checkTestNonAdminBackupStatus(nonAdminBackup, scenario.status, oadpNamespace)
return err == nil, err
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())

gomega.Expect(checkTestNonAdminBackupStatus(nonAdminBackup, scenario.status, oadpNamespace)).To(gomega.Succeed())
ginkgo.By("Checking if NonAdminBackup Spec was not changed")
gomega.Expect(reflect.DeepEqual(
nonAdminBackup.Spec,
scenario.spec,
)).To(gomega.BeTrue())

veleroBackup := &velerov1.Backup{}
if scenario.status.VeleroBackup != nil && len(nonAdminBackup.Status.VeleroBackup.NACUUID) > 0 {
ginkgo.By("Checking if NonAdminBackup Spec was not changed")
gomega.Expect(reflect.DeepEqual(
nonAdminBackup.Spec,
scenario.spec,
)).To(gomega.BeTrue())

veleroBackup := &velerov1.Backup{}
gomega.Expect(k8sClient.Get(
ctxTimeout,
ctx,
types.NamespacedName{
Name: nonAdminBackup.Status.VeleroBackup.NACUUID,
Namespace: oadpNamespace,
Expand All @@ -936,22 +944,17 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackup Controller",
}

ginkgo.By("Simulating VeleroBackup update to finished state")

veleroBackup.Status = velerov1.BackupStatus{
Phase: velerov1.BackupPhaseCompleted,
CompletionTimestamp: &metav1.Time{Time: time.Now()},
}

// can not call .Status().Update() for veleroBackup object https://github.com/vmware-tanzu/velero/issues/8285
gomega.Expect(k8sClient.Update(ctxTimeout, veleroBackup)).To(gomega.Succeed())

ginkgo.By("VeleroBackup updated")
gomega.Expect(k8sClient.Update(ctx, veleroBackup)).To(gomega.Succeed())

// wait NAB reconcile

gomega.Eventually(func() (bool, error) {
err := k8sClient.Get(
ctxTimeout,
ctx,
types.NamespacedName{
Name: nonAdminObjectName,
Namespace: nonAdminObjectNamespace,
Expand All @@ -969,8 +972,45 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackup Controller",
}

ginkgo.By("Waiting Reconcile of delete event")
gomega.Expect(k8sClient.Delete(ctxTimeout, nonAdminBackup)).To(gomega.Succeed())
time.Sleep(1 * time.Second)
nonAdminBackup.Spec.ForceDeleteBackup = true
gomega.Expect(k8sClient.Update(ctx, nonAdminBackup)).To(gomega.Succeed())
if scenario.status.VeleroBackup != nil && len(nonAdminBackup.Status.VeleroBackup.NACUUID) > 0 {
gomega.Eventually(func() (bool, error) {
err := k8sClient.Get(
ctx,
types.NamespacedName{
Name: nonAdminBackup.Status.VeleroBackup.Name,
Namespace: oadpNamespace,
},
veleroBackup,
)
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}, 10*time.Second, 1*time.Second).Should(gomega.BeTrue())
}
gomega.Eventually(func() (bool, error) {
err := k8sClient.Get(
ctx,
types.NamespacedName{
Name: nonAdminObjectName,
Namespace: nonAdminObjectNamespace,
},
nonAdminBackup,
)
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}, 10*time.Second, 1*time.Second).Should(gomega.BeTrue())
gomega.Eventually(func() (bool, error) {
logOutput := ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput
deletelog := "DEBUG Accepted NAB Delete event"
return strings.Contains(logOutput, deletelog) && strings.Count(logOutput, deletelog) == 1, nil
}, 5*time.Second, 1*time.Second).Should(gomega.BeTrue())

gomega.Expect(strings.Count(ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput, "ERROR")).Should(gomega.Equal(scenario.errorLogs))
},
ginkgo.Entry("Should update NonAdminBackup until VeleroBackup completes and then delete it", nonAdminBackupFullReconcileScenario{
spec: nacv1alpha1.NonAdminBackupSpec{
Expand Down Expand Up @@ -1025,6 +1065,7 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackup Controller",
enforcedBackupSpec: &velerov1.BackupSpec{
SnapshotVolumes: ptr.To(false),
},
errorLogs: 1,
}),
)
})
Loading

0 comments on commit ea85095

Please sign in to comment.