Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/controller/questdbsnapshotschedule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *QuestDBSnapshotScheduleReconciler) Reconcile(ctx context.Context, req c

// Calculate the requeue time before any modifications
// are made to the NextSnapshot time
requeueTime := time.Until(nextSnapshotTime)
requeueTime := nextSnapshotTime.Sub(r.TimeSource.Now())
if requeueTime < 0 {
requeueTime = 0
}
Expand Down
68 changes: 38 additions & 30 deletions internal/controller/questdbsnapshotschedule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
q *crdv1beta1.QuestDB
sched *crdv1beta1.QuestDBSnapshotSchedule

timeSource = abtime.NewManual()
timeSource *abtime.ManualTime

timeout = time.Second * 2
//consistencyTimeout = time.Millisecond * 600
Expand All @@ -40,7 +40,7 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
Client: k8sClient,
Scheme: scheme.Scheme,
Recorder: record.NewFakeRecorder(100),
TimeSource: timeSource,
TimeSource: abtime.NewManual(),
}

By("Creating a QuestDB")
Expand All @@ -60,19 +60,29 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
Schedule: "*/1 * * * *",
},
}

})

It("should requeue at the correct time when a schedule is created", func() {

Expect(k8sClient.Create(ctx, sched)).To(Succeed())
r.TimeSource = abtime.NewManualAtTime(sched.CreationTimestamp.Time)
timeSource = r.TimeSource.(*abtime.ManualTime)

By("Reconciling the QuestDBSnapshotSchedule")
_, err := r.Reconcile(ctx, ctrl.Request{
res, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(err).ToNot(HaveOccurred())
nextRunTime := r.TimeSource.Now().Add(time.Minute).Truncate(time.Minute)
Expect(res.RequeueAfter).To(Equal(nextRunTime.Sub(r.TimeSource.Now())))

})

It("should create a snapshot if the cron schedule has triggered", func() {

By("Bumping the clock a bit more than 1 minute")
timeSource.Advance(time.Minute + 10*time.Second)
By("Bumping the clock more than 1 minute")
timeSource.Advance(time.Minute + 5*time.Second)

By("Forcing a reconcile")
_, err := r.Reconcile(ctx, ctrl.Request{
Expand Down Expand Up @@ -115,27 +125,21 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {

It("should take a second snapshot if the cron schedule has triggered", func() {
By("Bumping the clock more than 1 minute")
timeSource.Advance(time.Minute + 10*time.Second)
timeSource.Advance(time.Minute + 5*time.Second)

By("Forcing a reconcile")
_, err := r.Reconcile(ctx, ctrl.Request{
res, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())

By("Checking that a snapshot has been created")
snapList := &crdv1beta1.QuestDBSnapshotList{}
Expect(k8sClient.List(ctx, snapList, client.InNamespace(sched.Namespace))).Should(Succeed())
Expect(snapList.Items).To(HaveLen(2))
})

It("should requeue the request to the time of the next trigger", func() {
By("Forcing a reconcile")
result, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(err).ToNot(HaveOccurred())
Expect(result.RequeueAfter).ToNot(BeZero())
Eventually(func(g Gomega) {
snapList := &crdv1beta1.QuestDBSnapshotList{}
g.Expect(k8sClient.List(ctx, snapList, client.InNamespace(sched.Namespace))).Should(Succeed())
g.Expect(snapList.Items).To(HaveLen(2))
}, timeout, interval).Should(Succeed())
})

It("should delete the snapshot if the retention policy is set to 1", func() {
Expand All @@ -146,16 +150,18 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
g.Expect(k8sClient.Update(ctx, sched)).To(Succeed())
}, timeout, interval).Should(Succeed())

By("Setting the status of the latest snapshot to Succeeded")
snapList := &crdv1beta1.QuestDBSnapshotList{}
Expect(k8sClient.List(ctx, snapList, client.InNamespace(sched.Namespace))).Should(Succeed())
Expect(snapList.Items).To(HaveLen(2))
for _, snap := range snapList.Items {
if snap.Status.Phase != crdv1beta1.SnapshotSucceeded {
snap.Status.Phase = crdv1beta1.SnapshotSucceeded
Expect(k8sClient.Status().Update(ctx, &snap)).To(Succeed())
By("Setting the status of all snapshots to Succeeded")
Eventually(func(g Gomega) {

snapList := &crdv1beta1.QuestDBSnapshotList{}
g.Expect(k8sClient.List(ctx, snapList, client.InNamespace(sched.Namespace))).Should(Succeed())
for _, snap := range snapList.Items {
if snap.Status.Phase != crdv1beta1.SnapshotSucceeded {
snap.Status.Phase = crdv1beta1.SnapshotSucceeded
g.Expect(k8sClient.Status().Update(ctx, &snap)).To(Succeed())
}
}
}
}, timeout, interval).Should(Succeed())

By("Forcing a reconcile")
_, err := r.Reconcile(ctx, ctrl.Request{
Expand All @@ -164,8 +170,10 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
Expect(err).ToNot(HaveOccurred())

By("Checking that a snapshot has been deleted")
Expect(k8sClient.List(ctx, snapList, client.InNamespace(sched.Namespace))).Should(Succeed())
Expect(snapList.Items).To(HaveLen(1))
Eventually(func(g Gomega) {
g.Expect(k8sClient.List(ctx, snapList, client.InNamespace(sched.Namespace))).Should(Succeed())
g.Expect(snapList.Items).To(HaveLen(1))
}, timeout, interval).Should(Succeed())
})

})
Expand Down